netwerk/test/unit/test_cookiejars.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * Test that channels with different
michael@0 7 * AppIds/inBrowserElements/usePrivateBrowsing (from nsILoadContext callback)
michael@0 8 * are stored in separate namespaces ("cookie jars")
michael@0 9 */
michael@0 10
michael@0 11 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 12 return "http://localhost:" + httpserver.identity.primaryPort;
michael@0 13 });
michael@0 14
michael@0 15 Cu.import("resource://testing-common/httpd.js");
michael@0 16 Cu.import("resource://gre/modules/Services.jsm");
michael@0 17 var httpserver = new HttpServer();
michael@0 18
michael@0 19 var cookieSetPath = "/setcookie";
michael@0 20 var cookieCheckPath = "/checkcookie";
michael@0 21
michael@0 22 function inChildProcess() {
michael@0 23 return Cc["@mozilla.org/xre/app-info;1"]
michael@0 24 .getService(Ci.nsIXULRuntime)
michael@0 25 .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
michael@0 26 }
michael@0 27
michael@0 28 // Test array:
michael@0 29 // - element 0: name for cookie, used both to set and later to check
michael@0 30 // - element 1: loadContext (determines cookie namespace)
michael@0 31 //
michael@0 32 // TODO: bug 722850: make private browsing work per-app, and add tests. For now
michael@0 33 // all values are 'false' for PB.
michael@0 34
michael@0 35 var tests = [
michael@0 36 { cookieName: 'LCC_App0_BrowF_PrivF',
michael@0 37 loadContext: new LoadContextCallback(0, false, false, 1) },
michael@0 38 { cookieName: 'LCC_App0_BrowT_PrivF',
michael@0 39 loadContext: new LoadContextCallback(0, true, false, 1) },
michael@0 40 { cookieName: 'LCC_App1_BrowF_PrivF',
michael@0 41 loadContext: new LoadContextCallback(1, false, false, 1) },
michael@0 42 { cookieName: 'LCC_App1_BrowT_PrivF',
michael@0 43 loadContext: new LoadContextCallback(1, true, false, 1) },
michael@0 44 ];
michael@0 45
michael@0 46 // test number: index into 'tests' array
michael@0 47 var i = 0;
michael@0 48
michael@0 49 function setupChannel(path)
michael@0 50 {
michael@0 51 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 52 var chan = ios.newChannel(URL + path, "", null);
michael@0 53 chan.notificationCallbacks = tests[i].loadContext;
michael@0 54 chan.QueryInterface(Ci.nsIHttpChannel);
michael@0 55 return chan;
michael@0 56 }
michael@0 57
michael@0 58 function setCookie() {
michael@0 59 var channel = setupChannel(cookieSetPath);
michael@0 60 channel.setRequestHeader("foo-set-cookie", tests[i].cookieName, false);
michael@0 61 channel.asyncOpen(new ChannelListener(setNextCookie, null), null);
michael@0 62 }
michael@0 63
michael@0 64 function setNextCookie(request, data, context)
michael@0 65 {
michael@0 66 if (++i == tests.length) {
michael@0 67 // all cookies set: switch to checking them
michael@0 68 i = 0;
michael@0 69 checkCookie();
michael@0 70 } else {
michael@0 71 do_print("setNextCookie:i=" + i);
michael@0 72 setCookie();
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 // Open channel that should send one and only one correct Cookie: header to
michael@0 77 // server, corresponding to it's namespace
michael@0 78 function checkCookie()
michael@0 79 {
michael@0 80 var channel = setupChannel(cookieCheckPath);
michael@0 81 channel.asyncOpen(new ChannelListener(completeCheckCookie, null), null);
michael@0 82 }
michael@0 83
michael@0 84 function completeCheckCookie(request, data, context) {
michael@0 85 // Look for all cookies in what the server saw: fail if we see any besides the
michael@0 86 // one expected cookie for each namespace;
michael@0 87 var expectedCookie = tests[i].cookieName;
michael@0 88 request.QueryInterface(Ci.nsIHttpChannel);
michael@0 89 var cookiesSeen = request.getResponseHeader("foo-saw-cookies");
michael@0 90
michael@0 91 var j;
michael@0 92 for (j = 0; j < tests.length; j++) {
michael@0 93 var cookieToCheck = tests[j].cookieName;
michael@0 94 found = (cookiesSeen.indexOf(cookieToCheck) != -1);
michael@0 95 if (found && expectedCookie != cookieToCheck) {
michael@0 96 do_throw("test index " + i + ": found unexpected cookie '"
michael@0 97 + cookieToCheck + "': in '" + cookiesSeen + "'");
michael@0 98 } else if (!found && expectedCookie == cookieToCheck) {
michael@0 99 do_throw("test index " + i + ": missing expected cookie '"
michael@0 100 + expectedCookie + "': in '" + cookiesSeen + "'");
michael@0 101 }
michael@0 102 }
michael@0 103 // If we get here we're good.
michael@0 104 do_print("Saw only correct cookie '" + expectedCookie + "'");
michael@0 105 do_check_true(true);
michael@0 106
michael@0 107
michael@0 108 if (++i == tests.length) {
michael@0 109 // end of tests
michael@0 110 httpserver.stop(do_test_finished);
michael@0 111 } else {
michael@0 112 checkCookie();
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 function run_test()
michael@0 117 {
michael@0 118 // Allow all cookies if the pref service is available in this process.
michael@0 119 if (!inChildProcess())
michael@0 120 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
michael@0 121
michael@0 122 httpserver.registerPathHandler(cookieSetPath, cookieSetHandler);
michael@0 123 httpserver.registerPathHandler(cookieCheckPath, cookieCheckHandler);
michael@0 124 httpserver.start(-1);
michael@0 125
michael@0 126 setCookie();
michael@0 127 do_test_pending();
michael@0 128 }
michael@0 129
michael@0 130 function cookieSetHandler(metadata, response)
michael@0 131 {
michael@0 132 var cookieName = metadata.getHeader("foo-set-cookie");
michael@0 133
michael@0 134 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 135 response.setHeader("Set-Cookie", cookieName + "=1; Path=/", false);
michael@0 136 response.setHeader("Content-Type", "text/plain");
michael@0 137 response.bodyOutputStream.write("Ok", "Ok".length);
michael@0 138 }
michael@0 139
michael@0 140 function cookieCheckHandler(metadata, response)
michael@0 141 {
michael@0 142 var cookies = metadata.getHeader("Cookie");
michael@0 143
michael@0 144 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 145 response.setHeader("foo-saw-cookies", cookies, false);
michael@0 146 response.setHeader("Content-Type", "text/plain");
michael@0 147 response.bodyOutputStream.write("Ok", "Ok".length);
michael@0 148 }
michael@0 149

mercurial