-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
268 lines (253 loc) · 7.19 KB
/
index.js
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const fs = require('fs')
const which = require('which')
const { spawn } = require('child_process')
const tape = require('fresh-tape')
const createServer = require('./dns-server')
const tests = require('./integration-tests')
const { Packet } = require('dns2')
const util = require('util')
const deepEqual = require('fast-deep-equal')
function inspect (obj, depth = 7) {
return util.inspect(obj, {
depth,
sorted: true,
breakLength: Infinity,
maxStringLength: Infinity
})
}
const SETUP = {}
for (const [key, test] of Object.entries(tests)) {
const subdomain = /^(t\d+):/.exec(key)[1]
const domain = `${subdomain}.${test.domain || 'dnslink.example.com'}`
test.targetDomain = domain
try {
const entriesByDomain = test.dns(domain)
if (!entriesByDomain) {
continue
}
for (const [testDomain, entries] of Object.entries(entriesByDomain)) {
if (SETUP[testDomain]) {
throw new Error(`Conflicting domain ${testDomain} for ${domain}`)
}
SETUP[testDomain] = normalizeDomainEntries(entries)
}
} catch (err) {
console.error(`Error while setting up ”${domain}” for "${key}" ↓`)
throw err
}
}
function normalizeDomainEntries (rawEntries) {
const result = {}
for (const rawEntry of Array.isArray(rawEntries) ? rawEntries : [rawEntries]) {
const entry = processEntry(rawEntry)
const byType = result[entry.type]
if (!byType) {
result[entry.type] = [entry]
} else {
byType.push(entry)
}
}
return result
}
function processEntry (rawEntry) {
if (typeof rawEntry === 'string') {
rawEntry = [rawEntry]
}
if (Array.isArray(rawEntry)) {
rawEntry = { data: rawEntry }
} else {
if (typeof rawEntry.data === 'string') {
rawEntry.data = [rawEntry.data]
}
}
if (!rawEntry.type) {
rawEntry.type = Packet.TYPE.TXT
}
if (!rawEntry.ttl) {
rawEntry.ttl = 100
}
return rawEntry
}
async function startServer () {
const server = await createServer(SETUP)
const addresses = server.addresses()
return {
serverOpts: {
udp: addresses.udp.port,
tcp: addresses.tcp.port,
doh: addresses.doh.port
},
close: () => server.close()
}
}
async function getCommand (cmd, serverOpts, signal) {
const executable = await getExecutable(cmd.shift())
return function (domain, options) {
return new Promise((resolve, reject) => {
if (signal.aborted) {
reject(Error('Aborted.'))
}
const end = (err, data) => {
signal.removeEventListener('abort', onAbort)
if (err) {
reject(err)
} else {
resolve(data)
}
}
const out = []
const err = []
const p = spawn(executable, [...cmd, ...toTestArgs(domain, options, serverOpts)])
p.on('error', end)
p.stdout.on('data', data => out.push(data))
p.stderr.on('data', data => err.push(data))
p.on('close', code => {
if (code) {
end(new Error(`Process Error [${code}]: ${Buffer.concat(err).toString()}`))
} else {
let json
const txt = Buffer.concat(out).toString()
try {
json = JSON.parse(txt)
} catch (error) {
end(new Error(`Non-JSON output: ${error}: ${txt}`))
}
end(null, json)
}
})
signal.addEventListener('abort', onAbort)
function onAbort () {
end(new Error('Aborted.'))
p.kill()
}
})
}
}
function toTestArgs (domain, options, serverOpts) {
return [domain, JSON.stringify({
...serverOpts,
...options
})]
}
function getExecutable (input) {
return which(input)
.catch(() => input)
.then(cmd => fs.promises.access(cmd, fs.constants.X_OK).then(
() => cmd,
() => { throw new Error(`EACCES: ${cmd} is not executable.`) }
))
}
function runTests (cmd, flags = {}, filter = {}) {
const test = tape.createHarness()
const _cmd = (domain, options) => cmd(domain, {
flags,
...options
})
test('Enabled Flags', t => {
let oneFound = false
for (const key in flags) {
oneFound = true
t.pass(`"${key}" enabled.`)
}
if (!oneFound) {
t.pass('No flags enabled.')
}
t.end()
})
Object.entries(tests).forEach(([name, { run, targetDomain, flag }]) => {
const key = /^(t\d.)?\s?/.exec(name)[1]
let skip
if (flag && !flags[flag]) {
skip = ` (Enable "${flag}" flag for this test to run.)`
} else if (filter.only && !filter.only.includes(key)) {
skip = ' (Disabled by --only)'
} else if (filter.skip && filter.skip.includes(key)) {
skip = ' (Disabled by --skip)'
}
test(`${name} (${targetDomain})${skip || ''}`, {
skip: skip !== undefined
}, async t => {
t.dnslink = dnslink.bind(null, flags, t)
await run(t, _cmd, targetDomain)
})
})
return test
}
function dnslink (flags, t, actual, expected) {
if (!actual) {
t.fail('Result is empty')
return
}
if (expected.error) {
if (!actual.error) {
t.fail(`Missing error ${expected.error.code}: ${JSON.stringify(actual)}`)
} else {
t.equals(actual.error.code, expected.error.code, `Expected error ${actual.error.code} == ${expected.error.code} of ${JSON.stringify(actual.error)}`)
}
return
}
t.deepEquals(excludeSpecial(actual), excludeSpecial(expected), inspect(expected))
if (actual.txtEntries) {
t.deepEquals(actual.txtEntries, compileTxtEntries(expected.links), 'Support for TXT entries')
}
if (flags.log && expected.log) {
let log = []
if (Array.isArray(actual.log)) {
log = actual.log
} else {
t.fail('No log given.')
}
const logSet = new Set(log)
for (const [expectedIndex, expectedEntry] of Object.entries(expected.log)) {
let foundIndex
for (const [actualIndex, actualEntry] of Object.entries(log)) {
if (deepEqual(expectedEntry, actualEntry)) {
logSet.delete(actualEntry)
foundIndex = actualIndex
break
}
}
if (foundIndex !== undefined) {
// Note: Redirect entries need to come in order but the entries inbetween may be shuffled.
if (foundIndex !== expectedIndex && expectedEntry.code === 'REDIRECT') {
t.fail('Expected log entry found, but at wrong index. actual=' + foundIndex + ' != expected=' + expectedIndex + ': ' + inspect(expectedEntry))
} else {
t.pass('Expected log entry returned: ' + inspect(expectedEntry))
}
} else {
t.fail('Log entry missing: ' + inspect(expectedEntry))
}
}
for (const logEntry of logSet) {
t.fail('Unexpected log entry: ' + inspect(logEntry))
}
}
}
function excludeSpecial (obj) {
obj = { ...obj }
delete obj.log
delete obj.txtEntries
return obj
}
function compileTxtEntries (links) {
const txtEntries = []
for (const ns of Object.keys(links).sort()) {
const linksByNS = links[ns]
for (const { identifier, ttl } of linksByNS.sort(sortByID)) {
txtEntries.push({ value: `/${ns}/${identifier}`, ttl })
}
}
return txtEntries
}
function sortByID (a, b) {
if (a.identifier > b.identifier) return 1
if (a.identifier < b.identifier) return -1
return 0
}
module.exports = {
getCommand,
startServer,
runTests,
SETUP,
tests
}