[Pkg-mozext-commits] [requestpolicy] 60/100: add mozmill test testPolicy/testIframeTree/testDefaultPolicies.js

David Prévot taffit at moszumanska.debian.org
Fri Dec 12 22:56:58 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository requestpolicy.

commit ec9268bffe448a343e868333d1f075bc17ec3963
Author: myrdd <myrdd at users.noreply.github.com>
Date:   Mon Oct 27 22:04:02 2014 +0100

    add mozmill test testPolicy/testIframeTree/testDefaultPolicies.js
---
 tests/html/destination.html                        | 25 +++++++-
 tests/html/iframe_3.html                           | 54 ++++++++++++++++
 tests/html/inc/global.js                           |  5 ++
 tests/html/inc/jquery.min.js                       |  2 +
 tests/mozmill/lib/constants.js                     | 39 ++++++++++--
 tests/mozmill/lib/domain-util.js                   | 55 +++++++++++++++++
 tests/mozmill/lib/rp-utils.js                      |  9 +++
 tests/mozmill/tests/manifest.ini                   |  1 +
 .../testPolicy/lib/default-policy-iterator.js      | 71 ++++++++++++++++++++++
 tests/mozmill/tests/testPolicy/lib/iframe.js       | 69 +++++++++++++++++++++
 tests/mozmill/tests/testPolicy/manifest.ini        |  3 +
 .../tests/testPolicy/testIframeTree/manifest.ini   |  3 +
 .../testIframeTree/testDefaultPolicies.js          | 54 ++++++++++++++++
 tests/run-mozmill.sh                               |  5 ++
 14 files changed, 389 insertions(+), 6 deletions(-)

diff --git a/tests/html/destination.html b/tests/html/destination.html
index 6ebda8a..9032974 100644
--- a/tests/html/destination.html
+++ b/tests/html/destination.html
@@ -1,9 +1,31 @@
 <html>
 <head>
   <title>Destination page</title>
+  <script src="inc/jquery.min.js"></script>
 </head>
 <body>
 
+<script>
+  // change this value if more or less scripts/whatever is placed on this page.
+  // this value is read out by MozMill tests
+  var numAdditionalSamedomainRequests = 1;
+
+  $('body').prepend('<p>' + location.hostname + '</p>');
+  if (location.search) {
+    var text = location.search.substr(1);
+    $('body').prepend('<p>' + text + '</p>');
+  }
+
+  $('body').append('<p>This page makes ' +
+      '<span id="num-additional-samedomain-requests">' +
+      numAdditionalSamedomainRequests +'</span> additional request ' +
+      '(a javascript file) to the same domain.</p>');
+</script>
+
+<noscript>
+  Please enable JavaScript for this test.
+</noscript>
+
 <p>
   This is the destination page. Other test pages link to this page.
 </p>
@@ -11,6 +33,5 @@
 <p>
   <a href="javascript:history.back()">go back</a>
 </p>
-
 </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/tests/html/iframe_3.html b/tests/html/iframe_3.html
new file mode 100644
index 0000000..5c5dbc6
--- /dev/null
+++ b/tests/html/iframe_3.html
@@ -0,0 +1,54 @@
+<html>
+<head>
+  <script src="inc/jquery.min.js"></script>
+  <script src="inc/global.js"></script>
+</head>
+<body>
+
+<script>
+  // change this value if more or less scripts/whatever is placed on this page.
+  var numAdditionalSamedomainRequests = 2;
+
+  $(document).ready(function() {
+    $('body').prepend('<h1>' + location.hostname + '</h1>');
+    var domains = wwwDomains;
+
+    // take location's hostname and convert:
+    // www.*.*    -->  sub-1.*.*    -- for outer page
+    // sub-1.*.*  -->  sub-2.*.*    -- for inner page (in iframe)
+    domains.push(location.hostname.replace('sub-1', 'sub-2').
+        replace('www', 'sub-1'));
+    for (var i = 0; i < domains.length; i++) {
+      var domain = domains[i];
+
+      var width = '600';
+      var height = '400';
+      var path = '/iframe_3.html?inner';
+      var id = 'iframe_' + i;
+      if (location.search == '?inner') {
+        width = '250';
+        height = '150';
+        path = '/destination.html';
+      }
+      var src = 'http://' + domain + path;
+
+      var html = '<b>' + domain + ':</b><br />' +
+          '<iframe id="' + id + '" src="' + src + '" ' +
+          ' width="'+width+'" height="'+height+'"></iframe><br />';
+      $('body').append(html);
+    }
+
+    $('body').append('<p>In addition to the iframe requests this page makes ' +
+        '<span id="num-additional-samedomain-requests">' +
+        numAdditionalSamedomainRequests +'</span> additional requests ' +
+        '(javascript files) to the same domain.</p>');
+  });
+</script>
+
+<noscript>
+  Please enable JavaScript for this test.
+</noscript>
+
+
+</body>
+</html>
diff --git a/tests/html/inc/global.js b/tests/html/inc/global.js
new file mode 100644
index 0000000..983f084
--- /dev/null
+++ b/tests/html/inc/global.js
@@ -0,0 +1,5 @@
+var wwwDomains = [
+  'www.maindomain.test',
+  'www.otherdomain.test',
+  'www.thirddomain.test'
+];
diff --git a/tests/html/inc/jquery.min.js b/tests/html/inc/jquery.min.js
new file mode 100644
index 0000000..f121291
--- /dev/null
+++ b/tests/html/inc/jquery.min.js
@@ -0,0 +1,2 @@
+/*! jQuery v at 1.8.0 jquery.com | jquery.org/license */
+(function(a,b){function G(a){var b=F[a]={};return p.each(a.split(s),function(a,c){b[c]=!0}),b}function J(a,c,d){if(d===b&&a.nodeType===1){var e="data-"+c.replace(I,"-$1").toLowerCase();d=a.getAttribute(e);if(typeof d=="string"){try{d=d==="true"?!0:d==="false"?!1:d==="null"?null:+d+""===d?+d:H.test(d)?p.parseJSON(d):d}catch(f){}p.data(a,c,d)}else d=b}return d}function K(a){var b;for(b in a){if(b==="data"&&p.isEmptyObject(a[b]))continue;if(b!=="toJSON")return!1}return!0}function ba(){retur [...]
\ No newline at end of file
diff --git a/tests/mozmill/lib/constants.js b/tests/mozmill/lib/constants.js
index f5ff7d6..b0c3a27 100644
--- a/tests/mozmill/lib/constants.js
+++ b/tests/mozmill/lib/constants.js
@@ -2,12 +2,43 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+"use strict";
 
-const PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow";
+exports.mozmillTestsRootDir = "../../../../";
 
-const REDIRECT_NOTIFICATION_VALUE = "request-policy-meta-redirect";
+exports.PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow";
+exports.PREF_DEFAULT_ALLOW_SAME_DOMAIN =
+    "extensions.requestpolicy.defaultPolicy.allowSameDomain";
+
+exports.REDIRECT_NOTIFICATION_VALUE = "request-policy-meta-redirect";
 
 
 // Export of variables
-exports.PREF_DEFAULT_ALLOW = PREF_DEFAULT_ALLOW;
-exports.REDIRECT_NOTIFICATION_VALUE = REDIRECT_NOTIFICATION_VALUE;
+// exports.PREF_DEFAULT_ALLOW = PREF_DEFAULT_ALLOW;
+// exports.REDIRECT_NOTIFICATION_VALUE = REDIRECT_NOTIFICATION_VALUE;
+
+
+
+// uncomment if needed
+// exports.PREF_LOG = "extensions.requestpolicy.log";
+// exports.PREF_LOG_LEVEL = "extensions.requestpolicy.log.level";
+// exports.PREF_LOG_TYPES = "extensions.requestpolicy.log.types";
+//
+exports.PREF_AUTO_RELOAD = "extensions.requestpolicy.autoReload";
+//
+// exports.PREF_WELCOME_WINDOW = "extensions.requestpolicy.welcomeWindowShown";
+//
+// exports. = "extensions.requestpolicy.indicateBlockedObjects";
+// exports. = "extensions.requestpolicy.startWithAllowAllEnabled";
+// exports. = "extensions.requestpolicy.privateBrowsingPermanentWhitelisting";
+//
+// exports. = "extensions.requestpolicy.prefetch.link.disableOnStartup";
+// exports. = "extensions.requestpolicy.prefetch.link.restoreDefaultOnUninstall";
+// exports. = "extensions.requestpolicy.prefetch.dns.disableOnStartup";
+// exports. = "extensions.requestpolicy.prefetch.dns.restoreDefaultOnUninstall";
+//
+// exports. = "extensions.requestpolicy.menu.sorting";
+exports.PREF_MENU_SHOW_NUM_REQUESTS = "extensions.requestpolicy.menu.info.showNumRequests";
+//
+// exports. = "extensions.requestpolicy.lastVersion";
+// exports. = "extensions.requestpolicy.lastAppVersion";
diff --git a/tests/mozmill/lib/domain-util.js b/tests/mozmill/lib/domain-util.js
new file mode 100644
index 0000000..10db309
--- /dev/null
+++ b/tests/mozmill/lib/domain-util.js
@@ -0,0 +1,55 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var DomainUtil = exports.DomainUtil = (function() {
+  let self = {};
+
+  var domains = {
+    'maindomain.test': [
+      'www.maindomain.test',
+      'sub-1.maindomain.test',
+      'sub-2.maindomain.test'
+    ],
+    'otherdomain.test': [
+      'www.otherdomain.test',
+      'sub-1.otherdomain.test',
+      'sub-2.otherdomain.test'
+    ],
+    'thirddomain.test': [
+      'www.thirddomain.test',
+      'sub-1.thirddomain.test',
+      'sub-2.thirddomain.test'
+    ]
+  };
+
+  self.isSameDomain = function(dom1, dom2) {
+    return dom1 == dom2;
+  };
+
+  self.isSameBaseDomain = function(dom1, dom2) {
+    for (var i in domains) {
+      var count = 0;
+      if (i == dom1 || i == dom2) {
+        ++count;
+      }
+      for (var j in domains[i]) {
+        var d = domains[i][j];
+        if (d == dom1 || d == dom2) {
+          if (++count >= 2) {
+            return true;
+          }
+        }
+      }
+    }
+    return false;
+  };
+
+  self.isDifferentDomain = function(dom1, dom2) {
+    return !self.isSameDomain(dom1, dom2);
+  };
+
+  return self;
+}());
diff --git a/tests/mozmill/lib/rp-utils.js b/tests/mozmill/lib/rp-utils.js
index 249dacd..314ce12 100644
--- a/tests/mozmill/lib/rp-utils.js
+++ b/tests/mozmill/lib/rp-utils.js
@@ -2,6 +2,8 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+"use strict";
+
 /**
  * @param {MozMillController} _controller
  * @param {ElemBase} tab
@@ -24,6 +26,13 @@ function selectText(_tabBrowser, text) {
   findBar.close(true);
 }
 
+function getElementById(ancestor, id) {
+  var element = ancestor.getElementById(id);
+  assert.ok(element, 'element #' + id + ' has been found.');
+  return element;
+}
+
 // Export of functions
 exports.waitForTabLoad = waitForTabLoad;
 exports.selectText = selectText;
+exports.getElementById = getElementById;
diff --git a/tests/mozmill/tests/manifest.ini b/tests/mozmill/tests/manifest.ini
index 2c390e1..f9e23cf 100644
--- a/tests/mozmill/tests/manifest.ini
+++ b/tests/mozmill/tests/manifest.ini
@@ -1,3 +1,4 @@
 [parent:../manifest.ini]
 
 [include:testLinks/manifest.ini]
+[include:testMenu/manifest.ini]
diff --git a/tests/mozmill/tests/testPolicy/lib/default-policy-iterator.js b/tests/mozmill/tests/testPolicy/lib/default-policy-iterator.js
new file mode 100644
index 0000000..744dde2
--- /dev/null
+++ b/tests/mozmill/tests/testPolicy/lib/default-policy-iterator.js
@@ -0,0 +1,71 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var rpRootDir = "../../../";
+var rpConst = require(rpRootDir + "lib/constants");
+var rootDir = rpRootDir + rpConst.mozmillTestsRootDir;
+
+var prefs = require(rootDir + "firefox/lib/prefs");
+
+
+let DefaultPolicyManager = exports.DefaultPolicyManager = (function() {
+  let self = {};
+
+  let maxBinaryValue = 3;
+  let currentBinaryValue;
+
+  function isDefaultAllow(binaryPref) {
+    // true if 1 or 3
+    return (binaryPref & 0x1) != 0;
+  }
+  function isInterSubdomainAllowed(binaryPref) {
+    // true if 2 or 3
+    return (binaryPref & 0x2) != 0;
+  }
+  self.isDefaultAllow = function() {
+    return isDefaultAllow(currentBinaryValue);
+  };
+  self.isInterSubdomainAllowed = function() {
+    return isInterSubdomainAllowed(currentBinaryValue);
+  };
+
+  self.dumpState = function() {
+    dump("[Prefs]  Default Allow: " + self.isDefaultAllow() + ", " +
+        "Default Allow same domain: " + self.isInterSubdomainAllowed()+".\n");
+  }
+
+  self.getCurrentBinaryValue = function() {
+    return currentBinaryValue;
+  };
+  self.setPref = function(binaryValue) {
+    if (binaryValue < 0 || binaryValue > maxBinaryValue) {
+      return false;
+    } else {
+      prefs.preferences.setPref(rpConst.PREF_DEFAULT_ALLOW,
+          isDefaultAllow(binaryValue));
+      prefs.preferences.setPref(rpConst.PREF_DEFAULT_ALLOW_SAME_DOMAIN,
+          isInterSubdomainAllowed(binaryValue));
+      currentBinaryValue = binaryValue;
+      return true;
+    }
+  };
+
+  // initialize
+  self.setPref(0);
+
+  return self;
+}());
+
+
+
+function* makeDefaultPolicyIterator() {
+  for (let i = 0; DefaultPolicyManager.setPref(i); ++i) {
+    yield i;
+  }
+};
+
+exports.DefaultPolicyManager = DefaultPolicyManager;
+exports.makeDefaultPolicyIterator = makeDefaultPolicyIterator;
diff --git a/tests/mozmill/tests/testPolicy/lib/iframe.js b/tests/mozmill/tests/testPolicy/lib/iframe.js
new file mode 100644
index 0000000..a48a380
--- /dev/null
+++ b/tests/mozmill/tests/testPolicy/lib/iframe.js
@@ -0,0 +1,69 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var rpRootDir = "../../../";
+
+var {DefaultPolicyManager} = require("default-policy-iterator");
+var {DomainUtil} = require(rpRootDir + "lib/domain-util");
+
+function Iframe(aIframe) {
+  // wait for the iframe to be loaded
+  findElement.Elem(aIframe).waitForElement();
+
+  this.ownerDoc = aIframe.ownerDocument;
+  this.iframeDoc = aIframe.contentDocument;
+  assert.ok(this.iframeDoc, 'iframe #'+aIframe.id+ ' has a content document.');
+
+  this.ownerHostname = this.ownerDoc.location.hostname;
+  this.iframeDocHostname = this.iframeDoc.location.hostname;
+  // extract hostname from the "src" attribute
+  this.iframeSrcHostname = /^http:\/\/([^/]+)\//.exec(aIframe.src)[1];
+}
+Iframe.prototype.getContentDocument = function() {
+  return this.iframeDoc;
+};
+Iframe.prototype.isAllowedByCurrentPref = function() {
+  return DefaultPolicyManager.isDefaultAllow() ||
+      DomainUtil.isSameDomain(this.ownerHostname, this.iframeSrcHostname) ||
+      (DomainUtil.isSameBaseDomain(this.ownerHostname, this.iframeSrcHostname) &&
+          DefaultPolicyManager.isInterSubdomainAllowed());
+};
+Iframe.prototype.hasIframeRequestBeenAllowed = function() {
+  return this.iframeSrcHostname == this.iframeDocHostname;
+};
+Iframe.prototype.doChecks = function() {
+  var hasRequestBeenAllowed = this.hasIframeRequestBeenAllowed();
+  var shouldRequestHaveBeenAllowed = this.isAllowedByCurrentPref();
+
+  var text = "iframe with location '" + this.iframeSrcHostname + "' on '" +
+      this.ownerHostname + "' has " +
+      (shouldRequestHaveBeenAllowed ? "" : "not ") + "been loaded.";
+  assert.ok(hasRequestBeenAllowed === shouldRequestHaveBeenAllowed, text);
+};
+Iframe.prototype.accumulateNumRequests = function(numRequestCounter) {
+  numRequestCounter.accumulate(this.ownerHostname, this.iframeSrcHostname, 1,
+      this.hasIframeRequestBeenAllowed());
+};
+
+
+function* allIframesOnDocument(doc) {
+  var iframes = doc.getElementsByTagName("iframe");
+  for (var i = 0, len = iframes.length; i < len; ++i) {
+    let iframe = new Iframe(iframes[i]);
+
+    yield iframe;
+
+    // continue only if the request has been allowed,
+    //   in other words: if the iframe has been loaded.
+    if (iframe.hasIframeRequestBeenAllowed()) {
+      yield* allIframesOnDocument(iframe.getContentDocument());
+    }
+  }
+}
+
+
+exports.Iframe = Iframe;
+exports.allIframesOnDocument = allIframesOnDocument;
diff --git a/tests/mozmill/tests/testPolicy/manifest.ini b/tests/mozmill/tests/testPolicy/manifest.ini
new file mode 100644
index 0000000..9ebff53
--- /dev/null
+++ b/tests/mozmill/tests/testPolicy/manifest.ini
@@ -0,0 +1,3 @@
+[parent:../manifest.ini]
+
+[include:testIframeTree/manifest.ini]
diff --git a/tests/mozmill/tests/testPolicy/testIframeTree/manifest.ini b/tests/mozmill/tests/testPolicy/testIframeTree/manifest.ini
new file mode 100644
index 0000000..df2c5c0
--- /dev/null
+++ b/tests/mozmill/tests/testPolicy/testIframeTree/manifest.ini
@@ -0,0 +1,3 @@
+[parent:../manifest.ini]
+
+[testDefaultPolicies.js]
diff --git a/tests/mozmill/tests/testPolicy/testIframeTree/testDefaultPolicies.js b/tests/mozmill/tests/testPolicy/testIframeTree/testDefaultPolicies.js
new file mode 100644
index 0000000..d29024d
--- /dev/null
+++ b/tests/mozmill/tests/testPolicy/testIframeTree/testDefaultPolicies.js
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var rpRootDir = "../../../";
+var rpConst = require(rpRootDir + "lib/constants");
+var rootDir = rpRootDir + rpConst.mozmillTestsRootDir;
+
+var {assert, expect} = require(rootDir + "lib/assertions");
+var prefs = require(rootDir + "firefox/lib/prefs");
+var tabs = require(rootDir + "firefox/lib/tabs");
+
+var {
+  DefaultPolicyManager,
+  makeDefaultPolicyIterator
+} = require("../lib/default-policy-iterator");
+var {Iframe, allIframesOnDocument} = require("../lib/iframe");
+
+const TEST_URL = "http://www.maindomain.test/iframe_3.html";
+
+
+var setupModule = function(aModule) {
+  aModule.controller = mozmill.getBrowserController();
+  aModule.tabBrowser = new tabs.tabBrowser(aModule.controller);
+  aModule.tabBrowser.closeAllTabs();
+}
+
+var teardownModule = function(aModule) {
+  prefs.preferences.clearUserPref(rpConst.PREF_DEFAULT_ALLOW);
+  prefs.preferences.clearUserPref(rpConst.PREF_DEFAULT_ALLOW_SAME_DOMAIN);
+  aModule.tabBrowser.closeAllTabs();
+}
+
+
+var testDefaultPolicies = function() {
+  controller.open(TEST_URL);
+  controller.waitForPageLoad();
+
+  let itDefaultPolicy = makeDefaultPolicyIterator();
+  while (!itDefaultPolicy.next().done) {
+    DefaultPolicyManager.dumpState();
+
+    // reload the page with the new preferences
+    controller.refresh();
+    controller.waitForPageLoad();
+
+    let doc = controller.window.content.document;
+    for (let iframe of allIframesOnDocument(doc)) {
+      iframe.doChecks();
+    }
+  }
+}
diff --git a/tests/run-mozmill.sh b/tests/run-mozmill.sh
new file mode 100755
index 0000000..7b947f3
--- /dev/null
+++ b/tests/run-mozmill.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+MANIFEST=`dirname $0`/mozmill/addon-manifest.ini
+
+mozmill --addon-manifests="$MANIFEST" $ARGUMENTS $@

-- 
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