dom/tests/mochitest/bugs/test_bug346659.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=346659
     5 -->
     6 <head>
     7   <title>Test for Bug 346659</title>
     8   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     9   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    10 </head>
    11 <body>
    12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=346659">Mozilla Bug 346659</a>
    13 <p id="display"></p>
    14 <div id="content" style="display: none">
    16 </div>
    17 <pre id="test">
    18 <script type="application/javascript">
    20 /** Test for Bug 346659 **/
    21 // do the tests in two batches, because otherwise the popup blocker kills off
    22 // our test because it opens too many windows.
    23 var numTestsSet1 = 6;
    24 var numTestsSet2 = 4;
    25 var numTestsSet3 = 2;
    26 var complete = 0;
    27 SimpleTest.waitForExplicitFinish();
    29 var wins = [];
    31 function r(base, tail) {
    32   return base.replace(/\/[^\/]*$/, "/" + tail);
    33 }
    35 function handleCmd(evt) {
    36   var cmd;
    37   try {
    38     cmd = JSON.parse(evt.data);
    39   } catch (e) {
    40     // Not json
    41     return false;
    42   }  
    44   // Grab privileges so we can access cross-domain windows
    45   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    47   if ("load" in cmd) {
    48     var testNum = cmd.load;
    49     var win = wins[testNum];
    50     win.childWin.x = testNum;
    51     if (win.childWin.opener == win) {
    52       if ("xsite" in cmd) {
    53         var loc = r(window.location.href, "bug346659-opener-echoer.html");
    54       } else {
    55         var loc = r(win.location.href, "bug346659-opener-echoer.html");
    56       }
    57     } else {
    58       if ("xsite" in cmd) {
    59         var loc = r(window.location.href, "bug346659-parent-echoer.html");
    60       } else {
    61         var loc = r(win.location.href, "bug346659-parent-echoer.html");
    62       }
    63     }
    64     win.childWin.location.href = loc;
    65     wins[testNum] = null;
    66   } else if ("write" in cmd) {
    67     var testNum = cmd.write;
    68     var win = wins[testNum];
    69     win.childWin.x = testNum;
    70     try {
    71       if (win.childWin.opener == win) {
    72         win.childWin.document.write('<script>window.opener.opener.postMessage(window.opener.testNum + " - " + window.x, "http://mochi.test:8888/"); window.opener.close(); window.close();<' + '/script>');
    73       } else {
    74         win.childWin.document.write('<script>window.parent.opener.postMessage(window.parent.testNum + " - " + window.x, "http://mochi.test:8888/"); window.parent.close();<' + '/script>');
    75       }
    76     } catch (e if (e.name == "SecurityError" && e.code == 18)) {
    77       // Security error on cross-site write() is fine
    78       if (win.childWin.opener == win) {
    79         win.childWin.close();
    80       }
    81       win.close()
    82       handleTestEnd();
    83     }
    84     wins[testNum] = null;
    85   }
    86   return true;
    87 }
    89 function messageReceiver(evt) {
    90   // First try to detect a load/write command
    91   if (handleCmd(evt)) {
    92     return;
    93   }
    95   var testNumber = parseInt(evt.data);
    96   var testResult = evt.data.substring(3 + Math.floor(Math.log(testNumber) * Math.LOG10E + 1));
    98   switch (testNumber) {
    99     case 1:
   100       is(testResult, 1, "Props on new window should be preserved when loading");
   101       break;
   102     case 2:
   103       is(testResult, 2, "Props on new window should be preserved when writing");
   104       break;
   105     case 3:
   106       is(testResult, 3, "Props on window opened from new window should be preserved when loading");
   107       break;
   108     case 4:
   109       is(testResult, 4, "Props on window opened from new window should be preserved when writing");
   110       break;
   111     case 5:
   112       is(testResult, "undefined", "Props on new window's child should go away when loading");
   113       break;
   114     case 6:
   115       is(testResult, "undefined", "Props on new window's child should go away when writing");
   116       break;
   117     case 7:
   118       is(testResult, 7, "Props on different-domain window opened from different-domain new window can stay");
   119       break;
   120     case 9:
   121       is(testResult, "undefined", "Props on different-domain new window's child should go away when loading");
   122       break;
   123     case 11:
   124       is(testResult, "undefined", "Props on same-domain window opened from different-domain new window should go away when loading");
   125       break;
   126     case 12:
   127       is(testResult, "undefined", "Props on different-domain new window's same-domain child should go away when loading");
   128       break;
   129     default:
   130       ok(0, "unexpected test number (" + testNumber + ") when data is " + evt.data);
   131   }
   133   handleTestEnd();
   134 }
   136 function handleTestEnd() {
   137   function gc() {
   138     SpecialPowers.DOMWindowUtils.garbageCollect();
   139   }
   140   if (numTestsSet1) {
   141     if (!--numTestsSet1) {
   142       // gc to get rid of all the old windows
   143       gc(); gc(); gc();
   144       setTimeout(startSecondBatch, 0);
   145     }
   146   } else if (numTestsSet2) {
   147     if (!--numTestsSet2) {
   148       // gc to get rid of all the old windows
   149       gc(); gc(); gc();
   150       setTimeout(startThirdBatch, 0);
   151     }
   152   } else if (!--numTestsSet3) {
   153     SimpleTest.finish();
   154   }
   155 }
   156 window.addEventListener("message", messageReceiver, false);
   158 var win = window.open("");
   159 win.x = 1;
   160 win.location.href = "bug346659-echoer.html";
   162 win = window.open("");
   163 win.x = 2;
   164 win.document.write('<script> window.opener.postMessage("2 - " + window.x, window.location.href); window.close(); </' + 'script>');
   166 wins[3] = window.open('bug346659-opener.html?{"load":3}');
   167 wins[4] = window.open('bug346659-opener.html?{"write":4}');
   168 wins[5] = window.open('bug346659-parent.html?{"load":5}');
   169 wins[6] = window.open('bug346659-parent.html?{"write":6}');
   171 is(location.host, "mochi.test:8888", "Unexpected host");
   173 function startSecondBatch() {
   174   var baseurl = window.location.href.replace(/mochi\.test:8888/, "example.com");
   175   wins[7] = window.open(r(baseurl, 'bug346659-opener.html?{"load":7}'));
   176   wins[8] = window.open(r(baseurl, 'bug346659-opener.html?{"write":8}'));
   177   wins[9] = window.open(r(baseurl, 'bug346659-parent.html?{"load":9}'));
   178   wins[10] = window.open(r(baseurl, 'bug346659-parent.html?{"write":10}'));
   179 }
   181 function startThirdBatch() {
   182   var baseurl = window.location.href.replace(/mochi\.test:8888/, "example.com");
   183   wins[11] = window.open(r(baseurl, 'bug346659-opener.html?{"load":11,"xsite":true}'));
   184   wins[12] = window.open(r(baseurl, 'bug346659-parent.html?{"load":12,"xsite":true}'));
   185 }
   186 </script>
   187 </pre>
   188 </body>
   189 </html>

mercurial