Skip to content

Commit 7bd8c77

Browse files
committed
Test InSim connection
1 parent 144117a commit 7bd8c77

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

tests/connection.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import Mitm from 'mitm';
2+
3+
import { createRoot } from '../src';
4+
5+
function stringToBytes(string: string) {
6+
return string.split('').map((char) => char.charCodeAt(0));
7+
}
8+
9+
describe('InSim connection', () => {
10+
let mitm: ReturnType<typeof Mitm>;
11+
12+
beforeEach(() => {
13+
mitm = Mitm();
14+
});
15+
16+
afterEach(() => {
17+
mitm.disable();
18+
});
19+
20+
it('should connect to InSim and send an IS_ISI packet', (done) => {
21+
// eslint-disable-next-line prefer-const
22+
let root: ReturnType<typeof createRoot>;
23+
24+
mitm.on('connection', (socket, opts) => {
25+
expect(opts.host).toEqual('127.0.0.1');
26+
expect(opts.port).toEqual(29999);
27+
28+
socket.on('data', (data) => {
29+
expect(data).toEqual(
30+
Buffer.from([
31+
11, // Size / 4
32+
1, // PacketType.ISP_ISI
33+
255, // ReqI = CONNECT_REQUEST_ID
34+
0, // Zero
35+
0, // UDPPort (1)
36+
0, // UDPPort (2)
37+
0, // InSimFlags.ISF_CON (1)
38+
0, // InSimFlags.ISF_CON (2)
39+
9, // InSimVer
40+
'!'.charCodeAt(0), // Prefix
41+
0, // Interval (1)
42+
0, // Interval (2)
43+
...stringToBytes('adminPassword\0\0\0'), // Admin[16]
44+
...stringToBytes('Test App\0\0\0\0\0\0\0\0'), // IName[16]
45+
]),
46+
);
47+
48+
root.disconnect();
49+
done();
50+
});
51+
});
52+
53+
root = createRoot({
54+
name: 'Test App',
55+
host: '127.0.0.1',
56+
port: 29999,
57+
adminPassword: 'adminPassword',
58+
prefix: '!',
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)