summaryrefslogtreecommitdiffstats
path: root/netwerk/cookie/test/unit/test_eviction.js
blob: f694cc80aa624491d0382f0959cf60766e84335b (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
186
187
188
189
190
191
192
193
194
195
196
197
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");

const BASE_HOST = "example.org";

const { CookieXPCShellUtils } = ChromeUtils.importESModule(
  "resource://testing-common/CookieXPCShellUtils.sys.mjs"
);

CookieXPCShellUtils.init(this);
CookieXPCShellUtils.createServer({ hosts: ["example.org"] });

add_task(async function test_basic_eviction() {
  do_get_profile();

  Services.prefs.setIntPref("network.cookie.staleThreshold", 0);
  Services.prefs.setIntPref("network.cookie.quotaPerHost", 2);
  Services.prefs.setIntPref("network.cookie.maxPerHost", 5);

  // We don't want to have CookieJarSettings blocking this test.
  Services.prefs.setBoolPref(
    "network.cookieJarSettings.unblocked_for_testing",
    true
  );

  const BASE_URI = Services.io.newURI("http://" + BASE_HOST);
  const FOO_PATH = Services.io.newURI("http://" + BASE_HOST + "/foo/");
  const BAR_PATH = Services.io.newURI("http://" + BASE_HOST + "/bar/");

  await setCookie("session_foo_path_1", null, "/foo", null, FOO_PATH);
  await setCookie("session_foo_path_2", null, "/foo", null, FOO_PATH);
  await setCookie("session_foo_path_3", null, "/foo", null, FOO_PATH);
  await setCookie("session_foo_path_4", null, "/foo", null, FOO_PATH);
  await setCookie("session_foo_path_5", null, "/foo", null, FOO_PATH);
  verifyCookies(
    [
      "session_foo_path_1",
      "session_foo_path_2",
      "session_foo_path_3",
      "session_foo_path_4",
      "session_foo_path_5",
    ],
    BASE_URI
  );

  // Check if cookies are evicted by creation time.
  await setCookie("session_foo_path_6", null, "/foo", null, FOO_PATH);
  verifyCookies(
    ["session_foo_path_4", "session_foo_path_5", "session_foo_path_6"],
    BASE_URI
  );

  await setCookie("session_bar_path_1", null, "/bar", null, BAR_PATH);
  await setCookie("session_bar_path_2", null, "/bar", null, BAR_PATH);

  verifyCookies(
    [
      "session_foo_path_4",
      "session_foo_path_5",
      "session_foo_path_6",
      "session_bar_path_1",
      "session_bar_path_2",
    ],
    BASE_URI
  );

  // Check if cookies are evicted by last accessed time.
  await CookieXPCShellUtils.getCookieStringFromDocument(FOO_PATH.spec);

  await setCookie("session_foo_path_7", null, "/foo", null, FOO_PATH);
  verifyCookies(
    ["session_foo_path_5", "session_foo_path_6", "session_foo_path_7"],
    BASE_URI
  );

  const EXPIRED_TIME = 3;

  await setCookie(
    "non_session_expired_foo_path_1",
    null,
    "/foo",
    EXPIRED_TIME,
    FOO_PATH
  );
  await setCookie(
    "non_session_expired_foo_path_2",
    null,
    "/foo",
    EXPIRED_TIME,
    FOO_PATH
  );
  verifyCookies(
    [
      "session_foo_path_5",
      "session_foo_path_6",
      "session_foo_path_7",
      "non_session_expired_foo_path_1",
      "non_session_expired_foo_path_2",
    ],
    BASE_URI
  );

  // Check if expired cookies are evicted first.
  await new Promise(resolve => do_timeout(EXPIRED_TIME * 1000, resolve));
  await setCookie("session_foo_path_8", null, "/foo", null, FOO_PATH);
  verifyCookies(
    ["session_foo_path_6", "session_foo_path_7", "session_foo_path_8"],
    BASE_URI
  );

  Services.cookies.removeAll();
});

// Verify that the given cookie names exist, and are ordered from least to most recently accessed
function verifyCookies(names, uri) {
  Assert.equal(Services.cookies.countCookiesFromHost(uri.host), names.length);
  let actual_cookies = [];
  for (let cookie of Services.cookies.getCookiesFromHost(uri.host, {})) {
    actual_cookies.push(cookie);
  }
  if (names.length != actual_cookies.length) {
    let left = names.filter(function (n) {
      return (
        actual_cookies.findIndex(function (c) {
          return c.name == n;
        }) == -1
      );
    });
    let right = actual_cookies
      .filter(function (c) {
        return (
          names.findIndex(function (n) {
            return c.name == n;
          }) == -1
        );
      })
      .map(function (c) {
        return c.name;
      });
    if (left.length) {
      info("unexpected cookies: " + left);
    }
    if (right.length) {
      info("expected cookies: " + right);
    }
  }
  Assert.equal(names.length, actual_cookies.length);
  actual_cookies.sort(function (a, b) {
    if (a.lastAccessed < b.lastAccessed) {
      return -1;
    }
    if (a.lastAccessed > b.lastAccessed) {
      return 1;
    }
    return 0;
  });
  for (var i = 0; i < names.length; i++) {
    Assert.equal(names[i], actual_cookies[i].name);
    Assert.equal(names[i].startsWith("session"), actual_cookies[i].isSession);
  }
}

var lastValue = 0;
function setCookie(name, domain, path, maxAge, url) {
  let value = name + "=" + ++lastValue;
  var s = "setting cookie " + value;
  if (domain) {
    value += "; Domain=" + domain;
    s += " (d=" + domain + ")";
  }
  if (path) {
    value += "; Path=" + path;
    s += " (p=" + path + ")";
  }
  if (maxAge) {
    value += "; Max-Age=" + maxAge;
    s += " (non-session)";
  } else {
    s += " (session)";
  }
  s += " for " + url.spec;
  info(s);

  let channel = NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
    contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
  });

  Services.cookies.setCookieStringFromHttp(url, value, channel);

  return new Promise(function (resolve) {
    // Windows XP has low precision timestamps that cause our cookie eviction
    // algorithm to produce different results from other platforms. We work around
    // this by ensuring that there's a clear gap between each cookie update.
    do_timeout(10, resolve);
  });
}