SandboxTestingChild.h (3001B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_SandboxTestingChild_h 8 #define mozilla_SandboxTestingChild_h 9 10 #include "mozilla/PSandboxTestingChild.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/Monitor.h" 13 #include "mozilla/StaticPtr.h" 14 #include "mozilla/UniquePtr.h" 15 #include "nsISupports.h" 16 17 #ifdef XP_UNIX 18 # include "nsString.h" 19 #endif 20 21 #if !defined(MOZ_SANDBOX) || !defined(MOZ_DEBUG) || !defined(ENABLE_TESTS) 22 # error "This file should not be used outside of debug with tests" 23 #endif 24 25 namespace mozilla { 26 27 class SandboxTestingThread; 28 29 /** 30 * Runs tests that check sandbox in child process, depending on process type. 31 */ 32 class SandboxTestingChild : public PSandboxTestingChild { 33 public: 34 static bool Initialize( 35 Endpoint<PSandboxTestingChild>&& aSandboxTestingEndpoint); 36 static SandboxTestingChild* GetInstance(); 37 static void Destroy(); 38 39 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SandboxTestingChild, override) 40 41 bool IsTestThread(); 42 void PostToTestThread(already_AddRefed<nsIRunnable>&& runnable); 43 44 void ActorDestroy(ActorDestroyReason aWhy) override; 45 46 virtual ipc::IPCResult RecvShutDown(); 47 48 // Helper to return that no test have been executed. Tests should make sure 49 // they have some fallback through that otherwise the framework will consider 50 // absence of test report as a failure. 51 inline void ReportNoTests(); 52 53 // For test cases that return an error number or 0, like newer POSIX 54 // APIs. If `aExpectSuccess` is true, the test passes if the status is 55 // 0; otherwise, the test requires a specific error if `aExpectedError` 56 // is `Some(n)` or any nonzero status if it's `Nothing()`. 57 void PosixTest(const nsCString& aName, bool aExpectSuccess, int aStatus, 58 Maybe<int> aExpectedError = Nothing()); 59 60 // For test cases that return a negative number and set `errno` to 61 // indicate error, like classical Unix APIs; takes a callable, which 62 // is used only in this function call (so `[&]` captures are safe). 63 template <typename F> 64 void ErrnoTest(const nsCString& aName, bool aExpectSuccess, F&& aFunction); 65 66 // Similar to ErrnoTest, except that we want to compare a specific `errno` 67 // being returned. 68 template <typename F> 69 void ErrnoValueTest(const nsCString& aName, int aExpectedErrno, 70 F&& aFunction); 71 72 private: 73 explicit SandboxTestingChild(SandboxTestingThread* aThread, 74 Endpoint<PSandboxTestingChild>&& aEndpoint); 75 ~SandboxTestingChild(); 76 77 void Bind(Endpoint<PSandboxTestingChild>&& aEndpoint); 78 79 UniquePtr<SandboxTestingThread> mThread; 80 81 static StaticRefPtr<SandboxTestingChild> sInstance; 82 }; 83 84 } // namespace mozilla 85 86 #endif // mozilla_SandboxTestingChild_h