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 <algorithm>
18 #include <string>
19 #include <string_view>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <gtest/gtest.h>
24 
25 #include "linkerconfig/context.h"
26 #include "linkerconfig/namespace.h"
27 #include "linkerconfig/namespacebuilder.h"
28 #include "linkerconfig/sectionbuilder.h"
29 
30 using namespace android::linkerconfig::contents;
31 using namespace android::linkerconfig::modules;
32 
33 namespace {
34 
35 typedef std::unordered_map<std::string, std::vector<std::string>> fsmap;
36 
Search(const Namespace & ns,std::string_view soname,fsmap fs)37 std::string Search(const Namespace& ns, std::string_view soname, fsmap fs) {
38   // search for current search_paths
39   for (auto path : ns.SearchPaths()) {
40     auto libs = fs[path];
41     if (std::find(std::begin(libs), std::end(libs), soname) != std::end(libs)) {
42       return path;
43     }
44   }
45   return "";
46 }
47 
48 }  // namespace
49 
TEST(vndk_namespace,vndk_ext)50 TEST(vndk_namespace, vndk_ext) {
51   Context vendor_context;
52   vendor_context.SetCurrentSection(SectionType::Vendor);
53   auto vndk_ns = BuildVndkNamespace(vendor_context, VndkUserPartition::Vendor);
54 
55   auto libvndk = "libvndk.so";
56   auto libvndksp = "libvndksp.so";
57 
58   auto system_lib_path = "/system/${LIB}";
59   auto vendor_lib_path = "/vendor/${LIB}";
60   auto vendor_vndk_lib_path = "/vendor/${LIB}/vndk";
61   auto vendor_vndksp_lib_path = "/vendor/${LIB}/vndk-sp";
62   auto apex_vndk_lib_path =
63       "/apex/com.android.vndk.v" + Var("VENDOR_VNDK_VERSION") + "/${LIB}";
64 
65   auto fs = fsmap{
66       {system_lib_path, {libvndk, libvndksp}},
67       {vendor_lib_path, {libvndk, libvndksp}},
68       {apex_vndk_lib_path, {libvndk, libvndksp}},
69   };
70 
71   EXPECT_EQ(apex_vndk_lib_path, Search(vndk_ns, libvndk, fs));
72   EXPECT_EQ(apex_vndk_lib_path, Search(vndk_ns, libvndksp, fs));
73 
74   // vndk-ext can eclipse vndk
75   fs[vendor_vndk_lib_path] = {libvndk};
76   EXPECT_EQ(vendor_vndk_lib_path, Search(vndk_ns, libvndk, fs));
77   EXPECT_EQ(apex_vndk_lib_path, Search(vndk_ns, libvndksp, fs));
78 
79   // likewise, vndk-sp-ext can eclipse vndk-sp
80   fs[vendor_vndksp_lib_path] = {libvndksp};
81   EXPECT_EQ(vendor_vndk_lib_path, Search(vndk_ns, libvndk, fs));
82   EXPECT_EQ(vendor_vndksp_lib_path, Search(vndk_ns, libvndksp, fs));
83 }
84