summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/bouncetrackingprotection/test/xpcshell/test_bouncetracking_importUserActivationPermissions.js
blob: 5150d074c2352f099be7225a69dc7724718f2b84 (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
/* Any copyright is dedicated to the Public Domain.
https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

const DOMAIN_A = "example.com";
const SUB_DOMAIN_A = "sub." + DOMAIN_A;
const DOMAIN_B = "example.org";
const DOMAIN_C = "example.net";

const ORIGIN_A = "https://" + DOMAIN_A;
const ORIGIN_SUB_A = "https://" + SUB_DOMAIN_A;
const ORIGIN_B = "https://" + DOMAIN_B;
const ORIGIN_C = "https://" + DOMAIN_C;
const ORIGIN_NON_HTTP = "file:///foo/bar.html";

const OA_PBM = { privateBrowsingId: 1 };
const PRINCIPAL_C_PBM = Services.scriptSecurityManager.createContentPrincipal(
  Services.io.newURI(ORIGIN_C),
  OA_PBM
);

let btp;
let userActivationLifetimeSec = Services.prefs.getIntPref(
  "privacy.bounceTrackingProtection.bounceTrackingActivationLifetimeSec"
);

function cleanup() {
  btp.clearAll();
  Services.perms.removeAll();
  Services.prefs.setBoolPref(
    "privacy.bounceTrackingProtection.hasMigratedUserActivationData",
    false
  );
}

add_setup(function () {
  // Need a profile to data clearing calls.
  do_get_profile();

  btp = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
    Ci.nsIBounceTrackingProtection
  );

  // Clean initial state.
  cleanup();
});

add_task(async function test_user_activation_perm_migration() {
  // Assert initial test state.
  Assert.deepEqual(
    btp.testGetUserActivationHosts({}),
    [],
    "No user activation hosts initially."
  );
  Assert.equal(
    Services.perms.getAllByTypes(["storageAccessAPI"]).length,
    0,
    "No user activation permissions initially."
  );

  info("Add test user activation permissions.");

  let now = Date.now();

  // Non-expired permissions.
  PermissionTestUtils.addWithModificationTime(
    ORIGIN_A,
    "storageAccessAPI",
    Services.perms.ALLOW_ACTION,
    now
  );
  PermissionTestUtils.addWithModificationTime(
    ORIGIN_C,
    "storageAccessAPI",
    Services.perms.ALLOW_ACTION,
    now - 1000
  );

  // A non expired permission for a subdomain of DOMAIN_A that has an older modification time.
  PermissionTestUtils.addWithModificationTime(
    ORIGIN_SUB_A,
    "storageAccessAPI",
    Services.perms.ALLOW_ACTION,
    now - 500
  );

  // An expired permission.
  PermissionTestUtils.addWithModificationTime(
    ORIGIN_B,
    "storageAccessAPI",
    Services.perms.ALLOW_ACTION,
    now - userActivationLifetimeSec * 1.2 * 1000
  );

  // A non-HTTP permission.
  PermissionTestUtils.addWithModificationTime(
    ORIGIN_NON_HTTP,
    "storageAccessAPI",
    Services.perms.ALLOW_ACTION,
    now
  );

  // A permission for PBM. Ideally we'd test a more persistent permission type
  // here with custom oa, but permission seperation by userContextId isn't
  // enabled yet (Bug 1641584).
  PermissionTestUtils.addWithModificationTime(
    PRINCIPAL_C_PBM,
    "storageAccessAPI",
    Services.perms.ALLOW_ACTION,
    now
  );

  info("Trigger migration.");
  btp.testMaybeMigrateUserInteractionPermissions();

  Assert.deepEqual(
    btp
      .testGetUserActivationHosts({})
      .map(entry => entry.siteHost)
      .sort(),
    [DOMAIN_A, DOMAIN_C].sort(),
    "Should have imported the correct user activation flags."
  );
  Assert.deepEqual(
    btp.testGetUserActivationHosts(OA_PBM).map(entry => entry.siteHost),
    [DOMAIN_C],
    "Should have imported the correct user activation flags for PBM."
  );

  info("Reset the BTP user activation store");
  btp.clearAll();

  info("Trigger migration again.");
  btp.testMaybeMigrateUserInteractionPermissions();

  Assert.deepEqual(
    btp.testGetUserActivationHosts({}),
    [],
    "Should not have imported the user activation flags again."
  );
  Assert.deepEqual(
    btp.testGetUserActivationHosts(OA_PBM),
    [],
    "Should not have imported the user activation flags again for PBM."
  );

  cleanup();
});