1 /*
2 * Copyright (C) 2019 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 #include <gtest/gtest.h>
17
18 #include <vector>
19
20 #include "linkerconfig/configuration.h"
21 #include "linkerconfig/variables.h"
22 #include "modules_testbase.h"
23
24 using namespace android::linkerconfig::modules;
25
26 constexpr const char* kExpectedConfiguration =
27 R"(dir.system = /system/bin
28 dir.system = /system/xbin
29 dir.system = /product/bin
30 dir.vendor = /odm/bin
31 dir.vendor = /vendor/bin
32 dir.vendor = /system/bin/vendor
33 dir.vendor = /product/bin/vendor
34 [system]
35 additional.namespaces = namespace1,namespace2
36 namespace.default.isolated = false
37 namespace.default.search.paths = /search_path1
38 namespace.default.search.paths += /search_path2
39 namespace.default.search.paths += /search_path3
40 namespace.default.permitted.paths = /permitted_path1
41 namespace.default.permitted.paths += /permitted_path2
42 namespace.default.permitted.paths += /permitted_path3
43 namespace.default.asan.search.paths = /data/asan/search_path1
44 namespace.default.asan.search.paths += /search_path1
45 namespace.default.asan.search.paths += /search_path2
46 namespace.default.asan.permitted.paths = /data/asan/permitted_path1
47 namespace.default.asan.permitted.paths += /permitted_path1
48 namespace.default.asan.permitted.paths += /permitted_path2
49 namespace.default.links = namespace1,namespace2
50 namespace.default.link.namespace1.shared_libs = lib1.so
51 namespace.default.link.namespace1.shared_libs += lib2.so
52 namespace.default.link.namespace1.shared_libs += lib3.so
53 namespace.default.link.namespace2.allow_all_shared_libs = true
54 namespace.namespace1.isolated = false
55 namespace.namespace1.search.paths = /search_path1
56 namespace.namespace1.search.paths += /search_path2
57 namespace.namespace1.search.paths += /search_path3
58 namespace.namespace1.permitted.paths = /permitted_path1
59 namespace.namespace1.permitted.paths += /permitted_path2
60 namespace.namespace1.permitted.paths += /permitted_path3
61 namespace.namespace1.asan.search.paths = /data/asan/search_path1
62 namespace.namespace1.asan.search.paths += /search_path1
63 namespace.namespace1.asan.search.paths += /search_path2
64 namespace.namespace1.asan.permitted.paths = /data/asan/permitted_path1
65 namespace.namespace1.asan.permitted.paths += /permitted_path1
66 namespace.namespace1.asan.permitted.paths += /permitted_path2
67 namespace.namespace2.isolated = false
68 namespace.namespace2.search.paths = /search_path1
69 namespace.namespace2.search.paths += /search_path2
70 namespace.namespace2.search.paths += /search_path3
71 namespace.namespace2.permitted.paths = /permitted_path1
72 namespace.namespace2.permitted.paths += /permitted_path2
73 namespace.namespace2.permitted.paths += /permitted_path3
74 namespace.namespace2.asan.search.paths = /data/asan/search_path1
75 namespace.namespace2.asan.search.paths += /search_path1
76 namespace.namespace2.asan.search.paths += /search_path2
77 namespace.namespace2.asan.permitted.paths = /data/asan/permitted_path1
78 namespace.namespace2.asan.permitted.paths += /permitted_path1
79 namespace.namespace2.asan.permitted.paths += /permitted_path2
80 [vendor]
81 namespace.default.isolated = false
82 namespace.default.search.paths = /search_path1
83 namespace.default.search.paths += /search_path2
84 namespace.default.search.paths += /search_path3
85 namespace.default.permitted.paths = /permitted_path1
86 namespace.default.permitted.paths += /permitted_path2
87 namespace.default.permitted.paths += /permitted_path3
88 namespace.default.asan.search.paths = /data/asan/search_path1
89 namespace.default.asan.search.paths += /search_path1
90 namespace.default.asan.search.paths += /search_path2
91 namespace.default.asan.permitted.paths = /data/asan/permitted_path1
92 namespace.default.asan.permitted.paths += /permitted_path1
93 namespace.default.asan.permitted.paths += /permitted_path2
94 )";
95
TEST(linkerconfig_configuration,generate_configuration)96 TEST(linkerconfig_configuration, generate_configuration) {
97 std::vector<Section> sections;
98
99 std::vector<DirToSection> dir_to_sections = {
100 {"/system/bin", "system"},
101 {"/system/xbin", "system"},
102 {"/product/bin", "system"},
103 {"/odm/bin", "vendor"},
104 {"/vendor/bin", "vendor"},
105 {"/system/bin/vendor", "vendor"},
106 {"/product/bin/vendor", "vendor"},
107 {"/product/bin", "vendor"}, // note that this is ignored
108 };
109
110 std::vector<Namespace> system_namespaces;
111
112 system_namespaces.emplace_back(CreateNamespaceWithLinks(
113 "default", false, false, "namespace1", "namespace2"));
114 system_namespaces.emplace_back(
115 CreateNamespaceWithPaths("namespace1", false, false));
116 system_namespaces.emplace_back(
117 CreateNamespaceWithPaths("namespace2", false, false));
118
119 Section system_section("system", std::move(system_namespaces));
120 sections.emplace_back(std::move(system_section));
121
122 std::vector<Namespace> vendor_namespaces;
123
124 vendor_namespaces.emplace_back(
125 CreateNamespaceWithPaths("default", false, false));
126
127 Section vendor_section("vendor", std::move(vendor_namespaces));
128 sections.emplace_back(std::move(vendor_section));
129
130 Configuration conf(std::move(sections), dir_to_sections);
131
132 android::linkerconfig::modules::ConfigWriter writer;
133 conf.WriteConfig(writer);
134
135 ASSERT_EQ(kExpectedConfiguration, writer.ToString());
136 }