blob: 444a1c2af3dc4dc5b6f2e0b7cba7c5ee9c296b47 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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 Cuib7724262016-03-02 13:56:28 -080019#include <memory>
20
21#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080022#include <android-base/logging.h>
Yabin Cuib7724262016-03-02 13:56:28 -080023#include <android-base/test_utils.h>
24#include <ziparchive/zip_archive.h>
Yabin Cui323e9452015-04-20 18:07:17 -070025
Yabin Cui97ad0352016-06-10 12:15:11 -070026#if defined(__ANDROID__)
27#include <sys/system_properties.h>
28#endif
29
Yabin Cui0bd2a5c2016-02-05 17:32:08 -080030#include "get_test_data.h"
Yabin Cuib7724262016-03-02 13:56:28 -080031#include "read_elf.h"
Yabin Cui0bd2a5c2016-02-05 17:32:08 -080032#include "utils.h"
33
34static std::string testdata_dir;
35
Yabin Cuib7724262016-03-02 13:56:28 -080036#if defined(IN_CTS_TEST)
37static const std::string testdata_section = ".testzipdata";
38
39static 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 Cuicb6b3872016-03-18 18:47:43 -070066 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
Yabin Cuib7724262016-03-02 13:56:28 -080067 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 Cuib7724262016-03-02 13:56:28 -080095 return true;
96}
Yabin Cui97ad0352016-06-10 12:15:11 -070097
98#if defined(__ANDROID__)
99class 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 &paranoid_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 &paranoid_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 Cuib7724262016-03-02 13:56:28 -0800135#endif // defined(IN_CTS_TEST)
136
Yabin Cui323e9452015-04-20 18:07:17 -0700137int main(int argc, char** argv) {
138 InitLogging(argv, android::base::StderrLogger);
139 testing::InitGoogleTest(&argc, argv);
Yabin Cuicb6b3872016-03-18 18:47:43 -0700140 android::base::LogSeverity log_severity = android::base::WARNING;
Yabin Cuib7724262016-03-02 13:56:28 -0800141
Yabin Cui0bd2a5c2016-02-05 17:32:08 -0800142 for (int i = 1; i < argc; ++i) {
143 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
144 testdata_dir = argv[i + 1];
Yabin Cuib7724262016-03-02 13:56:28 -0800145 i++;
Yabin Cuicb6b3872016-03-18 18:47:43 -0700146 } 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 Cui0bd2a5c2016-02-05 17:32:08 -0800157 }
158 }
Yabin Cuicb6b3872016-03-18 18:47:43 -0700159 android::base::ScopedLogSeverity severity(log_severity);
Yabin Cuib7724262016-03-02 13:56:28 -0800160
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 Cui97ad0352016-06-10 12:15:11 -0700171
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 Cuib7724262016-03-02 13:56:28 -0800178#endif
179 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
180 printf("Usage: %s -t <testdata_dir>\n", argv[0]);
Yabin Cui0bd2a5c2016-02-05 17:32:08 -0800181 return 1;
182 }
183 if (testdata_dir.back() != '/') {
184 testdata_dir.push_back('/');
185 }
Yabin Cuib7724262016-03-02 13:56:28 -0800186 LOG(INFO) << "testdata is in " << testdata_dir;
Yabin Cui323e9452015-04-20 18:07:17 -0700187 return RUN_ALL_TESTS();
188}
Yabin Cui0bd2a5c2016-02-05 17:32:08 -0800189
190std::string GetTestData(const std::string& filename) {
191 return testdata_dir + filename;
192}
Yabin Cui72885012016-02-14 19:18:02 -0800193
194const std::string& GetTestDataDir() {
195 return testdata_dir;
196}