From c23efa3eedf3b56058d991492e30c24cd186d138 Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Wed, 29 Mar 2017 17:24:01 -0700 Subject: [PATCH] Added smoketest for our exclusion list. This queries the leveldb file directly and spot checks a few known includes/excludes. Auditors: @mrose17, @bbondy, @alexwykoff Test Plan: 1. Make sure you have latest npm packages installed (from this PR) 2. In window 1, run `npm run watch-all`. 3. In window 2, run LEDGER_VERBOSE=1 LEDGER_RULES_TESTING=1 npm start` 4. Wait for ledger to finish downloading the entries (took me 6 minutes, may take more/less) 5. When finished, close the browser 6. In window 2, run `npm run test -- --grep="smoketest for our exclusion list"` --- test/taxonomy/exclusionListTest.js | 125 +++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 test/taxonomy/exclusionListTest.js diff --git a/test/taxonomy/exclusionListTest.js b/test/taxonomy/exclusionListTest.js new file mode 100644 index 00000000000..036e4fecd2d --- /dev/null +++ b/test/taxonomy/exclusionListTest.js @@ -0,0 +1,125 @@ +/* global describe, it, before */ + +/* jshint asi: true, node: true, laxbreak: true, laxcomma: true, undef: true, unused: true */ +const levelup = require('level') +const assert = require('assert') + +const includes = (db, domain, done) => { + db.get('domain:' + domain, function (err, value) { + assert.equal(!!err, true) + done() + }) +} + +const excludes = (db, domain, done) => { + db.get('domain:' + domain, function (err, value) { + if (err) throw err + const parsedValue = JSON.parse(value) + assert.equal(parsedValue && parsedValue.exclude, true) + done() + }) +} + +const excludesSubdomain = (db, subdomain, done) => { + db.get('SLD:' + subdomain, function (err, value) { + if (err) throw err + const parsedValue = JSON.parse(value) + assert.equal(parsedValue && parsedValue.exclude, true) + done() + }) +} + +const excludesTLD = (db, tld, done) => { + db.get('TLD:' + tld, function (err, value) { + if (err) throw err + const parsedValue = JSON.parse(value) + assert.equal(parsedValue && parsedValue.exclude, true) + done() + }) +} + +const waitForDownload = (db, done) => { + const entries = [] + + db.createKeyStream() + .on('data', function (data) { + entries.push(data) + }).on('error', function (err) { + throw err + }).on('end', function () { + if (entries.length === 0) { + const err = 'No entries found in leveldb;\n' + + 'manually run `LEDGER_VERBOSE=1 LEDGER_RULES_TESTING=1 npm start` and wait for all entries to download\n' + + '(takes about 6 minutes).\n' + + 'If running in CI, make a test case that just sleeps for 10 minutes and run prior to this.' + throw err + } + done() + }) +} + +let dbHandle + +// https://github.com/Level/levelup + +describe('smoketest for our exclusion list', function () { + before(function (done) { + // TODO(bsclifton): only works on macOS at the moment + const filepath = + require('os').homedir() + + '/Library/Application Support' + + '/brave-development/ledger-rulesV2.leveldb' + + levelup(filepath, + {}, + function (err, db) { + if (err) throw err + dbHandle = db + + // TODO: wait until the DB has entries + waitForDownload(db, done) + }) + }) + + describe('includes', function () { + it('brianbondy.com', function (done) { + includes(dbHandle, 'brianbondy.com', done) + }) + it('clifton.io', function (done) { + includes(dbHandle, 'clifton.io', done) + }) + it('coindesk.com', function (done) { + includes(dbHandle, 'coindesk.com', done) + }) + it('cnn.com', function (done) { + includes(dbHandle, 'cnn.com', done) + }) + it('cnet.com', function (done) { + includes(dbHandle, 'cnet.com', done) + }) + it('Hacker News', function (done) { + includes(dbHandle, 'news.ycombinator.com', done) + }) + }) + + describe('excludes', function () { + it('bankofamerica.com', function (done) { + excludes(dbHandle, 'bankofamerica.com', done) + }) + it('brave.com', function (done) { + excludes(dbHandle, 'brave.com', done) + }) + it('Facebook', function (done) { + excludesSubdomain(dbHandle, 'facebook', done) + }) + it('.gov TLD', function (done) { + excludesTLD(dbHandle, 'gov', done) + }) + it('Google', function (done) { + excludesSubdomain(dbHandle, 'google', done) + }) + it('walmart.com', function (done) { + excludes(dbHandle, 'walmart.com', done) + }) + }) +})