2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
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>
26 #if defined(__ANDROID__)
27 #include <sys/system_properties.h>
30 #include "get_test_data.h"
34 static std::string testdata_dir;
36 #if defined(IN_CTS_TEST)
37 static const std::string testdata_section = ".testzipdata";
39 static bool ExtractTestDataFromElfSection() {
40 if (!MkdirWithParents(testdata_dir)) {
41 PLOG(ERROR) << "failed to create testdata_dir " << testdata_dir;
45 if (!ReadSectionFromElfFile("/proc/self/exe", testdata_section, &content)) {
46 LOG(ERROR) << "failed to read section " << testdata_section;
49 TemporaryFile tmp_file;
50 if (!android::base::WriteStringToFile(content, tmp_file.path)) {
51 PLOG(ERROR) << "failed to write file " << tmp_file.path;
54 ArchiveHelper ahelper(tmp_file.fd, tmp_file.path);
56 LOG(ERROR) << "failed to open archive " << tmp_file.path;
59 ZipArchiveHandle& handle = ahelper.archive_handle();
61 int ret = StartIteration(handle, &cookie, nullptr, nullptr);
63 LOG(ERROR) << "failed to start iterating zip entries";
66 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
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;
73 if (path.back() == '/') {
76 if (!MkdirWithParents(path)) {
77 LOG(ERROR) << "failed to create dir for " << path;
80 FileHelper fhelper = FileHelper::OpenWriteOnly(path);
82 PLOG(ERROR) << "failed to create file " << path;
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;
90 if (!android::base::WriteFully(fhelper.fd(), data.data(), data.size())) {
91 LOG(ERROR) << "failed to write file " << path;
98 #if defined(__ANDROID__)
99 class SavedPerfHardenProperty {
101 SavedPerfHardenProperty() {
102 __system_property_get("security.perf_harden", prop_value_);
103 if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid",
105 PLOG(ERROR) << "failed to read /proc/sys/kernel/perf_event_paranoid";
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";
115 // Sleep one second to wait for security.perf_harden changing
116 // /proc/sys/kernel/perf_event_paranoid.
118 std::string paranoid_value;
119 if (!android::base::ReadFileToString("/proc/sys/kernel/perf_event_paranoid",
121 PLOG(ERROR) << "failed to read /proc/sys/kernel/perf_event_paranoid";
124 if (paranoid_value_ != paranoid_value) {
125 LOG(ERROR) << "failed to restore /proc/sys/kernel/perf_event_paranoid";
131 char prop_value_[PROP_VALUE_MAX];
132 std::string paranoid_value_;
134 #endif // defined(__ANDROID__)
135 #endif // defined(IN_CTS_TEST)
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;
142 for (int i = 1; i < argc; ++i) {
143 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
144 testdata_dir = argv[i + 1];
146 } else if (strcmp(argv[i], "--log") == 0) {
149 if (!GetLogSeverity(argv[i], &log_severity)) {
150 LOG(ERROR) << "Unknown log severity: " << argv[i];
154 LOG(ERROR) << "Missing argument for --log option.\n";
159 android::base::ScopedLogSeverity severity(log_severity);
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";
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;
179 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
180 printf("Usage: %s -t <testdata_dir>\n", argv[0]);
183 if (testdata_dir.back() != '/') {
184 testdata_dir.push_back('/');
186 LOG(INFO) << "testdata is in " << testdata_dir;
187 return RUN_ALL_TESTS();
190 std::string GetTestData(const std::string& filename) {
191 return testdata_dir + filename;
194 const std::string& GetTestDataDir() {