[Pkg-mozext-commits] [requestpolicy] 251/280: [mozmill] add `testRuleWithSchemeOnly`

David Prévot taffit at moszumanska.debian.org
Sat May 2 20:30:35 UTC 2015


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

taffit pushed a commit to branch master
in repository requestpolicy.

commit 59e56ee61ea6ecd940613c467e06fbedfefef3f7
Author: Martin Kimmerle <dev at 256k.de>
Date:   Tue Apr 28 10:18:33 2015 +0200

    [mozmill] add `testRuleWithSchemeOnly`
---
 tests/mozmill/lib/request-log-utils.js             |  36 ++-
 tests/mozmill/tests/testPolicy/manifest.ini        |   1 +
 .../tests/testPolicy/testRuleWithSchemeOnly.js     | 268 +++++++++++++++++++++
 3 files changed, 304 insertions(+), 1 deletion(-)

diff --git a/tests/mozmill/lib/request-log-utils.js b/tests/mozmill/lib/request-log-utils.js
index a901936..d6a0a50 100644
--- a/tests/mozmill/lib/request-log-utils.js
+++ b/tests/mozmill/lib/request-log-utils.js
@@ -37,8 +37,15 @@ RequestLog.prototype.open = function () {
 
   var tree = this.requestLogDoc.getElementById("requestpolicy-requestLog-tree");
   this.treeView = tree.view;
+
+  this.originCol = tree.columns
+      .getNamedColumn("requestpolicy-requestLog-origin");
   this.destCol = tree.columns
       .getNamedColumn("requestpolicy-requestLog-destination");
+  this.blockedCol = tree.columns
+      .getNamedColumn("requestpolicy-requestLog-blocked");
+  this.timeCol = tree.columns
+      .getNamedColumn("requestpolicy-requestLog-time");
 };
 
 RequestLog.prototype.close = function () {
@@ -47,9 +54,13 @@ RequestLog.prototype.close = function () {
 
 RequestLog.prototype.clear = function () {
   findElement.ID(this.windowDoc, "requestpolicy-requestLog-clear").click();
-}
+};
 
 
+RequestLog.prototype.getOrigin = function (aRow) {
+  return this.treeView.getCellText(aRow, this.originCol);
+};
+
 RequestLog.prototype.getDestination = function (aRow) {
   return this.treeView.getCellText(aRow, this.destCol);
 };
@@ -62,4 +73,27 @@ RequestLog.prototype.getDestinations = function () {
   return destinations;
 };
 
+RequestLog.prototype.hasBeenAllowed = function (aRow) {
+  let imageSrc = this.treeView.getImageSrc(aRow, this.blockedCol);
+  return imageSrc !== "chrome://requestpolicy/skin/dot.png";
+};
+
+RequestLog.prototype.getRow = function(aRow) {
+  return {
+    origin: this.getOrigin(aRow),
+    destination: this.getDestination(aRow),
+    hasBeenAllowed: this.hasBeenAllowed(aRow)
+  };
+};
+
+RequestLog.prototype.getRowsByDestination = function (aDestination) {
+  let rows = [];
+  for (let i = 0, len = this.treeView.rowCount; i < len; ++i) {
+    if (this.getDestination(i) === aDestination) {
+      rows.push(this.getRow(i));
+    }
+  }
+  return rows;
+};
+
 exports.RequestLog = RequestLog;
diff --git a/tests/mozmill/tests/testPolicy/manifest.ini b/tests/mozmill/tests/testPolicy/manifest.ini
index 0edd796..8c38597 100644
--- a/tests/mozmill/tests/testPolicy/manifest.ini
+++ b/tests/mozmill/tests/testPolicy/manifest.ini
@@ -1,4 +1,5 @@
 [parent:../manifest.ini]
 
+[testRuleWithSchemeOnly.js]
 [include:testIframeTree/manifest.ini]
 [include:testSchemeWorkaround/manifest.ini]
diff --git a/tests/mozmill/tests/testPolicy/testRuleWithSchemeOnly.js b/tests/mozmill/tests/testPolicy/testRuleWithSchemeOnly.js
new file mode 100644
index 0000000..0b53aeb
--- /dev/null
+++ b/tests/mozmill/tests/testPolicy/testRuleWithSchemeOnly.js
@@ -0,0 +1,268 @@
+/* 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 + "lib/prefs");
+var tabs = require(rootDir + "firefox/lib/tabs");
+
+var rpUtils = require(rpRootDir + "lib/rp-utils");
+var requestLogUtils = require(rpRootDir + "lib/request-log-utils");
+
+const TEST_URL = "http://www.maindomain.test/scheme-unknown-and-without-host-2.html";
+const SCHEME = "rpc";
+
+
+var setupModule = function (aModule) {
+  aModule.controller = mozmill.getBrowserController();
+  aModule.tabBrowser = new tabs.tabBrowser(aModule.controller);
+  aModule.tabBrowser.closeAllTabs();
+
+  aModule.requestLog = new requestLogUtils.RequestLog(aModule.controller);
+  aModule.requestLog.open();
+  aModule.requestLog.clear();
+};
+
+var teardownModule = function (aModule) {
+  aModule.requestLog.close();
+  aModule.tabBrowser.closeAllTabs();
+};
+
+var teardownTest = function (aModule) {
+  prefs.clearUserPref(rpConst.PREF_DEFAULT_ALLOW);
+  removeRules();
+}
+
+
+var RowUtils = {
+  getPolicy: function (row) {
+    return row.children[0].textContent;
+  },
+  getOrigin: function (row) {
+    return row.children[1].textContent;
+  },
+  getDest: function (row) {
+    return row.children[2].textContent;
+  },
+  getRuleSet: function (row) {
+    return row.children[3].textContent;
+  },
+  removeRule: function (row) {
+    findElement.Elem(row.children[4].getElementsByTagName("a")[0]).click();
+  },
+  countTableRows: function (tbody) {
+    return tbody.children.length;
+  }
+};
+
+
+/**
+ * Get all table row (html: <tr>) elements whose content of the
+ * "detination" column contains the specified string.
+ */
+function getTableRowElements(aTbody, aDestinationString) {
+  var rows = Array.prototype.slice.call(aTbody.children);
+
+  var filteredRows = rows.filter(function (row) {
+    if (RowUtils.getDest(row).indexOf(aDestinationString) === -1) {
+      return false;
+    }
+
+    return true;
+  });
+
+  return filteredRows;
+}
+
+/**
+ * Add a rule that either allows or denies requests to
+ * the scheme SCHEME.
+ */
+function addRule(aIsAllowRule) {
+  controller.open("about:requestpolicy?yourpolicy");
+  controller.waitForPageLoad();
+
+  var doc = controller.window.content.document;
+  var tbody = doc.getElementById("rules");
+
+  function numberOfRules() {
+    return RowUtils.countTableRows(tbody);
+  }
+
+  var numberOfRulesBefore = numberOfRules();
+
+  // fill the form
+  {
+    let destSchemeField = findElement.Name(doc, "destscheme");
+    destSchemeField.sendKeys(SCHEME);
+    let radioButtonId = aIsAllowRule ? "allowrule" : "denyrule";
+    findElement.ID(doc, radioButtonId).select();
+  }
+
+  // submit the form
+  {
+    let button = findElement.XPath(doc, "//button[@data-string='addRule']");
+    button.click();
+  }
+
+  //
+  // below, do checks whether the rule has been added correctly
+  //
+
+  controller.waitFor(function () {
+    return numberOfRules() > numberOfRulesBefore;
+  }, "The number of rules in the table has increased.");
+
+  assert.equal(numberOfRules() - numberOfRulesBefore, 1,
+               "The number of rules added is exactly one.");
+
+  var rows = getTableRowElements(tbody, SCHEME + ":");
+
+  assert.equal(rows.length, 1,
+               "There is exactly one rule with '" + SCHEME + "' " +
+               "being the destination scheme.");
+
+  // check the content of every single cell of that table row
+  {
+    let row = rows[0];
+
+    expect.equal(RowUtils.getPolicy(row), aIsAllowRule ? "Allow" : "Block",
+                 "The policy of the rule is correct.");
+    expect.equal(RowUtils.getOrigin(row),
+                 "",
+                 "The origin of the rule is correct.");
+    expect.equal(RowUtils.getDest(row),
+                 SCHEME + ":*",
+                 "The destination of the rule is correct.");
+    expect.equal(RowUtils.getRuleSet(row), "User",
+                 "The Rule Set of the rule is correct.");
+  }
+}
+
+/**
+ * Remove all rules that specify the scheme SCHEME.
+ */
+function removeRules() {
+  controller.open("about:requestpolicy?yourpolicy");
+  controller.waitForPageLoad();
+
+  var doc = controller.window.content.document;
+  var tbody = doc.getElementById("rules");
+
+  // remove the rules
+  {
+    let rows = getTableRowElements(tbody, SCHEME + ":");
+    rows.forEach(function (row) {
+      RowUtils.removeRule(row);
+    });
+  }
+
+  // do final checks
+  {
+    let rows = getTableRowElements(tbody, SCHEME + ":");
+    expect.equal(rows.length, 0, "There are no more rules for " +
+                 "URI scheme '" + SCHEME + "'.");
+  }
+}
+
+/**
+ * Return the string that is displayed in the table of rules.
+ */
+function isAllowedToString(aIsAllowed) {
+  return aIsAllowed ? "allowed" : "denied";
+}
+
+
+/**
+ * Do anything that has to be done before the test URL is opened.
+ */
+function setupTestCase({isDefaultAllow, allowRuleExists, denyRuleExists}) {
+  prefs.setPref(rpConst.PREF_DEFAULT_ALLOW, isDefaultAllow);
+
+  if (allowRuleExists) {
+    addRule(true);
+  }
+  if (denyRuleExists) {
+    addRule(false);
+  }
+
+  requestLog.clear();
+}
+
+
+/**
+ * Common function for all test cases
+ */
+function runTestCase(aTestCase) {
+  var {isDefaultAllow, allowRuleExists, denyRuleExists,
+       requestShouldBeAllowed} = aTestCase;
+
+  setupTestCase(aTestCase);
+
+  assert.ok(requestLog.treeView.rowCount === 0,
+            "The request log is empty.");
+
+  controller.open(TEST_URL);
+  controller.waitForPageLoad();
+
+  var iframeURI = controller.window.content.document
+      .getElementsByTagName("iframe")[0].src;
+
+  assert.ok(iframeURI.indexOf(SCHEME + ":") === 0,
+            "The iframe's URI starts with the correct scheme: '" +
+            SCHEME + "'.");
+
+  var rows = requestLog.getRowsByDestination(iframeURI);
+  assert.equal(rows.length, 1,
+               "There is exactly one entry in the request log with '" +
+               iframeURI + "' as the destination.");
+
+  // check the decision (allow or deny) on the request
+  {
+    let expectedDecision = isAllowedToString(requestShouldBeAllowed);
+    let actualDecision = isAllowedToString(rows[0].hasBeenAllowed);
+    assert.equal(actualDecision, expectedDecision,
+                 "The request has been " + expectedDecision + ".");
+  }
+}
+
+//
+// below, generate the test cases
+//
+
+var testDefaultDeny = runTestCase.bind(null,
+    {
+      isDefaultAllow: false,
+      allowRuleExists: false,
+      denyRuleExists: false,
+      requestShouldBeAllowed: false
+    });
+
+var testDefaultDenyWithAllowRule = runTestCase.bind(null,
+    {
+      isDefaultAllow: false,
+      allowRuleExists: true,
+      denyRuleExists: false,
+      requestShouldBeAllowed: true
+    });
+
+var testDefaultAllow = runTestCase.bind(null,
+    {
+      isDefaultAllow: true,
+      allowRuleExists: false,
+      denyRuleExists: false,
+      requestShouldBeAllowed: true
+    });
+
+var testDefaultAllowWithDenyRule = runTestCase.bind(null,
+    {
+      isDefaultAllow: true,
+      allowRuleExists: false,
+      denyRuleExists: true,
+      requestShouldBeAllowed: false
+    });

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