blob: dcbff82595ef00e03b349476ebc051ebf07f7dd4 [file] [log] [blame]
Tom Cherrycb0f9bb2017-09-12 15:58:47 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "subcontext.h"
18
19#include <unistd.h>
20
21#include <chrono>
22
23#include <android-base/properties.h>
24#include <android-base/strings.h>
25#include <gtest/gtest.h>
Tom Cherrye6d37cd2017-10-19 14:15:37 -070026#include <selinux/selinux.h>
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070027
28#include "builtin_arguments.h"
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070029
30using namespace std::literals;
31
32using android::base::GetProperty;
33using android::base::Join;
34using android::base::SetProperty;
35using android::base::Split;
36using android::base::WaitForProperty;
37
38namespace android {
39namespace init {
40
Tom Cherrye6d37cd2017-10-19 14:15:37 -070041// I would use test fixtures, but I cannot skip the test if not root with them, so instead we have
42// this test runner.
43template <typename F>
44void RunTest(F&& test_function) {
45 if (getuid() != 0) {
Tom Cherry17b2be02019-08-20 10:43:48 -070046 GTEST_SKIP() << "Skipping test, must be run as root.";
Tom Cherrye6d37cd2017-10-19 14:15:37 -070047 return;
48 }
49
50 char* context;
51 ASSERT_EQ(0, getcon(&context));
52 auto context_string = std::string(context);
53 free(context);
54
55 auto subcontext = Subcontext("dummy_path", context_string);
56 ASSERT_NE(0, subcontext.pid());
57
58 test_function(subcontext, context_string);
59
60 if (subcontext.pid() > 0) {
61 kill(subcontext.pid(), SIGTERM);
62 kill(subcontext.pid(), SIGKILL);
63 }
64}
65
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070066TEST(subcontext, CheckDifferentPid) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070067 RunTest([](auto& subcontext, auto& context_string) {
68 auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
69 ASSERT_FALSE(result);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070070
Jiyong Park8fd64c82019-05-31 03:43:34 +090071 auto pids = Split(result.error().message(), " ");
Tom Cherrye6d37cd2017-10-19 14:15:37 -070072 ASSERT_EQ(2U, pids.size());
73 auto our_pid = std::to_string(getpid());
74 EXPECT_NE(our_pid, pids[0]);
75 EXPECT_EQ(our_pid, pids[1]);
76 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070077}
78
79TEST(subcontext, SetProp) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070080 RunTest([](auto& subcontext, auto& context_string) {
81 SetProperty("init.test.subcontext", "fail");
82 WaitForProperty("init.test.subcontext", "fail");
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070083
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070084 auto args = std::vector<std::string>{
Tom Cherrye6d37cd2017-10-19 14:15:37 -070085 "setprop",
86 "init.test.subcontext",
87 "success",
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070088 };
89 auto result = subcontext.Execute(args);
90 ASSERT_TRUE(result) << result.error();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070091
Tom Cherrye6d37cd2017-10-19 14:15:37 -070092 EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
93 });
94}
95
96TEST(subcontext, MultipleCommands) {
97 RunTest([](auto& subcontext, auto& context_string) {
98 auto first_pid = subcontext.pid();
99
100 auto expected_words = std::vector<std::string>{
101 "this",
102 "is",
103 "a",
104 "test",
105 };
106
107 for (const auto& word : expected_words) {
108 auto args = std::vector<std::string>{
109 "add_word",
110 word,
111 };
112 auto result = subcontext.Execute(args);
113 ASSERT_TRUE(result) << result.error();
114 }
115
116 auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
117 ASSERT_FALSE(result);
Jiyong Park8fd64c82019-05-31 03:43:34 +0900118 EXPECT_EQ(Join(expected_words, " "), result.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700119 EXPECT_EQ(first_pid, subcontext.pid());
120 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700121}
122
123TEST(subcontext, RecoverAfterAbort) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700124 RunTest([](auto& subcontext, auto& context_string) {
125 auto first_pid = subcontext.pid();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700126
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700127 auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
128 ASSERT_FALSE(result);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700129
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700130 auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
131 ASSERT_FALSE(result2);
Jiyong Park8fd64c82019-05-31 03:43:34 +0900132 EXPECT_EQ("Sane error!", result2.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700133 EXPECT_NE(subcontext.pid(), first_pid);
134 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700135}
136
137TEST(subcontext, ContextString) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700138 RunTest([](auto& subcontext, auto& context_string) {
139 auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
140 ASSERT_FALSE(result);
Jiyong Park8fd64c82019-05-31 03:43:34 +0900141 ASSERT_EQ(context_string, result.error().message());
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700142 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700143}
144
Tom Cherryc49719f2018-01-10 11:04:34 -0800145TEST(subcontext, ExpandArgs) {
146 RunTest([](auto& subcontext, auto& context_string) {
147 auto args = std::vector<std::string>{
148 "first",
149 "${ro.hardware}",
150 "$$third",
151 };
152 auto result = subcontext.ExpandArgs(args);
153 ASSERT_TRUE(result) << result.error();
154 ASSERT_EQ(3U, result->size());
155 EXPECT_EQ(args[0], result->at(0));
156 EXPECT_EQ(GetProperty("ro.hardware", ""), result->at(1));
157 EXPECT_EQ("$third", result->at(2));
158 });
159}
160
161TEST(subcontext, ExpandArgsFailure) {
162 RunTest([](auto& subcontext, auto& context_string) {
163 auto args = std::vector<std::string>{
164 "first",
165 "${",
166 };
167 auto result = subcontext.ExpandArgs(args);
168 ASSERT_FALSE(result);
Tom Cherryc5cf85d2019-07-31 13:59:15 -0700169 EXPECT_EQ("unexpected end of string in '" + args[1] + "', looking for }",
170 result.error().message());
Tom Cherryc49719f2018-01-10 11:04:34 -0800171 });
172}
173
Tom Cherryd52a5b32019-07-22 16:05:36 -0700174BuiltinFunctionMap BuildTestFunctionMap() {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700175 // For CheckDifferentPid
Tom Cherryd52a5b32019-07-22 16:05:36 -0700176 auto do_return_pids_as_error = [](const BuiltinArguments& args) -> Result<void> {
177 return Error() << getpid() << " " << getppid();
178 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700179
180 // For SetProp
Tom Cherryd52a5b32019-07-22 16:05:36 -0700181 auto do_setprop = [](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700182 android::base::SetProperty(args[1], args[2]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700183 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700184 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700185
186 // For MultipleCommands
187 // Using a shared_ptr to extend lifetime of words to both lambdas
188 auto words = std::make_shared<std::vector<std::string>>();
Tom Cherryd52a5b32019-07-22 16:05:36 -0700189 auto do_add_word = [words](const BuiltinArguments& args) {
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700190 words->emplace_back(args[1]);
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700191 return Result<void>{};
Tom Cherryd52a5b32019-07-22 16:05:36 -0700192 };
193 auto do_return_words_as_error = [words](const BuiltinArguments& args) -> Result<void> {
194 return Error() << Join(*words, " ");
195 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700196
197 // For RecoverAfterAbort
Tom Cherryd52a5b32019-07-22 16:05:36 -0700198 auto do_cause_log_fatal = [](const BuiltinArguments& args) -> Result<void> {
199 return Error() << std::string(4097, 'f');
200 };
201 auto do_generate_sane_error = [](const BuiltinArguments& args) -> Result<void> {
202 return Error() << "Sane error!";
203 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700204
205 // For ContextString
Tom Cherryd52a5b32019-07-22 16:05:36 -0700206 auto do_return_context_as_error = [](const BuiltinArguments& args) -> Result<void> {
207 return Error() << args.context;
208 };
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700209
Tom Cherryd52a5b32019-07-22 16:05:36 -0700210 // clang-format off
211 BuiltinFunctionMap test_function_map = {
212 {"return_pids_as_error", {0, 0, {true, do_return_pids_as_error}}},
213 {"setprop", {2, 2, {true, do_setprop}}},
214 {"add_word", {1, 1, {true, do_add_word}}},
215 {"return_words_as_error", {0, 0, {true, do_return_words_as_error}}},
216 {"cause_log_fatal", {0, 0, {true, do_cause_log_fatal}}},
217 {"generate_sane_error", {0, 0, {true, do_generate_sane_error}}},
218 {"return_context_as_error", {0, 0, {true, do_return_context_as_error}}},
219 };
220 // clang-format on
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700221 return test_function_map;
222}
223
224} // namespace init
225} // namespace android
226
227int main(int argc, char** argv) {
228 if (argc > 1 && !strcmp(basename(argv[1]), "subcontext")) {
229 auto test_function_map = android::init::BuildTestFunctionMap();
230 return android::init::SubcontextMain(argc, argv, &test_function_map);
231 }
232
233 testing::InitGoogleTest(&argc, argv);
234 return RUN_ALL_TESTS();
235}