summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/xpcshell/test_cookie_behavior.js
blob: 3ce1f8bfb7dd38aec6071d506531167c8c171b89 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Note: This test may cause intermittents if run at exactly midnight.

"use strict";

const PREF_FPI = "privacy.firstparty.isolate";
const PREF_COOKIE_BEHAVIOR = "network.cookie.cookieBehavior";
const PREF_COOKIE_BEHAVIOR_PBMODE = "network.cookie.cookieBehavior.pbmode";

registerCleanupFunction(() => {
  Services.prefs.clearUserPref(PREF_FPI);
  Services.prefs.clearUserPref(PREF_COOKIE_BEHAVIOR);
  Services.prefs.clearUserPref(PREF_COOKIE_BEHAVIOR_PBMODE);
});

add_task(function test_FPI_off() {
  Services.prefs.setBoolPref(PREF_FPI, false);

  for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
    Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR, i);
    equal(Services.prefs.getIntPref(PREF_COOKIE_BEHAVIOR), i);
    equal(Services.cookies.getCookieBehavior(false), i);
  }

  Services.prefs.clearUserPref(PREF_COOKIE_BEHAVIOR);

  for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
    Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR_PBMODE, i);
    equal(Services.prefs.getIntPref(PREF_COOKIE_BEHAVIOR_PBMODE), i);
    equal(Services.cookies.getCookieBehavior(true), i);
  }
});

add_task(function test_FPI_on() {
  Services.prefs.setBoolPref(PREF_FPI, true);

  for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
    Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR, i);
    equal(Services.prefs.getIntPref(PREF_COOKIE_BEHAVIOR), i);
    equal(
      Services.cookies.getCookieBehavior(false),
      i == Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN
        ? Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
        : i
    );
  }

  Services.prefs.clearUserPref(PREF_COOKIE_BEHAVIOR);

  for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
    Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR_PBMODE, i);
    equal(Services.prefs.getIntPref(PREF_COOKIE_BEHAVIOR_PBMODE), i);
    equal(
      Services.cookies.getCookieBehavior(true),
      i == Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN
        ? Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
        : i
    );
  }

  Services.prefs.clearUserPref(PREF_FPI);
});

add_task(function test_private_cookieBehavior_mirroring() {
  // Test that the private cookieBehavior getter will return the regular pref if
  // the regular pref has a user value and the private pref has a default value.
  Services.prefs.clearUserPref(PREF_COOKIE_BEHAVIOR_PBMODE);
  for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
    Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR, i);
    if (!Services.prefs.prefHasUserValue(PREF_COOKIE_BEHAVIOR)) {
      continue;
    }

    equal(Services.cookies.getCookieBehavior(true), i);
  }

  // Test that the private cookieBehavior getter will always return the private
  // pref if the private cookieBehavior has a user value.
  for (let i = 0; i <= Ci.nsICookieService.BEHAVIOR_LAST; ++i) {
    Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR_PBMODE, i);
    if (!Services.prefs.prefHasUserValue(PREF_COOKIE_BEHAVIOR_PBMODE)) {
      continue;
    }

    for (let j = 0; j <= Ci.nsICookieService.BEHAVIOR_LAST; ++j) {
      Services.prefs.setIntPref(PREF_COOKIE_BEHAVIOR, j);

      equal(Services.cookies.getCookieBehavior(true), i);
    }
  }
});