-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdns_server_test.ts
74 lines (66 loc) · 2.86 KB
/
dns_server_test.ts
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
import { assertEquals} from "https://deno.land/std/testing/asserts.ts";
import { DNSServer } from "./dns_server.ts";
import { DNSConfig } from "./dns_server_config.ts";
Deno.test('DNSServer correctly answers basic A record request', () => {
// Direct from a wireshark DNS capture made by Win10 `nslookup`.
const data = Uint8Array.from([
0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x65, 0x78, 0x61,
0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
]);
// Based on a wireshark DNS capture - TTL, IP and response code changed.
const expectedResponse = Uint8Array.from([
0x00, 0x04, 0x80, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x65, 0x78, 0x61,
0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0x07, 0x65, 0x78,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
0x00, 0x64, 0x00, 0x04, 0x7f, 0x00, 0x00, 0x01
]);
const config:DNSConfig = {
'example.com': {
ttl: 100,
class: {
'IN': {
'A': '127.0.0.1',
}
}
}
};
const server = new DNSServer(config);
const response = server.HandleRequest(data);
assertEquals(response, expectedResponse);
});
Deno.test('DNSServer handles a CNAME + A record request.', () => {
const request = Uint8Array.from([0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77,
0x09, 0x62, 0x79, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x78, 0x79, 0x7a, 0x00, 0x00,
0x01, 0x00, 0x01
]);
const expectedResponse = Uint8Array.from([0x00, 0x04, 0x80, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, 0x77, 0x77,
0x09, 0x62, 0x79, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03, 0x78, 0x79, 0x7a, 0x00, 0x00,
0x01, 0x00, 0x01, 0x03, 0x77, 0x77, 0x77, 0x09, 0x62, 0x79, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c,
0x65, 0x03, 0x78, 0x79, 0x7a, 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x2a, 0x30, 0x00, 0x18,
0x08, 0x77, 0x65, 0x62, 0x72, 0x65, 0x64, 0x69, 0x72, 0x03, 0x76, 0x69, 0x70, 0x05, 0x67, 0x61,
0x6e, 0x64, 0x69, 0x03, 0x6e, 0x65, 0x74, 0x00, 0x08, 0x77, 0x65, 0x62, 0x72, 0x65, 0x64, 0x69,
0x72, 0x03, 0x76, 0x69, 0x70, 0x05, 0x67, 0x61, 0x6e, 0x64, 0x69, 0x03, 0x6e, 0x65, 0x74, 0x00,
0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x2a, 0x30, 0x00, 0x04, 0xd9, 0x46, 0xb8, 0x32
]);
const config:DNSConfig = {
'www.byexample.xyz': {
ttl: 10800,
class: {
'IN': {
'CNAME': 'webredir.vip.gandi.net',
}
}
},
'webredir.vip.gandi.net': {
ttl: 10800,
class: {
'IN': {
'A': '217.70.184.50',
}
}
},
};
const server = new DNSServer(config);
const response = server.HandleRequest(request);
assertEquals(response, expectedResponse);
});