summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug650995.js
blob: 4eff87c72195ca9f364098277819633e04ab823a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Test that "max_entry_size" prefs for disk- and memory-cache prevents
// caching resources with size out of bounds
//

"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

do_get_profile();

const prefService = Services.prefs;

const httpserver = new HttpServer();

// Repeats the given data until the total size is larger than 1K
function repeatToLargerThan1K(data) {
  while (data.length <= 1024) {
    data += data;
  }
  return data;
}

function setupChannel(suffix, value) {
  var chan = NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + suffix,
    loadUsingSystemPrincipal: true,
  });
  var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
  httpChan.setRequestHeader("x-request", value, false);

  return httpChan;
}

var tests = [
  new InitializeCacheDevices(true, false), // enable and create mem-device
  new TestCacheEntrySize(
    function () {
      prefService.setIntPref("browser.cache.memory.max_entry_size", 1);
    },
    "012345",
    "9876543210",
    "012345"
  ), // expect cached value
  new TestCacheEntrySize(
    function () {
      prefService.setIntPref("browser.cache.memory.max_entry_size", 1);
    },
    "0123456789a",
    "9876543210",
    "9876543210"
  ), // expect fresh value
  new TestCacheEntrySize(
    function () {
      prefService.setIntPref("browser.cache.memory.max_entry_size", -1);
    },
    "0123456789a",
    "9876543210",
    "0123456789a"
  ), // expect cached value

  new InitializeCacheDevices(false, true), // enable and create disk-device
  new TestCacheEntrySize(
    function () {
      prefService.setIntPref("browser.cache.disk.max_entry_size", 1);
    },
    "012345",
    "9876543210",
    "012345"
  ), // expect cached value
  new TestCacheEntrySize(
    function () {
      prefService.setIntPref("browser.cache.disk.max_entry_size", 1);
    },
    "0123456789a",
    "9876543210",
    "9876543210"
  ), // expect fresh value
  new TestCacheEntrySize(
    function () {
      prefService.setIntPref("browser.cache.disk.max_entry_size", -1);
    },
    "0123456789a",
    "9876543210",
    "0123456789a"
  ), // expect cached value
];

function nextTest() {
  // We really want each test to be self-contained. Make sure cache is
  // cleared and also let all operations finish before starting a new test
  syncWithCacheIOThread(function () {
    Services.cache2.clear();
    syncWithCacheIOThread(runNextTest);
  });
}

function runNextTest() {
  var aTest = tests.shift();
  if (!aTest) {
    httpserver.stop(do_test_finished);
    return;
  }
  executeSoon(function () {
    aTest.start();
  });
}

// Just make sure devices are created
function InitializeCacheDevices(memDevice, diskDevice) {
  this.start = function () {
    prefService.setBoolPref("browser.cache.memory.enable", memDevice);
    if (memDevice) {
      let cap = prefService.getIntPref("browser.cache.memory.capacity", 0);
      if (cap == 0) {
        prefService.setIntPref("browser.cache.memory.capacity", 1024);
      }
    }
    prefService.setBoolPref("browser.cache.disk.enable", diskDevice);
    if (diskDevice) {
      let cap = prefService.getIntPref("browser.cache.disk.capacity", 0);
      if (cap == 0) {
        prefService.setIntPref("browser.cache.disk.capacity", 1024);
      }
    }
    var channel = setupChannel("/bug650995", "Initial value");
    channel.asyncOpen(new ChannelListener(nextTest, null));
  };
}

function TestCacheEntrySize(
  setSizeFunc,
  firstRequest,
  secondRequest,
  secondExpectedReply
) {
  // Initially, this test used 10 bytes as the limit for caching entries.
  // Since we now use 1K granularity we have to extend lengths to be larger
  // than 1K if it is larger than 10
  if (firstRequest.length > 10) {
    firstRequest = repeatToLargerThan1K(firstRequest);
  }
  if (secondExpectedReply.length > 10) {
    secondExpectedReply = repeatToLargerThan1K(secondExpectedReply);
  }

  this.start = function () {
    setSizeFunc();
    var channel = setupChannel("/bug650995", firstRequest);
    channel.asyncOpen(new ChannelListener(this.initialLoad, this));
  };
  this.initialLoad = function (request, data, ctx) {
    Assert.equal(firstRequest, data);
    var channel = setupChannel("/bug650995", secondRequest);
    executeSoon(function () {
      channel.asyncOpen(new ChannelListener(ctx.testAndTriggerNext, ctx));
    });
  };
  this.testAndTriggerNext = function (request, data, ctx) {
    Assert.equal(secondExpectedReply, data);
    executeSoon(nextTest);
  };
}

function run_test() {
  httpserver.registerPathHandler("/bug650995", handler);
  httpserver.start(-1);

  prefService.setBoolPref("network.http.rcwn.enabled", false);

  nextTest();
  do_test_pending();
}

function handler(metadata, response) {
  var body = "BOOM!";
  try {
    body = metadata.getHeader("x-request");
  } catch (e) {}

  response.setStatusLine(metadata.httpVersion, 200, "Ok");
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Cache-Control", "max-age=3600", false);
  response.bodyOutputStream.write(body, body.length);
}