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 += /apex/search_path2
39 namespace.default.permitted.paths = /permitted_path1
40 namespace.default.permitted.paths += /apex/permitted_path2
41 namespace.default.asan.search.paths = /data/asan/search_path1
42 namespace.default.asan.search.paths += /search_path1
43 namespace.default.asan.search.paths += /apex/search_path2
44 namespace.default.asan.permitted.paths = /data/asan/permitted_path1
45 namespace.default.asan.permitted.paths += /permitted_path1
46 namespace.default.asan.permitted.paths += /apex/permitted_path2
47 namespace.default.links = namespace1,namespace2
48 namespace.default.link.namespace1.shared_libs = lib1.so
49 namespace.default.link.namespace1.shared_libs += lib2.so
50 namespace.default.link.namespace1.shared_libs += lib3.so
51 namespace.default.link.namespace2.allow_all_shared_libs = true
52 namespace.namespace1.isolated = false
53 namespace.namespace1.search.paths = /search_path1
54 namespace.namespace1.search.paths += /apex/search_path2
55 namespace.namespace1.permitted.paths = /permitted_path1
56 namespace.namespace1.permitted.paths += /apex/permitted_path2
57 namespace.namespace1.asan.search.paths = /data/asan/search_path1
58 namespace.namespace1.asan.search.paths += /search_path1
59 namespace.namespace1.asan.search.paths += /apex/search_path2
60 namespace.namespace1.asan.permitted.paths = /data/asan/permitted_path1
61 namespace.namespace1.asan.permitted.paths += /permitted_path1
62 namespace.namespace1.asan.permitted.paths += /apex/permitted_path2
63 namespace.namespace2.isolated = false
64 namespace.namespace2.search.paths = /search_path1
65 namespace.namespace2.search.paths += /apex/search_path2
66 namespace.namespace2.permitted.paths = /permitted_path1
67 namespace.namespace2.permitted.paths += /apex/permitted_path2
68 namespace.namespace2.asan.search.paths = /data/asan/search_path1
69 namespace.namespace2.asan.search.paths += /search_path1
70 namespace.namespace2.asan.search.paths += /apex/search_path2
71 namespace.namespace2.asan.permitted.paths = /data/asan/permitted_path1
72 namespace.namespace2.asan.permitted.paths += /permitted_path1
73 namespace.namespace2.asan.permitted.paths += /apex/permitted_path2
74 [vendor]
75 namespace.default.isolated = false
76 namespace.default.search.paths = /search_path1
77 namespace.default.search.paths += /apex/search_path2
78 namespace.default.permitted.paths = /permitted_path1
79 namespace.default.permitted.paths += /apex/permitted_path2
80 namespace.default.asan.search.paths = /data/asan/search_path1
81 namespace.default.asan.search.paths += /search_path1
82 namespace.default.asan.search.paths += /apex/search_path2
83 namespace.default.asan.permitted.paths = /data/asan/permitted_path1
84 namespace.default.asan.permitted.paths += /permitted_path1
85 namespace.default.asan.permitted.paths += /apex/permitted_path2
86 )";
87 
TEST(linkerconfig_configuration,generate_configuration)88 TEST(linkerconfig_configuration, generate_configuration) {
89   std::vector<Section> sections;
90 
91   std::vector<DirToSection> dir_to_sections = {
92       {"/system/bin", "system"},
93       {"/system/xbin", "system"},
94       {"/product/bin", "system"},
95       {"/odm/bin", "vendor"},
96       {"/vendor/bin", "vendor"},
97       {"/system/bin/vendor", "vendor"},
98       {"/product/bin/vendor", "vendor"},
99       {"/product/bin", "vendor"},  // note that this is ignored
100   };
101 
102   std::vector<Namespace> system_namespaces;
103 
104   system_namespaces.emplace_back(CreateNamespaceWithLinks(
105       "default", false, false, "namespace1", "namespace2"));
106   system_namespaces.emplace_back(
107       CreateNamespaceWithPaths("namespace1", false, false));
108   system_namespaces.emplace_back(
109       CreateNamespaceWithPaths("namespace2", false, false));
110 
111   Section system_section("system", std::move(system_namespaces));
112   sections.emplace_back(std::move(system_section));
113 
114   std::vector<Namespace> vendor_namespaces;
115 
116   vendor_namespaces.emplace_back(
117       CreateNamespaceWithPaths("default", false, false));
118 
119   Section vendor_section("vendor", std::move(vendor_namespaces));
120   sections.emplace_back(std::move(vendor_section));
121 
122   Configuration conf(std::move(sections), dir_to_sections);
123 
124   android::linkerconfig::modules::ConfigWriter writer;
125   conf.WriteConfig(writer);
126 
127   ASSERT_EQ(kExpectedConfiguration, writer.ToString());
128 }