using ::android::base::Result; using ::testing::_; using ::testing::AllOf; using ::testing::Gt; using ::testing::HasSubstr; using ::testing::InSequence; using ::testing::MockFunction; using ::testing::Ne; using ::testing::Return;
// A workaround to avoid MOCK_METHOD on a method with an `std::string*` parameter, which will lead // to a conflict between gmock and android-base/logging.h (b/132668253).
std::optional<int64_t> GetUptimeMs(std::string* error_msg) const override {
Result<int64_t> result = DoGetUptimeMs(); if (result.ok()) { return *result;
}
*error_msg = result.error().message(); return std::nullopt;
}
};
class AlwaysFallbackExecUtils : public TestingExecUtils { protected:
android::base::unique_fd PidfdOpen(pid_t) const override { return android::base::unique_fd(-1); }
};
class ExecUtilsTest : public CommonRuntimeTest, public ::testing::WithParamInterface<bool> { protected: void SetUp() override {
CommonRuntimeTest::SetUp(); bool always_fallback = GetParam(); if (always_fallback) {
exec_utils_ = std::make_unique<AlwaysFallbackExecUtils>();
} else { if (GetKernelVersion() >= std::make_tuple(5, 4)) {
exec_utils_ = std::make_unique<NeverFallbackExecUtils>();
} else {
GTEST_SKIP() << "Kernel version older than 5.4";
}
}
}
std::unique_ptr<TestingExecUtils> exec_utils_;
};
TEST_P(ExecUtilsTest, ExecSuccess) {
std::vector<std::string> command;
command.push_back(GetBin("id"));
std::string error_msg; // Historical note: Running on Valgrind failed due to some memory // that leaks in thread alternate signal stacks.
EXPECT_TRUE(exec_utils_->Exec(command, &error_msg));
EXPECT_EQ(0U, error_msg.size()) << error_msg;
}
TEST_P(ExecUtilsTest, ExecError) {
std::vector<std::string> command;
command.push_back("bogus");
std::string error_msg; // Historical note: Running on Valgrind failed due to some memory // that leaks in thread alternate signal stacks.
ExecResult result = exec_utils_->ExecAndReturnResult(command, /*timeout_sec=*/-1, &error_msg);
EXPECT_EQ(result.status, ExecResult::kSignaled);
EXPECT_EQ(result.signal, SIGABRT);
EXPECT_FALSE(error_msg.empty());
}
TEST_P(ExecUtilsTest, EnvSnapshotAdditionsAreNotVisible) { static constexpr constchar* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS"; static constexpr int kOverwrite = 1; // Set an variable in the current environment.
EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0); // Test that it is not exported.
std::vector<std::string> command;
command.push_back(GetBin("printenv"));
command.push_back(kModifiedVariable);
std::string error_msg; // Historical note: Running on Valgrind failed due to some memory // that leaks in thread alternate signal stacks.
EXPECT_FALSE(exec_utils_->Exec(command, &error_msg));
EXPECT_NE(0U, error_msg.size()) << error_msg;
}
TEST_P(ExecUtilsTest, EnvSnapshotDeletionsAreNotVisible) { static constexpr constchar* kDeletedVariable = "PATH"; static constexpr int kOverwrite = 1; // Save the variable's value. constchar* save_value = getenv(kDeletedVariable);
EXPECT_NE(save_value, nullptr); // Delete the variable.
EXPECT_EQ(unsetenv(kDeletedVariable), 0); // Test that it is not exported.
std::vector<std::string> command;
command.push_back(GetBin("printenv"));
command.push_back(kDeletedVariable);
std::string error_msg; // Historical note: Running on Valgrind failed due to some memory // that leaks in thread alternate signal stacks.
EXPECT_TRUE(exec_utils_->Exec(command, &error_msg));
EXPECT_EQ(0U, error_msg.size()) << error_msg; // Restore the variable's value.
EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
}
// This will always time out.
ASSERT_EQ(exec_utils_
->ExecAndReturnResult(command, /*timeout_sec=*/1,
ExecCallbacks(), /*new_process_group=*/false,
&stat,
&error_msg)
.status,
ExecResult::kTimedOut);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.