зеркало из https://github.com/mozilla/gecko-dev.git
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:
Родитель
08c45b9f68
Коммит
7bf48bbf12
|
@ -3,9 +3,19 @@
|
|||
/* 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 https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "SandboxTestingChild.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 {
|
||||
|
||||
SandboxTestingChild* SandboxTestingChild::sInstance = nullptr;
|
||||
|
@ -50,11 +60,28 @@ void SandboxTestingChild::Bind(Endpoint<PSandboxTestingChild>&& aEndpoint) {
|
|||
DebugOnly<bool> ok = aEndpoint.Bind(this);
|
||||
MOZ_ASSERT(ok);
|
||||
|
||||
// Placeholder usage of the APIs needed to report test results.
|
||||
// This will be fleshed out with tests that are OS and process type dependent.
|
||||
SendReportTestResults(nsCString("testId1"), true /* shouldSucceed */,
|
||||
true /* didSucceed*/,
|
||||
nsCString("These are some test results!"));
|
||||
if (XRE_IsContentProcess()) {
|
||||
#ifdef XP_UNIX
|
||||
struct stat st;
|
||||
static const char kAllowedPath[] = "/usr/lib";
|
||||
|
||||
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.
|
||||
SendTestCompleted();
|
||||
}
|
||||
|
@ -77,4 +104,26 @@ bool SandboxTestingChild::RecvShutDown() {
|
|||
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
|
||||
|
|
|
@ -11,6 +11,10 @@
|
|||
#include "mozilla/Monitor.h"
|
||||
#include "mozilla/UniquePtr.h"
|
||||
|
||||
#ifdef XP_UNIX
|
||||
# include "nsString.h"
|
||||
#endif
|
||||
|
||||
#if !defined(MOZ_SANDBOX) || !defined(MOZ_DEBUG) || !defined(ENABLE_TESTS)
|
||||
# error "This file should not be used outside of debug with tests"
|
||||
#endif
|
||||
|
@ -41,6 +45,17 @@ class SandboxTestingChild : public PSandboxTestingChild {
|
|||
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;
|
||||
|
||||
static SandboxTestingChild* sInstance;
|
||||
|
|
Загрузка…
Ссылка в новой задаче