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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "FileSystemParentTypes.h"
#include "TestHelpers.h"
#include "datamodel/FileSystemDataManager.h"
#include "datamodel/FileSystemDatabaseManager.h"
#include "datamodel/FileSystemFileManager.h"
#include "gtest/gtest.h"
#include "mozIStorageService.h"
#include "mozStorageCID.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/dom/FileSystemQuotaClientFactory.h"
#include "mozilla/dom/PFileSystemManager.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/quota/FileStreams.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/dom/quota/QuotaManagerService.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "mozilla/dom/quota/UsageInfo.h"
#include "mozilla/dom/quota/test/QuotaManagerDependencyFixture.h"
#include "mozilla/gtest/MozAssertions.h"
#include "nsIFile.h"
#include "nsIFileURL.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsIQuotaCallbacks.h"
#include "nsIQuotaRequests.h"
#include "nsNetUtil.h"
#include "nsScriptSecurityManager.h"
namespace mozilla::dom::fs::test {
quota::OriginMetadata GetTestQuotaOriginMetadata() {
return quota::OriginMetadata{""_ns,
"quotaexample.com"_ns,
"http://quotaexample.com"_ns,
"http://quotaexample.com"_ns,
/* aIsPrivate */ false,
quota::PERSISTENCE_TYPE_DEFAULT};
}
class TestFileSystemQuotaClient
: public quota::test::QuotaManagerDependencyFixture {
public:
static const int sPage = 64 * 512;
// ExceedsPreallocation value may depend on platform and sqlite version!
static const int sExceedsPreallocation = sPage;
protected:
void SetUp() override { ASSERT_NO_FATAL_FAILURE(InitializeFixture()); }
void TearDown() override {
EXPECT_NO_FATAL_FAILURE(
ClearStoragesForOrigin(GetTestQuotaOriginMetadata()));
ASSERT_NO_FATAL_FAILURE(ShutdownFixture());
}
static const Name& GetTestFileName() {
static Name testFileName = []() {
nsCString testCFileName;
testCFileName.SetLength(sExceedsPreallocation);
std::fill(testCFileName.BeginWriting(), testCFileName.EndWriting(), 'x');
return NS_ConvertASCIItoUTF16(testCFileName.BeginReading(),
sExceedsPreallocation);
}();
return testFileName;
}
static uint64_t BytesOfName(const Name& aName) {
return static_cast<uint64_t>(aName.Length() * sizeof(Name::char_type));
}
static const nsCString& GetTestData() {
static const nsCString sTestData = "There is a way out of every box"_ns;
return sTestData;
}
static void CreateNewEmptyFile(
data::FileSystemDatabaseManager* const aDatabaseManager,
const FileSystemChildMetadata& aFileSlot, EntryId& aEntryId) {
// The file should not exist yet
Result<EntryId, QMResult> existingTestFile =
aDatabaseManager->GetOrCreateFile(aFileSlot, /* create */ false);
ASSERT_TRUE(existingTestFile.isErr());
ASSERT_NSEQ(NS_ERROR_DOM_NOT_FOUND_ERR,
ToNSResult(existingTestFile.unwrapErr()));
// Create a new file
TEST_TRY_UNWRAP(aEntryId, aDatabaseManager->GetOrCreateFile(
aFileSlot, /* create */ true));
}
static void WriteDataToFile(
data::FileSystemDatabaseManager* const aDatabaseManager,
const EntryId& aEntryId, const nsCString& aData) {
TEST_TRY_UNWRAP(FileId fileId, aDatabaseManager->EnsureFileId(aEntryId));
ASSERT_FALSE(fileId.IsEmpty());
ContentType type;
TimeStamp lastModMilliS = 0;
Path path;
nsCOMPtr<nsIFile> fileObj;
ASSERT_NSEQ(NS_OK,
aDatabaseManager->GetFile(aEntryId, fileId, FileMode::EXCLUSIVE,
type, lastModMilliS, path, fileObj));
uint32_t written = 0;
ASSERT_NE(written, aData.Length());
const quota::OriginMetadata& testOriginMeta = GetTestQuotaOriginMetadata();
TEST_TRY_UNWRAP(nsCOMPtr<nsIOutputStream> fileStream,
quota::CreateFileOutputStream(
quota::PERSISTENCE_TYPE_DEFAULT, testOriginMeta,
quota::Client::FILESYSTEM, fileObj));
auto finallyClose = MakeScopeExit(
[&fileStream]() { ASSERT_NSEQ(NS_OK, fileStream->Close()); });
ASSERT_NSEQ(NS_OK,
fileStream->Write(aData.get(), aData.Length(), &written));
ASSERT_EQ(aData.Length(), written);
}
/* Static for use in callbacks */
static void CreateRegisteredDataManager(
Registered<data::FileSystemDataManager>& aRegisteredDataManager) {
bool done = false;
data::FileSystemDataManager::GetOrCreateFileSystemDataManager(
GetTestQuotaOriginMetadata())
->Then(
GetCurrentSerialEventTarget(), __func__,
[&aRegisteredDataManager,
&done](Registered<data::FileSystemDataManager>
registeredDataManager) mutable {
auto doneOnReturn = MakeScopeExit([&done]() { done = true; });
ASSERT_TRUE(registeredDataManager->IsOpen());
aRegisteredDataManager = std::move(registeredDataManager);
},
[&done](nsresult rejectValue) {
auto doneOnReturn = MakeScopeExit([&done]() { done = true; });
ASSERT_NSEQ(NS_OK, rejectValue);
});
SpinEventLoopUntil("Promise is fulfilled"_ns, [&done]() { return done; });
ASSERT_TRUE(aRegisteredDataManager);
ASSERT_TRUE(aRegisteredDataManager->IsOpen());
ASSERT_TRUE(aRegisteredDataManager->MutableDatabaseManagerPtr());
}
static void CheckUsageEqualTo(const quota::UsageInfo& aUsage,
uint64_t expected) {
EXPECT_TRUE(aUsage.FileUsage().isNothing());
auto dbUsage = aUsage.DatabaseUsage();
ASSERT_TRUE(dbUsage.isSome());
const auto actual = dbUsage.value();
ASSERT_EQ(actual, expected);
}
static void CheckUsageGreaterThan(const quota::UsageInfo& aUsage,
uint64_t expected) {
EXPECT_TRUE(aUsage.FileUsage().isNothing());
auto dbUsage = aUsage.DatabaseUsage();
ASSERT_TRUE(dbUsage.isSome());
const auto actual = dbUsage.value();
ASSERT_GT(actual, expected);
}
};
TEST_F(TestFileSystemQuotaClient, CheckUsageBeforeAnyFilesOnDisk) {
auto backgroundTask = []() {
mozilla::Atomic<bool> isCanceled{false};
auto ioTask = [&isCanceled](const RefPtr<quota::Client>& quotaClient,
data::FileSystemDatabaseManager* dbm) {
ASSERT_FALSE(isCanceled);
const quota::OriginMetadata& testOriginMeta =
GetTestQuotaOriginMetadata();
const Origin& testOrigin = testOriginMeta.mOrigin;
// After initialization,
// * database size is not zero
// * GetUsageForOrigin and InitOrigin should agree
TEST_TRY_UNWRAP(quota::UsageInfo usageNow,
quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageGreaterThan(usageNow, 0u));
const auto initialDbUsage = usageNow.DatabaseUsage().value();
TEST_TRY_UNWRAP(usageNow, quotaClient->GetUsageForOrigin(
quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, initialDbUsage));
// Create a new file
TEST_TRY_UNWRAP(const EntryId rootId, data::GetRootHandle(testOrigin));
FileSystemChildMetadata fileData(rootId, GetTestFileName());
EntryId testFileId;
ASSERT_NO_FATAL_FAILURE(CreateNewEmptyFile(dbm, fileData, testFileId));
// After a new file has been created (only in the database),
// * database size has increased
// * GetUsageForOrigin and InitOrigin should agree
const auto expectedUse = initialDbUsage + 2 * sPage;
TEST_TRY_UNWRAP(usageNow, quotaClient->GetUsageForOrigin(
quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, expectedUse));
TEST_TRY_UNWRAP(usageNow,
quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, expectedUse));
};
RefPtr<mozilla::dom::quota::Client> quotaClient = fs::CreateQuotaClient();
ASSERT_TRUE(quotaClient);
// For uninitialized database, file usage is nothing
auto checkTask =
[&isCanceled](const RefPtr<mozilla::dom::quota::Client>& quotaClient) {
TEST_TRY_UNWRAP(quota::UsageInfo usageNow,
quotaClient->GetUsageForOrigin(
quota::PERSISTENCE_TYPE_DEFAULT,
GetTestQuotaOriginMetadata(), isCanceled));
ASSERT_TRUE(usageNow.DatabaseUsage().isNothing());
EXPECT_TRUE(usageNow.FileUsage().isNothing());
};
PerformOnIOThread(std::move(checkTask),
RefPtr<mozilla::dom::quota::Client>{quotaClient});
// Initialize database
Registered<data::FileSystemDataManager> rdm;
ASSERT_NO_FATAL_FAILURE(CreateRegisteredDataManager(rdm));
// Run tests with an initialized database
PerformOnIOThread(std::move(ioTask), std::move(quotaClient),
rdm->MutableDatabaseManagerPtr());
};
PerformOnBackgroundThread(std::move(backgroundTask));
}
TEST_F(TestFileSystemQuotaClient, WritesToFilesShouldIncreaseUsage) {
auto backgroundTask = []() {
mozilla::Atomic<bool> isCanceled{false};
auto ioTask = [&isCanceled](
const RefPtr<mozilla::dom::quota::Client>& quotaClient,
data::FileSystemDatabaseManager* dbm) {
const quota::OriginMetadata& testOriginMeta =
GetTestQuotaOriginMetadata();
const Origin& testOrigin = testOriginMeta.mOrigin;
TEST_TRY_UNWRAP(const EntryId rootId, data::GetRootHandle(testOrigin));
FileSystemChildMetadata fileData(rootId, GetTestFileName());
EntryId testFileId;
ASSERT_NO_FATAL_FAILURE(CreateNewEmptyFile(dbm, fileData, testFileId));
// const auto testFileDbUsage = usageNow.DatabaseUsage().value();
TEST_TRY_UNWRAP(
quota::UsageInfo usageNow,
quotaClient->GetUsageForOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_TRUE(usageNow.DatabaseUsage().isSome());
const auto testFileDbUsage = usageNow.DatabaseUsage().value();
TEST_TRY_UNWRAP(usageNow,
quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, testFileDbUsage));
// Fill the file with some content
const nsCString& testData = GetTestData();
ASSERT_NO_FATAL_FAILURE(WriteDataToFile(dbm, testFileId, testData));
// In this test we don't lock the file -> no rescan is expected
// and InitOrigin should return the previous value
TEST_TRY_UNWRAP(usageNow,
quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, testFileDbUsage));
// When data manager unlocks the file, it should call update
// but in this test we call it directly
ASSERT_NSEQ(NS_OK, dbm->UpdateUsage(FileId(testFileId)));
const auto expectedTotalUsage = testFileDbUsage + testData.Length();
// Disk usage should have increased after writing
TEST_TRY_UNWRAP(usageNow,
quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, expectedTotalUsage));
// The usage values should now agree
TEST_TRY_UNWRAP(usageNow, quotaClient->GetUsageForOrigin(
quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, expectedTotalUsage));
};
RefPtr<mozilla::dom::quota::Client> quotaClient = fs::CreateQuotaClient();
ASSERT_TRUE(quotaClient);
// Initialize database
Registered<data::FileSystemDataManager> rdm;
ASSERT_NO_FATAL_FAILURE(CreateRegisteredDataManager(rdm));
// Run tests with an initialized database
PerformOnIOThread(std::move(ioTask), std::move(quotaClient),
rdm->MutableDatabaseManagerPtr());
};
PerformOnBackgroundThread(std::move(backgroundTask));
}
TEST_F(TestFileSystemQuotaClient, TrackedFilesOnInitOriginShouldCauseRescan) {
auto backgroundTask = []() {
mozilla::Atomic<bool> isCanceled{false};
EntryId* testFileId = new EntryId();
auto cleanupFileId = MakeScopeExit([&testFileId] { delete testFileId; });
auto fileCreation = [&testFileId](data::FileSystemDatabaseManager* dbm) {
const Origin& testOrigin = GetTestQuotaOriginMetadata().mOrigin;
TEST_TRY_UNWRAP(const EntryId rootId, data::GetRootHandle(testOrigin));
FileSystemChildMetadata fileData(rootId, GetTestFileName());
EntryId someId;
ASSERT_NO_FATAL_FAILURE(CreateNewEmptyFile(dbm, fileData, someId));
testFileId->Append(someId);
};
auto writingToFile =
[&isCanceled, testFileId](
const RefPtr<mozilla::dom::quota::Client>& quotaClient,
data::FileSystemDatabaseManager* dbm) {
const quota::OriginMetadata& testOriginMeta =
GetTestQuotaOriginMetadata();
TEST_TRY_UNWRAP(
quota::UsageInfo usageNow,
quotaClient->GetUsageForOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_TRUE(usageNow.DatabaseUsage().isSome());
const auto testFileDbUsage = usageNow.DatabaseUsage().value();
// Fill the file with some content
const auto& testData = GetTestData();
const auto expectedTotalUsage = testFileDbUsage + testData.Length();
ASSERT_NO_FATAL_FAILURE(WriteDataToFile(dbm, *testFileId, testData));
// We don't call update now - because the file is tracked and
// InitOrigin should perform a rescan
TEST_TRY_UNWRAP(
usageNow, quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(
CheckUsageEqualTo(usageNow, expectedTotalUsage));
// As always, the cached and scanned values should agree
TEST_TRY_UNWRAP(usageNow, quotaClient->GetUsageForOrigin(
quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(
CheckUsageEqualTo(usageNow, expectedTotalUsage));
};
RefPtr<mozilla::dom::quota::Client> quotaClient = fs::CreateQuotaClient();
ASSERT_TRUE(quotaClient);
// Initialize database
Registered<data::FileSystemDataManager> rdm;
ASSERT_NO_FATAL_FAILURE(CreateRegisteredDataManager(rdm));
PerformOnIOThread(std::move(fileCreation),
rdm->MutableDatabaseManagerPtr());
// This should force a rescan
TEST_TRY_UNWRAP(FileId fileId, rdm->LockExclusive(*testFileId));
ASSERT_FALSE(fileId.IsEmpty());
PerformOnIOThread(std::move(writingToFile), std::move(quotaClient),
rdm->MutableDatabaseManagerPtr());
};
PerformOnBackgroundThread(std::move(backgroundTask));
}
TEST_F(TestFileSystemQuotaClient, RemovingFileShouldDecreaseUsage) {
auto backgroundTask = []() {
mozilla::Atomic<bool> isCanceled{false};
auto ioTask = [&isCanceled](
const RefPtr<mozilla::dom::quota::Client>& quotaClient,
data::FileSystemDatabaseManager* dbm) {
const quota::OriginMetadata& testOriginMeta =
GetTestQuotaOriginMetadata();
const Origin& testOrigin = testOriginMeta.mOrigin;
TEST_TRY_UNWRAP(const EntryId rootId, data::GetRootHandle(testOrigin));
FileSystemChildMetadata fileData(rootId, GetTestFileName());
EntryId testFileId;
ASSERT_NO_FATAL_FAILURE(CreateNewEmptyFile(dbm, fileData, testFileId));
TEST_TRY_UNWRAP(quota::UsageInfo usageNow,
quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_TRUE(usageNow.DatabaseUsage().isSome());
const auto testFileDbUsage = usageNow.DatabaseUsage().value();
// Fill the file with some content
const nsCString& testData = GetTestData();
const auto expectedTotalUsage = testFileDbUsage + testData.Length();
ASSERT_NO_FATAL_FAILURE(WriteDataToFile(dbm, testFileId, testData));
// Currently, usage is expected to be updated on unlock by data manager
// but here UpdateUsage() is called directly
ASSERT_NSEQ(NS_OK, dbm->UpdateUsage(FileId(testFileId)));
// At least some file disk usage should have appeared after unlocking
TEST_TRY_UNWRAP(usageNow, quotaClient->GetUsageForOrigin(
quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, expectedTotalUsage));
TEST_TRY_UNWRAP(bool wasRemoved,
dbm->RemoveFile({rootId, GetTestFileName()}));
ASSERT_TRUE(wasRemoved);
// Removes cascade and usage table should be up to date immediately
TEST_TRY_UNWRAP(usageNow,
quotaClient->InitOrigin(quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, testFileDbUsage));
// GetUsageForOrigin should agree
TEST_TRY_UNWRAP(usageNow, quotaClient->GetUsageForOrigin(
quota::PERSISTENCE_TYPE_DEFAULT,
testOriginMeta, isCanceled));
ASSERT_NO_FATAL_FAILURE(CheckUsageEqualTo(usageNow, testFileDbUsage));
};
RefPtr<mozilla::dom::quota::Client> quotaClient = fs::CreateQuotaClient();
ASSERT_TRUE(quotaClient);
// Initialize database
Registered<data::FileSystemDataManager> rdm;
ASSERT_NO_FATAL_FAILURE(CreateRegisteredDataManager(rdm));
// Run tests with an initialized database
PerformOnIOThread(std::move(ioTask), std::move(quotaClient),
rdm->MutableDatabaseManagerPtr());
};
PerformOnBackgroundThread(std::move(backgroundTask));
}
} // namespace mozilla::dom::fs::test
|