michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Test that channels with different michael@0: * AppIds/inBrowserElements/usePrivateBrowsing (from nsILoadContext callback) michael@0: * are stored in separate namespaces ("cookie jars") michael@0: */ michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "URL", function() { michael@0: return "http://localhost:" + httpserver.identity.primaryPort; michael@0: }); michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: var httpserver = new HttpServer(); michael@0: michael@0: var cookieSetPath = "/setcookie"; michael@0: var cookieCheckPath = "/checkcookie"; michael@0: michael@0: function inChildProcess() { michael@0: return Cc["@mozilla.org/xre/app-info;1"] michael@0: .getService(Ci.nsIXULRuntime) michael@0: .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; michael@0: } michael@0: michael@0: // Test array: michael@0: // - element 0: name for cookie, used both to set and later to check michael@0: // - element 1: loadContext (determines cookie namespace) michael@0: // michael@0: // TODO: bug 722850: make private browsing work per-app, and add tests. For now michael@0: // all values are 'false' for PB. michael@0: michael@0: var tests = [ michael@0: { cookieName: 'LCC_App0_BrowF_PrivF', michael@0: loadContext: new LoadContextCallback(0, false, false, 1) }, michael@0: { cookieName: 'LCC_App0_BrowT_PrivF', michael@0: loadContext: new LoadContextCallback(0, true, false, 1) }, michael@0: { cookieName: 'LCC_App1_BrowF_PrivF', michael@0: loadContext: new LoadContextCallback(1, false, false, 1) }, michael@0: { cookieName: 'LCC_App1_BrowT_PrivF', michael@0: loadContext: new LoadContextCallback(1, true, false, 1) }, michael@0: ]; michael@0: michael@0: // test number: index into 'tests' array michael@0: var i = 0; michael@0: michael@0: function setupChannel(path) michael@0: { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel(URL + path, "", null); michael@0: chan.notificationCallbacks = tests[i].loadContext; michael@0: chan.QueryInterface(Ci.nsIHttpChannel); michael@0: return chan; michael@0: } michael@0: michael@0: function setCookie() { michael@0: var channel = setupChannel(cookieSetPath); michael@0: channel.setRequestHeader("foo-set-cookie", tests[i].cookieName, false); michael@0: channel.asyncOpen(new ChannelListener(setNextCookie, null), null); michael@0: } michael@0: michael@0: function setNextCookie(request, data, context) michael@0: { michael@0: if (++i == tests.length) { michael@0: // all cookies set: switch to checking them michael@0: i = 0; michael@0: checkCookie(); michael@0: } else { michael@0: do_print("setNextCookie:i=" + i); michael@0: setCookie(); michael@0: } michael@0: } michael@0: michael@0: // Open channel that should send one and only one correct Cookie: header to michael@0: // server, corresponding to it's namespace michael@0: function checkCookie() michael@0: { michael@0: var channel = setupChannel(cookieCheckPath); michael@0: channel.asyncOpen(new ChannelListener(completeCheckCookie, null), null); michael@0: } michael@0: michael@0: function completeCheckCookie(request, data, context) { michael@0: // Look for all cookies in what the server saw: fail if we see any besides the michael@0: // one expected cookie for each namespace; michael@0: var expectedCookie = tests[i].cookieName; michael@0: request.QueryInterface(Ci.nsIHttpChannel); michael@0: var cookiesSeen = request.getResponseHeader("foo-saw-cookies"); michael@0: michael@0: var j; michael@0: for (j = 0; j < tests.length; j++) { michael@0: var cookieToCheck = tests[j].cookieName; michael@0: found = (cookiesSeen.indexOf(cookieToCheck) != -1); michael@0: if (found && expectedCookie != cookieToCheck) { michael@0: do_throw("test index " + i + ": found unexpected cookie '" michael@0: + cookieToCheck + "': in '" + cookiesSeen + "'"); michael@0: } else if (!found && expectedCookie == cookieToCheck) { michael@0: do_throw("test index " + i + ": missing expected cookie '" michael@0: + expectedCookie + "': in '" + cookiesSeen + "'"); michael@0: } michael@0: } michael@0: // If we get here we're good. michael@0: do_print("Saw only correct cookie '" + expectedCookie + "'"); michael@0: do_check_true(true); michael@0: michael@0: michael@0: if (++i == tests.length) { michael@0: // end of tests michael@0: httpserver.stop(do_test_finished); michael@0: } else { michael@0: checkCookie(); michael@0: } michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: // Allow all cookies if the pref service is available in this process. michael@0: if (!inChildProcess()) michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: michael@0: httpserver.registerPathHandler(cookieSetPath, cookieSetHandler); michael@0: httpserver.registerPathHandler(cookieCheckPath, cookieCheckHandler); michael@0: httpserver.start(-1); michael@0: michael@0: setCookie(); michael@0: do_test_pending(); michael@0: } michael@0: michael@0: function cookieSetHandler(metadata, response) michael@0: { michael@0: var cookieName = metadata.getHeader("foo-set-cookie"); michael@0: michael@0: response.setStatusLine(metadata.httpVersion, 200, "Ok"); michael@0: response.setHeader("Set-Cookie", cookieName + "=1; Path=/", false); michael@0: response.setHeader("Content-Type", "text/plain"); michael@0: response.bodyOutputStream.write("Ok", "Ok".length); michael@0: } michael@0: michael@0: function cookieCheckHandler(metadata, response) michael@0: { michael@0: var cookies = metadata.getHeader("Cookie"); michael@0: michael@0: response.setStatusLine(metadata.httpVersion, 200, "Ok"); michael@0: response.setHeader("foo-saw-cookies", cookies, false); michael@0: response.setHeader("Content-Type", "text/plain"); michael@0: response.bodyOutputStream.write("Ok", "Ok".length); michael@0: } michael@0: