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 
17 #include "linkerconfig/namespace.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include <android-base/strings.h>
23 #include <gtest/gtest.h>
24 
25 #include "linkerconfig/configwriter.h"
26 #include "linkerconfig/link.h"
27 #include "modules_testbase.h"
28 
29 using namespace android::linkerconfig::modules;
30 using namespace android::base;
31 
32 constexpr const char* kExpectedSimpleNamespaceConfig =
33     R"(namespace.test_namespace.isolated = false
34 namespace.test_namespace.search.paths = /search_path1
35 namespace.test_namespace.search.paths += /apex/search_path2
36 namespace.test_namespace.permitted.paths = /permitted_path1
37 namespace.test_namespace.permitted.paths += /apex/permitted_path2
38 namespace.test_namespace.asan.search.paths = /data/asan/search_path1
39 namespace.test_namespace.asan.search.paths += /search_path1
40 namespace.test_namespace.asan.search.paths += /apex/search_path2
41 namespace.test_namespace.asan.permitted.paths = /data/asan/permitted_path1
42 namespace.test_namespace.asan.permitted.paths += /permitted_path1
43 namespace.test_namespace.asan.permitted.paths += /apex/permitted_path2
44 )";
45 
46 constexpr const char* kExpectedNamespaceWithLinkConfig =
47     R"(namespace.test_namespace.isolated = true
48 namespace.test_namespace.visible = true
49 namespace.test_namespace.search.paths = /search_path1
50 namespace.test_namespace.search.paths += /apex/search_path2
51 namespace.test_namespace.permitted.paths = /permitted_path1
52 namespace.test_namespace.permitted.paths += /apex/permitted_path2
53 namespace.test_namespace.asan.search.paths = /data/asan/search_path1
54 namespace.test_namespace.asan.search.paths += /search_path1
55 namespace.test_namespace.asan.search.paths += /apex/search_path2
56 namespace.test_namespace.asan.permitted.paths = /data/asan/permitted_path1
57 namespace.test_namespace.asan.permitted.paths += /permitted_path1
58 namespace.test_namespace.asan.permitted.paths += /apex/permitted_path2
59 namespace.test_namespace.links = target_namespace1,target_namespace2
60 namespace.test_namespace.link.target_namespace1.shared_libs = lib1.so
61 namespace.test_namespace.link.target_namespace1.shared_libs += lib2.so
62 namespace.test_namespace.link.target_namespace1.shared_libs += lib3.so
63 namespace.test_namespace.link.target_namespace2.allow_all_shared_libs = true
64 )";
65 
66 constexpr const char* kExpectedNamespaceWithWhitelisted =
67     R"(namespace.test_namespace.isolated = false
68 namespace.test_namespace.search.paths = /search_path1
69 namespace.test_namespace.search.paths += /apex/search_path2
70 namespace.test_namespace.permitted.paths = /permitted_path1
71 namespace.test_namespace.permitted.paths += /apex/permitted_path2
72 namespace.test_namespace.asan.search.paths = /data/asan/search_path1
73 namespace.test_namespace.asan.search.paths += /search_path1
74 namespace.test_namespace.asan.search.paths += /apex/search_path2
75 namespace.test_namespace.asan.permitted.paths = /data/asan/permitted_path1
76 namespace.test_namespace.asan.permitted.paths += /permitted_path1
77 namespace.test_namespace.asan.permitted.paths += /apex/permitted_path2
78 namespace.test_namespace.whitelisted = whitelisted_path1
79 namespace.test_namespace.whitelisted += whitelisted_path2
80 )";
81 
TEST(linkerconfig_namespace,simple_namespace)82 TEST(linkerconfig_namespace, simple_namespace) {
83   ConfigWriter writer;
84   auto ns = CreateNamespaceWithPaths("test_namespace", false, false);
85   ns.WriteConfig(writer);
86   auto config = writer.ToString();
87 
88   ASSERT_EQ(config, kExpectedSimpleNamespaceConfig);
89 }
90 
TEST(linkerconfig_namespace,namespace_with_links)91 TEST(linkerconfig_namespace, namespace_with_links) {
92   ConfigWriter writer;
93 
94   auto ns = CreateNamespaceWithLinks("test_namespace", true, true,
95                                      "target_namespace1", "target_namespace2");
96   ns.WriteConfig(writer);
97   auto config = writer.ToString();
98 
99   ASSERT_EQ(config, kExpectedNamespaceWithLinkConfig);
100 }
101 
TEST(linkerconfig_namespace,namespace_with_whitelisted)102 TEST(linkerconfig_namespace, namespace_with_whitelisted) {
103   ConfigWriter writer;
104   auto ns = CreateNamespaceWithPaths("test_namespace", false, false);
105   ns.AddWhitelisted("whitelisted_path1");
106   ns.AddWhitelisted("whitelisted_path2");
107   ns.WriteConfig(writer);
108 
109   auto config = writer.ToString();
110 
111   ASSERT_EQ(config, kExpectedNamespaceWithWhitelisted);
112 }
113 
TEST(linkerconfig_namespace,namespace_links_should_be_ordered)114 TEST(linkerconfig_namespace, namespace_links_should_be_ordered) {
115   std::vector<std::string> expected_links = {"z", "a", "o"};
116 
117   Namespace ns("test_namespace");
118   for (auto link : expected_links) {
119     ns.GetLink(link);
120   }
121 
122   ConfigWriter writer;
123   ns.WriteConfig(writer);
124 
125   std::string actual_links;
126   for (auto line : Split(writer.ToString(), "\n")) {
127     if (StartsWith(line, "namespace.test_namespace.links")) {
128       actual_links = Split(line, " ").back();
129     }
130   }
131   ASSERT_EQ(android::base::Join(expected_links, ","), actual_links);
132 }
133