Merge "init: clean up subcontext_test"
am: 4e2a8e375e
Change-Id: I0ab7401d36d32c5e0df7dc68286117a1ac60d2f8
This commit is contained in:
commit
be96bdb09d
3 changed files with 103 additions and 81 deletions
|
|
@ -35,7 +35,7 @@ extern const std::string kVendorContext;
|
||||||
class Subcontext {
|
class Subcontext {
|
||||||
public:
|
public:
|
||||||
Subcontext(std::string path_prefix, std::string context)
|
Subcontext(std::string path_prefix, std::string context)
|
||||||
: path_prefix_(std::move(path_prefix)), context_(std::move(context)) {
|
: path_prefix_(std::move(path_prefix)), context_(std::move(context)), pid_(0) {
|
||||||
Fork();
|
Fork();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,21 +55,6 @@ class Subcontext {
|
||||||
android::base::unique_fd socket_;
|
android::base::unique_fd socket_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// For testing, to kill the subcontext after the test has completed.
|
|
||||||
class SubcontextKiller {
|
|
||||||
public:
|
|
||||||
SubcontextKiller(const Subcontext& subcontext) : subcontext_(subcontext) {}
|
|
||||||
~SubcontextKiller() {
|
|
||||||
if (subcontext_.pid() > 0) {
|
|
||||||
kill(subcontext_.pid(), SIGTERM);
|
|
||||||
kill(subcontext_.pid(), SIGKILL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
const Subcontext& subcontext_;
|
|
||||||
};
|
|
||||||
|
|
||||||
int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map);
|
int SubcontextMain(int argc, char** argv, const KeywordFunctionMap* function_map);
|
||||||
std::vector<Subcontext>* InitializeSubcontexts();
|
std::vector<Subcontext>* InitializeSubcontexts();
|
||||||
bool SubcontextChildReap(pid_t pid);
|
bool SubcontextChildReap(pid_t pid);
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
#include "subcontext.h"
|
#include "subcontext.h"
|
||||||
|
|
||||||
#include <benchmark/benchmark.h>
|
#include <benchmark/benchmark.h>
|
||||||
|
#include <selinux/selinux.h>
|
||||||
|
|
||||||
#include "test_function_map.h"
|
#include "test_function_map.h"
|
||||||
|
|
||||||
|
|
@ -24,12 +25,27 @@ namespace android {
|
||||||
namespace init {
|
namespace init {
|
||||||
|
|
||||||
static void BenchmarkSuccess(benchmark::State& state) {
|
static void BenchmarkSuccess(benchmark::State& state) {
|
||||||
auto subcontext = Subcontext("path", kVendorContext);
|
if (getuid() != 0) {
|
||||||
auto subcontext_killer = SubcontextKiller(subcontext);
|
state.SkipWithError("Skipping benchmark, must be run as root.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
char* context;
|
||||||
|
if (getcon(&context) != 0) {
|
||||||
|
state.SkipWithError("getcon() failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto subcontext = Subcontext("path", context);
|
||||||
|
free(context);
|
||||||
|
|
||||||
while (state.KeepRunning()) {
|
while (state.KeepRunning()) {
|
||||||
subcontext.Execute(std::vector<std::string>{"return_success"});
|
subcontext.Execute(std::vector<std::string>{"return_success"});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (subcontext.pid() > 0) {
|
||||||
|
kill(subcontext.pid(), SIGTERM);
|
||||||
|
kill(subcontext.pid(), SIGKILL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BENCHMARK(BenchmarkSuccess);
|
BENCHMARK(BenchmarkSuccess);
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
#include <android-base/properties.h>
|
#include <android-base/properties.h>
|
||||||
#include <android-base/strings.h>
|
#include <android-base/strings.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include <selinux/selinux.h>
|
||||||
|
|
||||||
#include "builtin_arguments.h"
|
#include "builtin_arguments.h"
|
||||||
#include "test_function_map.h"
|
#include "test_function_map.h"
|
||||||
|
|
@ -38,88 +39,108 @@ using android::base::WaitForProperty;
|
||||||
namespace android {
|
namespace android {
|
||||||
namespace init {
|
namespace init {
|
||||||
|
|
||||||
|
// I would use test fixtures, but I cannot skip the test if not root with them, so instead we have
|
||||||
|
// this test runner.
|
||||||
|
template <typename F>
|
||||||
|
void RunTest(F&& test_function) {
|
||||||
|
if (getuid() != 0) {
|
||||||
|
GTEST_LOG_(INFO) << "Skipping test, must be run as root.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* context;
|
||||||
|
ASSERT_EQ(0, getcon(&context));
|
||||||
|
auto context_string = std::string(context);
|
||||||
|
free(context);
|
||||||
|
|
||||||
|
auto subcontext = Subcontext("dummy_path", context_string);
|
||||||
|
ASSERT_NE(0, subcontext.pid());
|
||||||
|
|
||||||
|
test_function(subcontext, context_string);
|
||||||
|
|
||||||
|
if (subcontext.pid() > 0) {
|
||||||
|
kill(subcontext.pid(), SIGTERM);
|
||||||
|
kill(subcontext.pid(), SIGKILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST(subcontext, CheckDifferentPid) {
|
TEST(subcontext, CheckDifferentPid) {
|
||||||
auto subcontext = Subcontext("path", kVendorContext);
|
RunTest([](auto& subcontext, auto& context_string) {
|
||||||
auto subcontext_killer = SubcontextKiller(subcontext);
|
auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
|
||||||
|
ASSERT_FALSE(result);
|
||||||
|
|
||||||
auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
|
auto pids = Split(result.error_string(), " ");
|
||||||
ASSERT_FALSE(result);
|
ASSERT_EQ(2U, pids.size());
|
||||||
|
auto our_pid = std::to_string(getpid());
|
||||||
auto pids = Split(result.error_string(), " ");
|
EXPECT_NE(our_pid, pids[0]);
|
||||||
ASSERT_EQ(2U, pids.size());
|
EXPECT_EQ(our_pid, pids[1]);
|
||||||
auto our_pid = std::to_string(getpid());
|
});
|
||||||
EXPECT_NE(our_pid, pids[0]);
|
|
||||||
EXPECT_EQ(our_pid, pids[1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(subcontext, SetProp) {
|
TEST(subcontext, SetProp) {
|
||||||
auto subcontext = Subcontext("path", kVendorContext);
|
RunTest([](auto& subcontext, auto& context_string) {
|
||||||
auto subcontext_killer = SubcontextKiller(subcontext);
|
SetProperty("init.test.subcontext", "fail");
|
||||||
|
WaitForProperty("init.test.subcontext", "fail");
|
||||||
|
|
||||||
SetProperty("init.test.subcontext", "fail");
|
|
||||||
WaitForProperty("init.test.subcontext", "fail");
|
|
||||||
|
|
||||||
auto args = std::vector<std::string>{
|
|
||||||
"setprop",
|
|
||||||
"init.test.subcontext",
|
|
||||||
"success",
|
|
||||||
};
|
|
||||||
auto result = subcontext.Execute(args);
|
|
||||||
ASSERT_TRUE(result) << result.error();
|
|
||||||
|
|
||||||
EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(subcontext, MultipleCommands) {
|
|
||||||
auto subcontext = Subcontext("path", kVendorContext);
|
|
||||||
auto subcontext_killer = SubcontextKiller(subcontext);
|
|
||||||
|
|
||||||
auto first_pid = subcontext.pid();
|
|
||||||
|
|
||||||
auto expected_words = std::vector<std::string>{
|
|
||||||
"this",
|
|
||||||
"is",
|
|
||||||
"a",
|
|
||||||
"test",
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const auto& word : expected_words) {
|
|
||||||
auto args = std::vector<std::string>{
|
auto args = std::vector<std::string>{
|
||||||
"add_word",
|
"setprop",
|
||||||
word,
|
"init.test.subcontext",
|
||||||
|
"success",
|
||||||
};
|
};
|
||||||
auto result = subcontext.Execute(args);
|
auto result = subcontext.Execute(args);
|
||||||
ASSERT_TRUE(result) << result.error();
|
ASSERT_TRUE(result) << result.error();
|
||||||
}
|
|
||||||
|
|
||||||
auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
|
EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
|
||||||
ASSERT_FALSE(result);
|
});
|
||||||
EXPECT_EQ(Join(expected_words, " "), result.error_string());
|
}
|
||||||
EXPECT_EQ(first_pid, subcontext.pid());
|
|
||||||
|
TEST(subcontext, MultipleCommands) {
|
||||||
|
RunTest([](auto& subcontext, auto& context_string) {
|
||||||
|
auto first_pid = subcontext.pid();
|
||||||
|
|
||||||
|
auto expected_words = std::vector<std::string>{
|
||||||
|
"this",
|
||||||
|
"is",
|
||||||
|
"a",
|
||||||
|
"test",
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& word : expected_words) {
|
||||||
|
auto args = std::vector<std::string>{
|
||||||
|
"add_word",
|
||||||
|
word,
|
||||||
|
};
|
||||||
|
auto result = subcontext.Execute(args);
|
||||||
|
ASSERT_TRUE(result) << result.error();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
|
||||||
|
ASSERT_FALSE(result);
|
||||||
|
EXPECT_EQ(Join(expected_words, " "), result.error_string());
|
||||||
|
EXPECT_EQ(first_pid, subcontext.pid());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(subcontext, RecoverAfterAbort) {
|
TEST(subcontext, RecoverAfterAbort) {
|
||||||
auto subcontext = Subcontext("path", kVendorContext);
|
RunTest([](auto& subcontext, auto& context_string) {
|
||||||
auto subcontext_killer = SubcontextKiller(subcontext);
|
auto first_pid = subcontext.pid();
|
||||||
|
|
||||||
auto first_pid = subcontext.pid();
|
auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
|
||||||
|
ASSERT_FALSE(result);
|
||||||
|
|
||||||
auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
|
auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
|
||||||
ASSERT_FALSE(result);
|
ASSERT_FALSE(result2);
|
||||||
|
EXPECT_EQ("Sane error!", result2.error_string());
|
||||||
auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
|
EXPECT_NE(subcontext.pid(), first_pid);
|
||||||
ASSERT_FALSE(result2);
|
});
|
||||||
EXPECT_EQ("Sane error!", result2.error_string());
|
|
||||||
EXPECT_NE(subcontext.pid(), first_pid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(subcontext, ContextString) {
|
TEST(subcontext, ContextString) {
|
||||||
auto subcontext = Subcontext("path", kVendorContext);
|
RunTest([](auto& subcontext, auto& context_string) {
|
||||||
auto subcontext_killer = SubcontextKiller(subcontext);
|
auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
|
||||||
|
ASSERT_FALSE(result);
|
||||||
auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
|
ASSERT_EQ(context_string, result.error_string());
|
||||||
ASSERT_FALSE(result);
|
});
|
||||||
ASSERT_EQ(kVendorContext, result.error_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TestFunctionMap BuildTestFunctionMap() {
|
TestFunctionMap BuildTestFunctionMap() {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue