summaryrefslogtreecommitdiff
path: root/data/extensions/https-everywhere@eff.org/test
diff options
context:
space:
mode:
Diffstat (limited to 'data/extensions/https-everywhere@eff.org/test')
-rw-r--r--data/extensions/https-everywhere@eff.org/test/.eslintrc6
-rw-r--r--data/extensions/https-everywhere@eff.org/test/incognito_test.js59
-rw-r--r--data/extensions/https-everywhere@eff.org/test/rules_test.js203
-rw-r--r--data/extensions/https-everywhere@eff.org/test/testing_utils.js29
4 files changed, 297 insertions, 0 deletions
diff --git a/data/extensions/https-everywhere@eff.org/test/.eslintrc b/data/extensions/https-everywhere@eff.org/test/.eslintrc
new file mode 100644
index 0000000..8a30eba
--- /dev/null
+++ b/data/extensions/https-everywhere@eff.org/test/.eslintrc
@@ -0,0 +1,6 @@
+{
+ "env": {
+ "node": true,
+ "mocha": true
+ }
+}
diff --git a/data/extensions/https-everywhere@eff.org/test/incognito_test.js b/data/extensions/https-everywhere@eff.org/test/incognito_test.js
new file mode 100644
index 0000000..4005515
--- /dev/null
+++ b/data/extensions/https-everywhere@eff.org/test/incognito_test.js
@@ -0,0 +1,59 @@
+'use strict'
+
+const expect = require('chai').expect,
+ tu = require('./testing_utils'),
+ incognito = require('../incognito');
+
+describe('incognito.js', function() {
+ beforeEach(function() {
+ tu.stubber([
+ ['chrome.windows.onCreated.addListener', tu.Mock()],
+ ['chrome.windows.onRemoved.addListener', tu.Mock()],
+ ['chrome.windows.getAll', tu.Mock()],
+ ]);
+ });
+
+ describe('onIncognitoDestruction', function() {
+ beforeEach(function() {
+ incognito.state.incognito_session_exists = false;
+ this.callbackCalled = false;
+ this.callback = () => this.callbackCalled = true;
+ this.instance = incognito.onIncognitoDestruction(this.callback);
+ })
+
+ it('no incognito session by default', function() {
+ expect(incognito.state.incognito_session_exists).to.be.false;
+ })
+
+ it('with no incognito, callback not called', async function() {
+ incognito.state.incognito_session_exists = false;
+
+ await this.instance.detect_incognito_destruction();
+
+ expect(this.callbackCalled).to.be.false;
+ });
+
+ it('with incognitos still open, callback not called', async function() {
+ incognito.state.incognito_session_exists = true;
+ chrome.windows.getAll = func => func([{incognito: true}]);
+
+ await this.instance.detect_incognito_destruction();
+
+ expect(this.callbackCalled, 'not called').to.be.false;
+ });
+
+ it('callback called when last incognito closed', async function() {
+ incognito.state.incognito_session_exists = true;
+ chrome.windows.getAll = func => func([]);
+
+ await this.instance.detect_incognito_destruction();
+ expect(incognito.state.incognito_session_exists, 'constant changed').to.be.false;
+ expect(this.callbackCalled).to.be.true;
+ });
+
+ it('detects when an incognito window is created', function() {
+ this.instance.detect_incognito_creation({incognito: true});
+ expect(incognito.state.incognito_session_exists, 'constant changed').to.be.true;
+ })
+ });
+});
diff --git a/data/extensions/https-everywhere@eff.org/test/rules_test.js b/data/extensions/https-everywhere@eff.org/test/rules_test.js
new file mode 100644
index 0000000..eaa7889
--- /dev/null
+++ b/data/extensions/https-everywhere@eff.org/test/rules_test.js
@@ -0,0 +1,203 @@
+'use strict'
+
+const assert = require('chai').assert,
+ rules = require('../rules');
+
+const Exclusion = rules.Exclusion,
+ Rule = rules.Rule,
+ RuleSet = rules.RuleSet,
+ RuleSets = rules.RuleSets;
+
+
+describe('rules.js', function() {
+ let test_str = 'test';
+
+ describe('nullIterable', function() {
+ it('is iterable zero times and is size 0', function() {
+ let count = 0;
+ for (let _ of rules.nullIterable) { // eslint-disable-line no-unused-vars
+ count += 1;
+ }
+ assert.strictEqual(count, 0);
+ assert.strictEqual(rules.nullIterable.size, 0);
+ assert.isEmpty(rules.nullIterable);
+ });
+ });
+
+ describe('Exclusion', function() {
+ it('constructs', function() {
+ let exclusion = new Exclusion(test_str);
+ assert.isTrue(exclusion.pattern_c.test(test_str), true);
+ });
+ });
+
+ describe('Rule', function() {
+ it('constructs trivial rule', function() {
+ let rule = new Rule('^http:', 'https:');
+ assert.equal(rule.to, rules.trivial_rule_to);
+ assert.equal(rule.from_c, rules.trivial_rule_from_c);
+ });
+ });
+
+ describe('RuleSet', function() {
+ beforeEach(function() {
+ this.ruleset = new RuleSet('set_name', true, 'note');
+ });
+
+ describe('#apply', function() {
+ it('excludes excluded uris', function() {
+ this.ruleset.exclusions = [new Exclusion(test_str)];
+ assert.isNull(this.ruleset.apply(test_str));
+ });
+
+ it('rewrites uris', function() {
+ let rule = new Rule('^http:', 'https:');
+ this.ruleset.rules.push(rule);
+ assert.equal(this.ruleset.apply('http://example.com/'), 'https://example.com/');
+ });
+
+ it('does nothing when empty', function() {
+ assert.isNull(this.ruleset.apply('http://example.com/'));
+ });
+ });
+
+ describe('#isEquivalentTo', function() {
+ let inputs = ['a', 'b', 'c'];
+
+ it('not equivalent with different input', function() {
+ let rs = new RuleSet(...inputs);
+ assert.isFalse(
+ rs.isEquivalentTo(new RuleSet('e', 'f', 'g'))
+ );
+ });
+ it('not equivalent with different exclusions', function() {
+ let rs_a = new RuleSet(...inputs),
+ rs_b = new RuleSet(...inputs);
+ rs_a.exclusions = [new Exclusion('foo')];
+ rs_b.exclusions = [new Exclusion('bar')];
+
+ assert.isFalse(rs_a.isEquivalentTo(rs_b));
+ });
+
+ it('not equivalent with different rules', function() {
+ let rs_a = new RuleSet(...inputs),
+ rs_b = new RuleSet(...inputs);
+ rs_a.rules.push(new Rule('a', 'a'));
+ rs_b.rules.push(new Rule('b', 'b'));
+
+ assert.isFalse(rs_a.isEquivalentTo(rs_b));
+ });
+
+ it('equivalent to self', function() {
+ let rs = new RuleSet(...inputs);
+ assert.isTrue(rs.isEquivalentTo(rs));
+ });
+ });
+ })
+
+ describe('RuleSets', function() {
+ let rules_json = [{
+ name: "Freerangekitten.com",
+ rule: [{
+ to: "https:",
+ from: "^http:"
+ }],
+ target: ["freerangekitten.com", "www.freerangekitten.com"]
+ }];
+
+ beforeEach(function() {
+ this.rsets = new RuleSets();
+ });
+
+ describe('#addFromJson', function() {
+ it('can add a rule', function() {
+ this.rsets.addFromJson(rules_json);
+
+ assert.isTrue(this.rsets.targets.has('freerangekitten.com'));
+ });
+ });
+
+ describe('#rewriteURI', function() {
+ it('rewrites host added from json', function() {
+ let host = 'freerangekitten.com';
+ this.rsets.addFromJson(rules_json);
+
+ let newuri = this.rsets.rewriteURI('http://' + host + '/', host);
+
+ assert.strictEqual(newuri, 'https://' + host + '/', 'protocol changed to https')
+ })
+
+ it('does not rewrite unknown hosts', function() {
+ assert.isNull(this.rsets.rewriteURI('http://unknown.com/', 'unknown.com'));
+ })
+ });
+
+ describe('#potentiallyApplicableRulesets', function() {
+ let host = 'example.com',
+ value = [host];
+
+ it('returns nothing when empty', function() {
+ assert.isEmpty(this.rsets.potentiallyApplicableRulesets(host));
+ });
+
+ it('returns nothing for malformed hosts', function() {
+ assert.isEmpty(this.rsets.potentiallyApplicableRulesets('....'));
+ });
+
+ it('returns nothing for empty hosts', function() {
+ assert.isEmpty(this.rsets.potentiallyApplicableRulesets(''));
+ });
+
+ it('returns cached rulesets', function() {
+ this.rsets.ruleCache.set(host, value);
+ assert.deepEqual(this.rsets.potentiallyApplicableRulesets(host), value);
+ });
+
+ it('caches results', function() {
+ this.rsets.targets.set(host, value);
+
+ assert.isEmpty(this.rsets.ruleCache);
+ this.rsets.potentiallyApplicableRulesets(host);
+ assert.isTrue(this.rsets.ruleCache.has(host));
+ });
+
+ describe('wildcard matching', function() {
+
+ it('no wildcard', function() {
+ let target = host;
+ this.rsets.targets.set(target, value);
+
+ let result = this.rsets.potentiallyApplicableRulesets(target),
+ expected = new Set(value);
+
+ assert.deepEqual(result, expected);
+ });
+
+ it('matches left hand side wildcards', function() {
+ let target = '*.' + host;
+ this.rsets.targets.set(target, value);
+
+ let res1 = this.rsets.potentiallyApplicableRulesets('sub.' + host);
+ assert.deepEqual(res1, new Set(value), 'default case');
+
+ let res2 = this.rsets.potentiallyApplicableRulesets(host);
+ assert.isEmpty(res2, 'wildcard does not match parent domains');
+
+ let res3 = this.rsets.potentiallyApplicableRulesets('moresub.sub.' + host);
+ assert.deepEqual(res3, new Set(value), 'wildcard matches sub domains');
+ });
+
+ it('matches middle wildcards', function() {
+ let target = 'sub.*.' + host;
+ this.rsets.targets.set(target, value);
+
+ let res1 = this.rsets.potentiallyApplicableRulesets('sub.star.' + host);
+ assert.deepEqual(res1, new Set(value), 'default case');
+
+ let res2 = this.rsets.potentiallyApplicableRulesets('sub.foo.bar.' + host);
+ assert.isEmpty(res2, new Set(value), 'only matches one label');
+ });
+ });
+ });
+ });
+})
diff --git a/data/extensions/https-everywhere@eff.org/test/testing_utils.js b/data/extensions/https-everywhere@eff.org/test/testing_utils.js
new file mode 100644
index 0000000..eddfadb
--- /dev/null
+++ b/data/extensions/https-everywhere@eff.org/test/testing_utils.js
@@ -0,0 +1,29 @@
+'use strict'
+
+function Mock() {
+ let out = function() {
+ out.calledWith = Array.from(arguments);
+ }
+ return out;
+}
+
+function stub(name, value) {
+ let parts = name.split('.'),
+ last = parts.pop(),
+ part = global;
+ parts.forEach(partName => {
+ if (!part.hasOwnProperty(partName)) {
+ part[partName] = {};
+ }
+ part = part[partName];
+ });
+ part[last] = value;
+}
+
+function stubber(namesValues) {
+ namesValues.forEach(nameValue => {
+ stub(...nameValue);
+ });
+}
+
+Object.assign(exports, {Mock, stub, stubber});