netwerk/test/unit/test_chunked_responses.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 /*
michael@0 2 * Test Chunked-Encoded response parsing.
michael@0 3 */
michael@0 4
michael@0 5 ////////////////////////////////////////////////////////////////////////////////
michael@0 6 // Test infrastructure
michael@0 7
michael@0 8 Cu.import("resource://testing-common/httpd.js");
michael@0 9
michael@0 10 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 11 return "http://localhost:" + httpserver.identity.primaryPort;
michael@0 12 });
michael@0 13
michael@0 14 var httpserver = new HttpServer();
michael@0 15 var index = 0;
michael@0 16 var test_flags = new Array();
michael@0 17 var testPathBase = "/chunked_hdrs";
michael@0 18
michael@0 19 function run_test()
michael@0 20 {
michael@0 21 httpserver.start(-1);
michael@0 22
michael@0 23 do_test_pending();
michael@0 24 run_test_number(1);
michael@0 25 }
michael@0 26
michael@0 27 function run_test_number(num)
michael@0 28 {
michael@0 29 testPath = testPathBase + num;
michael@0 30 httpserver.registerPathHandler(testPath, eval("handler" + num));
michael@0 31
michael@0 32 var channel = setupChannel(testPath);
michael@0 33 flags = test_flags[num]; // OK if flags undefined for test
michael@0 34 channel.asyncOpen(new ChannelListener(eval("completeTest" + num),
michael@0 35 channel, flags), null);
michael@0 36 }
michael@0 37
michael@0 38 function setupChannel(url)
michael@0 39 {
michael@0 40 var ios = Components.classes["@mozilla.org/network/io-service;1"].
michael@0 41 getService(Ci.nsIIOService);
michael@0 42 var chan = ios.newChannel(URL + url, "", null);
michael@0 43 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 44 return httpChan;
michael@0 45 }
michael@0 46
michael@0 47 function endTests()
michael@0 48 {
michael@0 49 httpserver.stop(do_test_finished);
michael@0 50 }
michael@0 51
michael@0 52 ////////////////////////////////////////////////////////////////////////////////
michael@0 53 // Test 1: FAIL because of overflowed chunked size. The parser uses long so
michael@0 54 // the test case uses >64bit to fail on all platforms.
michael@0 55 test_flags[1] = CL_EXPECT_LATE_FAILURE|CL_ALLOW_UNKNOWN_CL;
michael@0 56
michael@0 57 function handler1(metadata, response)
michael@0 58 {
michael@0 59 var body = "12345678123456789\r\ndata never reached";
michael@0 60
michael@0 61 response.seizePower();
michael@0 62 response.write("HTTP/1.1 200 OK\r\n");
michael@0 63 response.write("Content-Type: text/plain\r\n");
michael@0 64 response.write("Transfer-Encoding: chunked\r\n");
michael@0 65 response.write("\r\n");
michael@0 66 response.write(body);
michael@0 67 response.finish();
michael@0 68 }
michael@0 69
michael@0 70 function completeTest1(request, data, ctx)
michael@0 71 {
michael@0 72 do_check_eq(request.status, Components.results.NS_ERROR_UNEXPECTED);
michael@0 73
michael@0 74 run_test_number(2);
michael@0 75 }
michael@0 76
michael@0 77 ////////////////////////////////////////////////////////////////////////////////
michael@0 78 // Test 2: FAIL because of non-hex in chunked length
michael@0 79
michael@0 80 test_flags[2] = CL_EXPECT_LATE_FAILURE|CL_ALLOW_UNKNOWN_CL;
michael@0 81
michael@0 82 function handler2(metadata, response)
michael@0 83 {
michael@0 84 var body = "junkintheway 123\r\ndata never reached";
michael@0 85
michael@0 86 response.seizePower();
michael@0 87 response.write("HTTP/1.1 200 OK\r\n");
michael@0 88 response.write("Content-Type: text/plain\r\n");
michael@0 89 response.write("Transfer-Encoding: chunked\r\n");
michael@0 90 response.write("\r\n");
michael@0 91 response.write(body);
michael@0 92 response.finish();
michael@0 93 }
michael@0 94
michael@0 95 function completeTest2(request, data, ctx)
michael@0 96 {
michael@0 97 do_check_eq(request.status, Components.results.NS_ERROR_UNEXPECTED);
michael@0 98 run_test_number(3);
michael@0 99 }
michael@0 100
michael@0 101 ////////////////////////////////////////////////////////////////////////////////
michael@0 102 // Test 3: OK in spite of non-hex digits after size in the length field
michael@0 103
michael@0 104 test_flags[3] = CL_ALLOW_UNKNOWN_CL;
michael@0 105
michael@0 106 function handler3(metadata, response)
michael@0 107 {
michael@0 108 var body = "c junkafter\r\ndata reached";
michael@0 109
michael@0 110 response.seizePower();
michael@0 111 response.write("HTTP/1.1 200 OK\r\n");
michael@0 112 response.write("Content-Type: text/plain\r\n");
michael@0 113 response.write("Transfer-Encoding: chunked\r\n");
michael@0 114 response.write("\r\n");
michael@0 115 response.write(body);
michael@0 116 response.finish();
michael@0 117 }
michael@0 118
michael@0 119 function completeTest3(request, data, ctx)
michael@0 120 {
michael@0 121 do_check_eq(request.status, 0);
michael@0 122 run_test_number(4);
michael@0 123 }
michael@0 124
michael@0 125 ////////////////////////////////////////////////////////////////////////////////
michael@0 126 // Test 4: Verify a fully compliant chunked response.
michael@0 127
michael@0 128 test_flags[4] = CL_ALLOW_UNKNOWN_CL;
michael@0 129
michael@0 130 function handler4(metadata, response)
michael@0 131 {
michael@0 132 var body = "c\r\ndata reached\r\n\0\r\n\r\n";
michael@0 133
michael@0 134 response.seizePower();
michael@0 135 response.write("HTTP/1.1 200 OK\r\n");
michael@0 136 response.write("Content-Type: text/plain\r\n");
michael@0 137 response.write("Transfer-Encoding: chunked\r\n");
michael@0 138 response.write("\r\n");
michael@0 139 response.write(body);
michael@0 140 response.finish();
michael@0 141 }
michael@0 142
michael@0 143 function completeTest4(request, data, ctx)
michael@0 144 {
michael@0 145 do_check_eq(request.status, 0);
michael@0 146 run_test_number(5);
michael@0 147 }
michael@0 148
michael@0 149 ////////////////////////////////////////////////////////////////////////////////
michael@0 150 // Test 5: A chunk size larger than 32 bit but smaller than 64bit also fails
michael@0 151 // This is probabaly subject to get improved at some point.
michael@0 152
michael@0 153 test_flags[5] = CL_EXPECT_LATE_FAILURE|CL_ALLOW_UNKNOWN_CL;
michael@0 154
michael@0 155 function handler5(metadata, response)
michael@0 156 {
michael@0 157 var body = "123456781\r\ndata never reached";
michael@0 158
michael@0 159 response.seizePower();
michael@0 160 response.write("HTTP/1.1 200 OK\r\n");
michael@0 161 response.write("Content-Type: text/plain\r\n");
michael@0 162 response.write("Transfer-Encoding: chunked\r\n");
michael@0 163 response.write("\r\n");
michael@0 164 response.write(body);
michael@0 165 response.finish();
michael@0 166 }
michael@0 167
michael@0 168 function completeTest5(request, data, ctx)
michael@0 169 {
michael@0 170 do_check_eq(request.status, Components.results.NS_ERROR_UNEXPECTED);
michael@0 171 endTests();
michael@0 172 // run_test_number(6);
michael@0 173 }

mercurial