2017-04-20 01:18:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "action.h"
|
2018-02-14 00:36:14 +01:00
|
|
|
#include "action_manager.h"
|
2018-02-14 00:25:29 +01:00
|
|
|
#include "action_parser.h"
|
2019-07-23 01:05:36 +02:00
|
|
|
#include "builtin_arguments.h"
|
2017-04-20 01:18:50 +02:00
|
|
|
#include "builtins.h"
|
|
|
|
#include "import_parser.h"
|
|
|
|
#include "keyword_map.h"
|
2017-07-27 21:54:48 +02:00
|
|
|
#include "parser.h"
|
2017-11-14 00:31:54 +01:00
|
|
|
#include "service.h"
|
2019-06-26 19:46:20 +02:00
|
|
|
#include "service_list.h"
|
|
|
|
#include "service_parser.h"
|
2017-04-20 01:18:50 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2017-06-22 21:53:17 +02:00
|
|
|
namespace android {
|
|
|
|
namespace init {
|
|
|
|
|
2017-04-20 01:18:50 +02:00
|
|
|
using ActionManagerCommand = std::function<void(ActionManager&)>;
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
void TestInit(const std::string& init_script_file, const BuiltinFunctionMap& test_function_map,
|
2017-11-14 00:31:54 +01:00
|
|
|
const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
|
2017-04-20 01:18:50 +02:00
|
|
|
ActionManager am;
|
|
|
|
|
|
|
|
Action::set_function_map(&test_function_map);
|
|
|
|
|
|
|
|
Parser parser;
|
2019-06-27 21:18:08 +02:00
|
|
|
parser.AddSectionParser("service",
|
|
|
|
std::make_unique<ServiceParser>(service_list, nullptr, std::nullopt));
|
2017-09-13 00:58:47 +02:00
|
|
|
parser.AddSectionParser("on", std::make_unique<ActionParser>(&am, nullptr));
|
2017-04-20 01:18:50 +02:00
|
|
|
parser.AddSectionParser("import", std::make_unique<ImportParser>(&parser));
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser.ParseConfig(init_script_file));
|
|
|
|
|
|
|
|
for (const auto& command : commands) {
|
|
|
|
command(am);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (am.HasMoreCommands()) {
|
|
|
|
am.ExecuteOneCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
void TestInitText(const std::string& init_script, const BuiltinFunctionMap& test_function_map,
|
2017-11-14 00:31:54 +01:00
|
|
|
const std::vector<ActionManagerCommand>& commands, ServiceList* service_list) {
|
2017-04-20 01:18:50 +02:00
|
|
|
TemporaryFile tf;
|
|
|
|
ASSERT_TRUE(tf.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(init_script, tf.fd));
|
2017-11-14 00:31:54 +01:00
|
|
|
TestInit(tf.path, test_function_map, commands, service_list);
|
2017-04-20 01:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, SimpleEventTrigger) {
|
|
|
|
bool expect_true = false;
|
|
|
|
std::string init_script =
|
|
|
|
R"init(
|
|
|
|
on boot
|
|
|
|
pass_test
|
|
|
|
)init";
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_pass_test = [&expect_true](const BuiltinArguments&) {
|
|
|
|
expect_true = true;
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"pass_test", {0, 0, {false, do_pass_test}}},
|
|
|
|
};
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
|
2017-11-14 00:31:54 +01:00
|
|
|
ServiceList service_list;
|
|
|
|
TestInitText(init_script, test_function_map, commands, &service_list);
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(expect_true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, EventTriggerOrder) {
|
|
|
|
std::string init_script =
|
|
|
|
R"init(
|
|
|
|
on boot
|
|
|
|
execute_first
|
|
|
|
|
|
|
|
on boot && property:ro.hardware=*
|
|
|
|
execute_second
|
|
|
|
|
|
|
|
on boot
|
|
|
|
execute_third
|
|
|
|
|
|
|
|
)init";
|
|
|
|
|
|
|
|
int num_executed = 0;
|
2019-07-23 01:05:36 +02:00
|
|
|
auto do_execute_first = [&num_executed](const BuiltinArguments&) {
|
|
|
|
EXPECT_EQ(0, num_executed++);
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
auto do_execute_second = [&num_executed](const BuiltinArguments&) {
|
|
|
|
EXPECT_EQ(1, num_executed++);
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
auto do_execute_third = [&num_executed](const BuiltinArguments&) {
|
|
|
|
EXPECT_EQ(2, num_executed++);
|
|
|
|
return Result<void>{};
|
|
|
|
};
|
|
|
|
|
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"execute_first", {0, 0, {false, do_execute_first}}},
|
|
|
|
{"execute_second", {0, 0, {false, do_execute_second}}},
|
|
|
|
{"execute_third", {0, 0, {false, do_execute_third}}},
|
|
|
|
};
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
|
2017-11-14 00:31:54 +01:00
|
|
|
ServiceList service_list;
|
|
|
|
TestInitText(init_script, test_function_map, commands, &service_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, OverrideService) {
|
|
|
|
std::string init_script = R"init(
|
|
|
|
service A something
|
|
|
|
class first
|
|
|
|
|
|
|
|
service A something
|
|
|
|
class second
|
|
|
|
override
|
|
|
|
|
|
|
|
)init";
|
|
|
|
|
|
|
|
ServiceList service_list;
|
2019-07-23 01:05:36 +02:00
|
|
|
TestInitText(init_script, BuiltinFunctionMap(), {}, &service_list);
|
2017-11-14 00:31:54 +01:00
|
|
|
ASSERT_EQ(1, std::distance(service_list.begin(), service_list.end()));
|
|
|
|
|
|
|
|
auto service = service_list.begin()->get();
|
|
|
|
ASSERT_NE(nullptr, service);
|
|
|
|
EXPECT_EQ(std::set<std::string>({"second"}), service->classnames());
|
|
|
|
EXPECT_EQ("A", service->name());
|
|
|
|
EXPECT_TRUE(service->is_override());
|
2017-04-20 01:18:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(init, EventTriggerOrderMultipleFiles) {
|
|
|
|
// 6 total files, which should have their triggers executed in the following order:
|
|
|
|
// 1: start - original script parsed
|
|
|
|
// 2: first_import - immediately imported by first_script
|
|
|
|
// 3: dir_a - file named 'a.rc' in dir; dir is imported after first_import
|
|
|
|
// 4: a_import - file imported by dir_a
|
|
|
|
// 5: dir_b - file named 'b.rc' in dir
|
|
|
|
// 6: last_import - imported after dir is imported
|
|
|
|
|
|
|
|
TemporaryFile first_import;
|
|
|
|
ASSERT_TRUE(first_import.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 2", first_import.fd));
|
|
|
|
|
|
|
|
TemporaryFile dir_a_import;
|
|
|
|
ASSERT_TRUE(dir_a_import.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 4", dir_a_import.fd));
|
|
|
|
|
|
|
|
TemporaryFile last_import;
|
|
|
|
ASSERT_TRUE(last_import.fd != -1);
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd("on boot\nexecute 6", last_import.fd));
|
|
|
|
|
|
|
|
TemporaryDir dir;
|
|
|
|
// clang-format off
|
|
|
|
std::string dir_a_script = "import " + std::string(dir_a_import.path) + "\n"
|
|
|
|
"on boot\n"
|
|
|
|
"execute 3";
|
|
|
|
// clang-format on
|
2017-05-05 03:17:33 +02:00
|
|
|
// WriteFile() ensures the right mode is set
|
2017-08-03 21:54:07 +02:00
|
|
|
ASSERT_TRUE(WriteFile(std::string(dir.path) + "/a.rc", dir_a_script));
|
2017-04-20 01:18:50 +02:00
|
|
|
|
2017-08-03 21:54:07 +02:00
|
|
|
ASSERT_TRUE(WriteFile(std::string(dir.path) + "/b.rc", "on boot\nexecute 5"));
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
std::string start_script = "import " + std::string(first_import.path) + "\n"
|
|
|
|
"import " + std::string(dir.path) + "\n"
|
|
|
|
"import " + std::string(last_import.path) + "\n"
|
|
|
|
"on boot\n"
|
|
|
|
"execute 1";
|
|
|
|
// clang-format on
|
|
|
|
TemporaryFile start;
|
|
|
|
ASSERT_TRUE(android::base::WriteStringToFd(start_script, start.fd));
|
|
|
|
|
|
|
|
int num_executed = 0;
|
2017-09-13 00:58:47 +02:00
|
|
|
auto execute_command = [&num_executed](const BuiltinArguments& args) {
|
2017-04-20 01:18:50 +02:00
|
|
|
EXPECT_EQ(2U, args.size());
|
|
|
|
EXPECT_EQ(++num_executed, std::stoi(args[1]));
|
2019-06-10 20:08:01 +02:00
|
|
|
return Result<void>{};
|
2017-04-20 01:18:50 +02:00
|
|
|
};
|
|
|
|
|
2019-07-23 01:05:36 +02:00
|
|
|
BuiltinFunctionMap test_function_map = {
|
|
|
|
{"execute", {1, 1, {false, execute_command}}},
|
|
|
|
};
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
ActionManagerCommand trigger_boot = [](ActionManager& am) { am.QueueEventTrigger("boot"); };
|
|
|
|
std::vector<ActionManagerCommand> commands{trigger_boot};
|
|
|
|
|
2017-11-14 00:31:54 +01:00
|
|
|
ServiceList service_list;
|
|
|
|
|
|
|
|
TestInit(start.path, test_function_map, commands, &service_list);
|
2017-04-20 01:18:50 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(6, num_executed);
|
|
|
|
}
|
2017-06-22 21:53:17 +02:00
|
|
|
|
|
|
|
} // namespace init
|
|
|
|
} // namespace android
|