-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.ts
186 lines (167 loc) · 6.6 KB
/
index.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
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
import {parseZone, stringifyZone} from "./index.ts";
import {readFileSync} from "node:fs";
test("roundtrip", () => {
const str = readFileSync(new URL("fixtures/simple.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("basic", () => {
const str = readFileSync(new URL("fixtures/origin.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
expect(parseZoned.records.length).toEqual(5);
for (const record of parseZoned.records) {
expect(record.name).toBeTruthy();
expect(record.ttl).toBeTruthy();
expect(record.class).toBeTruthy();
expect(record.type).toBeTruthy();
expect(record.content).toBeTruthy();
expect(typeof record.comment === "string" || record.comment === null).toBeTruthy();
expect(!record.name.includes("@")).toBeTruthy();
expect(!record.content.includes("@")).toBeTruthy();
}
parseZoned.origin = "testzone.com";
const withOrigin = stringifyZone(parseZoned);
expect(/^\$ORIGIN\s.+$/m.test(withOrigin)).toBe(true);
});
test("origin", () => {
const data = {
"origin": "originzone.com",
"records": [
{
"name": "originzone.com.",
"ttl": 3600,
"class": "IN",
"type": "SOA",
"content": "originzone.com. root.originzone.com. 2031242781 7200 3600 86400 3600",
"comment": null
},
{
"name": "a.originzone.com.",
"ttl": 60,
"class": "IN",
"type": "A",
"content": "1.2.3.4",
"comment": "a comment"
},
],
};
const result = stringifyZone(data, {sections: true, dots: true});
expect(/^@/m.test(result)).toBe(true);
});
test("ttl", () => {
const str = readFileSync(new URL("fixtures/ttl.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
expect(parseZoned.records.length).toEqual(5);
for (const record of parseZoned.records) {
expect(record.name).toBeTruthy();
expect(record.ttl).toBeTruthy();
expect(record.class).toBeTruthy();
expect(record.type).toBeTruthy();
expect(record.content).toBeTruthy();
expect(typeof record.comment === "string" || record.comment === null).toBeTruthy();
expect(!record.name.includes("@")).toBeTruthy();
expect(!record.content.includes("@")).toBeTruthy();
}
parseZoned.ttl = 60;
const withTTL = stringifyZone(parseZoned);
expect(/^\$TTL\s[0-9]+$/m.test(withTTL)).toBe(true);
});
test("header", () => {
const str = readFileSync(new URL("fixtures/header.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("replaceOrigin", () => {
const str = readFileSync(new URL("fixtures/origin.txt", import.meta.url), "utf8");
const replaceOrigin = "another.com";
const parseZoned = parseZone(str, {replaceOrigin});
expect(parseZoned.origin).toEqual(replaceOrigin);
});
test("nosections", () => {
const str = readFileSync(new URL("fixtures/nosections.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned, {sections: false});
expect(roundtripped).toEqual(str);
});
test("noname", () => {
const str = readFileSync(new URL("fixtures/noname.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("nottl", () => {
const str = readFileSync(new URL("fixtures/nottl.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
for (const record of parseZoned.records) {
expect(typeof record.name).toEqual("string");
expect(typeof record.ttl).toEqual("number");
expect(record.class).toBeTruthy();
expect(record.type).toBeTruthy();
expect(record.content).toBeTruthy();
}
});
test("ttlunits", () => {
const str = readFileSync(new URL("fixtures/ttlunits.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
for (const record of parseZoned.records) {
if (record.type === "SOA") {
expect(record.ttl).toEqual(3600);
}
expect(typeof record.name).toEqual("string");
expect(typeof record.ttl).toEqual("number");
expect(!Number.isNaN(record.ttl)).toBeTruthy();
expect(record.class).toBeTruthy();
expect(record.type).toBeTruthy();
expect(record.content).toBeTruthy();
}
});
test("semicontent", () => {
const str = readFileSync(new URL("fixtures/semicontent.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("dash", () => {
const str = readFileSync(new URL("fixtures/dash.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("wildcard", () => {
const str = readFileSync(new URL("fixtures/wildcard.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("dots", () => {
const dotsstr = readFileSync(new URL("fixtures/dots.txt", import.meta.url), "utf8");
const nodotsstr = readFileSync(new URL("fixtures/nodots.txt", import.meta.url), "utf8");
expect(stringifyZone(parseZone(dotsstr, {dots: false}), {dots: false})).toEqual(dotsstr);
expect(stringifyZone(parseZone(dotsstr, {dots: false}), {dots: true})).toEqual(dotsstr);
expect(stringifyZone(parseZone(dotsstr, {dots: true}), {dots: true})).toEqual(dotsstr);
expect(stringifyZone(parseZone(nodotsstr, {dots: false}), {dots: false})).toEqual(nodotsstr);
expect(stringifyZone(parseZone(nodotsstr, {dots: false}), {dots: true})).toEqual(dotsstr);
expect(stringifyZone(parseZone(nodotsstr, {dots: true}), {dots: true})).toEqual(dotsstr);
});
test("comments", () => {
const str = readFileSync(new URL("fixtures/comments.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("soa parens", () => {
const str = readFileSync(new URL("fixtures/soaparens.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});
test("type65534", () => {
const str = readFileSync(new URL("fixtures/type65534.txt", import.meta.url), "utf8");
const parseZoned = parseZone(str);
const roundtripped = stringifyZone(parseZoned);
expect(roundtripped).toEqual(str);
});