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
|
/* -*- 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 "mozilla/dom/quota/Client.h"
#include "mozilla/dom/quota/CommonMetadata.h"
#include "mozilla/dom/quota/FileStreams.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/gtest/MozAssertions.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "QuotaManagerDependencyFixture.h"
namespace mozilla::dom::quota::test {
class TestFileOutputStream : public QuotaManagerDependencyFixture {
public:
static void SetUpTestCase() {
ASSERT_NO_FATAL_FAILURE(InitializeFixture());
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
prefs->SetIntPref("dom.quotaManager.temporaryStorage.fixedLimit",
mQuotaLimit);
}
static void TearDownTestCase() {
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
prefs->ClearUserPref("dom.quotaManager.temporaryStorage.fixedLimit");
ASSERT_NO_FATAL_FAILURE(ShutdownFixture());
}
static const int32_t mQuotaLimit = 8192;
};
TEST_F(TestFileOutputStream, extendFileStreamWithSetEOF) {
auto ioTask = []() {
quota::QuotaManager* quotaManager = quota::QuotaManager::Get();
auto originMetadata =
quota::OriginMetadata{""_ns,
"example.com"_ns,
"http://example.com"_ns,
"http://example.com"_ns,
/* aIsPrivate */ false,
quota::PERSISTENCE_TYPE_DEFAULT};
{
ASSERT_NS_SUCCEEDED(quotaManager->EnsureStorageIsInitialized());
ASSERT_NS_SUCCEEDED(quotaManager->EnsureTemporaryStorageIsInitialized());
auto res = quotaManager->EnsureTemporaryOriginIsInitialized(
quota::PERSISTENCE_TYPE_DEFAULT, originMetadata);
ASSERT_TRUE(res.isOk());
}
const int64_t groupLimit =
static_cast<int64_t>(quotaManager->GetGroupLimit());
ASSERT_TRUE(mQuotaLimit * 1024LL == groupLimit);
// We don't use the tested stream itself to check the file size as it
// may report values which have not been written to disk.
RefPtr<quota::FileOutputStream> check = MakeRefPtr<quota::FileOutputStream>(
quota::PERSISTENCE_TYPE_DEFAULT, originMetadata,
quota::Client::Type::SDB);
RefPtr<quota::FileOutputStream> stream =
MakeRefPtr<quota::FileOutputStream>(quota::PERSISTENCE_TYPE_DEFAULT,
originMetadata,
quota::Client::Type::SDB);
{
auto testPathRes = quotaManager->GetOriginDirectory(originMetadata);
ASSERT_TRUE(testPathRes.isOk());
nsCOMPtr<nsIFile> testPath = testPathRes.unwrap();
ASSERT_NS_SUCCEEDED(testPath->AppendRelativePath(u"sdb"_ns));
ASSERT_NS_SUCCEEDED(
testPath->AppendRelativePath(u"tTestFileOutputStream.txt"_ns));
bool exists = true;
ASSERT_NS_SUCCEEDED(testPath->Exists(&exists));
if (exists) {
ASSERT_NS_SUCCEEDED(testPath->Remove(/* recursive */ false));
}
ASSERT_NS_SUCCEEDED(testPath->Exists(&exists));
ASSERT_FALSE(exists);
ASSERT_NS_SUCCEEDED(testPath->Create(nsIFile::NORMAL_FILE_TYPE, 0666));
ASSERT_NS_SUCCEEDED(testPath->Exists(&exists));
ASSERT_TRUE(exists);
nsCOMPtr<nsIFile> checkPath;
ASSERT_NS_SUCCEEDED(testPath->Clone(getter_AddRefs(checkPath)));
const int32_t IOFlags = -1;
const int32_t perm = -1;
const int32_t behaviorFlags = 0;
ASSERT_NS_SUCCEEDED(stream->Init(testPath, IOFlags, perm, behaviorFlags));
ASSERT_NS_SUCCEEDED(check->Init(testPath, IOFlags, perm, behaviorFlags));
}
// Check that we start with an empty file
int64_t avail = 42;
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(0 == avail);
// Enlarge the file
const int64_t toSize = groupLimit;
ASSERT_NS_SUCCEEDED(stream->Seek(nsISeekableStream::NS_SEEK_SET, toSize));
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(0 == avail);
ASSERT_NS_SUCCEEDED(stream->SetEOF());
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(toSize == avail);
// Try to enlarge the file past the limit
const int64_t overGroupLimit = groupLimit + 1;
// Seeking is allowed
ASSERT_NS_SUCCEEDED(
stream->Seek(nsISeekableStream::NS_SEEK_SET, overGroupLimit));
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(toSize == avail);
// Setting file size to exceed quota should yield no device space error
ASSERT_TRUE(NS_ERROR_FILE_NO_DEVICE_SPACE == stream->SetEOF());
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(toSize == avail);
// Shrink the file
const int64_t toHalfSize = toSize / 2;
ASSERT_NS_SUCCEEDED(
stream->Seek(nsISeekableStream::NS_SEEK_SET, toHalfSize));
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(toSize == avail);
ASSERT_NS_SUCCEEDED(stream->SetEOF());
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(toHalfSize == avail);
// Shrink the file back to nothing
ASSERT_NS_SUCCEEDED(stream->Seek(nsISeekableStream::NS_SEEK_SET, 0));
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(toHalfSize == avail);
ASSERT_NS_SUCCEEDED(stream->SetEOF());
ASSERT_NS_SUCCEEDED(check->GetSize(&avail));
ASSERT_TRUE(0 == avail);
};
PerformOnIOThread(std::move(ioTask));
}
} // namespace mozilla::dom::quota::test
|