updated the pattern matching library to make 'example.com' match '*.example.com' (and similar), added unit tests, fixes issue #31

This commit is contained in:
Peter Snyder 2017-11-03 06:50:37 +00:00
parent 436ab0f73a
commit 3779653f72
No known key found for this signature in database
GPG key ID: 9690F0C12F24762F
4 changed files with 96 additions and 2 deletions

View file

@ -58,6 +58,14 @@
return next; return next;
} }
// Also apply a slightly looser match, to make rules in the form
// of *.example.com match example.com.
if (next.startsWith("*.") &&
next.endsWith(hostName) &&
next.length === hostName.length + 2) {
return next;
}
return prev; return prev;
}; };

View file

@ -2,6 +2,17 @@
// the "namespace" we'll use for all the content scripts in the extension. // the "namespace" we'll use for all the content scripts in the extension.
(function () { (function () {
"use strict"; "use strict";
// If this code is being included by a unit test, the window object
// won't exist, so stub it out here.
try {
if (window === undefined) {
// This will throw in node...
}
} catch (e) {
global.window = {};
}
window.WEB_API_MANAGER = { window.WEB_API_MANAGER = {
constants: { constants: {
// The name of the cookie that will be used to push domain // The name of the cookie that will be used to push domain

View file

@ -26,9 +26,9 @@
"firefox": "web-ext -s add-on run", "firefox": "web-ext -s add-on run",
"lint": "node_modules/eslint/bin/eslint.js .", "lint": "node_modules/eslint/bin/eslint.js .",
"lint:fix": "node_modules/eslint/bin/eslint.js --fix .", "lint:fix": "node_modules/eslint/bin/eslint.js --fix .",
"test": "npm run clean; npm run bundle && ln -s `ls dist/` dist/webapi_manager.zip && cross-env node_modules/mocha/bin/mocha test/functional/*.js --only-local-tests", "test": "npm run clean; npm run bundle && ln -s `ls dist/` dist/webapi_manager.zip && cross-env node_modules/mocha/bin/mocha test/unit/*.js test/functional/*.js --only-local-tests",
"test:watch": "cross-env npm test --watch", "test:watch": "cross-env npm test --watch",
"test:all": "npm run clean; npm run bundle && ln -s `ls dist/` dist/webapi_manager.zip && cross-env node_modules/mocha/bin/mocha test/functional/*.js" "test:all": "npm run clean; npm run bundle && ln -s `ls dist/` dist/webapi_manager.zip && cross-env node_modules/mocha/bin/mocha test/unit/*.js test/functional/*.js"
}, },
"pre-push": { "pre-push": {
"run": [ "run": [

View file

@ -0,0 +1,75 @@
/**
* Tests to ensure that the pattern matching code (what determins which
* standard blocking rules should be applied to which domain) is correct.
*
* The code being tested here mostly lives in (from the project root)
* add-on/lib/domainmatcher.js
*/
"use strict";
const assert = require("assert");
const path = require("path");
const addonLibPath = path.join(__dirname, "..", "..", "add-on", "lib");
// These will end up not returing anything, but will instead populate
// window.WEB_API_MANAGER
const webAPIInitStub = require(path.join(addonLibPath, "init.js"));
const domainMatcherStub = require(path.join(addonLibPath, "domainmatcher.js"));
const domainMatcherLib = window.WEB_API_MANAGER.domainMatcherLib;
describe("Host Pattern Matching", function () {
const testPatterns = [
"*.example.com",
"www.uic.edu",
"cs.uic.edu",
];
describe("Exact matches", function () {
it("Positive case: input 'www.uic.edu' gives pattern 'www.uic.edu'", function (done) {
const testHostName = "www.uic.edu";
const matchingPattern = domainMatcherLib.matchHostName(testPatterns, testHostName);
assert.equal(matchingPattern, "www.uic.edu");
done();
});
it("Negative case: input 'nope.uic.edu' gives no pattern", function (done) {
const testHostName = "nope.uic.edu";
const matchingPattern = domainMatcherLib.matchHostName(testPatterns, testHostName);
assert.equal(matchingPattern, undefined);
done();
});
});
describe("Wildcard matches", function () {
it("Positive case: input 'www.example.com' gives pattern '*.example.com'", function (done) {
const testHostName = "www.example.com";
const matchingPattern = domainMatcherLib.matchHostName(testPatterns, testHostName);
assert.equal(matchingPattern, "*.example.com");
done();
});
it("Negative case: input 'www.example.com.co.uk' gives no pattern", function (done) {
const testHostName = "www.example.com.co.uk";
const matchingPattern = domainMatcherLib.matchHostName(testPatterns, testHostName);
assert.equal(matchingPattern, undefined);
done();
});
});
describe("Collapsed matches", function () {
it("Positive case: input 'example.com' gives pattern '*.example.com'", function (done) {
const testHostName = "example.com";
const matchingPattern = domainMatcherLib.matchHostName(testPatterns, testHostName);
assert.equal(matchingPattern, "*.example.com");
done();
});
});
});