From 1a507ecc2da2ce6886a26ead0adf59243519f73c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Mon, 8 Apr 2024 16:51:38 +0200 Subject: [PATCH] Node: update tests to use portRange --- node/src/test/test-PipeTransport.ts | 22 ++++- node/src/test/test-PlainTransport.ts | 17 +++- node/src/test/test-WebRtcServer.ts | 71 ++++++++++++++- node/src/test/test-WebRtcTransport.ts | 126 +++++++++++++++++++++----- 4 files changed, 211 insertions(+), 25 deletions(-) diff --git a/node/src/test/test-PipeTransport.ts b/node/src/test/test-PipeTransport.ts index 3b9052374d..bfa17f9c18 100644 --- a/node/src/test/test-PipeTransport.ts +++ b/node/src/test/test-PipeTransport.ts @@ -453,6 +453,16 @@ test('router.createPipeTransport() with wrong arguments rejects with TypeError', // @ts-ignore await expect(ctx.router1!.createPipeTransport({})).rejects.toThrow(TypeError); + await expect( + ctx.router1!.createPipeTransport({ + listenInfo: { + protocol: 'udp', + ip: '127.0.0.1', + portRange: { min: 4000, max: 3000 }, + }, + }) + ).rejects.toThrow(TypeError); + await expect( ctx.router1!.createPipeTransport({ listenIp: '123' }) ).rejects.toThrow(TypeError); @@ -479,7 +489,11 @@ test('router.createPipeTransport() with wrong arguments rejects with TypeError', test('router.createPipeTransport() with enableRtx succeeds', async () => { const pipeTransport = await ctx.router1!.createPipeTransport({ - listenInfo: { protocol: 'udp', ip: '127.0.0.1' }, + listenInfo: { + protocol: 'udp', + ip: '127.0.0.1', + portRange: { min: 2000, max: 3000 }, + }, enableRtx: true, }); @@ -583,7 +597,11 @@ test('pipeTransport.connect() with valid SRTP parameters succeeds', async () => test('pipeTransport.connect() with srtpParameters fails if enableSrtp is unset', async () => { const pipeTransport = await ctx.router1!.createPipeTransport({ - listenInfo: { protocol: 'udp', ip: '127.0.0.1' }, + listenInfo: { + protocol: 'udp', + ip: '127.0.0.1', + portRange: { min: 2000, max: 3000 }, + }, enableRtx: true, }); diff --git a/node/src/test/test-PlainTransport.ts b/node/src/test/test-PlainTransport.ts index 6588fe55cb..c0e1a0afc1 100644 --- a/node/src/test/test-PlainTransport.ts +++ b/node/src/test/test-PlainTransport.ts @@ -60,7 +60,11 @@ afterEach(async () => { test('router.createPlainTransport() succeeds', async () => { const plainTransport = await ctx.router!.createPlainTransport({ - listenInfo: { protocol: 'udp', ip: '127.0.0.1' }, + listenInfo: { + protocol: 'udp', + ip: '127.0.0.1', + portRange: { min: 2000, max: 3000 }, + }, }); await expect(ctx.router!.dump()).resolves.toMatchObject({ @@ -77,6 +81,7 @@ test('router.createPlainTransport() succeeds', async () => { protocol: 'udp', ip: '127.0.0.1', announcedAddress: '9.9.9.1', + portRange: { min: 2000, max: 3000 }, }, enableSctp: true, appData: { foo: 'bar' }, @@ -171,6 +176,16 @@ test('router.createPlainTransport() with wrong arguments rejects with TypeError' // @ts-ignore await expect(ctx.router!.createPlainTransport({})).rejects.toThrow(TypeError); + await expect( + ctx.router!.createPlainTransport({ + listenInfo: { + protocol: 'udp', + ip: '127.0.0.1', + portRange: { min: 4000, max: 3000 }, + }, + }) + ).rejects.toThrow(TypeError); + await expect( ctx.router!.createPlainTransport({ listenIp: '123' }) ).rejects.toThrow(TypeError); diff --git a/node/src/test/test-WebRtcServer.ts b/node/src/test/test-WebRtcServer.ts index e5e2e2b703..4641f7415a 100644 --- a/node/src/test/test-WebRtcServer.ts +++ b/node/src/test/test-WebRtcServer.ts @@ -91,7 +91,76 @@ test('worker.createWebRtcServer() succeeds', async () => { expect(ctx.worker!.webRtcServersForTesting.size).toBe(0); }, 2000); -test('worker.createWebRtcServer() without specifying port succeeds', async () => { +test('worker.createWebRtcServer() with portRange succeeds', async () => { + const onObserverNewWebRtcServer = jest.fn(); + + ctx.worker!.observer.once('newwebrtcserver', onObserverNewWebRtcServer); + + const port1 = await pickPort({ + type: 'udp', + ip: '127.0.0.1', + reserveTimeout: 0, + }); + const port2 = await pickPort({ + type: 'udp', + ip: '127.0.0.1', + reserveTimeout: 0, + }); + + const webRtcServer = await ctx.worker!.createWebRtcServer({ + listenInfos: [ + { + protocol: 'udp', + ip: '127.0.0.1', + portRange: { min: port1, max: port1 }, + }, + { + protocol: 'tcp', + ip: '127.0.0.1', + announcedAddress: '1.2.3.4', + portRange: { min: port2, max: port2 }, + }, + ], + appData: { foo: 123 }, + }); + + expect(onObserverNewWebRtcServer).toHaveBeenCalledTimes(1); + expect(onObserverNewWebRtcServer).toHaveBeenCalledWith(webRtcServer); + expect(typeof webRtcServer.id).toBe('string'); + expect(webRtcServer.closed).toBe(false); + expect(webRtcServer.appData).toEqual({ foo: 123 }); + + await expect(ctx.worker!.dump()).resolves.toMatchObject({ + pid: ctx.worker!.pid, + webRtcServerIds: [webRtcServer.id], + routerIds: [], + channelMessageHandlers: { + channelRequestHandlers: [webRtcServer.id], + channelNotificationHandlers: [], + }, + }); + + await expect(webRtcServer.dump()).resolves.toMatchObject({ + id: webRtcServer.id, + udpSockets: [{ ip: '127.0.0.1', port: port1 }], + tcpServers: [{ ip: '127.0.0.1', port: port2 }], + webRtcTransportIds: [], + localIceUsernameFragments: [], + tupleHashes: [], + }); + + // Private API. + expect(ctx.worker!.webRtcServersForTesting.size).toBe(1); + + ctx.worker!.close(); + + expect(webRtcServer.closed).toBe(true); + + // Private API. + expect(ctx.worker!.webRtcServersForTesting.size).toBe(0); +}, 2000); + +test('worker.createWebRtcServer() without specifying port/portRange succeeds', async () => { const onObserverNewWebRtcServer = jest.fn(); ctx.worker!.observer.once('newwebrtcserver', onObserverNewWebRtcServer); diff --git a/node/src/test/test-WebRtcTransport.ts b/node/src/test/test-WebRtcTransport.ts index 109b15499b..c069b54c0c 100644 --- a/node/src/test/test-WebRtcTransport.ts +++ b/node/src/test/test-WebRtcTransport.ts @@ -70,12 +70,42 @@ test('router.createWebRtcTransport() succeeds', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ listenInfos: [ - { protocol: 'udp', ip: '127.0.0.1', announcedAddress: '9.9.9.1' }, - { protocol: 'tcp', ip: '127.0.0.1', announcedAddress: '9.9.9.1' }, - { protocol: 'udp', ip: '0.0.0.0', announcedAddress: 'foo1.bar.org' }, - { protocol: 'tcp', ip: '0.0.0.0', announcedAddress: 'foo2.bar.org' }, - { protocol: 'udp', ip: '127.0.0.1', announcedAddress: undefined }, - { protocol: 'tcp', ip: '127.0.0.1', announcedAddress: undefined }, + { + protocol: 'udp', + ip: '127.0.0.1', + announcedAddress: '9.9.9.1', + portRange: { min: 2000, max: 3000 }, + }, + { + protocol: 'tcp', + ip: '127.0.0.1', + announcedAddress: '9.9.9.1', + portRange: { min: 2000, max: 3000 }, + }, + { + protocol: 'udp', + ip: '0.0.0.0', + announcedAddress: 'foo1.bar.org', + portRange: { min: 2000, max: 3000 }, + }, + { + protocol: 'tcp', + ip: '0.0.0.0', + announcedAddress: 'foo2.bar.org', + portRange: { min: 2000, max: 3000 }, + }, + { + protocol: 'udp', + ip: '127.0.0.1', + announcedAddress: undefined, + portRange: { min: 2000, max: 3000 }, + }, + { + protocol: 'tcp', + ip: '127.0.0.1', + announcedAddress: undefined, + portRange: { min: 2000, max: 3000 }, + }, ], enableTcp: true, preferUdp: true, @@ -254,6 +284,18 @@ test('router.createWebRtcTransport() with wrong arguments rejects with TypeError TypeError ); + await expect( + ctx.router!.createWebRtcTransport({ + listenInfos: [ + { + protocol: 'udp', + ip: '127.0.0.1', + portRange: { min: 4000, max: 3000 }, + }, + ], + }) + ).rejects.toThrow(TypeError); + await expect( // @ts-ignore ctx.router!.createWebRtcTransport({ listenIps: [123] }) @@ -290,7 +332,9 @@ test('router.createWebRtcTransport() with wrong arguments rejects with TypeError test('router.createWebRtcTransport() with non bindable IP rejects with Error', async () => { await expect( ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '8.8.8.8' }], + listenInfos: [ + { protocol: 'udp', ip: '8.8.8.8', portRange: { min: 2000, max: 3000 } }, + ], }) ).rejects.toThrow(Error); }, 2000); @@ -298,7 +342,12 @@ test('router.createWebRtcTransport() with non bindable IP rejects with Error', a test('webRtcTransport.getStats() succeeds', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ listenInfos: [ - { protocol: 'udp', ip: '127.0.0.1', announcedAddress: '9.9.9.1' }, + { + protocol: 'udp', + ip: '127.0.0.1', + announcedAddress: '9.9.9.1', + portRange: { min: 2000, max: 3000 }, + }, ], }); @@ -334,7 +383,12 @@ test('webRtcTransport.getStats() succeeds', async () => { test('webRtcTransport.connect() succeeds', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ listenInfos: [ - { protocol: 'udp', ip: '127.0.0.1', announcedAddress: '9.9.9.1' }, + { + protocol: 'udp', + ip: '127.0.0.1', + announcedAddress: '9.9.9.1', + portRange: { min: 2000, max: 3000 }, + }, ], }); @@ -368,7 +422,12 @@ test('webRtcTransport.connect() succeeds', async () => { test('webRtcTransport.connect() with wrong arguments rejects with TypeError', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ listenInfos: [ - { protocol: 'udp', ip: '127.0.0.1', announcedAddress: '9.9.9.1' }, + { + protocol: 'udp', + ip: '127.0.0.1', + announcedAddress: '9.9.9.1', + portRange: { min: 2000, max: 3000 }, + }, ], }); @@ -428,7 +487,12 @@ test('webRtcTransport.connect() with wrong arguments rejects with TypeError', as test('webRtcTransport.setMaxIncomingBitrate() succeeds', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ listenInfos: [ - { protocol: 'udp', ip: '127.0.0.1', announcedAddress: '9.9.9.1' }, + { + protocol: 'udp', + ip: '127.0.0.1', + announcedAddress: '9.9.9.1', + portRange: { min: 2000, max: 3000 }, + }, ], }); @@ -444,7 +508,9 @@ test('webRtcTransport.setMaxIncomingBitrate() succeeds', async () => { test('webRtcTransport.setMaxOutgoingBitrate() succeeds', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); await expect( @@ -459,7 +525,9 @@ test('webRtcTransport.setMaxOutgoingBitrate() succeeds', async () => { test('webRtcTransport.setMinOutgoingBitrate() succeeds', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); await expect( @@ -474,7 +542,9 @@ test('webRtcTransport.setMinOutgoingBitrate() succeeds', async () => { test('webRtcTransport.setMaxOutgoingBitrate() fails if value is lower than current min limit', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); await expect( @@ -493,7 +563,9 @@ test('webRtcTransport.setMaxOutgoingBitrate() fails if value is lower than curre test('webRtcTransport.setMinOutgoingBitrate() fails if value is higher than current max limit', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); await expect( @@ -512,7 +584,9 @@ test('webRtcTransport.setMinOutgoingBitrate() fails if value is higher than curr test('webRtcTransport.restartIce() succeeds', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); const previousIceUsernameFragment = @@ -535,7 +609,9 @@ test('webRtcTransport.restartIce() succeeds', async () => { test('transport.enableTraceEvent() succeed', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); // @ts-ignore @@ -563,7 +639,9 @@ test('transport.enableTraceEvent() succeed', async () => { test('transport.enableTraceEvent() with wrong arguments rejects with TypeError', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); // @ts-ignore @@ -584,7 +662,9 @@ test('transport.enableTraceEvent() with wrong arguments rejects with TypeError', test('WebRtcTransport events succeed', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); // Private API. @@ -715,7 +795,9 @@ test('WebRtcTransport events succeed', async () => { test('WebRtcTransport methods reject if closed', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); const onObserverClose = jest.fn(); @@ -778,7 +860,9 @@ test('WebRtcTransport emits "routerclose" if Router is closed', async () => { test('WebRtcTransport emits "routerclose" if Worker is closed', async () => { const webRtcTransport = await ctx.router!.createWebRtcTransport({ - listenInfos: [{ protocol: 'udp', ip: '127.0.0.1' }], + listenInfos: [ + { protocol: 'udp', ip: '127.0.0.1', portRange: { min: 2000, max: 3000 } }, + ], }); const onObserverClose = jest.fn();