diff options
author | Fabricio Voznika <fvoznika@google.com> | 2021-01-29 13:54:34 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-01-29 13:56:17 -0800 |
commit | fdbfd447a02e52296f48a5cb1020030756ed8da6 (patch) | |
tree | 47735f8082f17e98118a268502a5fc7b72b99c11 /test/util | |
parent | 5e2edfb8726ddb255a02352e2f68ea028f543e4b (diff) |
Remove side effect from test cases
Individual test cases must not rely on being executed in a clean environment.
PiperOrigin-RevId: 354604389
Diffstat (limited to 'test/util')
-rw-r--r-- | test/util/capability_util.h | 13 | ||||
-rw-r--r-- | test/util/logging.h | 15 |
2 files changed, 28 insertions, 0 deletions
diff --git a/test/util/capability_util.h b/test/util/capability_util.h index bb9ea1fe5..a03bc7e05 100644 --- a/test/util/capability_util.h +++ b/test/util/capability_util.h @@ -96,6 +96,19 @@ inline PosixError DropPermittedCapability(int cap) { PosixErrorOr<bool> CanCreateUserNamespace(); +class AutoCapability { + public: + AutoCapability(int cap, bool set) : cap_(cap), set_(set) { + EXPECT_NO_ERRNO(SetCapability(cap_, set_)); + } + + ~AutoCapability() { EXPECT_NO_ERRNO(SetCapability(cap_, !set_)); } + + private: + int cap_; + bool set_; +}; + } // namespace testing } // namespace gvisor #endif // GVISOR_TEST_UTIL_CAPABILITY_UTIL_H_ diff --git a/test/util/logging.h b/test/util/logging.h index 9d224ea05..5c17f1233 100644 --- a/test/util/logging.h +++ b/test/util/logging.h @@ -96,6 +96,21 @@ void CheckFailure(const char* cond, size_t cond_size, const char* msg, std::move(_expr_result).ValueOrDie(); \ }) +// cond must be greater or equal than 0. Used to test result of syscalls. +// +// This macro is async-signal-safe. +#define TEST_CHECK_SUCCESS(cond) TEST_PCHECK((cond) >= 0) + +// cond must be -1 and errno must match errno_value. Used to test errors from +// syscalls. +// +// This macro is async-signal-safe. +#define TEST_CHECK_ERRNO(cond, errno_value) \ + do { \ + TEST_PCHECK((cond) == -1); \ + TEST_PCHECK_MSG(errno == (errno_value), #cond " expected " #errno_value); \ + } while (0) + } // namespace testing } // namespace gvisor |