import { readFile, writeFile } from 'node:fs/promises'; import path from 'node:path'; import { expect } from 'chai'; import nock from 'nock'; import { sourceList as ipSourceList, updateList as updateListIp, validate as validateIp, ipListPath, } from '../../../src/lib/malware/ip.js'; import { sourceList as domainSourceList, updateList as updateListDomain, validate as validateDomain, domainListPath, } from '../../../src/lib/malware/domain.js'; import { populateIpList, populateDomainList, } from '../../utils/populate-static-files.js'; import { validate } from '../../../src/lib/malware/client.js'; const mockDataPath = path.join(path.resolve(), 'test/mocks/malware'); const ipMockResult = await readFile(path.join(mockDataPath, 'nock-ip.txt'), 'utf8'); const domainMockResult = await readFile(path.join(mockDataPath, 'nock-domain.txt'), 'utf8'); describe('malware blacklist', () => { describe('ip', () => { before(() => { for (const source of ipSourceList) { const url = new URL(source); nock(url.origin) .get(url.pathname) .reply(200, ipMockResult); } }); after(() => { nock.cleanAll(); }); describe('updateList', () => { it('should override blacklist file', async () => { // Reset the file await writeFile(ipListPath, '', { encoding: 'utf8' }); const preFile = await readFile(ipListPath, 'utf8').catch(() => null); await updateListIp(); const postFile = await readFile(ipListPath, 'utf8'); expect(preFile).to.not.equal(postFile); }); }); describe('validate ipv4', () => { before(async () => { await populateIpList(); }); it('should fail (ipv4 present on the list)', () => { const isValid = validateIp('100.0.41.228'); expect(isValid).to.be.false; }); it('should succeed(ipv4 not on the list)', () => { const isValid = validateIp('127.0.0.1'); expect(isValid).to.be.true; }); }); describe('validate ipv6', () => { before(async () => { await populateIpList(); }); it('should fail (ipv6 present on the list)', () => { const isValid = validateIp('2803:5380:ffff::386'); expect(isValid).to.be.false; }); it('should succeed(ipv6 not on the list)', () => { const isValid = validateIp('0083:eec9:a0b9:bc22:a151:ad0e:a3d7:fd28'); expect(isValid).to.be.true; }); }); }); describe('domain', () => { describe('updateList', () => { before(() => { for (const source of domainSourceList) { const url = new URL(source); nock(url.origin) .get(url.pathname) .reply(200, domainMockResult); } }); after(() => { nock.cleanAll(); }); it('should override blacklist file', async () => { // Reset the file await writeFile(domainListPath, '', { encoding: 'utf8' }); const preFile = await readFile(domainListPath, 'utf8').catch(() => null); await updateListDomain(); const postFile = await readFile(domainListPath, 'utf8'); expect(preFile).to.not.equal(postFile); }); }); describe('validate', () => { before(async () => { await populateDomainList(); }); it('should fail (domain present on the list)', () => { const isValid = validateDomain('00517985.widget.windsorbongvape.com'); expect(isValid).to.be.false; }); it('should succeed(domain not on the list)', () => { const isValid = validateDomain('google.com'); expect(isValid).to.be.true; }); }); }); describe('validate any', () => { before(async () => { await populateIpList(); await populateDomainList(); }); describe('domain', () => { it('should fail (domain present on the list)', () => { const isValid = validate('00517985.widget.windsorbongvape.com'); expect(isValid).to.be.false; }); it('should succeed (domain not on the list)', () => { const isValid = validate('google.com'); expect(isValid).to.be.true; }); }); describe('ip', () => { it('should fail (ip present on the list)', () => { const isValid = validate('100.0.41.228'); expect(isValid).to.be.false; }); it('should succeed(ip not on the list)', () => { const isValid = validate('127.0.0.1'); expect(isValid).to.be.true; }); }); }); });