-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
255 lines (219 loc) · 7.35 KB
/
test.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
var tape = require('tape')
var createDatDNS = require('./index')
var datDns = createDatDNS()
var cabalDns = createDatDNS({
hashRegex: /^[0-9a-f]{64}?$/i,
recordName: 'cabal',
protocolRegex: /^cabal:\/\/([0-9a-f]{64})/i,
txtRegex: /^"?cabalkey=([0-9a-f]{64})"?$/i
})
var FAKE_DAT = 'f'.repeat(64)
tape('Successful test against cblgh.org', function (t) {
cabalDns.resolveName('cblgh.org', function (err, name) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(name))
cabalDns.resolveName('cblgh.org').then(function (name2) {
t.equal(name, name2)
t.end()
})
})
})
tape('Works for keys', function (t) {
cabalDns.resolveName('14bc77d788fdaf07b89b28e9d276e47f2e44011f4adb981921056e1b3b40e99e', function (err, name) {
t.error(err)
t.equal(name, '14bc77d788fdaf07b89b28e9d276e47f2e44011f4adb981921056e1b3b40e99e')
t.end()
})
})
tape('Successful test against pfrazee.hashbase.io', function (t) {
datDns.resolveName('pfrazee.hashbase.io', function (err, name) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(name))
datDns.resolveName('pfrazee.hashbase.io').then(function (name2) {
t.equal(name, name2)
t.end()
})
})
})
tape('Works for keys', function (t) {
datDns.resolveName('40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9', function (err, name) {
t.error(err)
t.equal(name, '40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9')
t.end()
})
})
tape('Works for versioned keys and URLs', function (t) {
datDns.resolveName('40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9+5', function (err, name) {
t.error(err)
t.equal(name, '40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9')
datDns.resolveName('pfrazee.hashbase.io+5', function (err, name) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(name))
t.end()
})
})
})
tape('Works for non-numeric versioned keys and URLs', function (t) {
datDns.resolveName('40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9+foo', function (err, name) {
t.error(err)
t.equal(name, '40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9')
datDns.resolveName('pfrazee.hashbase.io+foo', function (err, name) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(name))
t.end()
})
})
})
tape('Works for full URLs', function (t) {
datDns.resolveName('dat://40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9', function (err, name) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(name))
datDns.resolveName('dat://pfrazee.hashbase.io/foo.txt?bar=baz', function (err, name) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(name))
t.end()
})
})
})
tape('A bad hostname fails gracefully', function (t) {
datDns.resolveName('example.com', {ignoreCache: true}, function (err, name) {
t.ok(err)
t.notOk(name)
datDns.resolveName(1234, function (err, name) {
t.ok(err)
t.notOk(name)
datDns.resolveName('foo bar', {ignoreCache: true}, function (err, name) {
t.ok(err)
t.notOk(name)
t.end()
})
})
})
})
tape('A bad DNS record fails gracefully', function (t) {
datDns.resolveName('bad-dat-record1.beakerbrowser.com', {ignoreCache: true}, function (err, name) {
t.ok(err)
t.notOk(name)
t.end()
})
})
tape('Unqualified domain fails gracefully', function (t) {
datDns.resolveName('bad-dat-domain-name', {ignoreCache: true}, function (err, name) {
t.ok(err)
t.notOk(name)
t.end()
})
})
tape('Successful test against dns-test-setup.dat-ecosystem.org', function (t) {
datDns.resolveName('dns-test-setup.dat-ecosystem.org', {ignoreCache: true}, function (err, name) {
t.error(err)
t.equals(name, '444231b5589a5099aa3610a8ee550dcd454c3e33f4cac93b7d41b6b850cde444')
datDns.resolveName('dns-test-setup.dat-ecosystem.org').then(function (name2) {
t.equal(name, name2)
t.end()
}).catch(function (err) {
t.error(err)
t.end()
})
})
})
tape('Successful test against dns-test-setup.dat-ecosystem.org (no dns-over-https)', function (t) {
datDns.resolveName('dns-test-setup.dat-ecosystem.org', {noDnsOverHttps: true, ignoreCache: true})
.then(function (name) {
t.equals(name, '111231b5589a5099aa3610a8ee550dcd454c3e33f4cac93b7d41b6b850cde111')
return datDns.resolveName('dns-test-setup.dat-ecosystem.org')
.then(function (name2) {
t.equal(name, name2)
})
})
.then(
function () { t.end() },
function (err) {
t.error(err)
t.end()
}
)
})
createDatDNS.DEFAULT_DNS_PROVIDERS.forEach(function (provider) {
const dns = createDatDNS({
dnsHost: provider[0],
dnsPort: provider[1],
dnsPath: provider[2]
})
tape('Successful test against dns-test-setup.dat-ecosystem.org (no well-known/dat) (' + provider[0] + ':' + provider[1] + provider[2] + ')', function (t) {
dns.resolveName('dns-test-setup.dat-ecosystem.org', {noWellknownDat: true, ignoreCache: true})
.then(function (name) {
t.equal(name, '444231b5589a5099aa3610a8ee550dcd454c3e33f4cac93b7d41b6b850cde444' /* the second txt entry */)
return dns.resolveName('dns-test-setup.dat-ecosystem.org')
.then(function (name2) {
t.equal(name, name2, 'cache test')
})
})
.then(
function () { t.end() },
function (err) {
t.error(err)
t.end()
}
)
})
})
tape('Successful test against dns-test-setup.dat-ecosystem.org (no well-known/dat)', function (t) {
datDns.resolveName('dns-test-setup.dat-ecosystem.org', {noWellknownDat: true, ignoreCache: true}, function (err, name) {
t.error(err)
t.equal(name, '444231b5589a5099aa3610a8ee550dcd454c3e33f4cac93b7d41b6b850cde444' /* the second txt entry */)
datDns.resolveName('dns-test-setup.dat-ecosystem.org').then(function (name2) {
t.equal(name, name2)
t.end()
}).catch(function (err) {
t.error(err)
t.end()
})
})
})
tape('List cache', function (t) {
t.is(Object.keys(datDns.listCache()).length, 6)
t.end()
})
tape('Persistent fallback cache', function (t) {
t.plan(8)
var persistentCache = {
read: function (name, err) {
if (name === 'foo') return '40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9'
throw err
},
write: function (name, key, ttl) {
t.deepEqual(name, 'pfrazee.hashbase.io')
t.ok(/[0-9a-f]{64}/.test(key))
}
}
var datDns = createDatDNS({persistentCache})
datDns.resolveName('pfrazee.hashbase.io', function (err, key) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(key))
datDns.resolveName('foo', function (err, key) {
t.error(err)
t.deepEqual(key, '40a7f6b6147ae695bcbcff432f684c7bb5291ea339c28c1755896cdeb80bd2f9')
datDns.resolveName('bar', function (err, key) {
t.ok(err)
t.notOk(key)
t.end()
})
})
})
})
tape('Persistent fallback cache doesnt override live results', function (t) {
var persistentCache = {
read: function (name, err) {
if (name === 'pfrazee.hashbase.io') return 'from-cache'
throw err
},
write: function (name, key, ttl) {}
}
var datDns = createDatDNS({persistentCache})
datDns.resolveName('pfrazee.hashbase.io', function (err, key) {
t.error(err)
t.ok(/[0-9a-f]{64}/.test(key))
t.end()
})
})