blob: c9cc7bdf0c190b69efa1e7ac1695c209189aafda [file] [log] [blame]
Tom Cherry7da54852017-05-01 14:16:41 -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 "service.h"
18
19#include <algorithm>
20#include <memory>
21#include <type_traits>
22#include <vector>
23
24#include <gtest/gtest.h>
25
Tom Cherry67dee622017-07-27 12:54:48 -070026#include "util.h"
27
Tom Cherry81f5d3e2017-06-22 12:53:17 -070028namespace android {
29namespace init {
30
Tom Cherry7da54852017-05-01 14:16:41 -070031TEST(service, pod_initialized) {
32 constexpr auto memory_size = sizeof(Service);
Tom Cherry247ffbf2019-07-08 15:09:36 -070033 alignas(alignof(Service)) unsigned char old_memory[memory_size];
Tom Cherry7da54852017-05-01 14:16:41 -070034
35 for (std::size_t i = 0; i < memory_size; ++i) {
36 old_memory[i] = 0xFF;
37 }
38
39 std::vector<std::string> dummy_args{"/bin/test"};
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070040 Service* service_in_old_memory =
41 new (old_memory) Service("test_old_memory", nullptr, dummy_args);
Tom Cherry7da54852017-05-01 14:16:41 -070042
43 EXPECT_EQ(0U, service_in_old_memory->flags());
44 EXPECT_EQ(0, service_in_old_memory->pid());
45 EXPECT_EQ(0, service_in_old_memory->crash_count());
46 EXPECT_EQ(0U, service_in_old_memory->uid());
47 EXPECT_EQ(0U, service_in_old_memory->gid());
Tom Cherry247ffbf2019-07-08 15:09:36 -070048 EXPECT_EQ(0, service_in_old_memory->namespace_flags());
Tom Cherry7da54852017-05-01 14:16:41 -070049 EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory->ioprio_class());
50 EXPECT_EQ(0, service_in_old_memory->ioprio_pri());
51 EXPECT_EQ(0, service_in_old_memory->priority());
52 EXPECT_EQ(-1000, service_in_old_memory->oom_score_adjust());
Tom Cherry33838b12017-05-04 11:32:36 -070053 EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
Tom Cherry7da54852017-05-01 14:16:41 -070054
55 for (std::size_t i = 0; i < memory_size; ++i) {
56 old_memory[i] = 0xFF;
57 }
58
Tom Cherrycb0f9bb2017-09-12 15:58:47 -070059 Service* service_in_old_memory2 = new (old_memory) Service(
Tom Cherry1cd082d2019-02-06 10:45:56 -080060 "test_old_memory", 0U, 0U, 0U, std::vector<gid_t>(), 0U, "", nullptr, dummy_args);
Tom Cherry7da54852017-05-01 14:16:41 -070061
62 EXPECT_EQ(0U, service_in_old_memory2->flags());
63 EXPECT_EQ(0, service_in_old_memory2->pid());
64 EXPECT_EQ(0, service_in_old_memory2->crash_count());
65 EXPECT_EQ(0U, service_in_old_memory2->uid());
66 EXPECT_EQ(0U, service_in_old_memory2->gid());
Tom Cherry247ffbf2019-07-08 15:09:36 -070067 EXPECT_EQ(0, service_in_old_memory2->namespace_flags());
Tom Cherry7da54852017-05-01 14:16:41 -070068 EXPECT_EQ(IoSchedClass_NONE, service_in_old_memory2->ioprio_class());
69 EXPECT_EQ(0, service_in_old_memory2->ioprio_pri());
70 EXPECT_EQ(0, service_in_old_memory2->priority());
71 EXPECT_EQ(-1000, service_in_old_memory2->oom_score_adjust());
Tom Cherry33838b12017-05-04 11:32:36 -070072 EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
Tom Cherry7da54852017-05-01 14:16:41 -070073}
Tom Cherry81f5d3e2017-06-22 12:53:17 -070074
Tom Cherry3b81f2d2017-07-28 14:48:41 -070075TEST(service, make_temporary_oneshot_service_invalid_syntax) {
Tom Cherry67dee622017-07-27 12:54:48 -070076 std::vector<std::string> args;
77 // Nothing.
Tom Cherry4772f1d2019-07-30 09:34:41 -070078 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
Tom Cherry67dee622017-07-27 12:54:48 -070079
80 // No arguments to 'exec'.
81 args.push_back("exec");
Tom Cherry4772f1d2019-07-30 09:34:41 -070082 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
Tom Cherry67dee622017-07-27 12:54:48 -070083
84 // No command in "exec --".
85 args.push_back("--");
Tom Cherry4772f1d2019-07-30 09:34:41 -070086 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
Tom Cherry67dee622017-07-27 12:54:48 -070087}
88
Tom Cherry3b81f2d2017-07-28 14:48:41 -070089TEST(service, make_temporary_oneshot_service_too_many_supplementary_gids) {
Tom Cherry67dee622017-07-27 12:54:48 -070090 std::vector<std::string> args;
91 args.push_back("exec");
92 args.push_back("seclabel");
93 args.push_back("root"); // uid.
94 args.push_back("root"); // gid.
95 for (int i = 0; i < NR_SVC_SUPP_GIDS; ++i) {
96 args.push_back("root"); // Supplementary gid.
97 }
98 args.push_back("--");
99 args.push_back("/system/bin/id");
Tom Cherry4772f1d2019-07-30 09:34:41 -0700100 ASSERT_FALSE(Service::MakeTemporaryOneshotService(args));
Tom Cherry67dee622017-07-27 12:54:48 -0700101}
102
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700103static void Test_make_temporary_oneshot_service(bool dash_dash, bool seclabel, bool uid, bool gid,
104 bool supplementary_gids) {
Tom Cherry67dee622017-07-27 12:54:48 -0700105 std::vector<std::string> args;
106 args.push_back("exec");
107 if (seclabel) {
108 args.push_back("u:r:su:s0"); // seclabel
109 if (uid) {
110 args.push_back("log"); // uid
111 if (gid) {
112 args.push_back("shell"); // gid
113 if (supplementary_gids) {
114 args.push_back("system"); // supplementary gid 0
115 args.push_back("adb"); // supplementary gid 1
116 }
117 }
118 }
119 }
120 if (dash_dash) {
121 args.push_back("--");
122 }
123 args.push_back("/system/bin/toybox");
124 args.push_back("id");
Tom Cherry4772f1d2019-07-30 09:34:41 -0700125 auto service_ret = Service::MakeTemporaryOneshotService(args);
126 ASSERT_TRUE(service_ret);
127 auto svc = std::move(*service_ret);
Tom Cherry67dee622017-07-27 12:54:48 -0700128
129 if (seclabel) {
130 ASSERT_EQ("u:r:su:s0", svc->seclabel());
131 } else {
132 ASSERT_EQ("", svc->seclabel());
133 }
134 if (uid) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700135 auto decoded_uid = DecodeUid("log");
136 ASSERT_TRUE(decoded_uid);
137 ASSERT_EQ(*decoded_uid, svc->uid());
Tom Cherry67dee622017-07-27 12:54:48 -0700138 } else {
139 ASSERT_EQ(0U, svc->uid());
140 }
141 if (gid) {
Tom Cherry11a3aee2017-08-03 12:54:07 -0700142 auto decoded_uid = DecodeUid("shell");
143 ASSERT_TRUE(decoded_uid);
144 ASSERT_EQ(*decoded_uid, svc->gid());
Tom Cherry67dee622017-07-27 12:54:48 -0700145 } else {
146 ASSERT_EQ(0U, svc->gid());
147 }
148 if (supplementary_gids) {
149 ASSERT_EQ(2U, svc->supp_gids().size());
Tom Cherry11a3aee2017-08-03 12:54:07 -0700150
151 auto decoded_uid = DecodeUid("system");
152 ASSERT_TRUE(decoded_uid);
153 ASSERT_EQ(*decoded_uid, svc->supp_gids()[0]);
154
155 decoded_uid = DecodeUid("adb");
156 ASSERT_TRUE(decoded_uid);
157 ASSERT_EQ(*decoded_uid, svc->supp_gids()[1]);
Tom Cherry67dee622017-07-27 12:54:48 -0700158 } else {
159 ASSERT_EQ(0U, svc->supp_gids().size());
160 }
161
162 ASSERT_EQ(static_cast<std::size_t>(2), svc->args().size());
163 ASSERT_EQ("/system/bin/toybox", svc->args()[0]);
164 ASSERT_EQ("id", svc->args()[1]);
165}
166
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700167TEST(service, make_temporary_oneshot_service_with_everything) {
168 Test_make_temporary_oneshot_service(true, true, true, true, true);
Tom Cherry67dee622017-07-27 12:54:48 -0700169}
170
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700171TEST(service, make_temporary_oneshot_service_with_seclabel_uid_gid) {
172 Test_make_temporary_oneshot_service(true, true, true, true, false);
Tom Cherry67dee622017-07-27 12:54:48 -0700173}
174
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700175TEST(service, make_temporary_oneshot_service_with_seclabel_uid) {
176 Test_make_temporary_oneshot_service(true, true, true, false, false);
Tom Cherry67dee622017-07-27 12:54:48 -0700177}
178
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700179TEST(service, make_temporary_oneshot_service_with_seclabel) {
180 Test_make_temporary_oneshot_service(true, true, false, false, false);
Tom Cherry67dee622017-07-27 12:54:48 -0700181}
182
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700183TEST(service, make_temporary_oneshot_service_with_just_command) {
184 Test_make_temporary_oneshot_service(true, false, false, false, false);
Tom Cherry67dee622017-07-27 12:54:48 -0700185}
186
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700187TEST(service, make_temporary_oneshot_service_with_just_command_no_dash) {
188 Test_make_temporary_oneshot_service(false, false, false, false, false);
Tom Cherry67dee622017-07-27 12:54:48 -0700189}
190
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700191} // namespace init
192} // namespace android