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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const NS_OK = Cr.NS_OK;
const NS_ERROR_FAILURE = Cr.NS_ERROR_FAILURE;
const NS_ERROR_UNEXPECTED = Cr.NS_ERROR_UNEXPECTED;
const NS_ERROR_FILE_NO_DEVICE_SPACE = Cr.NS_ERROR_FILE_NO_DEVICE_SPACE;
const loggingEnabled = false;
var testGenerator;
loadScript("dom/quota/test/common/xpcshell.js");
function log(msg) {
if (loggingEnabled) {
info(msg);
}
}
function is(a, b, msg) {
Assert.equal(a, b, msg);
}
function ok(cond, msg) {
Assert.ok(!!cond, msg);
}
function todo(cond, msg) {
todo_check_true(cond);
}
function run_test() {
runTest();
}
if (!this.runTest) {
this.runTest = function () {
do_get_profile();
enableStorageTesting();
enableTesting();
// In order to support converting tests to using async functions from using
// generator functions, we detect async functions by checking the name of
// function's constructor.
Assert.ok(
typeof testSteps === "function",
"There should be a testSteps function"
);
if (testSteps.constructor.name === "AsyncFunction") {
// Do run our existing cleanup function that would normally be called by
// the generator's call to finishTest().
registerCleanupFunction(function () {
resetStorageTesting();
resetTesting();
});
add_task(testSteps);
// Since we defined run_test, we must invoke run_next_test() to start the
// async test.
run_next_test();
} else {
Assert.ok(
testSteps.constructor.name === "GeneratorFunction",
"Unsupported function type"
);
do_test_pending();
testGenerator = testSteps();
testGenerator.next();
}
};
}
function finishTest() {
resetStorageTesting();
resetTesting();
executeSoon(function () {
do_test_finished();
});
}
function grabArgAndContinueHandler(arg) {
testGenerator.next(arg);
}
function continueToNextStep() {
executeSoon(function () {
testGenerator.next();
});
}
function continueToNextStepSync() {
testGenerator.next();
}
function enableTesting() {
SpecialPowers.setBoolPref(
"dom.storage.enable_unsupported_legacy_implementation",
false
);
}
function resetTesting() {
SpecialPowers.clearUserPref(
"dom.storage.enable_unsupported_legacy_implementation"
);
}
function setGlobalLimit(globalLimit) {
SpecialPowers.setIntPref(
"dom.quotaManager.temporaryStorage.fixedLimit",
globalLimit
);
}
function resetGlobalLimit() {
SpecialPowers.clearUserPref("dom.quotaManager.temporaryStorage.fixedLimit");
}
function storageInitialized(callback) {
let request = SpecialPowers._getQuotaManager().storageInitialized();
request.callback = callback;
return request;
}
function temporaryStorageInitialized(callback) {
let request = SpecialPowers._getQuotaManager().temporaryStorageInitialized();
request.callback = callback;
return request;
}
function init(callback) {
let request = SpecialPowers._getQuotaManager().init();
request.callback = callback;
return request;
}
function initTemporaryStorage(callback) {
let request = SpecialPowers._getQuotaManager().initTemporaryStorage();
request.callback = callback;
return request;
}
function initPersistentOrigin(principal, callback) {
let request =
SpecialPowers._getQuotaManager().initializePersistentOrigin(principal);
request.callback = callback;
return request;
}
function initTemporaryOrigin(persistence, principal, callback) {
let request = SpecialPowers._getQuotaManager().initializeTemporaryOrigin(
persistence,
principal
);
request.callback = callback;
return request;
}
function initPersistentClient(principal, client, callback) {
let request = SpecialPowers._getQuotaManager().initializePersistentClient(
principal,
client
);
request.callback = callback;
return request;
}
function initTemporaryClient(persistence, principal, client, callback) {
let request = SpecialPowers._getQuotaManager().initializeTemporaryClient(
persistence,
principal,
client
);
request.callback = callback;
return request;
}
function getFullOriginMetadata(persistence, principal, callback) {
const request = SpecialPowers._getQuotaManager().getFullOriginMetadata(
persistence,
principal
);
request.callback = callback;
return request;
}
function clearClient(principal, persistence, client, callback) {
let request = SpecialPowers._getQuotaManager().clearStoragesForPrincipal(
principal,
persistence,
client
);
request.callback = callback;
return request;
}
function clearOrigin(principal, persistence, callback) {
let request = SpecialPowers._getQuotaManager().clearStoragesForPrincipal(
principal,
persistence
);
request.callback = callback;
return request;
}
function clearOriginsByPrefix(principal, persistence, callback) {
let request = SpecialPowers._getQuotaManager().clearStoragesForOriginPrefix(
principal,
persistence
);
request.callback = callback;
return request;
}
function clearPrivateBrowsing(callback) {
let request =
SpecialPowers._getQuotaManager().clearStoragesForPrivateBrowsing();
request.callback = callback;
return request;
}
function resetClient(principal, client) {
let request = Services.qms.resetStoragesForPrincipal(
principal,
"default",
client
);
return request;
}
function persist(principal, callback) {
let request = SpecialPowers._getQuotaManager().persist(principal);
request.callback = callback;
return request;
}
function persisted(principal, callback) {
let request = SpecialPowers._getQuotaManager().persisted(principal);
request.callback = callback;
return request;
}
function estimateOrigin(principal, callback) {
let request = SpecialPowers._getQuotaManager().estimate(principal);
request.callback = callback;
return request;
}
function listOrigins(callback) {
let request = SpecialPowers._getQuotaManager().listOrigins(callback);
request.callback = callback;
return request;
}
function getPersistedFromMetadata(readBuffer) {
const persistedPosition = 8; // Persisted state is stored in the 9th byte
let view =
readBuffer instanceof Uint8Array ? readBuffer : new Uint8Array(readBuffer);
return !!view[persistedPosition];
}
function grabResultAndContinueHandler(request) {
testGenerator.next(request.result);
}
function grabUsageAndContinueHandler(request) {
testGenerator.next(request.result.usage);
}
function getUsage(usageHandler, getAll) {
let request = SpecialPowers._getQuotaManager().getUsage(usageHandler, getAll);
return request;
}
function getOriginUsage(principal, fromMemory = false) {
let request = Services.qms.getUsageForPrincipal(
principal,
function () {},
fromMemory
);
return request;
}
function getCurrentUsage(usageHandler) {
let principal = Cc["@mozilla.org/systemprincipal;1"].createInstance(
Ci.nsIPrincipal
);
let request = SpecialPowers._getQuotaManager().getUsageForPrincipal(
principal,
usageHandler
);
return request;
}
function getPrincipal(url, attr = {}) {
let uri = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService)
.newURI(url);
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
Ci.nsIScriptSecurityManager
);
return ssm.createContentPrincipal(uri, attr);
}
var SpecialPowers = {
getBoolPref(prefName) {
return this._getPrefs().getBoolPref(prefName);
},
setBoolPref(prefName, value) {
this._getPrefs().setBoolPref(prefName, value);
},
setIntPref(prefName, value) {
this._getPrefs().setIntPref(prefName, value);
},
clearUserPref(prefName) {
this._getPrefs().clearUserPref(prefName);
},
_getPrefs() {
let prefService = Cc["@mozilla.org/preferences-service;1"].getService(
Ci.nsIPrefService
);
return prefService.getBranch(null);
},
_getQuotaManager() {
return Cc["@mozilla.org/dom/quota-manager-service;1"].getService(
Ci.nsIQuotaManagerService
);
},
};
function installPackages(packageRelativePaths) {
if (packageRelativePaths.length != 2) {
throw new Error("Unsupported number of package relative paths");
}
for (const packageRelativePath of packageRelativePaths) {
installPackage(packageRelativePath);
}
}
// Take current storage structure on disk and compare it with the expected
// structure. The expected structure is defined in JSON and consists of a per
// test package definition and a shared package definition. The shared package
// definition should contain unknown stuff which needs to be properly handled
// in all situations.
function verifyStorage(packageDefinitionRelativePaths, key, sharedKey) {
if (packageDefinitionRelativePaths.length != 2) {
throw new Error("Unsupported number of package definition relative paths");
}
function verifyEntries(entries, name, indent = "") {
log(`${indent}Verifying ${name} entries`);
indent += " ";
for (const entry of entries) {
const maybeName = entry.name;
log(`${indent}Verifying entry ${maybeName}`);
let hasName = false;
let hasDir = false;
let hasEntries = false;
for (const property in entry) {
switch (property) {
case "note":
case "todo":
break;
case "name":
hasName = true;
break;
case "dir":
hasDir = true;
break;
case "entries":
hasEntries = true;
break;
default:
throw new Error(`Unknown property ${property}`);
}
}
if (!hasName) {
throw new Error("An entry must have the name property");
}
if (!hasDir) {
throw new Error("An entry must have the dir property");
}
if (hasEntries && !entry.dir) {
throw new Error("An entry can't have entries if it's not a directory");
}
if (hasEntries) {
verifyEntries(entry.entries, entry.name, indent);
}
}
}
function getCurrentEntries() {
log("Getting current entries");
function getEntryForFile(file) {
let entry = {
name: file.leafName,
dir: file.isDirectory(),
};
if (file.isDirectory()) {
const enumerator = file.directoryEntries;
let nextFile;
while ((nextFile = enumerator.nextFile)) {
if (!entry.entries) {
entry.entries = [];
}
entry.entries.push(getEntryForFile(nextFile));
}
}
return entry;
}
let entries = [];
let file = getRelativeFile("indexedDB");
if (file.exists()) {
entries.push(getEntryForFile(file));
}
file = getRelativeFile("storage");
if (file.exists()) {
entries.push(getEntryForFile(file));
}
file = getRelativeFile("storage.sqlite");
if (file.exists()) {
entries.push(getEntryForFile(file));
}
verifyEntries(entries, "current");
return entries;
}
function getEntriesFromPackageDefinition(
packageDefinitionRelativePath,
lookupKey
) {
log(`Getting ${lookupKey} entries from ${packageDefinitionRelativePath}`);
const currentDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
const file = getRelativeFile(
packageDefinitionRelativePath + ".json",
currentDir
);
const fileInputStream = Cc[
"@mozilla.org/network/file-input-stream;1"
].createInstance(Ci.nsIFileInputStream);
fileInputStream.init(file, -1, -1, 0);
const scriptableInputStream = Cc[
"@mozilla.org/scriptableinputstream;1"
].createInstance(Ci.nsIScriptableInputStream);
scriptableInputStream.init(fileInputStream);
const data = scriptableInputStream.readBytes(
scriptableInputStream.available()
);
const obj = JSON.parse(data);
const result = obj.find(({ key: elementKey }) => elementKey == lookupKey);
if (!result) {
throw new Error("The file doesn't contain an element for given key");
}
if (!result.entries) {
throw new Error("The element doesn't have the entries property");
}
verifyEntries(result.entries, lookupKey);
return result.entries;
}
function addSharedEntries(expectedEntries, sharedEntries, name, indent = "") {
log(`${indent}Checking common ${name} entries`);
indent += " ";
for (const sharedEntry of sharedEntries) {
const expectedEntry = expectedEntries.find(
({ name: elementName }) => elementName == sharedEntry.name
);
if (expectedEntry) {
log(`${indent}Checking common entry ${sharedEntry.name}`);
if (!expectedEntry.dir || !sharedEntry.dir) {
throw new Error("A common entry must be a directory");
}
if (!expectedEntry.entries && !sharedEntry.entries) {
throw new Error("A common entry must not be a leaf");
}
if (sharedEntry.entries) {
if (!expectedEntry.entries) {
expectedEntry.entries = [];
}
addSharedEntries(
expectedEntry.entries,
sharedEntry.entries,
sharedEntry.name,
indent
);
}
} else {
log(`${indent}Adding entry ${sharedEntry.name}`);
expectedEntries.push(sharedEntry);
}
}
}
function compareEntries(currentEntries, expectedEntries, name, indent = "") {
log(`${indent}Comparing ${name} entries`);
indent += " ";
if (currentEntries.length != expectedEntries.length) {
throw new Error("Entries must have the same length");
}
for (const currentEntry of currentEntries) {
log(`${indent}Comparing entry ${currentEntry.name}`);
const expectedEntry = expectedEntries.find(
({ name: elementName }) => elementName == currentEntry.name
);
if (!expectedEntry) {
throw new Error("Cannot find a matching entry");
}
if (expectedEntry.dir != currentEntry.dir) {
throw new Error("The dir property doesn't match");
}
if (
(expectedEntry.entries && !currentEntry.entries) ||
(!expectedEntry.entries && currentEntry.entries)
) {
throw new Error("The entries property doesn't match");
}
if (expectedEntry.entries) {
compareEntries(
currentEntry.entries,
expectedEntry.entries,
currentEntry.name,
indent
);
}
}
}
const currentEntries = getCurrentEntries();
log("Stringified current entries: " + JSON.stringify(currentEntries));
const expectedEntries = getEntriesFromPackageDefinition(
packageDefinitionRelativePaths[0],
key
);
const sharedEntries = getEntriesFromPackageDefinition(
packageDefinitionRelativePaths[1],
sharedKey ? sharedKey : key
);
addSharedEntries(expectedEntries, sharedEntries, key);
log("Stringified expected entries: " + JSON.stringify(expectedEntries));
compareEntries(currentEntries, expectedEntries, key);
}
async function verifyInitializationStatus(
expectStorageIsInitialized,
expectTemporaryStorageIsInitialized
) {
if (!expectStorageIsInitialized && expectTemporaryStorageIsInitialized) {
throw new Error("Invalid expectation");
}
let request = storageInitialized();
await requestFinished(request);
const storageIsInitialized = request.result;
request = temporaryStorageInitialized();
await requestFinished(request);
const temporaryStorageIsInitialized = request.result;
ok(
!(!storageIsInitialized && temporaryStorageIsInitialized),
"Initialization status is consistent"
);
if (expectStorageIsInitialized) {
ok(storageIsInitialized, "Storage is initialized");
} else {
ok(!storageIsInitialized, "Storage is not initialized");
}
if (expectTemporaryStorageIsInitialized) {
ok(temporaryStorageIsInitialized, "Temporary storage is initialized");
} else {
ok(!temporaryStorageIsInitialized, "Temporary storage is not initialized");
}
}
|