summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/bouncetrackingprotection/test/marionette/test_bouncetracking_storage_persistence.py
blob: afc32398398ddc013f8971cbdec5849d5409ff85 (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
# 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/.

from marionette_harness import MarionetteTestCase

# Tests the persistence of the bounce tracking protection storage across
# restarts.


class BounceTrackingStoragePersistenceTestCase(MarionetteTestCase):
    def setUp(self):
        super(BounceTrackingStoragePersistenceTestCase, self).setUp()
        self.marionette.enforce_gecko_prefs(
            {
                "privacy.bounceTrackingProtection.enabled": True,
                "privacy.bounceTrackingProtection.enableTestMode": True,
            }
        )

        self.marionette.set_context("chrome")
        self.populate_state()

    def populate_state(self):
        # Add some data to test persistence.
        self.marionette.execute_script(
            """
            let bounceTrackingProtection = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
            Ci.nsIBounceTrackingProtection
            );

            bounceTrackingProtection.testAddBounceTrackerCandidate({}, "bouncetracker.net", Date.now() * 10000);
            bounceTrackingProtection.testAddBounceTrackerCandidate({}, "bouncetracker.org", Date.now() * 10000);
            bounceTrackingProtection.testAddBounceTrackerCandidate({ userContextId: 3 }, "tracker.com", Date.now() * 10000);
            // A private browsing entry which must not be persisted across restarts.
            bounceTrackingProtection.testAddBounceTrackerCandidate({ privateBrowsingId: 1 }, "tracker.net", Date.now() * 10000);

            bounceTrackingProtection.testAddUserActivation({}, "example.com", (Date.now() + 5000) * 10000);
            // A private browsing entry which must not be persisted across restarts.
            bounceTrackingProtection.testAddUserActivation({ privateBrowsingId: 1 }, "example.org", (Date.now() + 2000) * 10000);
            """
        )

    def test_state_after_restart(self):
        self.marionette.restart(clean=False, in_app=True)
        bounceTrackerCandidates = self.marionette.execute_script(
            """
                let bounceTrackingProtection = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
                Ci.nsIBounceTrackingProtection
                );
                return bounceTrackingProtection.testGetBounceTrackerCandidateHosts({}).sort();
            """,
        )
        self.assertEqual(
            len(bounceTrackerCandidates),
            2,
            msg="There should be two entries for default OA",
        )
        self.assertEqual(bounceTrackerCandidates[0], "bouncetracker.net")
        self.assertEqual(bounceTrackerCandidates[1], "bouncetracker.org")

        bounceTrackerCandidates = self.marionette.execute_script(
            """
                let bounceTrackingProtection = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
                Ci.nsIBounceTrackingProtection
                );
                return bounceTrackingProtection.testGetBounceTrackerCandidateHosts({ userContextId: 3 }).sort();
            """,
        )
        self.assertEqual(
            len(bounceTrackerCandidates),
            1,
            msg="There should be only one entry for user context 3",
        )
        self.assertEqual(bounceTrackerCandidates[0], "tracker.com")

        # Unrelated user context should not have any entries.
        bounceTrackerCandidates = self.marionette.execute_script(
            """
                let bounceTrackingProtection = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
                Ci.nsIBounceTrackingProtection
                );
                return bounceTrackingProtection.testGetBounceTrackerCandidateHosts({ userContextId: 4 }).length;
            """,
        )
        self.assertEqual(
            bounceTrackerCandidates,
            0,
            msg="There should be no entries for user context 4",
        )

        # Private browsing entries should not be persisted across restarts.
        bounceTrackerCandidates = self.marionette.execute_script(
            """
                let bounceTrackingProtection = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
                Ci.nsIBounceTrackingProtection
                );
                return bounceTrackingProtection.testGetBounceTrackerCandidateHosts({ privateBrowsingId: 1 }).length;
            """,
        )
        self.assertEqual(
            bounceTrackerCandidates,
            0,
            msg="There should be no entries for private browsing",
        )

        userActivations = self.marionette.execute_script(
            """
                let bounceTrackingProtection = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
                Ci.nsIBounceTrackingProtection
                );
                return bounceTrackingProtection.testGetUserActivationHosts({}).sort();
            """,
        )
        self.assertEqual(
            len(userActivations),
            1,
            msg="There should be only one entry for user activation",
        )
        self.assertEqual(userActivations[0], "example.com")

        # Private browsing entries should not be persisted across restarts.
        userActivations = self.marionette.execute_script(
            """
                let bounceTrackingProtection = Cc["@mozilla.org/bounce-tracking-protection;1"].getService(
                Ci.nsIBounceTrackingProtection
                );
                return bounceTrackingProtection.testGetUserActivationHosts({ privateBrowsingId: 1 }).length;
            """,
        )
        self.assertEqual(
            userActivations, 0, msg="There should be no entries for private browsing"
        )