Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_cache2-29d-concurrent_read_half-corrupted-206.js
blobd9886a6fbfbc756fcb1260dfbe34640c8f66a3e9
1 /*
3 Checkes if the concurrent cache read/write works when the write is interrupted because of max-entry-size limits.
4 This is enhancement of 29c test, this test checks that a corrupted 206 response is correctly handled (no crashes or asserion failures)
5 This test is using a resumable response.
6 - with a profile, set max-entry-size to 1 (=1024 bytes)
7 - first channel makes a request for a resumable response
8 - second channel makes a request for the same resource, concurrent read happens
9 - first channel sets predicted data size on the entry with every chunk, it's doomed on 1024
10 - second channel now must engage interrupted concurrent write algorithm and read the rest of the content from the network
11 - the response to the range request is broken (bad Content-Range header)
12 - the first must deliver full content w/o errors
13 - the second channel must correctly fail
16 "use strict";
18 const { HttpServer } = ChromeUtils.importESModule(
19   "resource://testing-common/httpd.sys.mjs"
22 var httpProtocolHandler = Cc[
23   "@mozilla.org/network/protocol;1?name=http"
24 ].getService(Ci.nsIHttpProtocolHandler);
26 ChromeUtils.defineLazyGetter(this, "URL", function () {
27   return "http://localhost:" + httpServer.identity.primaryPort;
28 });
30 var httpServer = null;
32 function make_channel(url, callback, ctx) {
33   return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
36 // need something bigger than 1024 bytes
37 const responseBody =
38   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
39   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
40   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
41   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
42   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
43   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
44   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
45   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
46   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
47   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
48   "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
50 function contentHandler(metadata, response) {
51   response.setHeader("Content-Type", "text/plain");
52   response.setHeader("ETag", "Just testing");
53   response.setHeader("Cache-Control", "max-age=99999");
54   response.setHeader("Accept-Ranges", "bytes");
55   response.setHeader("Content-Length", "" + responseBody.length);
56   if (metadata.hasHeader("If-Range")) {
57     response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
58     // Deliberately broken response header to trigger corrupted content error on the second channel
59     response.setHeader("Content-Range", "0-1/2");
60   }
61   response.bodyOutputStream.write(responseBody, responseBody.length);
64 function run_test() {
65   // Static check
66   Assert.ok(responseBody.length > 1024);
68   do_get_profile();
70   Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1);
71   Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
73   httpServer = new HttpServer();
74   httpServer.registerPathHandler("/content", contentHandler);
75   httpServer.start(-1);
77   httpProtocolHandler.EnsureHSTSDataReady().then(function () {
78     var chan1 = make_channel(URL + "/content");
79     chan1.asyncOpen(new ChannelListener(firstTimeThrough, null));
80     var chan2 = make_channel(URL + "/content");
81     chan2.asyncOpen(
82       new ChannelListener(secondTimeThrough, null, CL_EXPECT_FAILURE)
83     );
84   });
86   do_test_pending();
89 function firstTimeThrough(request, buffer) {
90   Assert.equal(buffer, responseBody);
93 function secondTimeThrough(request, buffer) {
94   httpServer.stop(do_test_finished);