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 #pragma once
17 
18 #include <filesystem>
19 
20 #include <android-base/file.h>
21 #include <apex_manifest.pb.h>
22 #include <gtest/gtest.h>
23 
24 #include "linkerconfig/apex.h"
25 
26 struct ApexTest : ::testing::Test {
27   TemporaryDir tmp_dir;
28   std::string root;
29 
SetUpApexTest30   void SetUp() override {
31     root = tmp_dir.path;
32   }
33 
PrepareApexApexTest34   void PrepareApex(const std::string& apex_name,
35                    const std::vector<std::string>& provide_libs,
36                    const std::vector<std::string>& require_libs,
37                    const std::vector<std::string>& jni_libs) {
38     ::apex::proto::ApexManifest manifest;
39     manifest.set_name(apex_name);
40     for (auto lib : provide_libs) {
41       manifest.add_providenativelibs(lib);
42     }
43     for (auto lib : require_libs) {
44       manifest.add_requirenativelibs(lib);
45     }
46     for (auto lib : jni_libs) {
47       manifest.add_jnilibs(lib);
48     }
49     WriteFile("/apex/" + apex_name + "/apex_manifest.pb",
50               manifest.SerializeAsString());
51   }
52 
MkdirApexTest53   void Mkdir(std::string dir_path) {
54     if (access(dir_path.c_str(), F_OK) == 0) return;
55     Mkdir(android::base::Dirname(dir_path));
56     ASSERT_NE(-1, mkdir(dir_path.c_str(), 0755) == -1)
57         << "Failed to create a directory: " << dir_path;
58   }
59 
WriteFileApexTest60   void WriteFile(const std::string& file, const std::string& content) {
61     std::string file_path = root + file;
62     Mkdir(::android::base::Dirname(file_path));
63     ASSERT_TRUE(::android::base::WriteStringToFile(content, file_path))
64         << "Failed to write a file: " << file_path;
65   }
66 
CreateApexInfoListApexTest67   void CreateApexInfoList() {
68     std::string content =
69         "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<apex-info-list>\n";
70     for (const auto& file :
71          std::filesystem::directory_iterator(root + "/apex")) {
72       if (!file.is_directory()) {
73         continue;
74       }
75 
76       content += "<apex-info moduleName=\"";
77       content += file.path().filename();
78       content += "\" modulePath=\"";
79       content += file.path().string();
80       content +=
81           "\" preinstalledModulePath=\"/test/path/1234\" isFactory=\"true\" "
82           "isActive=\"true\" />\n";
83     }
84     content += "</apex-info-list>";
85 
86     WriteFile("/apex/apex-info-list.xml", content);
87   }
88 
CreatePublicLibrariesTxtApexTest89   void CreatePublicLibrariesTxt() {
90     std::string content = "foo.so\nbar.so\nbaz.so";
91     WriteFile("/system/etc/public.libraries.txt", content);
92   }
93 };