netwerk/test/unit/test_bug618835.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.

     1 //
     2 // If a response to a non-safe HTTP request-method contains the Location- or
     3 // Content-Location header, we must make sure to invalidate any cached entry
     4 // representing the URIs pointed to by either header. RFC 2616 section 13.10
     5 //
     6 // This test uses 3 URIs: "/post" is the target of a POST-request and always
     7 // redirects (301) to "/redirect". The URIs "/redirect" and "/cl" both counts
     8 // the number of loads from the server (handler). The response from "/post"
     9 // always contains the headers "Location: /redirect" and "Content-Location:
    10 // /cl", whose cached entries are to be invalidated. The tests verifies that
    11 // "/redirect" and "/cl" are loaded from server the expected number of times.
    12 //
    14 Cu.import("resource://testing-common/httpd.js");
    16 var httpserv;
    18 function setupChannel(path) {
    19     var ios =
    20         Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    21     return chan = ios.newChannel(path, "", null)
    22                                 .QueryInterface(Ci.nsIHttpChannel);
    23 }
    25 // Verify that Content-Location-URI has been loaded once, load post_target
    26 function InitialListener() { }
    27 InitialListener.prototype = {
    28     onStartRequest: function(request, context) { },
    29     onStopRequest: function(request, context, status) {
    30         do_check_eq(1, numberOfCLHandlerCalls);
    31         do_execute_soon(function() {
    32             var channel = setupChannel("http://localhost:" +
    33                                        httpserv.identity.primaryPort + "/post");
    34             channel.requestMethod = "POST";
    35             channel.asyncOpen(new RedirectingListener(), null);
    36         });
    37     }
    38 };
    40 // Verify that Location-URI has been loaded once, reload post_target
    41 function RedirectingListener() { }
    42 RedirectingListener.prototype = {
    43     onStartRequest: function(request, context) { },
    44     onStopRequest: function(request, context, status) {
    45         do_check_eq(1, numberOfHandlerCalls);
    46         do_execute_soon(function() {
    47             var channel = setupChannel("http://localhost:" +
    48                                        httpserv.identity.primaryPort + "/post");
    49             channel.requestMethod = "POST";
    50             channel.asyncOpen(new VerifyingListener(), null);
    51         });
    52     }
    53 };
    55 // Verify that Location-URI has been loaded twice (cached entry invalidated),
    56 // reload Content-Location-URI
    57 function VerifyingListener() { }
    58 VerifyingListener.prototype = {
    59     onStartRequest: function(request, context) { },
    60     onStopRequest: function(request, context, status) {
    61         do_check_eq(2, numberOfHandlerCalls);
    62         var channel = setupChannel("http://localhost:" +
    63                                    httpserv.identity.primaryPort + "/cl");
    64         channel.asyncOpen(new FinalListener(), null);
    65     }
    66 };
    68 // Verify that Location-URI has been loaded twice (cached entry invalidated),
    69 // stop test
    70 function FinalListener() { }
    71 FinalListener.prototype = {
    72     onStartRequest: function(request, context) { },
    73     onStopRequest: function(request, context, status) {
    74         do_check_eq(2, numberOfCLHandlerCalls);
    75         httpserv.stop(do_test_finished);
    76     }
    77 };
    79 function run_test() {
    80   httpserv = new HttpServer();
    81   httpserv.registerPathHandler("/cl", content_location);
    82   httpserv.registerPathHandler("/post", post_target);
    83   httpserv.registerPathHandler("/redirect", redirect_target);
    84   httpserv.start(-1);
    86   // Clear cache
    87   evict_cache_entries();
    89   // Load Content-Location URI into cache and start the chain of loads
    90   var channel = setupChannel("http://localhost:" +
    91                              httpserv.identity.primaryPort + "/cl");
    92   channel.asyncOpen(new InitialListener(), null);
    94   do_test_pending();
    95 }
    97 var numberOfCLHandlerCalls = 0;
    98 function content_location(metadata, response) {
    99     numberOfCLHandlerCalls++;
   100     response.setStatusLine(metadata.httpVersion, 200, "Ok");
   101     response.setHeader("Cache-Control", "max-age=360000", false);
   102 }
   104 function post_target(metadata, response) {
   105     response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
   106     response.setHeader("Location", "/redirect", false);
   107     response.setHeader("Content-Location", "/cl", false);
   108     response.setHeader("Cache-Control", "max-age=360000", false);
   109 }
   111 var numberOfHandlerCalls = 0;
   112 function redirect_target(metadata, response) {
   113     numberOfHandlerCalls++;
   114     response.setStatusLine(metadata.httpVersion, 200, "Ok");
   115     response.setHeader("Cache-Control", "max-age=360000", false);
   116 }

mercurial