netwerk/test/unit/test_cookie_header.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // This file tests bug 250375
michael@0 2
michael@0 3 Cu.import("resource://testing-common/httpd.js");
michael@0 4 Cu.import("resource://gre/modules/Services.jsm");
michael@0 5
michael@0 6 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 7 return "http://localhost:" + httpserv.identity.primaryPort + "/";
michael@0 8 });
michael@0 9
michael@0 10 function inChildProcess() {
michael@0 11 return Cc["@mozilla.org/xre/app-info;1"]
michael@0 12 .getService(Ci.nsIXULRuntime)
michael@0 13 .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
michael@0 14 }
michael@0 15
michael@0 16 function check_request_header(chan, name, value) {
michael@0 17 var chanValue;
michael@0 18 try {
michael@0 19 chanValue = chan.getRequestHeader(name);
michael@0 20 } catch (e) {
michael@0 21 do_throw("Expected to find header '" + name + "' but didn't find it, got exception: " + e);
michael@0 22 }
michael@0 23 dump("Value for header '" + name + "' is '" + chanValue + "'\n");
michael@0 24 do_check_eq(chanValue, value);
michael@0 25 }
michael@0 26
michael@0 27 var cookieVal = "C1=V1";
michael@0 28
michael@0 29 var listener = {
michael@0 30 onStartRequest: function test_onStartR(request, ctx) {
michael@0 31 try {
michael@0 32 var chan = request.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 33 check_request_header(chan, "Cookie", cookieVal);
michael@0 34 } catch (e) {
michael@0 35 do_throw("Unexpected exception: " + e);
michael@0 36 }
michael@0 37
michael@0 38 throw Components.results.NS_ERROR_ABORT;
michael@0 39 },
michael@0 40
michael@0 41 onDataAvailable: function test_ODA() {
michael@0 42 throw Components.results.NS_ERROR_UNEXPECTED;
michael@0 43 },
michael@0 44
michael@0 45 onStopRequest: function test_onStopR(request, ctx, status) {
michael@0 46 if (this._iteration == 1) {
michael@0 47 run_test_continued();
michael@0 48 } else {
michael@0 49 do_test_pending();
michael@0 50 httpserv.stop(do_test_finished);
michael@0 51 }
michael@0 52 do_test_finished();
michael@0 53 },
michael@0 54
michael@0 55 _iteration: 1
michael@0 56 };
michael@0 57
michael@0 58 function makeChan() {
michael@0 59 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 60 .getService(Components.interfaces.nsIIOService);
michael@0 61 var chan = ios.newChannel(URL, null, null)
michael@0 62 .QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 63
michael@0 64 return chan;
michael@0 65 }
michael@0 66
michael@0 67 var httpserv = null;
michael@0 68
michael@0 69 function run_test() {
michael@0 70 // Allow all cookies if the pref service is available in this process.
michael@0 71 if (!inChildProcess())
michael@0 72 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
michael@0 73
michael@0 74 httpserv = new HttpServer();
michael@0 75 httpserv.start(-1);
michael@0 76
michael@0 77 var chan = makeChan();
michael@0 78
michael@0 79 chan.setRequestHeader("Cookie", cookieVal, false);
michael@0 80
michael@0 81 chan.asyncOpen(listener, null);
michael@0 82
michael@0 83 do_test_pending();
michael@0 84 }
michael@0 85
michael@0 86 function run_test_continued() {
michael@0 87 var chan = makeChan();
michael@0 88
michael@0 89 var cookServ = Components.classes["@mozilla.org/cookieService;1"]
michael@0 90 .getService(Components.interfaces.nsICookieService);
michael@0 91 var cookie2 = "C2=V2";
michael@0 92 cookServ.setCookieString(chan.URI, null, cookie2, chan);
michael@0 93 chan.setRequestHeader("Cookie", cookieVal, false);
michael@0 94
michael@0 95 // We expect that the setRequestHeader overrides the
michael@0 96 // automatically-added one, so insert cookie2 in front
michael@0 97 cookieVal = cookie2 + "; " + cookieVal;
michael@0 98
michael@0 99 listener._iteration++;
michael@0 100 chan.asyncOpen(listener, null);
michael@0 101
michael@0 102 do_test_pending();
michael@0 103 }

mercurial