|
| 1 | +import os from 'os'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs'; |
| 4 | +import Logger, { LogLevel, StreamHandler } from '@matrixai/logger'; |
| 5 | +import * as testNatUtils from './utils'; |
| 6 | + |
| 7 | +describe('endpoint dependent NAT traversal', () => { |
| 8 | + const logger = new Logger('EDM NAT test', LogLevel.WARN, [ |
| 9 | + new StreamHandler(), |
| 10 | + ]); |
| 11 | + let dataDir: string; |
| 12 | + beforeEach(async () => { |
| 13 | + dataDir = await fs.promises.mkdtemp( |
| 14 | + path.join(os.tmpdir(), 'polykey-test-'), |
| 15 | + ); |
| 16 | + }); |
| 17 | + afterEach(async () => { |
| 18 | + await fs.promises.rm(dataDir, { |
| 19 | + force: true, |
| 20 | + recursive: true, |
| 21 | + }); |
| 22 | + }); |
| 23 | + test( |
| 24 | + 'Node1 behind EDM NAT connects to Node2', |
| 25 | + async () => { |
| 26 | + const { |
| 27 | + userPid, |
| 28 | + agent1Pid, |
| 29 | + password, |
| 30 | + dataDir, |
| 31 | + agent1NodePath, |
| 32 | + agent2NodeId, |
| 33 | + tearDownNAT, |
| 34 | + } = await testNatUtils.setupNAT('edm', 'dmz', logger); |
| 35 | + const { exitCode, stdout } = await testNatUtils.pkExecNs( |
| 36 | + userPid, |
| 37 | + agent1Pid, |
| 38 | + ['nodes', 'ping', agent2NodeId, '--format', 'json'], |
| 39 | + { |
| 40 | + PK_NODE_PATH: agent1NodePath, |
| 41 | + PK_PASSWORD: password, |
| 42 | + }, |
| 43 | + dataDir, |
| 44 | + ); |
| 45 | + expect(exitCode).toBe(0); |
| 46 | + expect(JSON.parse(stdout)).toEqual({ |
| 47 | + success: true, |
| 48 | + message: 'Node is Active.', |
| 49 | + }); |
| 50 | + await tearDownNAT(); |
| 51 | + }, |
| 52 | + global.defaultTimeout * 2, |
| 53 | + ); |
| 54 | + test( |
| 55 | + 'Node1 connects to Node2 behind EDM NAT', |
| 56 | + async () => { |
| 57 | + const { |
| 58 | + userPid, |
| 59 | + agent1Pid, |
| 60 | + password, |
| 61 | + dataDir, |
| 62 | + agent1NodePath, |
| 63 | + agent2NodeId, |
| 64 | + tearDownNAT, |
| 65 | + } = await testNatUtils.setupNAT('dmz', 'edm', logger); |
| 66 | + const { exitCode, stdout } = await testNatUtils.pkExecNs( |
| 67 | + userPid, |
| 68 | + agent1Pid, |
| 69 | + ['nodes', 'ping', agent2NodeId, '--format', 'json'], |
| 70 | + { |
| 71 | + PK_NODE_PATH: agent1NodePath, |
| 72 | + PK_PASSWORD: password, |
| 73 | + }, |
| 74 | + dataDir, |
| 75 | + ); |
| 76 | + expect(exitCode).toBe(0); |
| 77 | + expect(JSON.parse(stdout)).toEqual({ |
| 78 | + success: true, |
| 79 | + message: 'Node is Active.', |
| 80 | + }); |
| 81 | + await tearDownNAT(); |
| 82 | + }, |
| 83 | + global.defaultTimeout * 2, |
| 84 | + ); |
| 85 | + test( |
| 86 | + 'Node1 behind EDM NAT cannot connect to Node2 behind EDM NAT', |
| 87 | + async () => { |
| 88 | + const { |
| 89 | + userPid, |
| 90 | + agent1Pid, |
| 91 | + password, |
| 92 | + dataDir, |
| 93 | + agent1NodePath, |
| 94 | + agent2NodeId, |
| 95 | + tearDownNAT, |
| 96 | + } = await testNatUtils.setupNAT('edm', 'edm', logger); |
| 97 | + const { exitCode, stdout } = await testNatUtils.pkExecNs( |
| 98 | + userPid, |
| 99 | + agent1Pid, |
| 100 | + ['nodes', 'ping', agent2NodeId, '--format', 'json'], |
| 101 | + { |
| 102 | + PK_NODE_PATH: agent1NodePath, |
| 103 | + PK_PASSWORD: password, |
| 104 | + }, |
| 105 | + dataDir, |
| 106 | + ); |
| 107 | + expect(exitCode).not.toBe(0); |
| 108 | + expect(JSON.parse(stdout)).toEqual({ |
| 109 | + success: false, |
| 110 | + message: `No response received`, |
| 111 | + }); |
| 112 | + await tearDownNAT(); |
| 113 | + }, |
| 114 | + global.defaultTimeout * 2, |
| 115 | + ); |
| 116 | + test( |
| 117 | + 'Node1 behind EDM NAT cannot connect to Node2 behind EIM NAT', |
| 118 | + async () => { |
| 119 | + const { |
| 120 | + userPid, |
| 121 | + agent1Pid, |
| 122 | + password, |
| 123 | + dataDir, |
| 124 | + agent1NodePath, |
| 125 | + agent2NodeId, |
| 126 | + tearDownNAT, |
| 127 | + } = await testNatUtils.setupNAT('edm', 'eim', logger); |
| 128 | + const { exitCode, stdout } = await testNatUtils.pkExecNs( |
| 129 | + userPid, |
| 130 | + agent1Pid, |
| 131 | + ['nodes', 'ping', agent2NodeId, '--format', 'json'], |
| 132 | + { |
| 133 | + PK_NODE_PATH: agent1NodePath, |
| 134 | + PK_PASSWORD: password, |
| 135 | + }, |
| 136 | + dataDir, |
| 137 | + ); |
| 138 | + expect(exitCode).not.toBe(0); |
| 139 | + expect(JSON.parse(stdout)).toEqual({ |
| 140 | + success: false, |
| 141 | + message: `No response received`, |
| 142 | + }); |
| 143 | + await tearDownNAT(); |
| 144 | + }, |
| 145 | + global.defaultTimeout * 2, |
| 146 | + ); |
| 147 | +}); |
0 commit comments