Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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 | |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 19 | #include <memory> |
| 20 | |
| 21 | #include <android-base/file.h> |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 23 | #include <android-base/test_utils.h> |
| 24 | #include <ziparchive/zip_archive.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 25 | |
Yabin Cui | 97ad035 | 2016-06-10 12:15:11 -0700 | [diff] [blame] | 26 | #if defined(__ANDROID__) |
| 27 | #include <sys/system_properties.h> |
| 28 | #endif |
| 29 | |
Yabin Cui | 0bd2a5c | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 30 | #include "get_test_data.h" |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 31 | #include "read_elf.h" |
Yabin Cui | 0bd2a5c | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 32 | #include "utils.h" |
| 33 | |
| 34 | static std::string testdata_dir; |
| 35 | |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 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 | } |
Yabin Cui | cb6b387 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 66 | std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 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 | } |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 95 | return true; |
| 96 | } |
Yabin Cui | 97ad035 | 2016-06-10 12:15:11 -0700 | [diff] [blame] | 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__) |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 135 | #endif // defined(IN_CTS_TEST) |
| 136 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 137 | int main(int argc, char** argv) { |
| 138 | InitLogging(argv, android::base::StderrLogger); |
| 139 | testing::InitGoogleTest(&argc, argv); |
Yabin Cui | cb6b387 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 140 | android::base::LogSeverity log_severity = android::base::WARNING; |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 141 | |
Yabin Cui | 0bd2a5c | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 142 | for (int i = 1; i < argc; ++i) { |
| 143 | if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) { |
| 144 | testdata_dir = argv[i + 1]; |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 145 | i++; |
Yabin Cui | cb6b387 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 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 | } |
Yabin Cui | 0bd2a5c | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
Yabin Cui | cb6b387 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 159 | android::base::ScopedLogSeverity severity(log_severity); |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 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 | } |
Yabin Cui | 97ad035 | 2016-06-10 12:15:11 -0700 | [diff] [blame] | 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 |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 178 | #endif |
| 179 | if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) { |
| 180 | printf("Usage: %s -t <testdata_dir>\n", argv[0]); |
Yabin Cui | 0bd2a5c | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 181 | return 1; |
| 182 | } |
| 183 | if (testdata_dir.back() != '/') { |
| 184 | testdata_dir.push_back('/'); |
| 185 | } |
Yabin Cui | b772426 | 2016-03-02 13:56:28 -0800 | [diff] [blame] | 186 | LOG(INFO) << "testdata is in " << testdata_dir; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 187 | return RUN_ALL_TESTS(); |
| 188 | } |
Yabin Cui | 0bd2a5c | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 189 | |
| 190 | std::string GetTestData(const std::string& filename) { |
| 191 | return testdata_dir + filename; |
| 192 | } |
Yabin Cui | 7288501 | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 193 | |
| 194 | const std::string& GetTestDataDir() { |
| 195 | return testdata_dir; |
| 196 | } |