[Pkg-mozext-commits] [requestpolicy] 147/257: [ref] create `oldrules.jsm`
David Prévot
taffit at moszumanska.debian.org
Thu Jan 28 03:20:07 UTC 2016
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository requestpolicy.
commit 73e7438dd3e34b89f4c6240d3270cae02df437be
Author: Martin Kimmerle <dev at 256k.de>
Date: Thu Nov 19 00:11:57 2015 +0100
[ref] create `oldrules.jsm`
The functions to import old rules (v0.5 rules) has been moved
from `common.js` to `oldrules.jsm`. XPCShell unit tests have
been created to test the module.
Other changes:
* Set `minVersion` to Gecko 33 to be able to use shorthand
properties in object literals.
https://developer.mozilla.org/en-US/Firefox/Releases/33
---
src/content/lib/old-rules.jsm | 177 +++++++++++++++++++++++++++++++++++++++
src/content/settings/common.js | 106 -----------------------
src/content/settings/oldrules.js | 7 +-
src/content/settings/setup.js | 9 +-
src/install.rdf | 4 +-
tests/xpcshell/test_oldrules.js | 147 ++++++++++++++++++++++++++++++++
tests/xpcshell/xpcshell.ini | 1 +
7 files changed, 340 insertions(+), 111 deletions(-)
diff --git a/src/content/lib/old-rules.jsm b/src/content/lib/old-rules.jsm
new file mode 100644
index 0000000..700cf9e
--- /dev/null
+++ b/src/content/lib/old-rules.jsm
@@ -0,0 +1,177 @@
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ *
+ * RequestPolicy - A Firefox extension for control over cross-site requests.
+ * Copyright (c) 2008-2012 Justin Samuel
+ * Copyright (c) 2014-2015 Martin Kimmerle
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * ***** END LICENSE BLOCK *****
+ */
+
+/* exported EXPORTED_SYMBOLS, OldRules */
+var EXPORTED_SYMBOLS = ["OldRules"];
+
+var globalScope = this;
+
+var {OldRules} = (function () {
+ "use strict";
+
+ /* global Components */
+ const {interfaces: Ci, results: Cr, utils: Cu} = Components;
+
+ /* global ScriptLoader */
+ Cu.import("chrome://rpcontinued/content/lib/script-loader.jsm");
+ ScriptLoader.importModules([
+ "lib/utils/domains", /* global DomainUtil */
+ "lib/prefs" /* global rpPrefBranch */
+ ], globalScope);
+
+ function OldRules(aOrigins = "", aDestinations = "",
+ aOriginsToDestinations = "") {
+ this._customPrefStrings = null;
+ if (aOrigins || aDestinations || aOriginsToDestinations) {
+ this._customPrefStrings = {
+ origins: String(aOrigins),
+ dests: String(aDestinations),
+ originsToDests: String(aOriginsToDestinations)
+ };
+ }
+ }
+
+ /**
+ * The three strings containing the old rules.
+ */
+ Object.defineProperty(OldRules.prototype, "prefStrings", {
+ get: function () {
+ if (!this._prefStrings) {
+ this._prefStrings = this._customPrefStrings || {
+ origins: OldRules._getPrefString("allowedOrigins"),
+ dests: OldRules._getPrefString("allowedDestinations"),
+ originsToDests: OldRules._getPrefString(
+ "allowedOriginsToDestinations")
+ };
+ }
+ return this._prefStrings;
+ }
+ });
+
+ /**
+ * Three `Set`s containing the rules as strings.
+ */
+ Object.defineProperty(OldRules.prototype, "prefStringSets", {
+ get: function () {
+ function splitString(aRulesString) {
+ var rules = new Set(aRulesString.split(" "));
+
+ // The string might contain double spaces.
+ rules.delete("");
+
+ return rules;
+ }
+
+ if (!this._prefStringSets) {
+ let {origins, dests, originsToDests} = this.prefStrings;
+ this._prefStringSets = {
+ origins: splitString(origins),
+ dests: splitString(dests),
+ originsToDests: splitString(originsToDests)
+ };
+ }
+ return this._prefStringSets;
+ }
+ });
+
+ /**
+ * Convert the pref strings to rule objects.
+ */
+ OldRules.prototype.getAsNewRules = function (addHostWildcard) {
+ var rules = [];
+ var {origins, dests, originsToDests} = this.prefStringSets;
+
+ for (let origin of origins) {
+ rules.push({
+ o: OldRules.getEndpointSpecFromString(origin, addHostWildcard)
+ });
+ }
+
+ for (let dest of dests) {
+ rules.push({
+ d: OldRules.getEndpointSpecFromString(dest, addHostWildcard)
+ });
+ }
+
+ for (let originToDest of originsToDests) {
+ let [origin, dest] = originToDest.split("|");
+
+ rules.push({
+ o: OldRules.getEndpointSpecFromString(origin, addHostWildcard),
+ d: OldRules.getEndpointSpecFromString(dest, addHostWildcard)
+ });
+ }
+
+ return rules;
+ };
+
+ /**
+ * @static
+ * @param {string} aEndpointString
+ * @param {boolean} aAddHostWildcard
+ * @return {Object} The endpoints' specifications.
+ */
+ OldRules.getEndpointSpecFromString = function (aEndpointString,
+ aAddHostWildcard) {
+ var spec = {};
+ if (DomainUtil.isValidUri(aEndpointString)) {
+ let uriObj = DomainUtil.getUriObject(aEndpointString);
+ spec.h = uriObj.host;
+ spec.s = uriObj.scheme;
+ if (uriObj.port !== -1) {
+ spec.port = uriObj.port;
+ }
+ } else {
+ spec.h = aEndpointString.split("/")[0];
+ }
+ // FIXME: Issue #731; Detect if the host is a Base Domain.
+ if (spec.h && aAddHostWildcard && OldRules._isHostname(spec.h)) {
+ spec.h = "*." + spec.h;
+ }
+ return spec;
+ };
+
+ // FIXME: This should be a function of DomainUtil.
+ OldRules._isHostname = function (host) {
+ return !DomainUtil.isValidUri(host) && !DomainUtil.isIPAddress(host);
+ };
+
+ /**
+ * @param {string} aPrefName
+ * @return {string} The value of the pref, or an empty string if
+ * the pref does not exist.
+ */
+ OldRules._getPrefString = function (aPrefName) {
+ try {
+ return rpPrefBranch.getComplexValue(aPrefName, Ci.nsISupportsString).data;
+ } catch (e) {
+ if (false === e.hasOwnProperty("result") ||
+ e.result !== Cr.NS_ERROR_UNEXPECTED) {
+ Cu.reportError(e);
+ }
+ return "";
+ }
+ };
+
+ return {OldRules};
+})();
diff --git a/src/content/settings/common.js b/src/content/settings/common.js
index dabf047..d44ecbe 100644
--- a/src/content/settings/common.js
+++ b/src/content/settings/common.js
@@ -130,115 +130,9 @@ common.ruleDataPartToDisplayString = function (ruleDataPart) {
};
-common.getOldRulesAsNewRules = function (addHostWildcard) {
- var origins = common.getPrefObj('allowedOrigins');
- var destinations = common.getPrefObj('allowedDestinations');
- var originsToDestinations = common.getPrefObj('allowedOriginsToDestinations');
- var rules = [];
-
- function isHostname(host) {
- return !DomainUtil.isValidUri(host) && !DomainUtil.isIPAddress(host);
- }
- for (var origin in origins) {
- var entry = {};
- entry['o'] = {};
- if (DomainUtil.isValidUri(origin)) {
- var uriObj = DomainUtil.getUriObject(origin);
- entry['o']['h'] = uriObj.host;
- entry['o']['s'] = uriObj.scheme;
- if (uriObj.port != -1) {
- entry['o']['port'] = uriObj.port;
- }
- } else {
- entry['o']['h'] = origin.split('/')[0];
- }
- if (entry['o']['h'] && addHostWildcard && isHostname(entry['o']['h'])) {
- entry['o']['h'] = '*.' + entry['o']['h']
- }
- rules.push(entry);
- }
-
- for (var dest in destinations) {
- entry = {};
- entry['d'] = {};
- if (DomainUtil.isValidUri(dest)) {
- var uriObj = DomainUtil.getUriObject(dest);
- entry['d']['h'] = uriObj.host;
- entry['d']['s'] = uriObj.scheme;
- if (uriObj.port != -1) {
- entry['d']['port'] = uriObj.port;
- }
- } else {
- entry['d']['h'] = dest.split('/')[0];
- }
- if (entry['d']['h'] && addHostWildcard && isHostname(entry['d']['h'])) {
- entry['d']['h'] = '*.' + entry['d']['h']
- }
- rules.push(entry);
- }
- for (var originToDest in originsToDestinations) {
- var parts = originToDest.split('|');
- var origin = parts[0];
- var dest = parts[1];
- entry = {};
- entry['o'] = {};
- entry['d'] = {};
-
- if (DomainUtil.isValidUri(origin)) {
- var uriObj = DomainUtil.getUriObject(origin);
- entry['o']['h'] = uriObj.host;
- entry['o']['s'] = uriObj.scheme;
- if (uriObj.port != -1) {
- entry['o']['port'] = uriObj.port;
- }
- } else {
- entry['o']['h'] = origin.split('/')[0];
- }
- if (entry['o']['h'] && addHostWildcard && isHostname(entry['o']['h'])) {
- entry['o']['h'] = '*.' + entry['o']['h']
- }
- if (DomainUtil.isValidUri(dest)) {
- var uriObj = DomainUtil.getUriObject(dest);
- entry['d']['h'] = uriObj.host;
- entry['d']['s'] = uriObj.scheme;
- if (uriObj.port != -1) {
- entry['d']['port'] = uriObj.port;
- }
- } else {
- entry['d']['h'] = dest.split('/')[0];
- }
- if (entry['d']['h'] && addHostWildcard && isHostname(entry['d']['h'])) {
- entry['d']['h'] = '*.' + entry['d']['h']
- }
-
- rules.push(entry);
- }
-
- return rules;
-};
-
-common.getPrefObj = function (pref) {
- try {
- var value = rpPrefBranch.getComplexValue(pref, Ci.nsISupportsString).data;
- } catch (e) {
- value = '';
- }
- return common.prefStringToObj(value);
-};
-
-common.prefStringToObj = function (prefString) {
- var prefObj = {};
- var prefArray = prefString.split(" ");
- if (prefArray[0] != "") {
- for (var i in prefArray) {
- prefObj[prefArray[i]] = true;
- }
- }
- return prefObj;
-};
common.localize = function(stringNames) {
stringNames.forEach(function(name) {
diff --git a/src/content/settings/oldrules.js b/src/content/settings/oldrules.js
index a4e275f..31640d8 100644
--- a/src/content/settings/oldrules.js
+++ b/src/content/settings/oldrules.js
@@ -12,6 +12,9 @@ $(function () {
common.localize(PAGE_STRINGS);
});
+ScriptLoader.importModules([
+ "lib/old-rules"
+], this);
var rules = null;
var addHostWildcard = true;
@@ -28,8 +31,10 @@ function clearRulesTable() {
function populateRuleTable() {
var table = $id('rules');
+
+ let oldRules = new OldRules();
// Setting the global rules var here.
- rules = common.getOldRulesAsNewRules(addHostWildcard);
+ rules = oldRules.getAsNewRules(addHostWildcard);
for (var i = 0; i < rules.length; i++) {
var entry = rules[i];
diff --git a/src/content/settings/setup.js b/src/content/settings/setup.js
index 0542822..6832b68 100644
--- a/src/content/settings/setup.js
+++ b/src/content/settings/setup.js
@@ -22,6 +22,10 @@ $(function () {
Cu.import("resource://gre/modules/Services.jsm");
+ScriptLoader.importModules([
+ "lib/old-rules"
+], this);
+
function showConfigure() {
$('#welcome').css('display', 'none');
$('#configure').css('display', 'block');
@@ -133,8 +137,9 @@ function onload() {
Logger.dump('Rule count: ' + ruleCount);
if (ruleCount <= 0) {
Logger.dump('Performing rule import.');
- var addHostWildcard = identLevel == 1;
- var rules = common.getOldRulesAsNewRules(addHostWildcard);
+ let addHostWildcard = identLevel == 1;
+ let oldRules = new OldRules();
+ let rules = oldRules.getAsNewRules(addHostWildcard);
PolicyManager.addAllowRules(rules);
}
diff --git a/src/install.rdf b/src/install.rdf
index 2d24e01..66c23e5 100644
--- a/src/install.rdf
+++ b/src/install.rdf
@@ -201,7 +201,7 @@
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
- <em:minVersion>22.0</em:minVersion>
+ <em:minVersion>33.0</em:minVersion>
<em:maxVersion>44.0a1</em:maxVersion>
</Description>
</em:targetApplication>
@@ -210,7 +210,7 @@
<em:targetApplication>
<Description>
<em:id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</em:id>
- <em:minVersion>2.19</em:minVersion>
+ <em:minVersion>2.30</em:minVersion>
<em:maxVersion>2.38.*</em:maxVersion>
</Description>
</em:targetApplication>
diff --git a/tests/xpcshell/test_oldrules.js b/tests/xpcshell/test_oldrules.js
new file mode 100644
index 0000000..0ff7fb8
--- /dev/null
+++ b/tests/xpcshell/test_oldrules.js
@@ -0,0 +1,147 @@
+/* exported run_test */
+/* global Cc, Ci, Cu, equal, deepEqual */
+
+/* global OldRules */
+Cu.import("chrome://rpcontinued/content/lib/old-rules.jsm");
+/* global rpPrefBranch */
+Cu.import("chrome://rpcontinued/content/lib/prefs.jsm");
+
+
+function run_test() {
+ "use strict";
+
+ test_0();
+}
+
+
+function test_0() {
+ "use strict";
+
+ testGetOldRulesAsNewRules(
+ [
+ "mozilla.org",
+ "mozilla.net",
+ "mozilla.org|mozilla.net"
+ ],
+ [
+ {o: {h: "*.mozilla.org"}},
+ {d: {h: "*.mozilla.net"}},
+ {o: {h: "*.mozilla.org"}, d: {h: "*.mozilla.net"}}
+ ]);
+
+ testGetOldRulesAsNewRules(
+ [
+ "https://www.mozilla.org " +
+ "www.mozilla.org " +
+ "mozilla.org",
+
+ "https://mozorg.cdn.mozilla.net " +
+ "mozorg.cdn.mozilla.net " +
+ "mozilla.net",
+
+ "https://www.mozilla.org|https://mozorg.cdn.mozilla.net " +
+ "www.mozilla.org|mozorg.cdn.mozilla.net " +
+ "mozilla.org|mozilla.net"
+ ],
+ [
+ {o: {s: "https", h: "*.www.mozilla.org"}},
+ {o: {h: "*.www.mozilla.org"}},
+ {o: {h: "*.mozilla.org"}},
+
+ {d: {s: "https", h: "*.mozorg.cdn.mozilla.net"}},
+ {d: {h: "*.mozorg.cdn.mozilla.net"}},
+ {d: {h: "*.mozilla.net"}},
+
+ {
+ o: {s: "https", h: "*.www.mozilla.org"},
+ d: {s: "https", h: "*.mozorg.cdn.mozilla.net"}
+ }, {
+ o: {h: "*.www.mozilla.org"},
+ d: {h: "*.mozorg.cdn.mozilla.net"}
+ }, {
+ o: {h: "*.mozilla.org"},
+ d: {h: "*.mozilla.net"}
+ }
+ ]);
+
+ // Get the old rules from the prefs.
+ // The prefs don't exist.
+ testGetOldRulesAsNewRules([undefined, undefined, undefined], []);
+
+ // Get the old rules from the prefs.
+ // Some rules are defined in the prefs.
+ usingOldRulePrefs({
+ "allowedOrigins": "mozilla.org",
+ "allowedDestinations": "mozilla.net",
+ "allowedOriginsToDestinations": "mozilla.org|mozilla.net"
+ }, function () {
+ testGetOldRulesAsNewRules([undefined, undefined, undefined], [
+ {o: {h: "*.mozilla.org"}},
+ {d: {h: "*.mozilla.net"}},
+ {o: {h: "*.mozilla.org"}, d: {h: "*.mozilla.net"}}
+ ]);
+ });
+}
+
+
+function usingOldRulePrefs(aPrefs, aFunction) {
+ "use strict";
+
+ function forEachPrefName(fn) {
+ Object.getOwnPropertyNames(aPrefs).forEach(fn);
+ }
+
+ // Set the prefs.
+ forEachPrefName(function (prefName) {
+ let prefValue = aPrefs[prefName];
+ setOldRulePref(prefName, prefValue);
+ });
+
+ aFunction.call(null);
+
+ // Clear the prefs.
+ forEachPrefName(function (prefName) {
+ rpPrefBranch.clearUserPref(prefName);
+ });
+}
+
+function setOldRulePref(aPrefName, aValue) {
+ "use strict";
+ // Code taken from RequestPolicy 0.5.28 (requestpolicyService.js).
+
+ // Not using just setCharPref because these values may contain Unicode
+ // strings (e.g. for IDNs).
+ var str = Cc["@mozilla.org/supports-string;1"]
+ .createInstance(Ci.nsISupportsString);
+ str.data = aValue;
+ rpPrefBranch.setComplexValue(aPrefName, Ci.nsISupportsString, str);
+}
+
+function testGetOldRulesAsNewRules(
+ [origins, destinations, originsToDestinations], expectedRuleSpecs) {
+ "use strict";
+
+ var oldRules = new OldRules(origins, destinations, originsToDestinations);
+ var actualRuleSpecs = oldRules.getAsNewRules(true);
+ assertRuleSpecsEqual(actualRuleSpecs, expectedRuleSpecs);
+}
+
+function assertRuleSpecsEqual(actual, expected) {
+ "use strict";
+
+ if (false === Array.isArray(actual)) {
+ actual = [actual];
+ }
+ if (false === Array.isArray(expected)) {
+ expected = [expected];
+ }
+
+ equal(actual.length, expected.length);
+
+ actual.sort();
+ expected.sort();
+
+ for (let i = 0, len = expected.length; i < len; ++i) {
+ deepEqual(actual[i], expected[i]);
+ }
+}
diff --git a/tests/xpcshell/xpcshell.ini b/tests/xpcshell/xpcshell.ini
index eb96eb4..f797b7a 100644
--- a/tests/xpcshell/xpcshell.ini
+++ b/tests/xpcshell/xpcshell.ini
@@ -3,6 +3,7 @@ head = head_loadrp.js head_utils.js
tail =
[test_fileutil.js]
+[test_oldrules.js]
;[test_policymanager.js]
[test_policystorage.js]
;[test_subscription.js]
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git
More information about the Pkg-mozext-commits
mailing list