-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathresource-test.js
163 lines (144 loc) · 4.74 KB
/
resource-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
'use strict';
const assert = require('bsert');
const {wire} = require('bns');
const {Resource} = require('../lib/dns/resource');
const {types} = wire;
describe('Resource', function() {
const json = {
records: [
{
type: 'DS',
keyTag: 57355,
algorithm: 8, // RSASHA256
digestType: 2, // SHA256
digest:
'95a57c3bab7849dbcddf7c72ada71a88146b141110318ca5be672057e865c3e2'
},
{
type: 'NS',
ns: 'ns1.hns.'
},
{
type: 'GLUE4',
ns: 'ns2.hns.',
address: '127.0.0.1'
},
{
type: 'GLUE4',
ns: 'ns3.some-other-domain.',
address: '10.20.30.40'
},
{
type: 'GLUE6',
ns: 'ns4.hns.',
address: '::1'
},
{
type: 'SYNTH4',
address: '127.0.0.2'
},
{
type: 'SYNTH6',
address: '::2'
},
{
type: 'TXT',
txt: ['hello world']
}
]
};
it('should serialize resource', () => {
const res1 = Resource.fromJSON(json);
const res2 = Resource.decode(res1.encode());
assert.deepStrictEqual(res1.toJSON(), json);
assert.deepStrictEqual(res1.toJSON(), res2.toJSON());
});
it('should synthesize a referral', () => {
const res = Resource.fromJSON(json);
const msg = res.toDNS('hns.', types.MX);
assert(msg.answer.length === 0);
assert(msg.authority.length === 8);
// Notice that the glue for `ns3.some-other-domain.` is omitted
assert(msg.additional.length === 4);
const [ns1, ns2, ns3, ns4, synth4, synth6, ds, rrsig] = msg.authority;
const [glue4, glue6, synthA, synthAAAA] = msg.additional;
assert.strictEqual(ns1.type, types.NS);
assert.strictEqual(ns1.name, 'hns.');
assert.strictEqual(ns2.type, types.NS);
assert.strictEqual(ns2.name, 'hns.');
assert.strictEqual(ns3.type, types.NS);
assert.strictEqual(ns3.name, 'hns.');
assert.strictEqual(ns4.type, types.NS);
assert.strictEqual(ns4.name, 'hns.');
assert.strictEqual(synth4.type, types.NS);
assert.strictEqual(synth4.name, 'hns.');
assert.strictEqual(synth6.type, types.NS);
assert.strictEqual(synth6.name, 'hns.');
assert.strictEqual(ds.type, types.DS);
assert.strictEqual(ds.name, 'hns.');
assert.strictEqual(rrsig.type, types.RRSIG);
assert.strictEqual(rrsig.name, 'hns.');
assert.strictEqual(glue4.type, types.A);
assert.strictEqual(glue4.name, 'ns2.hns.');
assert.strictEqual(glue6.type, types.AAAA);
assert.strictEqual(glue6.name, 'ns4.hns.');
assert.strictEqual(synthA.type, types.A);
assert.strictEqual(synthA.name, '_fs0000g._synth.');
assert.strictEqual(synthAAAA.type, types.AAAA);
assert.strictEqual(synthAAAA.name, '_00000000000000000000000008._synth.');
assert.strictEqual(ns1.data.ns, 'ns1.hns.');
assert.strictEqual(ns2.data.ns, 'ns2.hns.');
assert.strictEqual(synth4.data.ns, '_fs0000g._synth.');
assert.strictEqual(synth6.data.ns, '_00000000000000000000000008._synth.');
assert.bufferEqual(ds.data.digest, json.records[0].digest);
assert.strictEqual(glue4.data.address, '127.0.0.1');
assert.strictEqual(glue6.data.address, '::1');
assert.strictEqual(synthA.data.address, '127.0.0.2');
assert.strictEqual(synthAAAA.data.address, '::2');
});
it('should not return TXT from root zone if NS is present', () => {
const res = Resource.fromJSON(json);
const msg = res.toDNS('hns.', types.TXT);
assert(!msg.aa);
assert(msg.answer.length === 0);
});
it('should return TXT from root zone if NS is not present', () => {
const res = Resource.fromJSON({
records: [
{
type: 'TXT',
txt: ['hello world']
}
]
});
const msg = res.toDNS('hns.', types.TXT);
assert(msg.aa);
assert(msg.answer.length === 2);
const [txt, sig] = msg.answer;
assert.strictEqual(txt.type, types.TXT);
assert.strictEqual(txt.name, 'hns.');
assert.strictEqual(txt.data.txt.length, 1);
assert.strictEqual(txt.data.txt[0], 'hello world');
assert.strictEqual(sig.type, types.RRSIG);
assert.strictEqual(sig.name, 'hns.');
});
it('should synthesize an answer', () => {
const res = Resource.fromJSON(json);
const msg = res.toDNS('hns.', types.DS);
assert(msg.aa);
assert(msg.answer.length === 2);
const [ds, sig] = msg.answer;
assert.strictEqual(ds.type, types.DS);
assert.strictEqual(ds.name, 'hns.');
assert.strictEqual(sig.type, types.RRSIG);
assert.strictEqual(sig.name, 'hns.');
assert.strictEqual(ds.data.keyTag, 57355);
assert.bufferEqual(
ds.data.digest,
Buffer.from(
'95a57c3bab7849dbcddf7c72ada71a88146b141110318ca5be672057e865c3e2',
'hex'
)
);
});
});