2017-09-13 00:58:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "subcontext.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <gtest/gtest.h>
|
2017-10-19 23:15:37 +02:00
|
|
|
#include <selinux/selinux.h>
|
2017-09-13 00:58:47 +02:00
|
|
|
|
|
|
|
#include "builtin_arguments.h"
|
2019-11-13 01:21:20 +01:00
|
|
|
#include "util.h"
|
2017-09-13 00:58:47 +02:00
|
|
|
|
|
|
|
using namespace std::literals;
|
|
|
|
|
|
|
|
using android::base::GetProperty;
|
|
|
|
using android::base::Join;
|
|
|
|
using android::base::SetProperty;
|
|
|
|
using android::base::Split;
|
|
|
|
using android::base::WaitForProperty;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
template <typename F>
|
|
|
|
void RunTest(F&& test_function) {
|
2019-11-21 00:51:36 +01:00
|
|
|
auto subcontext = Subcontext({"dummy_path"}, kTestContext);
|
2017-10-19 23:15:37 +02:00
|
|
|
ASSERT_NE(0, subcontext.pid());
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2019-11-21 00:51:36 +01:00
|
|
|
test_function(subcontext);
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
if (subcontext.pid() > 0) {
|
|
|
|
kill(subcontext.pid(), SIGTERM);
|
|
|
|
kill(subcontext.pid(), SIGKILL);
|
|
|
|
}
|
2017-09-13 00:58:47 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
TEST(subcontext, CheckDifferentPid) {
|
2019-11-21 00:51:36 +01:00
|
|
|
RunTest([](auto& subcontext) {
|
2017-10-19 23:15:37 +02:00
|
|
|
auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(result.ok());
|
2017-10-19 23:15:37 +02:00
|
|
|
|
2019-05-30 20:43:34 +02:00
|
|
|
auto pids = Split(result.error().message(), " ");
|
2017-10-19 23:15:37 +02:00
|
|
|
ASSERT_EQ(2U, pids.size());
|
|
|
|
auto our_pid = std::to_string(getpid());
|
|
|
|
EXPECT_NE(our_pid, pids[0]);
|
|
|
|
EXPECT_EQ(our_pid, pids[1]);
|
|
|
|
});
|
|
|
|
}
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
TEST(subcontext, SetProp) {
|
2019-11-21 00:51:36 +01:00
|
|
|
if (getuid() != 0) {
|
|
|
|
GTEST_SKIP() << "Skipping test, must be run as root.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RunTest([](auto& subcontext) {
|
2017-10-19 23:15:37 +02:00
|
|
|
SetProperty("init.test.subcontext", "fail");
|
|
|
|
WaitForProperty("init.test.subcontext", "fail");
|
2017-09-13 00:58:47 +02:00
|
|
|
|
|
|
|
auto args = std::vector<std::string>{
|
2017-10-19 23:15:37 +02:00
|
|
|
"setprop",
|
|
|
|
"init.test.subcontext",
|
|
|
|
"success",
|
2017-09-13 00:58:47 +02:00
|
|
|
};
|
|
|
|
auto result = subcontext.Execute(args);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(result);
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
|
|
|
|
});
|
2017-09-13 00:58:47 +02:00
|
|
|
}
|
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
TEST(subcontext, MultipleCommands) {
|
2019-11-21 00:51:36 +01:00
|
|
|
RunTest([](auto& subcontext) {
|
2017-10-19 23:15:37 +02:00
|
|
|
auto first_pid = subcontext.pid();
|
|
|
|
|
|
|
|
auto expected_words = std::vector<std::string>{
|
|
|
|
"this",
|
|
|
|
"is",
|
|
|
|
"a",
|
|
|
|
"test",
|
|
|
|
};
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
for (const auto& word : expected_words) {
|
|
|
|
auto args = std::vector<std::string>{
|
|
|
|
"add_word",
|
|
|
|
word,
|
|
|
|
};
|
|
|
|
auto result = subcontext.Execute(args);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(result);
|
2017-10-19 23:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(result.ok());
|
2019-05-30 20:43:34 +02:00
|
|
|
EXPECT_EQ(Join(expected_words, " "), result.error().message());
|
2017-10-19 23:15:37 +02:00
|
|
|
EXPECT_EQ(first_pid, subcontext.pid());
|
|
|
|
});
|
|
|
|
}
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
TEST(subcontext, RecoverAfterAbort) {
|
2019-11-21 00:51:36 +01:00
|
|
|
RunTest([](auto& subcontext) {
|
2017-10-19 23:15:37 +02:00
|
|
|
auto first_pid = subcontext.pid();
|
2017-09-13 00:58:47 +02:00
|
|
|
|
2017-10-19 23:15:37 +02:00
|
|
|
auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(result.ok());
|
2017-10-19 23:15:37 +02:00
|
|
|
|
|
|
|
auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(result2.ok());
|
2019-05-30 20:43:34 +02:00
|
|
|
EXPECT_EQ("Sane error!", result2.error().message());
|
2017-10-19 23:15:37 +02:00
|
|
|
EXPECT_NE(subcontext.pid(), first_pid);
|
|
|
|
});
|
2017-09-13 00:58:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(subcontext, ContextString) {
|
2019-11-21 00:51:36 +01:00
|
|
|
RunTest([](auto& subcontext) {
|
2017-10-19 23:15:37 +02:00
|
|
|
auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(result.ok());
|
2019-11-21 00:51:36 +01:00
|
|
|
ASSERT_EQ(kTestContext, result.error().message());
|
2017-10-19 23:15:37 +02:00
|
|
|
});
|
2017-09-13 00:58:47 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 01:21:20 +01:00
|
|
|
TEST(subcontext, TriggerShutdown) {
|
|
|
|
static constexpr const char kTestShutdownCommand[] = "reboot,test-shutdown-command";
|
|
|
|
static std::string trigger_shutdown_command;
|
|
|
|
trigger_shutdown = [](const std::string& command) { trigger_shutdown_command = command; };
|
2019-11-21 00:51:36 +01:00
|
|
|
RunTest([](auto& subcontext) {
|
2019-11-13 01:21:20 +01:00
|
|
|
auto result = subcontext.Execute(
|
|
|
|
std::vector<std::string>{"trigger_shutdown", kTestShutdownCommand});
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(result);
|
2019-11-13 01:21:20 +01:00
|
|
|
});
|
|
|
|
EXPECT_EQ(kTestShutdownCommand, trigger_shutdown_command);
|
|
|
|
}
|
|
|
|
|
2018-01-10 20:04:34 +01:00
|
|
|
TEST(subcontext, ExpandArgs) {
|
2019-11-21 00:51:36 +01:00
|
|
|
RunTest([](auto& subcontext) {
|
2018-01-10 20:04:34 +01:00
|
|
|
auto args = std::vector<std::string>{
|
|
|
|
"first",
|
|
|
|
"${ro.hardware}",
|
|
|
|
"$$third",
|
|
|
|
};
|
|
|
|
auto result = subcontext.ExpandArgs(args);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_RESULT_OK(result);
|
2018-01-10 20:04:34 +01:00
|
|
|
ASSERT_EQ(3U, result->size());
|
|
|
|
EXPECT_EQ(args[0], result->at(0));
|
|
|
|
EXPECT_EQ(GetProperty("ro.hardware", ""), result->at(1));
|
|
|
|
EXPECT_EQ("$third", result->at(2));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(subcontext, ExpandArgsFailure) {
|
2019-11-21 00:51:36 +01:00
|
|
|
RunTest([](auto& subcontext) {
|
2018-01-10 20:04:34 +01:00
|
|
|
auto args = std::vector<std::string>{
|
|
|
|
"first",
|
|
|
|
"${",
|
|
|
|
};
|
|
|
|
auto result = subcontext.ExpandArgs(args);
|
2020-02-05 19:49:33 +01:00
|
|
|
ASSERT_FALSE(result.ok());
|
2019-07-31 22:59:15 +02:00
|
|
|
EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }",
|
|
|
|
result.error().message());
|
2018-01-10 20:04:34 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
BuiltinFunctionMap BuildTestFunctionMap() {
|
2017-09-13 00:58:47 +02:00
|
|
|
// For CheckDifferentPid
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
|
|
|
|
return Error() << getpid() << " " << getppid();
|
|
|
|
};
|
2017-09-13 00:58:47 +02:00
|
|
|
|
|
|
|
// For SetProp
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_setprop = [](const BuiltinArguments& args) {
|
2017-09-13 00:58:47 +02:00
|
|
|
android::base::SetProperty(args[1], args[2]);
|
2019-06-10 20:08:01 +02:00
|
|
|
return Result<void>{};
|
2019-07-23 01:05:36 +02:00
|
|
|
};
|
2017-09-13 00:58:47 +02:00
|
|
|
|
|
|
|
// For MultipleCommands
|
|
|
|
// Using a shared_ptr to extend lifetime of words to both lambdas
|
|
|
|
auto words = std::make_shared<std::vector<std::string>>();
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_add_word = [words](const BuiltinArguments& args) {
|
2017-09-13 00:58:47 +02:00
|
|
|
words->emplace_back(args[1]);
|
2019-06-10 20:08:01 +02:00
|
|
|
return Result<void>{};
|
2019-07-23 01:05:36 +02:00
|
|
|
};
|
|
|
|
auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
|
|
|
|
return Error() << Join(*words, " ");
|
|
|
|
};
|
2017-09-13 00:58:47 +02:00
|
|
|
|
|
|
|
// For RecoverAfterAbort
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
|
2020-12-15 15:38:55 +01:00
|
|
|
// Since this is an expected failure, disable debuggerd to not generate a tombstone.
|
|
|
|
signal(SIGABRT, SIG_DFL);
|
2019-07-23 01:05:36 +02:00
|
|
|
return Error() << std::string(4097, 'f');
|
|
|
|
};
|
|
|
|
auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
|
|
|
|
return Error() << "Sane error!";
|
|
|
|
};
|
2017-09-13 00:58:47 +02:00
|
|
|
|
|
|
|
// For ContextString
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
|
|
|
|
return Error() << args.context;
|
|
|
|
};
|
|
|
|
|
2019-11-13 01:21:20 +01:00
|
|
|
auto do_trigger_shutdown = [](const BuiltinArguments& args) -> Result<void> {
|
|
|
|
trigger_shutdown(args[1]);
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
// clang-format off
|
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}},
|
|
|
|
{"setprop", {2, 2, {true, do_setprop}}},
|
|
|
|
{"add_word", {1, 1, {true, do_add_word}}},
|
|
|
|
{"return_words_as_error", {0, 0, {true, do_return_words_as_error}}},
|
|
|
|
{"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}},
|
|
|
|
{"generate_sane_error", {0, 0, {true, do_generate_sane_error}}},
|
|
|
|
{"return_context_as_error", {0, 0, {true, do_return_context_as_error}}},
|
2019-11-13 01:21:20 +01:00
|
|
|
{"trigger_shutdown", {1, 1, {true, do_trigger_shutdown}}},
|
2019-07-23 01:05:36 +02:00
|
|
|
};
|
|
|
|
// clang-format on
|
2017-09-13 00:58:47 +02:00
|
|
|
return test_function_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|
|
|
|
|
2019-08-08 01:02:28 +02:00
|
|
|
// init_test.cpp contains the main entry point for all init tests.
|
|
|
|
int SubcontextTestChildMain(int argc, char** argv) {
|
|
|
|
auto test_function_map = android::init::BuildTestFunctionMap();
|
|
|
|
return android::init::SubcontextMain(argc, argv, &test_function_map);
|
2017-09-13 00:58:47 +02:00
|
|
|
}
|