diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/syscalls/linux/mount.cc | 4 | ||||
-rw-r--r-- | test/util/capability_util.h | 13 | ||||
-rw-r--r-- | test/util/posix_error.h | 7 |
3 files changed, 19 insertions, 5 deletions
diff --git a/test/syscalls/linux/mount.cc b/test/syscalls/linux/mount.cc index cdc223d07..92d0fb05c 100644 --- a/test/syscalls/linux/mount.cc +++ b/test/syscalls/linux/mount.cc @@ -67,9 +67,7 @@ TEST(MountTest, MountInvalidTarget) { TEST(MountTest, MountPermDenied) { // Clear CAP_SYS_ADMIN. - if (ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))) { - EXPECT_NO_ERRNO(SetCapability(CAP_SYS_ADMIN, false)); - } + AutoCapability cap(CAP_SYS_ADMIN, false); // Linux expects a valid target before checking capability. auto const dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()); diff --git a/test/util/capability_util.h b/test/util/capability_util.h index a03bc7e05..f2c370125 100644 --- a/test/util/capability_util.h +++ b/test/util/capability_util.h @@ -99,14 +99,23 @@ PosixErrorOr<bool> CanCreateUserNamespace(); class AutoCapability { public: AutoCapability(int cap, bool set) : cap_(cap), set_(set) { - EXPECT_NO_ERRNO(SetCapability(cap_, set_)); + const bool has = EXPECT_NO_ERRNO_AND_VALUE(HaveCapability(cap)); + if (set != has) { + EXPECT_NO_ERRNO(SetCapability(cap_, set_)); + applied_ = true; + } } - ~AutoCapability() { EXPECT_NO_ERRNO(SetCapability(cap_, !set_)); } + ~AutoCapability() { + if (applied_) { + EXPECT_NO_ERRNO(SetCapability(cap_, !set_)); + } + } private: int cap_; bool set_; + bool applied_ = false; }; } // namespace testing diff --git a/test/util/posix_error.h b/test/util/posix_error.h index 27557ad44..9ca09b77c 100644 --- a/test/util/posix_error.h +++ b/test/util/posix_error.h @@ -438,6 +438,13 @@ IsPosixErrorOkAndHolds(InnerMatcher&& inner_matcher) { std::move(_expr_result).ValueOrDie(); \ }) +#define EXPECT_NO_ERRNO_AND_VALUE(expr) \ + ({ \ + auto _expr_result = (expr); \ + EXPECT_NO_ERRNO(_expr_result); \ + std::move(_expr_result).ValueOrDie(); \ + }) + } // namespace testing } // namespace gvisor |