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