gtest_main.cpp revision 6acf8c67e949bb5a4642ff0035a49a459bca9022
1/* 2 * Copyright (C) 2015 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 <gtest/gtest.h> 18 19#include <memory> 20 21#include <android-base/file.h> 22#include <android-base/logging.h> 23#include <android-base/test_utils.h> 24#include <ziparchive/zip_archive.h> 25 26#include "get_test_data.h" 27#include "read_elf.h" 28#include "utils.h" 29 30static std::string testdata_dir; 31 32#if defined(__ANDROID__) 33static const std::string testdata_section = ".testzipdata"; 34 35static bool ExtractTestDataFromElfSection() { 36 if (!MkdirWithParents(testdata_dir)) { 37 PLOG(ERROR) << "failed to create testdata_dir " << testdata_dir; 38 return false; 39 } 40 std::string content; 41 if (!ReadSectionFromElfFile("/proc/self/exe", testdata_section, &content)) { 42 LOG(ERROR) << "failed to read section " << testdata_section; 43 return false; 44 } 45 TemporaryFile tmp_file; 46 if (!android::base::WriteStringToFile(content, tmp_file.path)) { 47 PLOG(ERROR) << "failed to write file " << tmp_file.path; 48 return false; 49 } 50 ArchiveHelper ahelper(tmp_file.fd, tmp_file.path); 51 if (!ahelper) { 52 LOG(ERROR) << "failed to open archive " << tmp_file.path; 53 return false; 54 } 55 ZipArchiveHandle& handle = ahelper.archive_handle(); 56 void* cookie; 57 int ret = StartIteration(handle, &cookie, nullptr, nullptr); 58 if (ret != 0) { 59 LOG(ERROR) << "failed to start iterating zip entries"; 60 return false; 61 } 62 ZipEntry entry; 63 ZipString name; 64 while (Next(cookie, &entry, &name) == 0) { 65 std::string entry_name(name.name, name.name + name.name_length); 66 std::string path = testdata_dir + entry_name; 67 // Skip dir. 68 if (path.back() == '/') { 69 continue; 70 } 71 if (!MkdirWithParents(path)) { 72 LOG(ERROR) << "failed to create dir for " << path; 73 return false; 74 } 75 FileHelper fhelper = FileHelper::OpenWriteOnly(path); 76 if (!fhelper) { 77 PLOG(ERROR) << "failed to create file " << path; 78 return false; 79 } 80 std::vector<uint8_t> data(entry.uncompressed_length); 81 if (ExtractToMemory(handle, &entry, data.data(), data.size()) != 0) { 82 LOG(ERROR) << "failed to extract entry " << entry_name; 83 return false; 84 } 85 if (!android::base::WriteFully(fhelper.fd(), data.data(), data.size())) { 86 LOG(ERROR) << "failed to write file " << path; 87 return false; 88 } 89 } 90 EndIteration(cookie); 91 return true; 92} 93#endif // defined(__ANDROID__) 94 95int main(int argc, char** argv) { 96 InitLogging(argv, android::base::StderrLogger); 97 testing::InitGoogleTest(&argc, argv); 98 99 for (int i = 1; i < argc; ++i) { 100 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) { 101 testdata_dir = argv[i + 1]; 102 i++; 103 } 104 } 105 106#if defined(__ANDROID__) 107 std::unique_ptr<TemporaryDir> tmp_dir; 108 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { 109 tmp_dir.reset(new TemporaryDir); 110 testdata_dir = std::string(tmp_dir->path) + "/"; 111 if (!ExtractTestDataFromElfSection()) { 112 LOG(ERROR) << "failed to extract test data from elf section"; 113 return 1; 114 } 115 } 116#endif 117 118 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { 119 printf("Usage: %s -t <testdata_dir>\n", argv[0]); 120 return 1; 121 } 122 if (testdata_dir.back() != '/') { 123 testdata_dir.push_back('/'); 124 } 125 LOG(INFO) << "testdata is in " << testdata_dir; 126 return RUN_ALL_TESTS(); 127} 128 129std::string GetTestData(const std::string& filename) { 130 return testdata_dir + filename; 131} 132 133const std::string& GetTestDataDir() { 134 return testdata_dir; 135} 136 137bool IsRoot() { 138 static int is_root = -1; 139 if (is_root == -1) { 140#if defined(__linux__) 141 is_root = (getuid() == 0) ? 1 : 0; 142#else 143 is_root = 0; 144#endif 145 } 146 return is_root == 1; 147} 148