gtest_main.cpp revision 1e8ad07d1a20c52c9450dc2e64dbc386c2e65296
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 <libgen.h> 20 21#include <memory> 22 23#include <android-base/file.h> 24#include <android-base/logging.h> 25#include <android-base/test_utils.h> 26#include <ziparchive/zip_archive.h> 27 28#if defined(__ANDROID__) 29#include <sys/system_properties.h> 30#endif 31 32#include "get_test_data.h" 33#include "read_elf.h" 34#include "utils.h" 35 36static std::string testdata_dir; 37 38#if defined(__ANDROID__) 39static const std::string testdata_section = ".testzipdata"; 40 41static bool ExtractTestDataFromElfSection() { 42 if (!MkdirWithParents(testdata_dir)) { 43 PLOG(ERROR) << "failed to create testdata_dir " << testdata_dir; 44 return false; 45 } 46 std::string content; 47 ElfStatus result = ReadSectionFromElfFile("/proc/self/exe", testdata_section, &content); 48 if (result != ElfStatus::NO_ERROR) { 49 LOG(ERROR) << "failed to read section " << testdata_section 50 << ": " << result; 51 return false; 52 } 53 TemporaryFile tmp_file; 54 if (!android::base::WriteStringToFile(content, tmp_file.path)) { 55 PLOG(ERROR) << "failed to write file " << tmp_file.path; 56 return false; 57 } 58 ArchiveHelper ahelper(tmp_file.fd, tmp_file.path); 59 if (!ahelper) { 60 LOG(ERROR) << "failed to open archive " << tmp_file.path; 61 return false; 62 } 63 ZipArchiveHandle& handle = ahelper.archive_handle(); 64 void* cookie; 65 int ret = StartIteration(handle, &cookie, nullptr, nullptr); 66 if (ret != 0) { 67 LOG(ERROR) << "failed to start iterating zip entries"; 68 return false; 69 } 70 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); 71 ZipEntry entry; 72 ZipString name; 73 while (Next(cookie, &entry, &name) == 0) { 74 std::string entry_name(name.name, name.name + name.name_length); 75 std::string path = testdata_dir + entry_name; 76 // Skip dir. 77 if (path.back() == '/') { 78 continue; 79 } 80 if (!MkdirWithParents(path)) { 81 LOG(ERROR) << "failed to create dir for " << path; 82 return false; 83 } 84 FileHelper fhelper = FileHelper::OpenWriteOnly(path); 85 if (!fhelper) { 86 PLOG(ERROR) << "failed to create file " << path; 87 return false; 88 } 89 std::vector<uint8_t> data(entry.uncompressed_length); 90 if (ExtractToMemory(handle, &entry, data.data(), data.size()) != 0) { 91 LOG(ERROR) << "failed to extract entry " << entry_name; 92 return false; 93 } 94 if (!android::base::WriteFully(fhelper.fd(), data.data(), data.size())) { 95 LOG(ERROR) << "failed to write file " << path; 96 return false; 97 } 98 } 99 return true; 100} 101 102class SavedPerfHardenProperty { 103 public: 104 SavedPerfHardenProperty() { 105 __system_property_get("security.perf_harden", prop_value_); 106 if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid", 107 ¶noid_value_)) { 108 PLOG(ERROR) << "failed to read /proc/sys/kernel/perf_event_paranoid"; 109 } 110 } 111 112 ~SavedPerfHardenProperty() { 113 if (strlen(prop_value_) != 0) { 114 __system_property_set("security.perf_harden", prop_value_); 115 // Sleep one second to wait for security.perf_harden changing 116 // /proc/sys/kernel/perf_event_paranoid. 117 sleep(1); 118 std::string paranoid_value; 119 if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid", 120 ¶noid_value)) { 121 PLOG(ERROR) << "failed to read /proc/sys/kernel/perf_event_paranoid"; 122 return; 123 } 124 if (paranoid_value_ != paranoid_value) { 125 LOG(ERROR) << "failed to restore /proc/sys/kernel/perf_event_paranoid"; 126 } 127 } 128 } 129 130 private: 131 char prop_value_[PROP_VALUE_MAX]; 132 std::string paranoid_value_; 133}; 134 135#endif // defined(__ANDROID__) 136 137int main(int argc, char** argv) { 138 InitLogging(argv, android::base::StderrLogger); 139 testing::InitGoogleTest(&argc, argv); 140 android::base::LogSeverity log_severity = android::base::WARNING; 141 142 for (int i = 1; i < argc; ++i) { 143 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) { 144 testdata_dir = argv[i + 1]; 145 i++; 146 } else if (strcmp(argv[i], "--log") == 0) { 147 if (i + 1 < argc) { 148 ++i; 149 if (!GetLogSeverity(argv[i], &log_severity)) { 150 LOG(ERROR) << "Unknown log severity: " << argv[i]; 151 return 1; 152 } 153 } else { 154 LOG(ERROR) << "Missing argument for --log option.\n"; 155 return 1; 156 } 157 } 158 } 159 android::base::ScopedLogSeverity severity(log_severity); 160 161#if defined(__ANDROID__) 162 std::unique_ptr<TemporaryDir> tmp_dir; 163 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { 164 testdata_dir = std::string(dirname(argv[0])) + "/testdata"; 165 if (!IsDir(testdata_dir)) { 166 tmp_dir.reset(new TemporaryDir); 167 testdata_dir = std::string(tmp_dir->path) + "/"; 168 if (!ExtractTestDataFromElfSection()) { 169 LOG(ERROR) << "failed to extract test data from elf section"; 170 return 1; 171 } 172 } 173 } 174 175 // A cts test PerfEventParanoidTest.java is testing if 176 // /proc/sys/kernel/perf_event_paranoid is 3, so restore perf_harden 177 // value after current test to not break that test. 178 SavedPerfHardenProperty saved_perf_harden; 179#endif 180 181 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { 182 printf("Usage: %s -t <testdata_dir>\n", argv[0]); 183 return 1; 184 } 185 if (testdata_dir.back() != '/') { 186 testdata_dir.push_back('/'); 187 } 188 LOG(INFO) << "testdata is in " << testdata_dir; 189 return RUN_ALL_TESTS(); 190} 191 192std::string GetTestData(const std::string& filename) { 193 return testdata_dir + filename; 194} 195 196const std::string& GetTestDataDir() { 197 return testdata_dir; 198} 199