Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 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" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 20 | #include "service.h" |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 21 | #include "util.h" |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 28 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 29 | TEST(init_parser, make_exec_oneshot_service_invalid_syntax) { |
| 30 | ServiceManager& sm = ServiceManager::GetInstance(); |
| 31 | std::vector<std::string> args; |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 32 | // Nothing. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 33 | ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args)); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 34 | |
| 35 | // No arguments to 'exec'. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 36 | args.push_back("exec"); |
| 37 | ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args)); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 38 | |
| 39 | // No command in "exec --". |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 40 | args.push_back("--"); |
| 41 | ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args)); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(init_parser, make_exec_oneshot_service_too_many_supplementary_gids) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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. |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 51 | for (int i = 0; i < NR_SVC_SUPP_GIDS; ++i) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 52 | args.push_back("root"); // Supplementary gid. |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 53 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 54 | args.push_back("--"); |
| 55 | args.push_back("/system/bin/id"); |
| 56 | ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args)); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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"); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 64 | if (seclabel) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 65 | args.push_back("u:r:su:s0"); // seclabel |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 66 | if (uid) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 67 | args.push_back("log"); // uid |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 68 | if (gid) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 69 | args.push_back("shell"); // gid |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 70 | if (supplementary_gids) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 71 | args.push_back("system"); // supplementary gid 0 |
| 72 | args.push_back("adb"); // supplementary gid 1 |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if (dash_dash) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 78 | args.push_back("--"); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 79 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 80 | args.push_back("/system/bin/toybox"); |
| 81 | args.push_back("id"); |
| 82 | Service* svc = sm.MakeExecOneshotService(args); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 83 | ASSERT_NE(nullptr, svc); |
| 84 | |
| 85 | if (seclabel) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 86 | ASSERT_EQ("u:r:su:s0", svc->seclabel()); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 87 | } else { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 88 | ASSERT_EQ("", svc->seclabel()); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 89 | } |
| 90 | if (uid) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 91 | ASSERT_EQ(decode_uid("log"), svc->uid()); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 92 | } else { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 93 | ASSERT_EQ(0U, svc->uid()); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 94 | } |
| 95 | if (gid) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 96 | ASSERT_EQ(decode_uid("shell"), svc->gid()); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 97 | } else { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 98 | ASSERT_EQ(0U, svc->gid()); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 99 | } |
| 100 | if (supplementary_gids) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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]); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 104 | } else { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 105 | ASSERT_EQ(0U, svc->supp_gids().size()); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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]); |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST(init_parser, make_exec_oneshot_service_with_everything) { |
| 114 | Test_make_exec_oneshot_service(true, true, true, true, true); |
| 115 | } |
| 116 | |
| 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 | |
| 121 | TEST(init_parser, make_exec_oneshot_service_with_seclabel_uid) { |
| 122 | Test_make_exec_oneshot_service(true, true, true, false, false); |
| 123 | } |
| 124 | |
| 125 | TEST(init_parser, make_exec_oneshot_service_with_seclabel) { |
| 126 | Test_make_exec_oneshot_service(true, true, false, false, false); |
| 127 | } |
| 128 | |
| 129 | TEST(init_parser, make_exec_oneshot_service_with_just_command) { |
| 130 | Test_make_exec_oneshot_service(true, false, false, false, false); |
| 131 | } |
| 132 | |
| 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 | } |