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

"use strict";

/**
 * Test that expired user activations are cleared by the the helper method
 * testClearExpiredUserActivations.
 */
add_task(async function test() {
  // Need a profile to data clearing calls.
  do_get_profile();

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

  // Reset global bounce tracking state.
  btp.clearAll();

  // Assert initial test state.
  Assert.deepEqual(
    btp.testGetBounceTrackerCandidateHosts({}),
    [],
    "No tracker candidates initially."
  );
  Assert.deepEqual(
    btp.testGetUserActivationHosts({}),
    [],
    "No user activation hosts initially."
  );

  // Get the bounce tracking activation lifetime. The pref is in seconds, we
  // need to convert it to microseconds, as the user activation timestamps are
  // in microseconds (PRTime).
  let bounceTrackingActivationLifetimeUSec =
    1000 *
    1000 *
    Services.prefs.getIntPref(
      "privacy.bounceTrackingProtection.bounceTrackingActivationLifetimeSec"
    );

  // Add some test data for user activation.
  btp.testAddUserActivation({}, "not-expired1.com", Date.now() * 1000);
  btp.testAddUserActivation(
    {},
    "not-expired2.com",
    Date.now() * 1000 - bounceTrackingActivationLifetimeUSec / 2
  );
  btp.testAddUserActivation(
    { privateBrowsingId: 1 },
    "pbm-not-expired.com",
    Date.now() * 1000
  );
  btp.testAddUserActivation(
    {},
    "expired1.com",
    Date.now() * 1000 - bounceTrackingActivationLifetimeUSec * 2
  );
  btp.testAddUserActivation(
    {},
    "expired2.com",
    Date.now() * 1000 - (bounceTrackingActivationLifetimeUSec + 1000 * 1000)
  );
  btp.testAddUserActivation({ privateBrowsingId: 1 }, "pbm-expired.com", 1);

  // Clear expired user activations.
  btp.testClearExpiredUserActivations();

  // Assert that expired user activations have been cleared.
  Assert.deepEqual(
    btp
      .testGetUserActivationHosts({})
      .map(entry => entry.siteHost)
      .sort(),
    ["not-expired1.com", "not-expired2.com"],
    "Expired user activation flags have been cleared for normal browsing."
  );

  Assert.deepEqual(
    btp
      .testGetUserActivationHosts({ privateBrowsingId: 1 })
      .map(entry => entry.siteHost)
      .sort(),
    ["pbm-not-expired.com"],
    "Expired user activation flags have been cleared for private browsing."
  );

  // Reset global bounce tracking state.
  btp.clearAll();
});