| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import * as util from 'node:util'; |
| b69ab31 | | | 9 | import {readExistingServerFile} from '../existingServerStateFiles'; |
| b69ab31 | | | 10 | import * as startServer from '../server'; |
| b69ab31 | | | 11 | import * as lifecycle from '../serverLifecycle'; |
| b69ab31 | | | 12 | import {parseArgs, runProxyMain} from '../startServer'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | /* eslint-disable require-await */ |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | // to prevent permission issues and races, mock FS read/writes in memory. |
| b69ab31 | | | 17 | let mockFsData: {[key: string]: string} = {}; |
| b69ab31 | | | 18 | jest.mock('node:fs', () => { |
| b69ab31 | | | 19 | return { |
| b69ab31 | | | 20 | promises: { |
| b69ab31 | | | 21 | writeFile: jest.fn(async (path: string, data: string) => { |
| b69ab31 | | | 22 | mockFsData[path] = data; |
| b69ab31 | | | 23 | }), |
| b69ab31 | | | 24 | readFile: jest.fn(async (path: string) => { |
| b69ab31 | | | 25 | return mockFsData[path]; |
| b69ab31 | | | 26 | }), |
| b69ab31 | | | 27 | stat: jest.fn(async (_path: string) => { |
| b69ab31 | | | 28 | return {mode: 0o700, isSymbolicLink: () => false}; |
| b69ab31 | | | 29 | }), |
| b69ab31 | | | 30 | rm: jest.fn(async (path: string) => { |
| b69ab31 | | | 31 | delete mockFsData[path]; |
| b69ab31 | | | 32 | }), |
| b69ab31 | | | 33 | mkdir: jest.fn(async (_path: string) => { |
| b69ab31 | | | 34 | // |
| b69ab31 | | | 35 | }), |
| b69ab31 | | | 36 | mkdtemp: jest.fn(async (_path: string) => { |
| b69ab31 | | | 37 | return '/tmp/'; |
| b69ab31 | | | 38 | }), |
| b69ab31 | | | 39 | }, |
| b69ab31 | | | 40 | }; |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | |
| b69ab31 | | | 43 | describe('run-proxy', () => { |
| b69ab31 | | | 44 | let stdout: Array<string> = []; |
| b69ab31 | | | 45 | let stderr: Array<string> = []; |
| b69ab31 | | | 46 | function allConsoleStdout() { |
| b69ab31 | | | 47 | return stdout.join('\n'); |
| b69ab31 | | | 48 | } |
| b69ab31 | | | 49 | function resetStdout() { |
| b69ab31 | | | 50 | stdout = []; |
| b69ab31 | | | 51 | stderr = []; |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | beforeEach(() => { |
| b69ab31 | | | 54 | resetStdout(); |
| b69ab31 | | | 55 | const appendStdout = jest.fn((...args) => stdout.push(util.format(...args))); |
| b69ab31 | | | 56 | const appendStderr = jest.fn((...args) => stderr.push(util.format(...args))); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | global.console = { |
| b69ab31 | | | 59 | log: appendStdout, |
| b69ab31 | | | 60 | info: appendStdout, |
| b69ab31 | | | 61 | warn: appendStdout, |
| b69ab31 | | | 62 | error: appendStderr, |
| b69ab31 | | | 63 | } as unknown as Console; |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | // reset mock filesystem |
| b69ab31 | | | 66 | mockFsData = {}; |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | jest.clearAllMocks(); |
| b69ab31 | | | 69 | }); |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | const killMock = jest.spyOn(process, 'kill').mockImplementation(() => true); |
| b69ab31 | | | 72 | const exitMock = jest.spyOn(process, 'exit').mockImplementation((): never => { |
| b69ab31 | | | 73 | throw new Error('exited'); |
| b69ab31 | | | 74 | }); |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | const defaultArgs = { |
| b69ab31 | | | 77 | help: false, |
| b69ab31 | | | 78 | // subprocess spawning without --foreground doesn't work well in tests |
| b69ab31 | | | 79 | // plus we don't want to manage closing servers after the tests |
| b69ab31 | | | 80 | foreground: true, |
| b69ab31 | | | 81 | // we don't want to actually open the url in the browser during a test |
| b69ab31 | | | 82 | openUrl: false, |
| b69ab31 | | | 83 | port: 3011, |
| b69ab31 | | | 84 | isDevMode: false, |
| b69ab31 | | | 85 | json: false, |
| b69ab31 | | | 86 | stdout: false, |
| b69ab31 | | | 87 | platform: undefined, |
| b69ab31 | | | 88 | kill: false, |
| b69ab31 | | | 89 | force: false, |
| b69ab31 | | | 90 | slVersion: '1.0', |
| b69ab31 | | | 91 | command: 'sl', |
| b69ab31 | | | 92 | cwd: undefined, |
| b69ab31 | | | 93 | sessionId: undefined, |
| b69ab31 | | | 94 | }; |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | it('spawns a server', async () => { |
| b69ab31 | | | 97 | const startServerSpy = jest |
| b69ab31 | | | 98 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 99 | .mockImplementation(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})); |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | await runProxyMain(defaultArgs); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | expect(startServerSpy).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 104 | }); |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | it('can output json', async () => { |
| b69ab31 | | | 107 | jest |
| b69ab31 | | | 108 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 109 | .mockImplementation(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})); |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | await runProxyMain({...defaultArgs, json: true}); |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | expect(JSON.parse(allConsoleStdout())).toEqual( |
| b69ab31 | | | 114 | expect.objectContaining({ |
| b69ab31 | | | 115 | command: 'sl', |
| b69ab31 | | | 116 | cwd: expect.stringContaining('isl-server'), |
| b69ab31 | | | 117 | logFileLocation: expect.stringContaining('isl-server.log'), |
| b69ab31 | | | 118 | pid: 1000, |
| b69ab31 | | | 119 | port: 3011, |
| b69ab31 | | | 120 | token: expect.stringMatching(/[a-z0-9]{32}/), |
| b69ab31 | | | 121 | url: expect.stringContaining('http://localhost:3011/'), |
| b69ab31 | | | 122 | wasServerReused: false, |
| b69ab31 | | | 123 | }), |
| b69ab31 | | | 124 | ); |
| b69ab31 | | | 125 | }); |
| b69ab31 | | | 126 | |
| b69ab31 | | | 127 | it('can set current working directory manually', async () => { |
| b69ab31 | | | 128 | jest |
| b69ab31 | | | 129 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 130 | .mockImplementation(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})); |
| b69ab31 | | | 131 | |
| b69ab31 | | | 132 | await runProxyMain({...defaultArgs, json: true, cwd: 'foobar'}); |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | expect(JSON.parse(allConsoleStdout())).toEqual( |
| b69ab31 | | | 135 | expect.objectContaining({ |
| b69ab31 | | | 136 | cwd: 'foobar', |
| b69ab31 | | | 137 | }), |
| b69ab31 | | | 138 | ); |
| b69ab31 | | | 139 | }); |
| b69ab31 | | | 140 | |
| b69ab31 | | | 141 | it('writes existing server info', async () => { |
| b69ab31 | | | 142 | jest |
| b69ab31 | | | 143 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 144 | .mockImplementation(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | await expect(readExistingServerFile(3011)).rejects.toEqual(expect.anything()); |
| b69ab31 | | | 147 | |
| b69ab31 | | | 148 | await runProxyMain(defaultArgs); |
| b69ab31 | | | 149 | |
| b69ab31 | | | 150 | expect(await readExistingServerFile(3011)).toEqual( |
| b69ab31 | | | 151 | expect.objectContaining({ |
| b69ab31 | | | 152 | sensitiveToken: expect.anything(), |
| b69ab31 | | | 153 | challengeToken: expect.anything(), |
| b69ab31 | | | 154 | command: 'sl', |
| b69ab31 | | | 155 | slVersion: '1.0', |
| b69ab31 | | | 156 | }), |
| b69ab31 | | | 157 | ); |
| b69ab31 | | | 158 | }); |
| b69ab31 | | | 159 | |
| b69ab31 | | | 160 | it('can output json for a reused server', async () => { |
| b69ab31 | | | 161 | jest |
| b69ab31 | | | 162 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 163 | .mockImplementationOnce(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})) |
| b69ab31 | | | 164 | .mockImplementationOnce(() => Promise.resolve({type: 'addressInUse'})); |
| b69ab31 | | | 165 | |
| b69ab31 | | | 166 | jest.spyOn(lifecycle, 'checkIfServerIsAliveAndIsISL').mockImplementation(() => { |
| b69ab31 | | | 167 | return Promise.resolve(1000); |
| b69ab31 | | | 168 | }); |
| b69ab31 | | | 169 | |
| b69ab31 | | | 170 | await runProxyMain(defaultArgs); |
| b69ab31 | | | 171 | resetStdout(); |
| b69ab31 | | | 172 | |
| b69ab31 | | | 173 | await expect(() => runProxyMain({...defaultArgs, json: true})).rejects.toEqual( |
| b69ab31 | | | 174 | new Error('exited'), |
| b69ab31 | | | 175 | ); |
| b69ab31 | | | 176 | |
| b69ab31 | | | 177 | expect(JSON.parse(allConsoleStdout())).toEqual( |
| b69ab31 | | | 178 | expect.objectContaining({ |
| b69ab31 | | | 179 | command: 'sl', |
| b69ab31 | | | 180 | cwd: expect.stringContaining('isl-server'), |
| b69ab31 | | | 181 | logFileLocation: expect.stringContaining('isl-server.log'), |
| b69ab31 | | | 182 | pid: 1000, |
| b69ab31 | | | 183 | port: 3011, |
| b69ab31 | | | 184 | token: expect.stringMatching(/[a-z0-9]{32}/), |
| b69ab31 | | | 185 | url: expect.stringContaining('http://localhost:3011/'), |
| b69ab31 | | | 186 | wasServerReused: true, |
| b69ab31 | | | 187 | }), |
| b69ab31 | | | 188 | ); |
| b69ab31 | | | 189 | expect(exitMock).toHaveBeenCalledWith(0); |
| b69ab31 | | | 190 | }); |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | it('can kill a server', async () => { |
| b69ab31 | | | 193 | const startServerSpy = jest |
| b69ab31 | | | 194 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 195 | .mockImplementation(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})); |
| b69ab31 | | | 196 | |
| b69ab31 | | | 197 | jest.spyOn(lifecycle, 'checkIfServerIsAliveAndIsISL').mockImplementation(() => { |
| b69ab31 | | | 198 | return Promise.resolve(1000); |
| b69ab31 | | | 199 | }); |
| b69ab31 | | | 200 | |
| b69ab31 | | | 201 | // successfully start normally |
| b69ab31 | | | 202 | await runProxyMain(defaultArgs); |
| b69ab31 | | | 203 | |
| b69ab31 | | | 204 | // now run with --kill |
| b69ab31 | | | 205 | await expect(() => runProxyMain({...defaultArgs, kill: true})).rejects.toEqual( |
| b69ab31 | | | 206 | new Error('exited'), |
| b69ab31 | | | 207 | ); |
| b69ab31 | | | 208 | |
| b69ab31 | | | 209 | expect(killMock).toHaveBeenCalled(); |
| b69ab31 | | | 210 | expect(exitMock).toHaveBeenCalledWith(0); // exits after killing |
| b69ab31 | | | 211 | expect(startServerSpy).toHaveBeenCalledTimes(1); // called for original server only |
| b69ab31 | | | 212 | }); |
| b69ab31 | | | 213 | |
| b69ab31 | | | 214 | it('--force kills and starts a new server', async () => { |
| b69ab31 | | | 215 | const startServerSpy = jest |
| b69ab31 | | | 216 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 217 | .mockImplementation(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})); |
| b69ab31 | | | 218 | |
| b69ab31 | | | 219 | jest.spyOn(lifecycle, 'checkIfServerIsAliveAndIsISL').mockImplementation(() => { |
| b69ab31 | | | 220 | return Promise.resolve(1000); |
| b69ab31 | | | 221 | }); |
| b69ab31 | | | 222 | |
| b69ab31 | | | 223 | // successfully start normally |
| b69ab31 | | | 224 | await runProxyMain(defaultArgs); |
| b69ab31 | | | 225 | |
| b69ab31 | | | 226 | // now run with --force |
| b69ab31 | | | 227 | await runProxyMain({...defaultArgs, force: true}); |
| b69ab31 | | | 228 | |
| b69ab31 | | | 229 | expect(killMock).toHaveBeenCalled(); |
| b69ab31 | | | 230 | expect(exitMock).not.toHaveBeenCalled(); |
| b69ab31 | | | 231 | expect(startServerSpy).toHaveBeenCalledTimes(2); // original to be killed and new instance |
| b69ab31 | | | 232 | }); |
| b69ab31 | | | 233 | |
| b69ab31 | | | 234 | it('forces a fresh server if sl version changed', async () => { |
| b69ab31 | | | 235 | jest |
| b69ab31 | | | 236 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 237 | .mockImplementationOnce(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})) |
| b69ab31 | | | 238 | .mockImplementationOnce(() => Promise.resolve({type: 'addressInUse'})); |
| b69ab31 | | | 239 | |
| b69ab31 | | | 240 | jest.spyOn(lifecycle, 'checkIfServerIsAliveAndIsISL').mockImplementation(() => { |
| b69ab31 | | | 241 | return Promise.resolve(1000); |
| b69ab31 | | | 242 | }); |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | await runProxyMain({...defaultArgs, slVersion: '0.1'}); |
| b69ab31 | | | 245 | resetStdout(); |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | await runProxyMain({...defaultArgs, json: true, slVersion: '0.2'}); |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | expect(JSON.parse(allConsoleStdout())).toEqual( |
| b69ab31 | | | 250 | expect.objectContaining({ |
| b69ab31 | | | 251 | wasServerReused: false, |
| b69ab31 | | | 252 | }), |
| b69ab31 | | | 253 | ); |
| b69ab31 | | | 254 | }); |
| b69ab31 | | | 255 | |
| b69ab31 | | | 256 | it('forces a fresh server if sl command changed', async () => { |
| b69ab31 | | | 257 | jest |
| b69ab31 | | | 258 | .spyOn(startServer, 'startServer') |
| b69ab31 | | | 259 | .mockImplementationOnce(() => Promise.resolve({type: 'success', port: 3011, pid: 1000})) |
| b69ab31 | | | 260 | .mockImplementationOnce(() => Promise.resolve({type: 'addressInUse'})); |
| b69ab31 | | | 261 | |
| b69ab31 | | | 262 | jest.spyOn(lifecycle, 'checkIfServerIsAliveAndIsISL').mockImplementation(() => { |
| b69ab31 | | | 263 | return Promise.resolve(1000); |
| b69ab31 | | | 264 | }); |
| b69ab31 | | | 265 | |
| b69ab31 | | | 266 | await runProxyMain({...defaultArgs, command: 'sl'}); |
| b69ab31 | | | 267 | resetStdout(); |
| b69ab31 | | | 268 | |
| b69ab31 | | | 269 | await runProxyMain({...defaultArgs, json: true, command: '/bin/sl'}); |
| b69ab31 | | | 270 | |
| b69ab31 | | | 271 | expect(JSON.parse(allConsoleStdout())).toEqual( |
| b69ab31 | | | 272 | expect.objectContaining({ |
| b69ab31 | | | 273 | wasServerReused: false, |
| b69ab31 | | | 274 | }), |
| b69ab31 | | | 275 | ); |
| b69ab31 | | | 276 | }); |
| b69ab31 | | | 277 | }); |
| b69ab31 | | | 278 | |
| b69ab31 | | | 279 | describe('argument parsing', () => { |
| b69ab31 | | | 280 | it('can parse arguments', () => { |
| b69ab31 | | | 281 | expect(parseArgs(['--port', '3001', '--force'])).toEqual( |
| b69ab31 | | | 282 | expect.objectContaining({ |
| b69ab31 | | | 283 | port: 3001, |
| b69ab31 | | | 284 | force: true, |
| b69ab31 | | | 285 | }), |
| b69ab31 | | | 286 | ); |
| b69ab31 | | | 287 | }); |
| b69ab31 | | | 288 | }); |
| b69ab31 | | | 289 | |
| b69ab31 | | | 290 | describe('validateServerChallengeResponse', () => { |
| b69ab31 | | | 291 | it('only accepts real responses', () => { |
| b69ab31 | | | 292 | expect(lifecycle.validateServerChallengeResponse(null)).toEqual(false); |
| b69ab31 | | | 293 | expect(lifecycle.validateServerChallengeResponse(undefined)).toEqual(false); |
| b69ab31 | | | 294 | expect(lifecycle.validateServerChallengeResponse(123)).toEqual(false); |
| b69ab31 | | | 295 | expect(lifecycle.validateServerChallengeResponse('1')).toEqual(false); |
| b69ab31 | | | 296 | expect(lifecycle.validateServerChallengeResponse({})).toEqual(false); |
| b69ab31 | | | 297 | expect(lifecycle.validateServerChallengeResponse([])).toEqual(false); |
| b69ab31 | | | 298 | expect(lifecycle.validateServerChallengeResponse({challengeToken: '123'})).toEqual(false); |
| b69ab31 | | | 299 | expect(lifecycle.validateServerChallengeResponse({pid: 123})).toEqual(false); |
| b69ab31 | | | 300 | expect(lifecycle.validateServerChallengeResponse({challengeToken: 123, pid: 123})).toEqual( |
| b69ab31 | | | 301 | false, |
| b69ab31 | | | 302 | ); |
| b69ab31 | | | 303 | |
| b69ab31 | | | 304 | expect(lifecycle.validateServerChallengeResponse({challengeToken: '123', pid: 123})).toEqual( |
| b69ab31 | | | 305 | true, |
| b69ab31 | | | 306 | ); |
| b69ab31 | | | 307 | }); |
| b69ab31 | | | 308 | }); |