netwerk/test/unit/test_cookiejars_safebrowsing.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 * Description of the test:
michael@0 7 * We show that we can separate the safebrowsing cookie by creating a custom
michael@0 8 * LoadContext using a reserved AppId (UINT_32_MAX - 1). Setting this
michael@0 9 * custom LoadContext as a callback on the channel allows us to query the
michael@0 10 * AppId and therefore separate the safebrowing cookie in its own cookie-jar.
michael@0 11 * For testing safebrowsing update we do >> NOT << emulate a response
michael@0 12 * in the body, rather we only set the cookies in the header of the response
michael@0 13 * and confirm that cookies are separated in their own cookie-jar.
michael@0 14 *
michael@0 15 * 1) We init safebrowsing and simulate an update (cookies are set for localhost)
michael@0 16 *
michael@0 17 * 2) We open a channel that should send regular cookies, but not the
michael@0 18 * safebrowsing cookie.
michael@0 19 *
michael@0 20 * 3) We open a channel with a custom callback, simulating a safebrowsing cookie
michael@0 21 * that should send this simulated safebrowsing cookie as well as the
michael@0 22 * real safebrowsing cookies. (Confirming that the safebrowsing cookies
michael@0 23 * actually get stored in the correct jar).
michael@0 24 */
michael@0 25
michael@0 26 Cu.import("resource://testing-common/httpd.js");
michael@0 27 Cu.import("resource://gre/modules/Services.jsm");
michael@0 28
michael@0 29 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 30 return "http://localhost:" + httpserver.identity.primaryPort;
michael@0 31 });
michael@0 32
michael@0 33 XPCOMUtils.defineLazyModuleGetter(this, "SafeBrowsing",
michael@0 34 "resource://gre/modules/SafeBrowsing.jsm");
michael@0 35
michael@0 36 var setCookiePath = "/setcookie";
michael@0 37 var checkCookiePath = "/checkcookie";
michael@0 38 var safebrowsingUpdatePath = "/safebrowsingUpdate";
michael@0 39 var httpserver;
michael@0 40
michael@0 41 function inChildProcess() {
michael@0 42 return Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
michael@0 43 .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
michael@0 44 }
michael@0 45
michael@0 46 function cookieSetHandler(metadata, response) {
michael@0 47 var cookieName = metadata.getHeader("set-cookie");
michael@0 48 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 49 response.setHeader("set-Cookie", cookieName + "=1; Path=/", false);
michael@0 50 response.setHeader("Content-Type", "text/plain");
michael@0 51 response.bodyOutputStream.write("Ok", "Ok".length);
michael@0 52 }
michael@0 53
michael@0 54 function cookieCheckHandler(metadata, response) {
michael@0 55 var cookies = metadata.getHeader("Cookie");
michael@0 56 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 57 response.setHeader("saw-cookies", cookies, false);
michael@0 58 response.setHeader("Content-Type", "text/plain");
michael@0 59 response.bodyOutputStream.write("Ok", "Ok".length);
michael@0 60 }
michael@0 61
michael@0 62 function safebrowsingUpdateHandler(metadata, response) {
michael@0 63 var cookieName = "sb-update-cookie";
michael@0 64 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 65 response.setHeader("set-Cookie", cookieName + "=1; Path=/", false);
michael@0 66 response.setHeader("Content-Type", "text/plain");
michael@0 67 response.bodyOutputStream.write("Ok", "Ok".length);
michael@0 68 }
michael@0 69
michael@0 70 function setupChannel(path, loadContext) {
michael@0 71 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 72 var channel = ios.newChannel(URL + path, "", null);
michael@0 73 channel.notificationCallbacks = loadContext;
michael@0 74 channel.QueryInterface(Ci.nsIHttpChannel);
michael@0 75 return channel;
michael@0 76 }
michael@0 77
michael@0 78 function run_test() {
michael@0 79
michael@0 80 // Set up a profile
michael@0 81 do_get_profile();
michael@0 82
michael@0 83 // Allow all cookies if the pref service is available in this process.
michael@0 84 if (!inChildProcess())
michael@0 85 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
michael@0 86
michael@0 87 httpserver = new HttpServer();
michael@0 88 httpserver.registerPathHandler(setCookiePath, cookieSetHandler);
michael@0 89 httpserver.registerPathHandler(checkCookiePath, cookieCheckHandler);
michael@0 90 httpserver.registerPathHandler(safebrowsingUpdatePath, safebrowsingUpdateHandler);
michael@0 91
michael@0 92 httpserver.start(-1);
michael@0 93 run_next_test();
michael@0 94 }
michael@0 95
michael@0 96 // this test does not emulate a response in the body,
michael@0 97 // rather we only set the cookies in the header of response.
michael@0 98 add_test(function test_safebrowsing_update() {
michael@0 99
michael@0 100 var dbservice = Cc["@mozilla.org/url-classifier/dbservice;1"]
michael@0 101 .getService(Ci.nsIUrlClassifierDBService);
michael@0 102 var streamUpdater = Cc["@mozilla.org/url-classifier/streamupdater;1"]
michael@0 103 .getService(Ci.nsIUrlClassifierStreamUpdater);
michael@0 104
michael@0 105 streamUpdater.updateUrl = URL + safebrowsingUpdatePath;
michael@0 106
michael@0 107 function onSuccess() {
michael@0 108 run_next_test();
michael@0 109 }
michael@0 110 function onUpdateError() {
michael@0 111 do_throw("ERROR: received onUpdateError!");
michael@0 112 }
michael@0 113 function onDownloadError() {
michael@0 114 do_throw("ERROR: received onDownloadError!");
michael@0 115 }
michael@0 116
michael@0 117 streamUpdater.downloadUpdates("test-phish-simple,test-malware-simple", "",
michael@0 118 onSuccess, onUpdateError, onDownloadError);
michael@0 119 });
michael@0 120
michael@0 121 add_test(function test_non_safebrowsing_cookie() {
michael@0 122
michael@0 123 var cookieName = 'regCookie_id0';
michael@0 124 var loadContext = new LoadContextCallback(0, false, false, false);
michael@0 125
michael@0 126 function setNonSafeBrowsingCookie() {
michael@0 127 var channel = setupChannel(setCookiePath, loadContext);
michael@0 128 channel.setRequestHeader("set-cookie", cookieName, false);
michael@0 129 channel.asyncOpen(new ChannelListener(checkNonSafeBrowsingCookie, null), null);
michael@0 130 }
michael@0 131
michael@0 132 function checkNonSafeBrowsingCookie() {
michael@0 133 var channel = setupChannel(checkCookiePath, loadContext);
michael@0 134 channel.asyncOpen(new ChannelListener(completeCheckNonSafeBrowsingCookie, null), null);
michael@0 135 }
michael@0 136
michael@0 137 function completeCheckNonSafeBrowsingCookie(request, data, context) {
michael@0 138 // Confirm that only the >> ONE << cookie is sent over the channel.
michael@0 139 var expectedCookie = cookieName + "=1";
michael@0 140 request.QueryInterface(Ci.nsIHttpChannel);
michael@0 141 var cookiesSeen = request.getResponseHeader("saw-cookies");
michael@0 142 do_check_eq(cookiesSeen, expectedCookie);
michael@0 143 run_next_test();
michael@0 144 }
michael@0 145
michael@0 146 setNonSafeBrowsingCookie();
michael@0 147 });
michael@0 148
michael@0 149 add_test(function test_safebrowsing_cookie() {
michael@0 150
michael@0 151 var cookieName = 'sbCookie_id4294967294';
michael@0 152 var loadContext = new LoadContextCallback(Ci.nsIScriptSecurityManager.SAFEBROWSING_APP_ID, false, false, false);
michael@0 153
michael@0 154 function setSafeBrowsingCookie() {
michael@0 155 var channel = setupChannel(setCookiePath, loadContext);
michael@0 156 channel.setRequestHeader("set-cookie", cookieName, false);
michael@0 157 channel.asyncOpen(new ChannelListener(checkSafeBrowsingCookie, null), null);
michael@0 158 }
michael@0 159
michael@0 160 function checkSafeBrowsingCookie() {
michael@0 161 var channel = setupChannel(checkCookiePath, loadContext);
michael@0 162 channel.asyncOpen(new ChannelListener(completeCheckSafeBrowsingCookie, null), null);
michael@0 163 }
michael@0 164
michael@0 165 function completeCheckSafeBrowsingCookie(request, data, context) {
michael@0 166 // Confirm that all >> THREE << cookies are sent back over the channel:
michael@0 167 // a) the safebrowsing cookie set when updating
michael@0 168 // b) the regular cookie with custom loadcontext defined in this test.
michael@0 169 var expectedCookies = "sb-update-cookie=1; ";
michael@0 170 expectedCookies += cookieName + "=1";
michael@0 171 request.QueryInterface(Ci.nsIHttpChannel);
michael@0 172 var cookiesSeen = request.getResponseHeader("saw-cookies");
michael@0 173
michael@0 174 do_check_eq(cookiesSeen, expectedCookies);
michael@0 175 httpserver.stop(do_test_finished);
michael@0 176 }
michael@0 177
michael@0 178 setSafeBrowsingCookie();
michael@0 179 });

mercurial