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 #if defined(__ANDROID__) 27 #include <sys/system_properties.h> 28 #endif 29 30 #include "get_test_data.h" 31 #include "read_elf.h" 32 #include "utils.h" 33 34 static std::string testdata_dir; 35 36 #if defined(IN_CTS_TEST) 37 static const std::string testdata_section = ".testzipdata"; 38 39 static bool ExtractTestDataFromElfSection() { 40 if (!MkdirWithParents(testdata_dir)) { 41 PLOG(ERROR) << "failed to create testdata_dir " << testdata_dir; 42 return false; 43 } 44 std::string content; 45 if (!ReadSectionFromElfFile("/proc/self/exe", testdata_section, &content)) { 46 LOG(ERROR) << "failed to read section " << testdata_section; 47 return false; 48 } 49 TemporaryFile tmp_file; 50 if (!android::base::WriteStringToFile(content, tmp_file.path)) { 51 PLOG(ERROR) << "failed to write file " << tmp_file.path; 52 return false; 53 } 54 ArchiveHelper ahelper(tmp_file.fd, tmp_file.path); 55 if (!ahelper) { 56 LOG(ERROR) << "failed to open archive " << tmp_file.path; 57 return false; 58 } 59 ZipArchiveHandle& handle = ahelper.archive_handle(); 60 void* cookie; 61 int ret = StartIteration(handle, &cookie, nullptr, nullptr); 62 if (ret != 0) { 63 LOG(ERROR) << "failed to start iterating zip entries"; 64 return false; 65 } 66 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); 67 ZipEntry entry; 68 ZipString name; 69 while (Next(cookie, &entry, &name) == 0) { 70 std::string entry_name(name.name, name.name + name.name_length); 71 std::string path = testdata_dir + entry_name; 72 // Skip dir. 73 if (path.back() == '/') { 74 continue; 75 } 76 if (!MkdirWithParents(path)) { 77 LOG(ERROR) << "failed to create dir for " << path; 78 return false; 79 } 80 FileHelper fhelper = FileHelper::OpenWriteOnly(path); 81 if (!fhelper) { 82 PLOG(ERROR) << "failed to create file " << path; 83 return false; 84 } 85 std::vector<uint8_t> data(entry.uncompressed_length); 86 if (ExtractToMemory(handle, &entry, data.data(), data.size()) != 0) { 87 LOG(ERROR) << "failed to extract entry " << entry_name; 88 return false; 89 } 90 if (!android::base::WriteFully(fhelper.fd(), data.data(), data.size())) { 91 LOG(ERROR) << "failed to write file " << path; 92 return false; 93 } 94 } 95 return true; 96 } 97 98 #if defined(__ANDROID__) 99 class SavedPerfHardenProperty { 100 public: 101 SavedPerfHardenProperty() { 102 __system_property_get("security.perf_harden", prop_value_); 103 if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid", 104 ¶noid_value_)) { 105 PLOG(ERROR) << "failed to read /proc/sys/kernel/perf_event_paranoid"; 106 } 107 } 108 109 ~SavedPerfHardenProperty() { 110 if (strlen(prop_value_) != 0) { 111 if (__system_property_set("security.perf_harden", prop_value_) != 0) { 112 PLOG(ERROR) << "failed to set security.perf_harden"; 113 return; 114 } 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 #endif // defined(__ANDROID__) 135 #endif // defined(IN_CTS_TEST) 136 137 int 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(IN_CTS_TEST) 162 std::unique_ptr<TemporaryDir> tmp_dir; 163 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { 164 tmp_dir.reset(new TemporaryDir); 165 testdata_dir = std::string(tmp_dir->path) + "/"; 166 if (!ExtractTestDataFromElfSection()) { 167 LOG(ERROR) << "failed to extract test data from elf section"; 168 return 1; 169 } 170 } 171 172 #if defined(__ANDROID__) 173 // A cts test PerfEventParanoidTest.java is testing if 174 // /proc/sys/kernel/perf_event_paranoid is 3, so restore perf_harden 175 // value after current test to not break that test. 176 SavedPerfHardenProperty saved_perf_harden; 177 #endif 178 #endif 179 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { 180 printf("Usage: %s -t <testdata_dir>\n", argv[0]); 181 return 1; 182 } 183 if (testdata_dir.back() != '/') { 184 testdata_dir.push_back('/'); 185 } 186 LOG(INFO) << "testdata is in " << testdata_dir; 187 return RUN_ALL_TESTS(); 188 } 189 190 std::string GetTestData(const std::string& filename) { 191 return testdata_dir + filename; 192 } 193 194 const std::string& GetTestDataDir() { 195 return testdata_dir; 196 } 197