Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
189 lines (152 loc) · 5.6 KB
/
Copy pathtest.js
File metadata and controls
189 lines (152 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const test = require('brittle')
const {
CAPABILITY_LEN,
HEADER_LEN,
MODE_TUNNEL,
MODE_PROBE,
encodeHeader,
decodeHeader,
encodeProbeResponse,
decodeProbeResponse
} = require('./index.js')
test('constants', function (t) {
t.is(CAPABILITY_LEN, 32)
t.is(HEADER_LEN, 33)
t.is(MODE_TUNNEL, 0)
t.is(MODE_PROBE, 1)
})
test('encodeHeader - tunnel mode', function (t) {
const cap = Buffer.alloc(32, 0xab)
const header = encodeHeader(cap, MODE_TUNNEL)
t.is(header.length, HEADER_LEN)
t.alike(Buffer.from(header.subarray(0, 32)), cap)
t.is(header[32], MODE_TUNNEL)
})
test('encodeHeader - probe mode', function (t) {
const cap = Buffer.alloc(32, 0xff)
const header = encodeHeader(cap, MODE_PROBE)
t.is(header[32], MODE_PROBE)
})
test('encodeHeader - rejects wrong capability length', function (t) {
t.exception(() => encodeHeader(Buffer.alloc(16), 0), /capability must be 32 bytes/)
t.exception(() => encodeHeader(Buffer.alloc(0), 0), /capability must be 32 bytes/)
t.exception(() => encodeHeader(Buffer.alloc(64), 0), /capability must be 32 bytes/)
})
test('encodeHeader - rejects invalid mode', function (t) {
const cap = Buffer.alloc(32)
t.exception(() => encodeHeader(cap, -1), /mode must be a byte/)
t.exception(() => encodeHeader(cap, 256), /mode must be a byte/)
t.exception(() => encodeHeader(cap, 1.5), /mode must be a byte/)
t.exception(() => encodeHeader(cap, NaN), /mode must be a byte/)
})
test('decodeHeader - round trip', function (t) {
const cap = Buffer.alloc(32)
for (let i = 0; i < 32; i++) cap[i] = i
const encoded = encodeHeader(cap, MODE_PROBE)
const decoded = decodeHeader(encoded)
t.alike(Buffer.from(decoded.capability), cap)
t.is(decoded.mode, MODE_PROBE)
t.is(decoded.leftover.length, 0)
})
test('decodeHeader - with leftover bytes', function (t) {
const cap = Buffer.alloc(32, 0x01)
const header = encodeHeader(cap, MODE_TUNNEL)
const extra = Buffer.from([0xde, 0xad])
const combined = Buffer.concat([header, extra])
const decoded = decodeHeader(combined)
t.alike(decoded.leftover, extra)
})
test('decodeHeader - returns null on short input', function (t) {
t.is(decodeHeader(Buffer.alloc(0)), null)
t.is(decodeHeader(Buffer.alloc(32)), null)
})
test('encodeProbeResponse - defaults', function (t) {
const probe = encodeProbeResponse()
t.is(probe[0], 0)
t.is(probe[1], 0)
t.is(probe[2], 0)
t.is(probe[3], 0)
t.is(probe.length, 4)
})
test('encodeProbeResponse - with host and port', function (t) {
const probe = encodeProbeResponse({ port: 8080, host: '127.0.0.1' })
t.is((probe[0] << 8) | probe[1], 8080)
t.is(probe[2], 0)
t.is(probe[3], 9)
t.is(probe.length, 4 + 9)
})
test('encodeProbeResponse - udp flag', function (t) {
const probe = encodeProbeResponse({ udp: true })
t.is(probe[2], 1)
const probe2 = encodeProbeResponse({ udp: false })
t.is(probe2[2], 0)
})
test('encodeProbeResponse - rejects host over 255 bytes', function (t) {
const longHost = 'a'.repeat(256)
t.exception(() => encodeProbeResponse({ host: longHost }), /host exceeds 255 bytes/)
})
test('encodeProbeResponse - rejects invalid port', function (t) {
t.exception(() => encodeProbeResponse({ port: -1 }), /port must fit in uint16/)
t.exception(() => encodeProbeResponse({ port: 0x10000 }), /port must fit in uint16/)
})
test('decodeProbeResponse - round trip', function (t) {
const original = { port: 443, host: 'example.com', udp: false }
const encoded = encodeProbeResponse(original)
const decoded = decodeProbeResponse(encoded)
t.is(decoded.port, 443)
t.is(decoded.host, 'example.com')
t.is(decoded.udp, false)
t.is(decoded.leftover.length, 0)
})
test('decodeProbeResponse - udp round trip', function (t) {
const encoded = encodeProbeResponse({ port: 53, host: '8.8.8.8', udp: true })
const decoded = decodeProbeResponse(encoded)
t.is(decoded.port, 53)
t.is(decoded.host, '8.8.8.8')
t.is(decoded.udp, true)
})
test('decodeProbeResponse - with leftover bytes', function (t) {
const encoded = encodeProbeResponse({ port: 80, host: 'localhost' })
const extra = Buffer.from([0x01, 0x02, 0x03])
const combined = Buffer.concat([encoded, extra])
const decoded = decodeProbeResponse(combined)
t.alike(decoded.leftover, extra)
})
test('decodeProbeResponse - returns null on short input', function (t) {
t.is(decodeProbeResponse(Buffer.alloc(0)), null)
t.is(decodeProbeResponse(Buffer.alloc(3)), null)
})
test('decodeProbeResponse - returns null when host is truncated', function (t) {
const buf = Buffer.from([0x00, 0x50, 0x00, 0x05, 0x61])
t.is(decodeProbeResponse(buf), null)
})
test('full header + probe round trip', function (t) {
const cap = Buffer.alloc(32, 0xcc)
const header = encodeHeader(cap, MODE_PROBE)
const probe = encodeProbeResponse({ port: 9999, host: '10.0.0.1', udp: true })
const wire = Buffer.concat([header, probe])
const h = decodeHeader(wire)
t.is(h.mode, MODE_PROBE)
t.alike(h.capability, cap)
const p = decodeProbeResponse(h.leftover)
t.is(p.port, 9999)
t.is(p.host, '10.0.0.1')
t.is(p.udp, true)
t.is(p.leftover.length, 0)
})
test('encodeProbeResponse - empty host', function (t) {
const encoded = encodeProbeResponse({ port: 1234, host: '' })
const decoded = decodeProbeResponse(encoded)
t.is(decoded.port, 1234)
t.is(decoded.host, '')
})
test('encodeProbeResponse - max valid port', function (t) {
const encoded = encodeProbeResponse({ port: 65535 })
const decoded = decodeProbeResponse(encoded)
t.is(decoded.port, 65535)
})
test('encodeHeader - max valid mode byte', function (t) {
const cap = Buffer.alloc(32)
const header = encodeHeader(cap, 255)
t.is(header[32], 255)
})