1 /*
2 * Copyright (C) 2015 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 "init_parser.h"
18
19 #include "init.h"
20 #include "service.h"
21 #include "util.h"
22
23 #include <errno.h>
24 #include <gtest/gtest.h>
25
26 #include <string>
27 #include <vector>
28
TEST(init_parser,make_exec_oneshot_service_invalid_syntax)29 TEST(init_parser, make_exec_oneshot_service_invalid_syntax) {
30 ServiceManager& sm = ServiceManager::GetInstance();
31 std::vector<std::string> args;
32 // Nothing.
33 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
34
35 // No arguments to 'exec'.
36 args.push_back("exec");
37 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
38
39 // No command in "exec --".
40 args.push_back("--");
41 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
42 }
43
TEST(init_parser,make_exec_oneshot_service_too_many_supplementary_gids)44 TEST(init_parser, make_exec_oneshot_service_too_many_supplementary_gids) {
45 ServiceManager& sm = ServiceManager::GetInstance();
46 std::vector<std::string> args;
47 args.push_back("exec");
48 args.push_back("seclabel");
49 args.push_back("root"); // uid.
50 args.push_back("root"); // gid.
51 for (int i = 0; i < NR_SVC_SUPP_GIDS; ++i) {
52 args.push_back("root"); // Supplementary gid.
53 }
54 args.push_back("--");
55 args.push_back("/system/bin/id");
56 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
57 }
58
Test_make_exec_oneshot_service(bool dash_dash,bool seclabel,bool uid,bool gid,bool supplementary_gids)59 static void Test_make_exec_oneshot_service(bool dash_dash, bool seclabel, bool uid,
60 bool gid, bool supplementary_gids) {
61 ServiceManager& sm = ServiceManager::GetInstance();
62 std::vector<std::string> args;
63 args.push_back("exec");
64 if (seclabel) {
65 args.push_back("u:r:su:s0"); // seclabel
66 if (uid) {
67 args.push_back("log"); // uid
68 if (gid) {
69 args.push_back("shell"); // gid
70 if (supplementary_gids) {
71 args.push_back("system"); // supplementary gid 0
72 args.push_back("adb"); // supplementary gid 1
73 }
74 }
75 }
76 }
77 if (dash_dash) {
78 args.push_back("--");
79 }
80 args.push_back("/system/bin/toybox");
81 args.push_back("id");
82 Service* svc = sm.MakeExecOneshotService(args);
83 ASSERT_NE(nullptr, svc);
84
85 if (seclabel) {
86 ASSERT_EQ("u:r:su:s0", svc->seclabel());
87 } else {
88 ASSERT_EQ("", svc->seclabel());
89 }
90 if (uid) {
91 ASSERT_EQ(decode_uid("log"), svc->uid());
92 } else {
93 ASSERT_EQ(0U, svc->uid());
94 }
95 if (gid) {
96 ASSERT_EQ(decode_uid("shell"), svc->gid());
97 } else {
98 ASSERT_EQ(0U, svc->gid());
99 }
100 if (supplementary_gids) {
101 ASSERT_EQ(2U, svc->supp_gids().size());
102 ASSERT_EQ(decode_uid("system"), svc->supp_gids()[0]);
103 ASSERT_EQ(decode_uid("adb"), svc->supp_gids()[1]);
104 } else {
105 ASSERT_EQ(0U, svc->supp_gids().size());
106 }
107
108 ASSERT_EQ(static_cast<std::size_t>(2), svc->args().size());
109 ASSERT_EQ("/system/bin/toybox", svc->args()[0]);
110 ASSERT_EQ("id", svc->args()[1]);
111 }
112
TEST(init_parser,make_exec_oneshot_service_with_everything)113 TEST(init_parser, make_exec_oneshot_service_with_everything) {
114 Test_make_exec_oneshot_service(true, true, true, true, true);
115 }
116
TEST(init_parser,make_exec_oneshot_service_with_seclabel_uid_gid)117 TEST(init_parser, make_exec_oneshot_service_with_seclabel_uid_gid) {
118 Test_make_exec_oneshot_service(true, true, true, true, false);
119 }
120
TEST(init_parser,make_exec_oneshot_service_with_seclabel_uid)121 TEST(init_parser, make_exec_oneshot_service_with_seclabel_uid) {
122 Test_make_exec_oneshot_service(true, true, true, false, false);
123 }
124
TEST(init_parser,make_exec_oneshot_service_with_seclabel)125 TEST(init_parser, make_exec_oneshot_service_with_seclabel) {
126 Test_make_exec_oneshot_service(true, true, false, false, false);
127 }
128
TEST(init_parser,make_exec_oneshot_service_with_just_command)129 TEST(init_parser, make_exec_oneshot_service_with_just_command) {
130 Test_make_exec_oneshot_service(true, false, false, false, false);
131 }
132
TEST(init_parser,make_exec_oneshot_service_with_just_command_no_dash)133 TEST(init_parser, make_exec_oneshot_service_with_just_command_no_dash) {
134 Test_make_exec_oneshot_service(false, false, false, false, false);
135 }
136