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 <android-base/file.h> 19 #include <apex_manifest.pb.h> 20 #include <gtest/gtest.h> 21 22 #include "linkerconfig/apex.h" 23 24 #include <iostream> 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 android::linkerconfig::modules::ApexInfo PrepareApex( 35 std::string apex_name, std::vector<std::string> provided_libs, 36 std::vector<std::string> required_libs) { 37 ::apex::proto::ApexManifest manifest; 38 manifest.set_name(apex_name); 39 for (auto lib : provided_libs) { 40 manifest.add_providenativelibs(lib); 41 } 42 for (auto lib : required_libs) { 43 manifest.add_requirenativelibs(lib); 44 } 45 const auto apex_path = "/apex/" + apex_name; 46 WriteFile(apex_path + "/apex_manifest.pb", manifest.SerializeAsString()); 47 return android::linkerconfig::modules::ApexInfo( 48 manifest.name(), 49 apex_path, 50 {manifest.providenativelibs().begin(), 51 manifest.providenativelibs().end()}, 52 {manifest.requirenativelibs().begin(), 53 manifest.requirenativelibs().end()}, 54 true, 55 true); 56 } 57 MkdirApexTest58 void Mkdir(std::string dir_path) { 59 if (access(dir_path.c_str(), F_OK) == 0) return; 60 Mkdir(android::base::Dirname(dir_path)); 61 std::cout << "mkdir(" + dir_path + ")\n"; 62 ASSERT_NE(-1, mkdir(dir_path.c_str(), 0755) == -1) 63 << "Failed to create a directory: " << dir_path; 64 } 65 WriteFileApexTest66 void WriteFile(std::string file, std::string content) { 67 std::string file_path = root + file; 68 Mkdir(::android::base::Dirname(file_path)); 69 std::cout << "writeFile(" + file_path + ")\n"; 70 ASSERT_TRUE(::android::base::WriteStringToFile(content, file_path)) 71 << "Failed to write a file: " << file_path; 72 } 73 };