content/base/test/test_mixed_content_blocker.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_mixed_content_blocker.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +Tests for Mixed Content Blocker
     1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=62178
     1.9 +-->
    1.10 +<head>
    1.11 +  <meta charset="utf-8">
    1.12 +  <title>Tests for Bug 62178</title>
    1.13 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.14 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.15 +
    1.16 +  <script>
    1.17 +  var ph = SpecialPowers.Cc["@mozilla.org/plugin/host;1"]
    1.18 +                        .getService(SpecialPowers.Ci.nsIPluginHost);
    1.19 +  var tags = ph.getPluginTags();
    1.20 +  for (var tag of tags) {
    1.21 +    if (tag.name == "Test Plug-in") {
    1.22 +      tag.enabledState = SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED;;
    1.23 +    }
    1.24 +  }
    1.25 +
    1.26 +
    1.27 +  var origBlockDisplay = SpecialPowers.getBoolPref("security.mixed_content.block_display_content");
    1.28 +  var origBlockActive = SpecialPowers.getBoolPref("security.mixed_content.block_active_content");
    1.29 +
    1.30 +  var counter = 0;
    1.31 +  var settings = [ [true, true], [true, false], [false, true], [false, false] ];
    1.32 +
    1.33 +  var blockActive;
    1.34 +  var blockDisplay;
    1.35 +
    1.36 +  //Cycle through 4 different preference settings.
    1.37 +  function changePrefs(x) {
    1.38 +    SpecialPowers.setBoolPref("security.mixed_content.block_display_content", settings[x][0]);
    1.39 +    SpecialPowers.setBoolPref("security.mixed_content.block_active_content", settings[x][1]);
    1.40 +    blockDisplay = SpecialPowers.getBoolPref("security.mixed_content.block_display_content");
    1.41 +    blockActive = SpecialPowers.getBoolPref("security.mixed_content.block_active_content");
    1.42 +  }
    1.43 +
    1.44 +  //Set the first set of settings (true, true) and increment the counter.
    1.45 +  changePrefs(counter);
    1.46 +  counter++;
    1.47 +
    1.48 +  var testsToRun = {
    1.49 +    iframe: false,
    1.50 +    image: false,
    1.51 +    script: false,
    1.52 +    stylesheet: false,
    1.53 +    object: false,
    1.54 +    media: false,
    1.55 +    xhr: false,
    1.56 +  };
    1.57 +
    1.58 +  function log(msg) {
    1.59 +    document.getElementById("log").textContent += "\n" + msg;
    1.60 +  }
    1.61 +
    1.62 +  function checkTestsCompleted() {
    1.63 +    for (var prop in testsToRun) {
    1.64 +      // some test hasn't run yet so we're not done
    1.65 +      if (!testsToRun[prop])
    1.66 +        return;
    1.67 +    }
    1.68 +    //if the testsToRun are all completed, chnage the pref and run the tests again until we have cycled through all the prefs.
    1.69 +    if(counter < 4) {
    1.70 +       for (var prop in testsToRun) {
    1.71 +         testsToRun[prop] = false;
    1.72 +       }
    1.73 +      //call to change the preferences
    1.74 +      changePrefs(counter);
    1.75 +      counter++;
    1.76 +      log("\nblockDisplay set to "+blockDisplay+", blockActive set to "+blockActive+".");
    1.77 +      document.getElementById('framediv').innerHTML = '<iframe id="testHarness" src="https://example.com/tests/content/base/test/file_mixed_content_main.html"></iframe>';
    1.78 +    }
    1.79 +    else {
    1.80 +      //set the prefs back to what they were set to originally
    1.81 +      SpecialPowers.setBoolPref("security.mixed_content.block_display_content", origBlockDisplay);
    1.82 +      SpecialPowers.setBoolPref("security.mixed_content.block_active_content", origBlockActive);
    1.83 +      SimpleTest.finish();
    1.84 +    }
    1.85 +  }
    1.86 +
    1.87 +  var firstTest = true;
    1.88 +
    1.89 +  // listen for a messages from the mixed content test harness
    1.90 +  window.addEventListener("message", receiveMessage, false);
    1.91 +  function receiveMessage(event) {
    1.92 +    if(firstTest) {
    1.93 +      log("blockActive set to "+blockActive+", blockDisplay set to "+blockDisplay+".");
    1.94 +      firstTest = false;
    1.95 +    }
    1.96 +
    1.97 +    log("test: "+event.data.test+", msg: "+event.data.msg + " logging message.");
    1.98 +    // test that the load type matches the pref for this type of content
    1.99 +    // (i.e. active vs. display)
   1.100 +
   1.101 +    switch(event.data.test) {
   1.102 +
   1.103 +      /* Mixed Script tests */
   1.104 +      case "iframe":
   1.105 +        ok(blockActive == (event.data.msg == "insecure iframe blocked"), "iframe did not follow block_active_content pref");
   1.106 +        testsToRun["iframe"] = true;
   1.107 +        break;
   1.108 +
   1.109 +      case "object":
   1.110 +        ok(blockActive == (event.data.msg == "insecure object blocked"), "object did not follow block_active_content pref");
   1.111 +        testsToRun["object"] = true;
   1.112 +        break;
   1.113 +
   1.114 +      case "script":
   1.115 +        ok(blockActive == (event.data.msg == "insecure script blocked"), "script did not follow block_active_content pref");
   1.116 +        testsToRun["script"] = true;
   1.117 +        break;
   1.118 +
   1.119 +      case "stylesheet":
   1.120 +        ok(blockActive == (event.data.msg == "insecure stylesheet blocked"), "stylesheet did not follow block_active_content pref");
   1.121 +        testsToRun["stylesheet"] = true;
   1.122 +        break;
   1.123 +
   1.124 +      case "xhr":
   1.125 +        ok(blockActive == (event.data.msg == "insecure xhr blocked"), "xhr did not follow block_active_content pref");
   1.126 +        testsToRun["xhr"] = true;
   1.127 +        break;
   1.128 +
   1.129 +      /* Mixed Display tests */
   1.130 +      case "image":
   1.131 +        //test that the image load matches the pref for dipslay content
   1.132 +        ok(blockDisplay == (event.data.msg == "insecure image blocked"), "image did not follow block_display_content pref");
   1.133 +        testsToRun["image"] = true;
   1.134 +        break;
   1.135 +
   1.136 +      case "media":
   1.137 +        ok(blockDisplay == (event.data.msg == "insecure media blocked"), "media did not follow block_display_content pref");
   1.138 +        testsToRun["media"] = true;
   1.139 +        break;
   1.140 +    }
   1.141 +    checkTestsCompleted();
   1.142 +  }
   1.143 +
   1.144 +  SimpleTest.waitForExplicitFinish();
   1.145 +  </script>
   1.146 +</head>
   1.147 +
   1.148 +<body>
   1.149 +  <div id="framediv">
   1.150 +    <iframe id="testHarness" src="https://example.com/tests/content/base/test/file_mixed_content_main.html"></iframe>
   1.151 +  </div>
   1.152 +  <pre id="log"></pre>
   1.153 +</body>
   1.154 +</html>

mercurial