summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/marionette/test_private_repository_cleanup.py
blob: 0d4c488757a8aebc8cb3f5ca7cd132a9b0fd9d0f (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/.

import os
import sys
from pathlib import Path

sys.path.append(os.fspath(Path(__file__).parents[0]))
from quota_test_case import QuotaTestCase

QM_TESTING_PREF = "dom.quotaManager.testing"
AUTOSTART_PBM_PREF = "browser.privatebrowsing.autostart"

"""
    This test ensures that private repository gets properly purged in all below scenarios:
        1. at PBM session end
        2. on firefox start when last session was abnormally terminated (crash)
        3. on firefox shutdown (when PBM autostart is enabled i.e. browser.privatebrowsing.autostart set to true)
"""


class PrivateRepositoryCleanup(QuotaTestCase):
    def setUp(self, autostartPBM=False):
        super(PrivateRepositoryCleanup, self).setUp()

        self.marionette.set_pref(AUTOSTART_PBM_PREF, autostartPBM)
        self.marionette.set_pref(QM_TESTING_PREF, True)

        self.marionette.restart(in_app=True)

        assert self.initStorage()
        assert self.initTemporaryStorage()

    def tearDown(self):
        self.marionette.clear_pref(AUTOSTART_PBM_PREF)
        self.marionette.clear_pref(QM_TESTING_PREF)

        self.marionette.restart(in_app=True)
        super(PrivateRepositoryCleanup, self).tearDown()

    def doStorageWork(self):
        origin = self.marionette.absolute_url("")[:-1] + "^privateBrowsingId=1"
        assert self.initTemporaryOrigin("private", origin)

        self.ensureInvariantHolds(lambda _: os.path.exists(self.getPrivateRepository()))

    def verifyCleanup(self):
        self.ensureInvariantHolds(
            lambda _: not os.path.exists(self.getPrivateRepository())
        )

    def getPrivateRepository(self):
        return self.getRepositoryPath("private")


class PBM(PrivateRepositoryCleanup):
    def test_ensure_cleanup(self):
        with self.using_new_window("", private=True):
            self.doStorageWork()

        # private window must have been close by now
        self.verifyCleanup()

    def test_ensure_cleanup_after_crash(self):
        with self.using_new_window("", private=True, skipCleanup=True):
            self.doStorageWork()

        # verify cleanup was performed after crashing and restarting
        # firefox with in_app=False
        self.marionette.restart(in_app=False)
        self.verifyCleanup()


class PBMAutoStart(PrivateRepositoryCleanup):
    def setUp(self):
        super(PBMAutoStart, self).setUp(True)

    def test_ensure_cleanup(self):
        self.doStorageWork()

        # verify cleanup was performed at the shutdown
        self.marionette.quit(in_app=True)
        self.verifyCleanup()

        self.marionette.start_session()

    def test_ensure_cleanup_after_crash(self):
        self.doStorageWork()

        # verify cleanup was performed after crashing and restarting
        # firefox with in_app=False
        self.marionette.restart(in_app=False)
        self.verifyCleanup()