Skip to content

Commit

Permalink
Add test for Notifier::EventFinished
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 705400675
Change-Id: I087556376f30c0b8d7070afa3f0b32f0e3e3b7ed
  • Loading branch information
happyCoder92 authored and copybara-github committed Dec 12, 2024
1 parent 56220f8 commit 19e0490
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions sandboxed_api/sandbox2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ cc_test(
srcs = ["notify_test.cc"],
copts = sapi_platform_copts(),
data = [
"@com_google_sandboxed_api//sandboxed_api/sandbox2/testcases:minimal",
"@com_google_sandboxed_api//sandboxed_api/sandbox2/testcases:personality",
"@com_google_sandboxed_api//sandboxed_api/sandbox2/testcases:pidcomms",
],
Expand Down
1 change: 1 addition & 0 deletions sandboxed_api/sandbox2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ if(BUILD_TESTING AND SAPI_BUILD_TESTING)
OUTPUT_NAME notify_test
)
add_dependencies(sandbox2_notify_test
sandbox2::testcase_minimal
sandbox2::testcase_personality
sandbox2::testcase_pidcomms
)
Expand Down
32 changes: 32 additions & 0 deletions sandboxed_api/sandbox2/notify_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ class PidCommsNotify : public Notify {
}
};

class FinishedNotify : public Notify {
public:
bool IsFinished() { return finished_; }
bool EventStarted(pid_t pid, Comms* comms) override {
EXPECT_FALSE(finished_);
return true;
}
void EventFinished(const Result& result) override { finished_ = true; }

private:
bool finished_ = false;
};

class NotifyTest : public ::testing::TestWithParam<bool> {
public:
// Allow typical syscalls and call SECCOMP_RET_TRACE for personality syscall,
Expand Down Expand Up @@ -144,6 +157,25 @@ TEST_P(NotifyTest, PrintPidAndComms) {
EXPECT_THAT(result.reason_code(), Eq(33));
}

// Test EventFinished by exchanging data after started but before sandboxed.
TEST_P(NotifyTest, EventFinished) {
const std::string path = GetTestSourcePath("sandbox2/testcases/minimal");
std::vector<std::string> args = {path};
auto executor = std::make_unique<Executor>(path, args);

auto notify = std::make_unique<FinishedNotify>();
FinishedNotify* notify_ptr = notify.get();
Sandbox2 s2(std::move(executor), NotifyTestcasePolicy(path),
std::move(notify));
ASSERT_THAT(SetUpSandbox(&s2), IsOk());
EXPECT_FALSE(notify_ptr->IsFinished());
auto result = s2.Run();
EXPECT_TRUE(notify_ptr->IsFinished());

ASSERT_THAT(result.final_status(), Eq(Result::OK));
EXPECT_THAT(result.reason_code(), Eq(0));
}

// Test EventSyscallTrap on personality syscall through TraceAllSyscalls
TEST_P(NotifyTest, TraceAllAllowPersonality) {
const std::string path = GetTestSourcePath("sandbox2/testcases/personality");
Expand Down

0 comments on commit 19e0490

Please sign in to comment.