Bug 1660901 - Add some test cases for fstatat inside the content sandbox. r=gcp

Differential Revision: https://phabricator.services.mozilla.com/D88500
This commit is contained in:
Jed Davis 2020-08-28 09:33:53 +00:00
Родитель 08c45b9f68
Коммит 7bf48bbf12
2 изменённых файлов: 69 добавлений и 5 удалений

Просмотреть файл

@ -3,9 +3,19 @@
/* This Source Code Form is subject to the terms of the Mozilla Public /* 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 * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "SandboxTestingChild.h" #include "SandboxTestingChild.h"
#include "SandboxTestingThread.h" #include "SandboxTestingThread.h"
#include "nsXULAppAPI.h"
#ifdef XP_UNIX
# include <fcntl.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
#endif
namespace mozilla { namespace mozilla {
SandboxTestingChild* SandboxTestingChild::sInstance = nullptr; SandboxTestingChild* SandboxTestingChild::sInstance = nullptr;
@ -50,11 +60,28 @@ void SandboxTestingChild::Bind(Endpoint<PSandboxTestingChild>&& aEndpoint) {
DebugOnly<bool> ok = aEndpoint.Bind(this); DebugOnly<bool> ok = aEndpoint.Bind(this);
MOZ_ASSERT(ok); MOZ_ASSERT(ok);
// Placeholder usage of the APIs needed to report test results. if (XRE_IsContentProcess()) {
// This will be fleshed out with tests that are OS and process type dependent. #ifdef XP_UNIX
SendReportTestResults(nsCString("testId1"), true /* shouldSucceed */, struct stat st;
true /* didSucceed*/, static const char kAllowedPath[] = "/usr/lib";
nsCString("These are some test results!"));
ErrnoTest("fstatat_as_stat"_ns, true,
[&] { return fstatat(AT_FDCWD, kAllowedPath, &st, 0); });
ErrnoTest("fstatat_as_lstat"_ns, true, [&] {
return fstatat(AT_FDCWD, kAllowedPath, &st, AT_SYMLINK_NOFOLLOW);
});
# ifdef XP_LINUX
ErrnoTest("fstatat_as_fstat"_ns, true,
[&] { return fstatat(0, "", &st, AT_EMPTY_PATH); });
# endif // XP_LINUX
#else // XP_UNIX
SendReportTestResults("dummy_test"_ns,
/* shouldSucceed */ true,
/* didSucceed */ true,
"The test framework fails if there are no cases."_ns);
#endif // XP_UNIX
}
// Tell SandboxTest that this process is done with all tests. // Tell SandboxTest that this process is done with all tests.
SendTestCompleted(); SendTestCompleted();
} }
@ -77,4 +104,26 @@ bool SandboxTestingChild::RecvShutDown() {
return true; return true;
} }
#ifdef XP_UNIX
template <typename F>
void SandboxTestingChild::ErrnoTest(const nsCString& aName, bool aExpectSuccess,
F&& aFunction) {
int status = aFunction() >= 0 ? 0 : errno;
PosixTest(aName, aExpectSuccess, status);
}
void SandboxTestingChild::PosixTest(const nsCString& aName, bool aExpectSuccess,
int aStatus) {
bool succeeded = aStatus == 0;
nsAutoCString message;
if (succeeded) {
message = "Succeeded"_ns;
} else {
message.AppendPrintf("Error: %s", strerror(aStatus));
}
SendReportTestResults(aName, aExpectSuccess, succeeded, message);
}
#endif // XP_UNIX
} // namespace mozilla } // namespace mozilla

Просмотреть файл

@ -11,6 +11,10 @@
#include "mozilla/Monitor.h" #include "mozilla/Monitor.h"
#include "mozilla/UniquePtr.h" #include "mozilla/UniquePtr.h"
#ifdef XP_UNIX
# include "nsString.h"
#endif
#if !defined(MOZ_SANDBOX) || !defined(MOZ_DEBUG) || !defined(ENABLE_TESTS) #if !defined(MOZ_SANDBOX) || !defined(MOZ_DEBUG) || !defined(ENABLE_TESTS)
# error "This file should not be used outside of debug with tests" # error "This file should not be used outside of debug with tests"
#endif #endif
@ -41,6 +45,17 @@ class SandboxTestingChild : public PSandboxTestingChild {
Endpoint<PSandboxTestingChild>&& aEndpoint); Endpoint<PSandboxTestingChild>&& aEndpoint);
void Bind(Endpoint<PSandboxTestingChild>&& aEndpoint); void Bind(Endpoint<PSandboxTestingChild>&& aEndpoint);
#ifdef XP_UNIX
// For test cases that return an error number or 0, like newer POSIX APIs.
void PosixTest(const nsCString& aName, bool aExpectSuccess, int aStatus);
// For test cases that return a negative number and set `errno` to
// indicate error, like classical Unix APIs; takes a callable, which
// is used only in this function call (so `[&]` captures are safe).
template <typename F>
void ErrnoTest(const nsCString& aName, bool aExpectSuccess, F&& aFunction);
#endif
UniquePtr<SandboxTestingThread> mThread; UniquePtr<SandboxTestingThread> mThread;
static SandboxTestingChild* sInstance; static SandboxTestingChild* sInstance;