blob: ca45266cc2b3a7b82b68ce32663cc38f49c093c5 [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"
29#include "test_function_map.h"
30
31using namespace std::literals;
32
33using android::base::GetProperty;
34using android::base::Join;
35using android::base::SetProperty;
36using android::base::Split;
37using android::base::WaitForProperty;
38
39namespace android {
40namespace init {
41
Tom Cherrye6d37cd2017-10-19 14:15:37 -070042// I would use test fixtures, but I cannot skip the test if not root with them, so instead we have
43// this test runner.
44template <typename F>
45void RunTest(F&& test_function) {
46 if (getuid() != 0) {
47 GTEST_LOG_(INFO) << "Skipping test, must be run as root.";
48 return;
49 }
50
51 char* context;
52 ASSERT_EQ(0, getcon(&context));
53 auto context_string = std::string(context);
54 free(context);
55
56 auto subcontext = Subcontext("dummy_path", context_string);
57 ASSERT_NE(0, subcontext.pid());
58
59 test_function(subcontext, context_string);
60
61 if (subcontext.pid() > 0) {
62 kill(subcontext.pid(), SIGTERM);
63 kill(subcontext.pid(), SIGKILL);
64 }
65}
66
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070067TEST(subcontext, CheckDifferentPid) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070068 RunTest([](auto& subcontext, auto& context_string) {
69 auto result = subcontext.Execute(std::vector<std::string>{"return_pids_as_error"});
70 ASSERT_FALSE(result);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070071
Tom Cherrye6d37cd2017-10-19 14:15:37 -070072 auto pids = Split(result.error_string(), " ");
73 ASSERT_EQ(2U, pids.size());
74 auto our_pid = std::to_string(getpid());
75 EXPECT_NE(our_pid, pids[0]);
76 EXPECT_EQ(our_pid, pids[1]);
77 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070078}
79
80TEST(subcontext, SetProp) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -070081 RunTest([](auto& subcontext, auto& context_string) {
82 SetProperty("init.test.subcontext", "fail");
83 WaitForProperty("init.test.subcontext", "fail");
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070084
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070085 auto args = std::vector<std::string>{
Tom Cherrye6d37cd2017-10-19 14:15:37 -070086 "setprop",
87 "init.test.subcontext",
88 "success",
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070089 };
90 auto result = subcontext.Execute(args);
91 ASSERT_TRUE(result) << result.error();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070092
Tom Cherrye6d37cd2017-10-19 14:15:37 -070093 EXPECT_TRUE(WaitForProperty("init.test.subcontext", "success", 10s));
94 });
95}
96
97TEST(subcontext, MultipleCommands) {
98 RunTest([](auto& subcontext, auto& context_string) {
99 auto first_pid = subcontext.pid();
100
101 auto expected_words = std::vector<std::string>{
102 "this",
103 "is",
104 "a",
105 "test",
106 };
107
108 for (const auto& word : expected_words) {
109 auto args = std::vector<std::string>{
110 "add_word",
111 word,
112 };
113 auto result = subcontext.Execute(args);
114 ASSERT_TRUE(result) << result.error();
115 }
116
117 auto result = subcontext.Execute(std::vector<std::string>{"return_words_as_error"});
118 ASSERT_FALSE(result);
119 EXPECT_EQ(Join(expected_words, " "), result.error_string());
120 EXPECT_EQ(first_pid, subcontext.pid());
121 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700122}
123
124TEST(subcontext, RecoverAfterAbort) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700125 RunTest([](auto& subcontext, auto& context_string) {
126 auto first_pid = subcontext.pid();
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700127
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700128 auto result = subcontext.Execute(std::vector<std::string>{"cause_log_fatal"});
129 ASSERT_FALSE(result);
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700130
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700131 auto result2 = subcontext.Execute(std::vector<std::string>{"generate_sane_error"});
132 ASSERT_FALSE(result2);
133 EXPECT_EQ("Sane error!", result2.error_string());
134 EXPECT_NE(subcontext.pid(), first_pid);
135 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700136}
137
138TEST(subcontext, ContextString) {
Tom Cherrye6d37cd2017-10-19 14:15:37 -0700139 RunTest([](auto& subcontext, auto& context_string) {
140 auto result = subcontext.Execute(std::vector<std::string>{"return_context_as_error"});
141 ASSERT_FALSE(result);
142 ASSERT_EQ(context_string, result.error_string());
143 });
Tom Cherrycb0f9bb2017-09-12 15:58:47 -0700144}
145
146TestFunctionMap BuildTestFunctionMap() {
147 TestFunctionMap test_function_map;
148 // For CheckDifferentPid
149 test_function_map.Add("return_pids_as_error", 0, 0, true,
150 [](const BuiltinArguments& args) -> Result<Success> {
151 return Error() << getpid() << " " << getppid();
152 });
153
154 // For SetProp
155 test_function_map.Add("setprop", 2, 2, true, [](const BuiltinArguments& args) {
156 android::base::SetProperty(args[1], args[2]);
157 return Success();
158 });
159
160 // For MultipleCommands
161 // Using a shared_ptr to extend lifetime of words to both lambdas
162 auto words = std::make_shared<std::vector<std::string>>();
163 test_function_map.Add("add_word", 1, 1, true, [words](const BuiltinArguments& args) {
164 words->emplace_back(args[1]);
165 return Success();
166 });
167 test_function_map.Add("return_words_as_error", 0, 0, true,
168 [words](const BuiltinArguments& args) -> Result<Success> {
169 return Error() << Join(*words, " ");
170 });
171
172 // For RecoverAfterAbort
173 test_function_map.Add("cause_log_fatal", 0, 0, true,
174 [](const BuiltinArguments& args) -> Result<Success> {
175 return Error() << std::string(4097, 'f');
176 });
177 test_function_map.Add(
178 "generate_sane_error", 0, 0, true,
179 [](const BuiltinArguments& args) -> Result<Success> { return Error() << "Sane error!"; });
180
181 // For ContextString
182 test_function_map.Add(
183 "return_context_as_error", 0, 0, true,
184 [](const BuiltinArguments& args) -> Result<Success> { return Error() << args.context; });
185
186 return test_function_map;
187}
188
189} // namespace init
190} // namespace android
191
192int main(int argc, char** argv) {
193 if (argc > 1 && !strcmp(basename(argv[1]), "subcontext")) {
194 auto test_function_map = android::init::BuildTestFunctionMap();
195 return android::init::SubcontextMain(argc, argv, &test_function_map);
196 }
197
198 testing::InitGoogleTest(&argc, argv);
199 return RUN_ALL_TESTS();
200}