summaryrefslogtreecommitdiff
path: root/init/service_test.cpp
diff options
context:
space:
mode:
authorTom Cherry <tomcherry@google.com>2017-07-27 12:54:48 -0700
committerTom Cherry <tomcherry@google.com>2017-07-27 13:23:32 -0700
commit67dee626e0185096bbaf73042f1a891ce436f714 (patch)
tree7d8dac0fe94f641494ea33e97ca00e0fb444795b /init/service_test.cpp
parent29b94116855b96e24beeb993396b251a1f85127a (diff)
init: remove Parser singleton and related cleanup
* Remove the Parser singleton (Hooray!) * Rename parser.* to tokenizer.* as this is actually a tokenizer * Rename init_parser.* to parser.* as this is a generic parser * Move contents of init_parser_test.cpp to service_test.cpp as this actually is a test of the parsing in MakeExecOneshotService() and nothing related to (init_)parser.cpp Test: boot bullhead Test: bool sailfish Test: init unit tests Change-Id: I4fe39e6483f58ebd3ce5ee715a45dbba0acf5d91
Diffstat (limited to 'init/service_test.cpp')
-rw-r--r--init/service_test.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/init/service_test.cpp b/init/service_test.cpp
index 44f28a37f..123c8a5d6 100644
--- a/init/service_test.cpp
+++ b/init/service_test.cpp
@@ -23,6 +23,8 @@
#include <gtest/gtest.h>
+#include "util.h"
+
namespace android {
namespace init {
@@ -71,5 +73,123 @@ TEST(service, pod_initialized) {
EXPECT_FALSE(service_in_old_memory->process_cgroup_empty());
}
+TEST(service, make_exec_oneshot_service_invalid_syntax) {
+ ServiceManager& sm = ServiceManager::GetInstance();
+ std::vector<std::string> args;
+ // Nothing.
+ ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
+
+ // No arguments to 'exec'.
+ args.push_back("exec");
+ ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
+
+ // No command in "exec --".
+ args.push_back("--");
+ ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
+}
+
+TEST(service, make_exec_oneshot_service_too_many_supplementary_gids) {
+ ServiceManager& sm = ServiceManager::GetInstance();
+ std::vector<std::string> args;
+ args.push_back("exec");
+ args.push_back("seclabel");
+ args.push_back("root"); // uid.
+ args.push_back("root"); // gid.
+ for (int i = 0; i < NR_SVC_SUPP_GIDS; ++i) {
+ args.push_back("root"); // Supplementary gid.
+ }
+ args.push_back("--");
+ args.push_back("/system/bin/id");
+ ASSERT_EQ(nullptr, sm.MakeExecOneshotService(args));
+}
+
+static void Test_make_exec_oneshot_service(bool dash_dash, bool seclabel, bool uid, bool gid,
+ bool supplementary_gids) {
+ ServiceManager& sm = ServiceManager::GetInstance();
+ std::vector<std::string> args;
+ args.push_back("exec");
+ if (seclabel) {
+ args.push_back("u:r:su:s0"); // seclabel
+ if (uid) {
+ args.push_back("log"); // uid
+ if (gid) {
+ args.push_back("shell"); // gid
+ if (supplementary_gids) {
+ args.push_back("system"); // supplementary gid 0
+ args.push_back("adb"); // supplementary gid 1
+ }
+ }
+ }
+ }
+ if (dash_dash) {
+ args.push_back("--");
+ }
+ args.push_back("/system/bin/toybox");
+ args.push_back("id");
+ Service* svc = sm.MakeExecOneshotService(args);
+ ASSERT_NE(nullptr, svc);
+
+ if (seclabel) {
+ ASSERT_EQ("u:r:su:s0", svc->seclabel());
+ } else {
+ ASSERT_EQ("", svc->seclabel());
+ }
+ if (uid) {
+ uid_t decoded_uid;
+ std::string err;
+ ASSERT_TRUE(DecodeUid("log", &decoded_uid, &err));
+ ASSERT_EQ(decoded_uid, svc->uid());
+ } else {
+ ASSERT_EQ(0U, svc->uid());
+ }
+ if (gid) {
+ uid_t decoded_uid;
+ std::string err;
+ ASSERT_TRUE(DecodeUid("shell", &decoded_uid, &err));
+ ASSERT_EQ(decoded_uid, svc->gid());
+ } else {
+ ASSERT_EQ(0U, svc->gid());
+ }
+ if (supplementary_gids) {
+ ASSERT_EQ(2U, svc->supp_gids().size());
+ uid_t decoded_uid;
+ std::string err;
+ ASSERT_TRUE(DecodeUid("system", &decoded_uid, &err));
+ ASSERT_EQ(decoded_uid, svc->supp_gids()[0]);
+ ASSERT_TRUE(DecodeUid("adb", &decoded_uid, &err));
+ ASSERT_EQ(decoded_uid, svc->supp_gids()[1]);
+ } else {
+ ASSERT_EQ(0U, svc->supp_gids().size());
+ }
+
+ ASSERT_EQ(static_cast<std::size_t>(2), svc->args().size());
+ ASSERT_EQ("/system/bin/toybox", svc->args()[0]);
+ ASSERT_EQ("id", svc->args()[1]);
+}
+
+TEST(service, make_exec_oneshot_service_with_everything) {
+ Test_make_exec_oneshot_service(true, true, true, true, true);
+}
+
+TEST(service, make_exec_oneshot_service_with_seclabel_uid_gid) {
+ Test_make_exec_oneshot_service(true, true, true, true, false);
+}
+
+TEST(service, make_exec_oneshot_service_with_seclabel_uid) {
+ Test_make_exec_oneshot_service(true, true, true, false, false);
+}
+
+TEST(service, make_exec_oneshot_service_with_seclabel) {
+ Test_make_exec_oneshot_service(true, true, false, false, false);
+}
+
+TEST(service, make_exec_oneshot_service_with_just_command) {
+ Test_make_exec_oneshot_service(true, false, false, false, false);
+}
+
+TEST(service, make_exec_oneshot_service_with_just_command_no_dash) {
+ Test_make_exec_oneshot_service(false, false, false, false, false);
+}
+
} // namespace init
} // namespace android