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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# 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
from contextlib import contextmanager
from marionette_driver import Wait
from marionette_harness import MarionetteTestCase
class QuotaTestCase(MarionetteTestCase):
def executeAsyncScript(self, script, script_args):
res = self.marionette.execute_async_script(
"""
const resolve = arguments[arguments.length - 1];
class RequestError extends Error {
constructor(resultCode, resultName) {
super(`Request failed (code: ${resultCode}, name: ${resultName})`);
this.name = "RequestError";
this.resultCode = resultCode;
this.resultName = resultName;
}
}
async function requestFinished(request) {
await new Promise(function (resolve) {
request.callback = function () {
resolve();
};
});
if (request.resultCode !== Cr.NS_OK) {
throw new RequestError(request.resultCode, request.resultName);
}
return request.result;
}
"""
+ script
+ """
main()
.then(function(result) {
resolve(result);
})
.catch(function() {
resolve(null);
});;
""",
script_args=script_args,
new_sandbox=False,
)
assert res is not None
return res
def ensureInvariantHolds(self, op):
maxWaitTime = 60
Wait(self.marionette, timeout=maxWaitTime).until(
op,
message=f"operation did not yield success even after waiting {maxWaitTime}s time",
)
def findDirObj(self, path, pattern, isFile):
for obj in os.scandir(path):
if obj.path.endswith(pattern) and (obj.is_file() == isFile):
return obj.path
return None
def getFullOriginMetadata(self, persistenceType, origin):
with self.marionette.using_context(self.marionette.CONTEXT_CHROME):
res = self.executeAsyncScript(
"""
const [persistenceType, origin] = arguments;
async function main() {
const principal = Services.scriptSecurityManager.
createContentPrincipalFromOrigin(origin);
const request = Services.qms.getFullOriginMetadata(
persistenceType, principal);
const metadata = await requestFinished(request);
return metadata;
}
""",
script_args=(persistenceType, origin),
)
assert res is not None
return res
def getStoragePath(self, profilePath, origin, persistenceType, client):
fullOriginMetadata = self.getFullOriginMetadata(persistenceType, origin)
storageOrigin = fullOriginMetadata["storageOrigin"]
sanitizedStorageOrigin = storageOrigin.replace(":", "+").replace("/", "+")
return os.path.join(
self.getRepositoryPath(persistenceType), sanitizedStorageOrigin, client
)
def getRepositoryPath(self, persistenceType):
profilePath = self.marionette.instance.profile.profile
assert profilePath is not None
return os.path.join(profilePath, "storage", persistenceType)
def initStorage(self):
with self.marionette.using_context(self.marionette.CONTEXT_CHROME):
return self.executeAsyncScript(
"""
async function main() {
let req = Services.qms.init();
await requestFinished(req);
return true;
}
""",
script_args=(),
)
def initTemporaryOrigin(self, persistenceType, origin):
with self.marionette.using_context(self.marionette.CONTEXT_CHROME):
return self.executeAsyncScript(
"""
const [persistenceType, origin] = arguments;
async function main() {
const principal = Services.scriptSecurityManager.
createContentPrincipalFromOrigin(origin);
let req = Services.qms.initializeTemporaryOrigin(persistenceType, principal);
await requestFinished(req)
return true;
}
""",
script_args=(
persistenceType,
origin,
),
)
def initTemporaryStorage(self):
with self.marionette.using_context(self.marionette.CONTEXT_CHROME):
return self.executeAsyncScript(
"""
async function main() {
const req = Services.qms.initTemporaryStorage();
await requestFinished(req);
return true;
}
""",
script_args=(),
)
def resetStoragesForPrincipal(self, origin, persistenceType, client):
# This method is used to force sqlite to write journal file contents to
# main sqlite database file
with self.marionette.using_context(self.marionette.CONTEXT_CHROME):
res = self.executeAsyncScript(
"""
const [origin, persistenceType, client] = arguments;
async function main() {
const principal = Services.scriptSecurityManager.
createContentPrincipalFromOrigin(origin);
const request = Services.qms.resetStoragesForPrincipal(principal, persistenceType, client);
await requestFinished(request);
return true;
}
""",
script_args=(origin, persistenceType, client),
)
assert res is not None
return res
@contextmanager
def using_new_window(self, path, private=False, skipCleanup=False):
"""
This helper method is created to ensure that a temporary
window required inside the test scope is lifetime'd properly
"""
oldWindow = self.marionette.current_window_handle
try:
newWindow = self.marionette.open(type="window", private=private)
self.marionette.switch_to_window(newWindow["handle"])
self.marionette.navigate(self.marionette.absolute_url(path))
origin = self.marionette.absolute_url("")[:-1]
if private:
origin += "^privateBrowsingId=1"
yield (origin, "private" if private else "default")
finally:
if not skipCleanup:
self.marionette.close()
self.marionette.switch_to_window(oldWindow)
|