blob: 52aaa37fe054a0cd485e435e89166d9d135f698b [file] [log] [blame]
Elliott Hughes8d82ea02015-02-06 20:15:18 -08001/*
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"
Tom Cherrybac32992015-07-31 12:45:25 -070020#include "service.h"
Elliott Hughes8d82ea02015-02-06 20:15:18 -080021#include "util.h"
22
23#include <errno.h>
24#include <gtest/gtest.h>
25
Tom Cherrybac32992015-07-31 12:45:25 -070026#include <string>
27#include <vector>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080028
Tom Cherrybac32992015-07-31 12:45:25 -070029TEST(init_parser, make_exec_oneshot_service_invalid_syntax) {
30 ServiceManager& sm = ServiceManager::GetInstance();
31 std::vector<std::string> args;
Elliott Hughes8d82ea02015-02-06 20:15:18 -080032 // Nothing.
Tom Cherrybac32992015-07-31 12:45:25 -070033 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
Elliott Hughes8d82ea02015-02-06 20:15:18 -080034
35 // No arguments to 'exec'.
Tom Cherrybac32992015-07-31 12:45:25 -070036 args.push_back("exec");
37 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
Elliott Hughes8d82ea02015-02-06 20:15:18 -080038
39 // No command in "exec --".
Tom Cherrybac32992015-07-31 12:45:25 -070040 args.push_back("--");
41 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
Elliott Hughes8d82ea02015-02-06 20:15:18 -080042}
43
44TEST(init_parser, make_exec_oneshot_service_too_many_supplementary_gids) {
Tom Cherrybac32992015-07-31 12:45:25 -070045 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.
Elliott Hughes8d82ea02015-02-06 20:15:18 -080051 for (int i = 0; i < NR_SVC_SUPP_GIDS; ++i) {
Tom Cherrybac32992015-07-31 12:45:25 -070052 args.push_back("root"); // Supplementary gid.
Elliott Hughes8d82ea02015-02-06 20:15:18 -080053 }
Tom Cherrybac32992015-07-31 12:45:25 -070054 args.push_back("--");
55 args.push_back("/system/bin/id");
56 ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
Elliott Hughes8d82ea02015-02-06 20:15:18 -080057}
58
Tom Cherrybac32992015-07-31 12:45:25 -070059static 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");
Elliott Hughes8d82ea02015-02-06 20:15:18 -080064 if (seclabel) {
Tom Cherrybac32992015-07-31 12:45:25 -070065 args.push_back("u:r:su:s0"); // seclabel
Elliott Hughes8d82ea02015-02-06 20:15:18 -080066 if (uid) {
Tom Cherrybac32992015-07-31 12:45:25 -070067 args.push_back("log"); // uid
Elliott Hughes8d82ea02015-02-06 20:15:18 -080068 if (gid) {
Tom Cherrybac32992015-07-31 12:45:25 -070069 args.push_back("shell"); // gid
Elliott Hughes8d82ea02015-02-06 20:15:18 -080070 if (supplementary_gids) {
Tom Cherrybac32992015-07-31 12:45:25 -070071 args.push_back("system"); // supplementary gid 0
72 args.push_back("adb"); // supplementary gid 1
Elliott Hughes8d82ea02015-02-06 20:15:18 -080073 }
74 }
75 }
76 }
77 if (dash_dash) {
Tom Cherrybac32992015-07-31 12:45:25 -070078 args.push_back("--");
Elliott Hughes8d82ea02015-02-06 20:15:18 -080079 }
Tom Cherrybac32992015-07-31 12:45:25 -070080 args.push_back("/system/bin/toybox");
81 args.push_back("id");
82 Service* svc = sm.MakeExecOneshotService(args);
Elliott Hughes8d82ea02015-02-06 20:15:18 -080083 ASSERT_NE(nullptr, svc);
84
85 if (seclabel) {
Tom Cherrybac32992015-07-31 12:45:25 -070086 ASSERT_EQ("u:r:su:s0", svc->seclabel());
Elliott Hughes8d82ea02015-02-06 20:15:18 -080087 } else {
Tom Cherrybac32992015-07-31 12:45:25 -070088 ASSERT_EQ("", svc->seclabel());
Elliott Hughes8d82ea02015-02-06 20:15:18 -080089 }
90 if (uid) {
Tom Cherrybac32992015-07-31 12:45:25 -070091 ASSERT_EQ(decode_uid("log"), svc->uid());
Elliott Hughes8d82ea02015-02-06 20:15:18 -080092 } else {
Tom Cherrybac32992015-07-31 12:45:25 -070093 ASSERT_EQ(0U, svc->uid());
Elliott Hughes8d82ea02015-02-06 20:15:18 -080094 }
95 if (gid) {
Tom Cherrybac32992015-07-31 12:45:25 -070096 ASSERT_EQ(decode_uid("shell"), svc->gid());
Elliott Hughes8d82ea02015-02-06 20:15:18 -080097 } else {
Tom Cherrybac32992015-07-31 12:45:25 -070098 ASSERT_EQ(0U, svc->gid());
Elliott Hughes8d82ea02015-02-06 20:15:18 -080099 }
100 if (supplementary_gids) {
Tom Cherrybac32992015-07-31 12:45:25 -0700101 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]);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800104 } else {
Tom Cherrybac32992015-07-31 12:45:25 -0700105 ASSERT_EQ(0U, svc->supp_gids().size());
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800106 }
107
Tom Cherrybac32992015-07-31 12:45:25 -0700108 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]);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800111}
112
113TEST(init_parser, make_exec_oneshot_service_with_everything) {
114 Test_make_exec_oneshot_service(true, true, true, true, true);
115}
116
117TEST(init_parser, make_exec_oneshot_service_with_seclabel_uid_gid) {
118 Test_make_exec_oneshot_service(true, true, true, true, false);
119}
120
121TEST(init_parser, make_exec_oneshot_service_with_seclabel_uid) {
122 Test_make_exec_oneshot_service(true, true, true, false, false);
123}
124
125TEST(init_parser, make_exec_oneshot_service_with_seclabel) {
126 Test_make_exec_oneshot_service(true, true, false, false, false);
127}
128
129TEST(init_parser, make_exec_oneshot_service_with_just_command) {
130 Test_make_exec_oneshot_service(true, false, false, false, false);
131}
132
133TEST(init_parser, make_exec_oneshot_service_with_just_command_no_dash) {
134 Test_make_exec_oneshot_service(false, false, false, false, false);
135}