/
dnsblock-spec.js
211 lines (192 loc) · 8.74 KB
/
dnsblock-spec.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
'use strict';
const expect = require('chai').expect;
const dnsblock = require('../dnsblock.js');
const crypto = require('crypto');
const fs = require('fs');
const ADSERVER = 'adserver.net';
const WWWADSERVER = 'www.adserver.net';
const WIKIPEDIA = 'www.wikipedia.org';
const REALADSERVER = 'ads.adnet.am';
function shuffle(b) {
let a = b.slice();
let j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function checksum (str, algorithm, encoding) {
return crypto
.createHash(algorithm || 'md5')
.update(str, 'utf8')
.digest(encoding || 'hex');
}
describe('Testing core functionality', () => {
describe('Testing the domain cache', () => {
it(`should accept domain ${ADSERVER} to block`, () => {
expect(!!dnsblock.blockedDomains.blockDomain(ADSERVER)).to.be.true;
});
it(`should detect domain ${WWWADSERVER} as already blocked`, () => {
expect(dnsblock.blockedDomains.blockDomain(WWWADSERVER)).to.be.false;
});
it(`should detect ${ADSERVER} as blocked`, () => {
expect(!!dnsblock.blockedDomains.isBlocked(ADSERVER)).to.be.true;
});
it(`should give the blocker for ADSERVER as ADSERVER`, () => {
expect(dnsblock.blockedDomains.isBlocked(ADSERVER)).to.equal(ADSERVER);
});
it(`should detect ${WIKIPEDIA} as not blocked`, () => {
expect(dnsblock.blockedDomains.isBlocked(WIKIPEDIA)).to.be.false;
});
});
describe('Testing the whitelisting', () => {
it(`should accept domain ${WIKIPEDIA} to whitelist`, () => {
expect(!!dnsblock.whitelistedDomains.whitelistDomain(WIKIPEDIA, 'dictionary')).to.be.true;
});
it(`should detect ${WIKIPEDIA} as whitelisted`, () => {
expect(!!dnsblock.whitelistedDomains.isWhitelisted(WIKIPEDIA)).to.be.true;
});
it(`should detect domain wikipedia.org as whitelisted`, () => {
expect(!!dnsblock.whitelistedDomains.isWhitelisted('wikipedia.org')).to.be.true;
});
it(`should detect domain ${'ftp.' + WIKIPEDIA} as not whitelisted`, () => {
expect(dnsblock.whitelistedDomains.isWhitelisted('ftp.' + WIKIPEDIA)).to.be.false;
});
it(`should detect domain ${ADSERVER} as not whitelisted`, () => {
expect(dnsblock.whitelistedDomains.isWhitelisted(ADSERVER)).to.be.false;
});
});
describe('Testing whitelisting and blocking together', () => {
it('should not block whitelisted domain', () => {
});
});
describe('Test line parsing', () => {
it('should parse a domain and a comment', () => {
expect(dnsblock.processHostsLine(' bad.adspammer.com # this is my comment ')).to.deep.equal({
domain: 'bad.adspammer.com',
comment: ' # this is my comment'
});
});
it('should throw an exception when domain is invalid', () => {
let badDomain = 'bad_domain@';
expect(() => dnsblock.processHostsLine(badDomain)).to.throw(`[${badDomain}] is not a valid domain, comment: []`);
});
});
describe('Test the regex for commented out lines', () => {
it('should detect an empty line as a comment', () => {
expect(dnsblock.isComment('')).to.be.true;
});
it('should detect a line with just spaces as a comment', () => {
expect(dnsblock.isComment(' ')).to.be.true;
});
it('should detect a line commented with //', () => {
expect(dnsblock.isComment('// comment')).to.be.true;
});
it('should detect a line starting with spaces and // as a comment', () => {
expect(dnsblock.isComment(' // ')).to.be.true;
});
it('should detect a line commented with #', () => {
expect(dnsblock.isComment('# something')).to.be.true;
});
it('should detect a line starting with spaces and # as a comment', () => {
expect(dnsblock.isComment(' #')).to.be.true;
});
it('should detect a normal domain as not being a commented out line', () => {
expect(dnsblock.isComment('www.google.com')).to.be.false;
});
it('should detect a normal domain followed by a comment symbol as not being a commented out line', () => {
expect(dnsblock.isComment('www.google.com // a comment')).to.be.false;
});
});
describe('Test the BlockedDomain class', () => {
it('should handle no comment', () => {
expect(new dnsblock.BlockedDomain(WIKIPEDIA).serialize()).to.equal(WIKIPEDIA);
});
});
});
describe('Testing file processing', () => {
describe('Test processing the hosts blocked file', () => {
dnsblock.blockedDomains.resetCache();
it(`should process the blocked hosts file`, () => {
return dnsblock.processHostsBlocked(dnsblock.blockedDomains, 'test/test1.domains.blocked', dnsblock.whitelistedDomains);
});
it(`domain ${REALADSERVER} should now be blocked`, () => {
expect(dnsblock.blockedDomains.isBlocked(REALADSERVER)).to.equal(REALADSERVER);
});
it(`domain ${WIKIPEDIA} should not be blocked`, () => {
expect(dnsblock.blockedDomains.isBlocked(WIKIPEDIA)).to.be.false;
});
});
});
describe('Testing output', () => {
const zoneFileName = 'zoneinfo-test.txt';
before(() => {try {fs.unlinkSync(zoneFileName) } catch(e){}});
// after(() => fs.unlinkSync(zoneFileName));
describe('Testing serialization', () => {
const domains = ['promotie.ads', 'publicitate.ads', 'reclame.ads', 'neptune.appads.com', 'adserver.net' ];
it('should produce a sorted list of domains', () => {
dnsblock.blockedDomains.resetCache();
shuffle(domains).forEach( (domain) => dnsblock.blockedDomains.blockDomain(domain) );
let result = dnsblock.blockedDomains.serializeBlockedDomains();
result.forEach((blockedDomain, index) => expect(blockedDomain.domain).to.be.equal(domains[index]) );
});
let zoneChecksum;
it('should write a zone file', (done) => {
dnsblock.generateZone(dnsblock.blockedDomains, zoneFileName);
fs.readFile(zoneFileName, (err, data) => {
zoneChecksum = checksum(data);
done(err);
});
});
it('checksum of the file should be', () => {
expect(zoneChecksum).to.equal('56ce8d73435a693cf160a0a2ee6d7dc7');
});
});
});
describe('Test command line', () => {
describe('Parse the command line parameters', () => {
it('should interpret correctly the names of the files', () => {
let commandLineParams = ['node', 'dnsblock.js', 'myhost.txt', 'mydom.blocked', 'myquery.log', 'myzones.adblock', 'unu', 'doi', 'trei' ];
let params = dnsblock.processCommandLine(commandLineParams);
expect(params).to.deep.equal({
hostsBlocked: 'myhost.txt',
hostsWhitelisted: '',
rpzFile: 'rpz.db',
domainsBlocked: 'mydom.blocked',
zonesFile: 'myzones.adblock',
dnsQueryLog: 'myquery.log',
command: 'unu',
commandParams: ['doi', 'trei']
});
});
});
});
describe('Test filtering', () => {
describe('Parse query log', () => {
beforeEach(() => dnsblock.blockedDomains.resetCache());
it('should parse a query for a blocked domain', () => {
});
it('should parse a query for a whitelisted domain', () => {
});
it('should resolve a query client', async () => {
let line = '22-Dec-2017 21:09:28.799 client 127.0.0.1#55983 (shavar.prod.mozaws.net): view internal: query: shavar.prod.mozaws.net IN A +ED (127.0.0.1)';
let parsedLine = await dnsblock.filterLine(line);
expect(parsedLine).to.be.equal('22-Dec-2017 21:09:28.799 client: localhost, query: shavar.prod.mozaws.net');
});
it('should not choke on the line that caused filtering bug 1', async () => {
let line = '10-Feb-2018 20:35:16.091 client 123.123.123.12#60697 (host.com): view external: query: host.com IN AXFR -T (10.0.0.1)';
let parsedLine = await dnsblock.filterLine(line);
});
});
describe('Test reverse DNS', () => {
let msIp = '184.24.199.187';
let msAkamai = 'a184-24-199-187.deploy.static.akamaitechnologies.com';
it(`should translate ${msIp} to ${msAkamai}`, async () => {
const hostname = await dnsblock.reverseDNS(msIp);
expect(hostname).to.equal(msAkamai);
});
});
});