Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 1426295

Browse files
committed
Bug 1870807 - Adjust BucketFS gtests to new partitioning checks. r=dom-storage-reviewers,asuth
Differential Revision: https://phabricator.services.mozilla.com/D236998
1 parent 2766d8a commit 1426295

File tree

4 files changed

+125
-7
lines changed

4 files changed

+125
-7
lines changed

dom/fs/api/FileSystemManager.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ void FileSystemManager::BeginRequest(
101101

102102
nsICookieJarSettings* cookieJarSettings = mGlobal->GetCookieJarSettings();
103103
nsIPrincipal* unpartitionedPrincipal = mGlobal->PrincipalOrNull();
104-
if (NS_WARN_IF(!cookieJarSettings || !unpartitionedPrincipal ||
105-
unpartitionedPrincipal->GetIsInPrivateBrowsing())) {
104+
if (NS_WARN_IF(!cookieJarSettings) || NS_WARN_IF(!unpartitionedPrincipal) ||
105+
NS_WARN_IF(unpartitionedPrincipal->GetIsInPrivateBrowsing())) {
106106
// ePartition values can be returned for Private Browsing Mode
107107
// for third-party iframes, so we also need to check the private browsing
108108
// in that case which means we need to check the principal.
@@ -117,10 +117,7 @@ void FileSystemManager::BeginRequest(
117117
const bool allowed = access == StorageAccess::eAllow ||
118118
StoragePartitioningEnabled(access, cookieJarSettings);
119119
if (NS_WARN_IF(!allowed)) {
120-
const nsresult err = StorageAccess::ePrivateBrowsing == access
121-
? NS_ERROR_DOM_NOT_SUPPORTED_ERR
122-
: NS_ERROR_DOM_SECURITY_ERR;
123-
aFailure(err);
120+
aFailure(NS_ERROR_DOM_SECURITY_ERR);
124121
return;
125122
}
126123

dom/fs/test/gtest/FileSystemMocks.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ nsIGlobalObject* GetGlobal() {
3232
return global.get();
3333
}
3434

35+
MockGlobalObject* GetMockGlobal() {
36+
nsCOMPtr<nsIGlobalObject> global = GetGlobal();
37+
MockGlobalObject* mock = new MockGlobalObject(std::move(global));
38+
39+
return mock;
40+
}
41+
3542
nsresult GetAsString(const RefPtr<Promise>& aPromise, nsAString& aString) {
3643
AutoJSAPI jsapi;
3744
DebugOnly<bool> ok = jsapi.Init(xpc::PrivilegedJunkScope());
@@ -92,3 +99,23 @@ mozilla::ipc::PrincipalInfo GetPrincipalInfo() {
9299
}
93100

94101
} // namespace mozilla::dom::fs::test
102+
103+
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MockGlobalObject)
104+
105+
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MockGlobalObject)
106+
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
107+
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
108+
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
109+
110+
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MockGlobalObject)
111+
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
112+
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
113+
114+
NS_INTERFACE_TABLE_HEAD(MockGlobalObject)
115+
NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
116+
NS_INTERFACE_TABLE(MockGlobalObject, nsIGlobalObject)
117+
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(MockGlobalObject)
118+
NS_INTERFACE_MAP_END
119+
120+
NS_IMPL_CYCLE_COLLECTING_ADDREF(MockGlobalObject)
121+
NS_IMPL_CYCLE_COLLECTING_RELEASE(MockGlobalObject)

dom/fs/test/gtest/FileSystemMocks.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "nsISupportsImpl.h"
3535
#include "nsITimer.h"
3636

37+
class MockGlobalObject;
38+
3739
namespace mozilla::dom::fs {
3840

3941
inline std::ostream& operator<<(std::ostream& aOut,
@@ -45,6 +47,8 @@ namespace test {
4547

4648
nsIGlobalObject* GetGlobal();
4749

50+
MockGlobalObject* GetMockGlobal();
51+
4852
nsresult GetAsString(const RefPtr<Promise>& aPromise, nsAString& aString);
4953

5054
mozilla::ipc::PrincipalInfo GetPrincipalInfo();
@@ -337,4 +341,64 @@ MOCK_PROMISE_LISTENER(
337341
MOCK_PROMISE_LISTENER(ExpectResolveCalled, mozilla::dom::fs::test::MockExpectMe,
338342
mozilla::dom::fs::test::FailOnCall);
339343

344+
#define MOCKGLOBALOBJECT_IID \
345+
{/* c0a93d91-9c15-49e1-abed-890b3679ac6e */ \
346+
0xc0a93d91, \
347+
0x9c15, \
348+
0x49e1, \
349+
{0xab, 0xed, 0x89, 0x0b, 0x36, 0x79, 0xac, 0x6e}}
350+
351+
class MockGlobalObject : public nsIGlobalObject, public nsWrapperCache {
352+
public:
353+
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
354+
NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MockGlobalObject)
355+
356+
NS_DECLARE_STATIC_IID_ACCESSOR(MOCKGLOBALOBJECT_IID)
357+
358+
explicit MockGlobalObject(nsCOMPtr<nsIGlobalObject>&& aGlobal)
359+
: mGlobal(std::move(aGlobal)) {}
360+
361+
JSObject* GetGlobalJSObject() override {
362+
return mGlobal->GetGlobalJSObject();
363+
}
364+
365+
JSObject* GetGlobalJSObjectPreserveColor() const override {
366+
return mGlobal->GetGlobalJSObjectPreserveColor();
367+
}
368+
369+
MOCK_METHOD(JSObject*, WrapObject,
370+
(JSContext * aCx, JS::Handle<JSObject*> aGivenProto), (override));
371+
372+
MOCK_METHOD(nsICookieJarSettings*, GetCookieJarSettings, (), (override));
373+
374+
MOCK_METHOD(nsIPrincipal*, PrincipalOrNull, (), (const));
375+
376+
MOCK_METHOD(mozilla::StorageAccess, GetStorageAccess, (), (override));
377+
378+
JS::loader::ModuleLoaderBase* GetModuleLoader(JSContext* aCx) override {
379+
return mGlobal->GetModuleLoader(aCx);
380+
}
381+
382+
bool ShouldResistFingerprinting(RFPTarget aTarget) const override {
383+
return mGlobal->ShouldResistFingerprinting(std::move(aTarget));
384+
}
385+
386+
nsISerialEventTarget* SerialEventTarget() const override {
387+
return mGlobal->SerialEventTarget();
388+
}
389+
390+
nsresult Dispatch(already_AddRefed<nsIRunnable>&& aRunnable) const override {
391+
return mGlobal->Dispatch(std::move(aRunnable));
392+
}
393+
394+
mozilla::OriginTrials Trials() const override { return mGlobal->Trials(); }
395+
396+
protected:
397+
~MockGlobalObject() = default;
398+
399+
nsCOMPtr<nsIGlobalObject> mGlobal;
400+
};
401+
402+
NS_DEFINE_STATIC_IID_ACCESSOR(MockGlobalObject, MOCKGLOBALOBJECT_IID)
403+
340404
#endif // DOM_FS_TEST_GTEST_FILESYSTEMMOCKS_H_

dom/fs/test/gtest/child/TestFileSystemRequestHandler.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mozilla/dom/StorageManager.h"
2121
#include "mozilla/ipc/FileDescriptorUtils.h"
2222
#include "mozilla/ipc/IPCCore.h"
23+
#include "mozilla/net/CookieJarSettings.h"
2324
#include "nsDirectoryServiceDefs.h"
2425
#include "nsIFile.h"
2526

@@ -33,6 +34,12 @@ namespace mozilla::dom::fs::test {
3334
class TestFileSystemRequestHandler : public ::testing::Test {
3435
protected:
3536
void SetUp() override {
37+
mMockGlobal = GetMockGlobal();
38+
mGlobal = mMockGlobal;
39+
40+
mCookieJarSettings = mozilla::net::CookieJarSettings::Create(
41+
mozilla::net::CookieJarSettings::CreateMode::eRegular,
42+
/* Should resist fingerprinting */ true);
3643
mListener = MakeAndAddRef<ExpectResolveCalled>();
3744

3845
mChild = FileSystemChildMetadata("parent"_ns, u"ChildName"_ns);
@@ -92,17 +99,30 @@ class TestFileSystemRequestHandler : public ::testing::Test {
9299
ASSERT_TRUE(mManager->IsShutdown());
93100
}
94101

95-
nsIGlobalObject* mGlobal = GetGlobal();
102+
void AllowStorageAccess() {
103+
EXPECT_CALL(*mMockGlobal, GetCookieJarSettings())
104+
.WillOnce(::testing::Return(&*mCookieJarSettings));
105+
106+
EXPECT_CALL(*mMockGlobal, GetStorageAccess())
107+
.WillOnce(::testing::Return(mozilla::StorageAccess::eAllow));
108+
}
109+
110+
MockGlobalObject* mMockGlobal;
111+
nsCOMPtr<nsIGlobalObject> mGlobal;
112+
96113
RefPtr<ExpectResolveCalled> mListener;
97114

98115
FileSystemChildMetadata mChild;
99116
FileSystemEntryMetadata mEntry;
100117
nsString mName;
101118
RefPtr<TestFileSystemManagerChild> mFileSystemManagerChild;
102119
RefPtr<FileSystemManager> mManager;
120+
RefPtr<nsICookieJarSettings> mCookieJarSettings;
103121
};
104122

105123
TEST_F(TestFileSystemRequestHandler, isGetRootHandleSuccessful) {
124+
AllowStorageAccess();
125+
106126
auto fakeResponse = [](auto&& aResolve, auto&& /* aReject */) {
107127
EntryId expected = "expected"_ns;
108128
FileSystemGetHandleResponse response(expected);
@@ -132,6 +152,8 @@ TEST_F(TestFileSystemRequestHandler, isGetRootHandleBlockedAfterShutdown) {
132152
}
133153

134154
TEST_F(TestFileSystemRequestHandler, isGetDirectoryHandleSuccessful) {
155+
AllowStorageAccess();
156+
135157
auto fakeResponse = [](const auto& /* aRequest */, auto&& aResolve,
136158
auto&& /* aReject */) {
137159
EntryId expected = "expected"_ns;
@@ -164,6 +186,8 @@ TEST_F(TestFileSystemRequestHandler, isGetDirectoryHandleBlockedAfterShutdown) {
164186
}
165187

166188
TEST_F(TestFileSystemRequestHandler, isGetFileHandleSuccessful) {
189+
AllowStorageAccess();
190+
167191
auto fakeResponse = [](const auto& /* aRequest */, auto&& aResolve,
168192
auto&& /* aReject */) {
169193
EntryId expected = "expected"_ns;
@@ -195,6 +219,8 @@ TEST_F(TestFileSystemRequestHandler, isGetFileHandleBlockedAfterShutdown) {
195219
}
196220

197221
TEST_F(TestFileSystemRequestHandler, isGetFileSuccessful) {
222+
AllowStorageAccess();
223+
198224
auto fakeResponse = [](const auto& /* aRequest */, auto&& aResolve,
199225
auto&& /* aReject */) {
200226
// We have to create a temporary file
@@ -276,6 +302,8 @@ TEST_F(TestFileSystemRequestHandler, isGetWritableBlockedAfterShutdown) {
276302
}
277303

278304
TEST_F(TestFileSystemRequestHandler, isGetEntriesSuccessful) {
305+
AllowStorageAccess();
306+
279307
auto fakeResponse = [](const auto& /* aRequest */, auto&& aResolve,
280308
auto&& /* aReject */) {
281309
nsTArray<FileSystemEntryMetadata> files;
@@ -320,6 +348,8 @@ TEST_F(TestFileSystemRequestHandler, isGetEntriesBlockedAfterShutdown) {
320348
}
321349

322350
TEST_F(TestFileSystemRequestHandler, isRemoveEntrySuccessful) {
351+
AllowStorageAccess();
352+
323353
auto fakeResponse = [](const auto& /* aRequest */, auto&& aResolve,
324354
auto&& /* aReject */) {
325355
FileSystemRemoveEntryResponse response(mozilla::void_t{});

0 commit comments

Comments
 (0)