summaryrefslogtreecommitdiffstats
path: root/extensions/cookie/test/unit/test_cookies_persistence.js
blob: d6de81b728a0b8298b7356d339d19b9797ae12d5 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// test for cookie persistence across sessions, for the cases:
// 1) network.cookie.lifetimePolicy = 0 (expire naturally)
// 2) network.cookie.lifetimePolicy = 2 (expire at end of session)

var test_generator = do_run_test();

function run_test() {
  do_test_pending();
  test_generator.next();
}

function finish_test() {
  do_execute_soon(function() {
    test_generator.close();
    do_test_finished();
  });
}

function do_run_test() {
  // Set up a profile.
  let profile = do_get_profile();

  // Create URIs and channels pointing to foo.com and bar.com.
  // We will use these to put foo.com into first and third party contexts.
  var spec1 = "http://foo.com/foo.html";
  var spec2 = "http://bar.com/bar.html";
  var uri1 = NetUtil.newURI(spec1);
  var uri2 = NetUtil.newURI(spec2);
  var channel1 = NetUtil.newChannel({uri: uri1, loadUsingSystemPrincipal: true});
  var channel2 = NetUtil.newChannel({uri: uri2, loadUsingSystemPrincipal: true});

  // Force the channel URI to be used when determining the originating URI of
  // the channel.
  var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
  var httpchannel2 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
  httpchannel1.forceAllowThirdPartyCookie = true;
  httpchannel2.forceAllowThirdPartyCookie = true;

  // test with cookies enabled, and third party cookies persistent.
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
  Services.prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", false);
  do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
  do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);

  // fake a profile change
  do_close_profile(test_generator);
  yield;
  do_load_profile();
  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);

  // Again, but don't wait for the async close to complete. This should always
  // work, since we blocked on close above and haven't kicked off any writes
  // since then.
  do_close_profile();
  do_load_profile();
  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);

  // test with cookies set to session-only
  Services.prefs.setIntPref("network.cookie.lifetimePolicy", 2);
  Services.cookies.removeAll();
  do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
  do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);

  // fake a profile change
  do_close_profile(test_generator);
  yield;
  do_load_profile();
  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);

  finish_test();
}