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