blob: 3cc929c85abbdbf70624fad8551b793b5591b838 [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
Colin Cross1e8ad072016-12-17 12:38:46 -080019#include <libgen.h>
20
Yabin Cuibe7ec662016-03-02 13:56:28 -080021#include <memory>
22
23#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080024#include <android-base/logging.h>
Yabin Cuibe7ec662016-03-02 13:56:28 -080025#include <android-base/test_utils.h>
26#include <ziparchive/zip_archive.h>
Yabin Cui323e9452015-04-20 18:07:17 -070027
Yabin Cui58961322016-06-10 12:15:11 -070028#if defined(__ANDROID__)
29#include <sys/system_properties.h>
30#endif
31
Yabin Cui569f64a2016-02-05 17:32:08 -080032#include "get_test_data.h"
Yabin Cuibe7ec662016-03-02 13:56:28 -080033#include "read_elf.h"
Yabin Cui569f64a2016-02-05 17:32:08 -080034#include "utils.h"
35
36static std::string testdata_dir;
37
Yabin Cui6acf8c62016-03-18 13:48:42 -070038#if defined(__ANDROID__)
Yabin Cuibe7ec662016-03-02 13:56:28 -080039static 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;
Yabin Cuidec43c12016-07-29 16:40:40 -070047 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;
Yabin Cuibe7ec662016-03-02 13:56:28 -080051 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 }
Yabin Cui8f680f62016-03-18 18:47:43 -070070 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
Yabin Cuibe7ec662016-03-02 13:56:28 -080071 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 }
Yabin Cuibe7ec662016-03-02 13:56:28 -080099 return true;
100}
Yabin Cui58961322016-06-10 12:15:11 -0700101
Yabin Cuif560a6f2016-12-14 17:43:26 -0800102class ScopedEnablingPerf {
Yabin Cui58961322016-06-10 12:15:11 -0700103 public:
Yabin Cuif560a6f2016-12-14 17:43:26 -0800104 ScopedEnablingPerf() {
105 memset(prop_value_, '\0', sizeof(prop_value_));
Yabin Cui58961322016-06-10 12:15:11 -0700106 __system_property_get("security.perf_harden", prop_value_);
Yabin Cuif560a6f2016-12-14 17:43:26 -0800107 SetProp("0");
Yabin Cui58961322016-06-10 12:15:11 -0700108 }
109
Yabin Cuif560a6f2016-12-14 17:43:26 -0800110 ~ScopedEnablingPerf() {
Yabin Cui58961322016-06-10 12:15:11 -0700111 if (strlen(prop_value_) != 0) {
Yabin Cuif560a6f2016-12-14 17:43:26 -0800112 SetProp(prop_value_);
Yabin Cui58961322016-06-10 12:15:11 -0700113 }
114 }
115
116 private:
Yabin Cuif560a6f2016-12-14 17:43:26 -0800117 void SetProp(const char* value) {
118 __system_property_set("security.perf_harden", value);
119 // Sleep one second to wait for security.perf_harden changing
120 // /proc/sys/kernel/perf_event_paranoid.
121 sleep(1);
122 }
123
Yabin Cui58961322016-06-10 12:15:11 -0700124 char prop_value_[PROP_VALUE_MAX];
Yabin Cui58961322016-06-10 12:15:11 -0700125};
126
Yabin Cuif560a6f2016-12-14 17:43:26 -0800127static bool TestInAppContext(int argc, char** argv) {
128 // Use run-as to move the test executable to the data directory of debuggable app
129 // 'com.android.simpleperf', and run it.
130 std::string exe_path;
131 if (!android::base::Readlink("/proc/self/exe", &exe_path)) {
132 PLOG(ERROR) << "readlink failed";
133 return false;
134 }
135 std::string copy_cmd = android::base::StringPrintf("run-as com.android.simpleperf cp %s .",
136 exe_path.c_str());
137 if (system(copy_cmd.c_str()) == -1) {
138 PLOG(ERROR) << "system(" << copy_cmd << ") failed";
139 return false;
140 }
141 std::string arg_str;
142 arg_str += basename(argv[0]);
143 for (int i = 1; i < argc; ++i) {
144 arg_str.push_back(' ');
145 arg_str += argv[i];
146 }
147 std::string test_cmd = android::base::StringPrintf("run-as com.android.simpleperf ./%s",
148 arg_str.c_str());
149 test_cmd += " --in-app-context";
150 if (system(test_cmd.c_str()) == -1) {
151 PLOG(ERROR) << "system(" << test_cmd << ") failed";
152 return false;
153 }
154 return true;
155}
156
Yabin Cui6acf8c62016-03-18 13:48:42 -0700157#endif // defined(__ANDROID__)
Yabin Cuibe7ec662016-03-02 13:56:28 -0800158
Yabin Cui323e9452015-04-20 18:07:17 -0700159int main(int argc, char** argv) {
Yabin Cuif560a6f2016-12-14 17:43:26 -0800160 android::base::InitLogging(argv, android::base::StderrLogger);
Yabin Cui8f680f62016-03-18 18:47:43 -0700161 android::base::LogSeverity log_severity = android::base::WARNING;
Yabin Cuif560a6f2016-12-14 17:43:26 -0800162 bool need_app_context __attribute__((unused)) = false;
163 bool in_app_context __attribute__((unused)) = false;
164
165#if defined(RUN_IN_APP_CONTEXT)
166 need_app_context = true;
167#endif
Yabin Cuibe7ec662016-03-02 13:56:28 -0800168
Yabin Cui569f64a2016-02-05 17:32:08 -0800169 for (int i = 1; i < argc; ++i) {
170 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
171 testdata_dir = argv[i + 1];
Yabin Cuibe7ec662016-03-02 13:56:28 -0800172 i++;
Yabin Cui8f680f62016-03-18 18:47:43 -0700173 } else if (strcmp(argv[i], "--log") == 0) {
174 if (i + 1 < argc) {
175 ++i;
176 if (!GetLogSeverity(argv[i], &log_severity)) {
177 LOG(ERROR) << "Unknown log severity: " << argv[i];
178 return 1;
179 }
180 } else {
181 LOG(ERROR) << "Missing argument for --log option.\n";
182 return 1;
183 }
Yabin Cuif560a6f2016-12-14 17:43:26 -0800184 } else if (strcmp(argv[i], "--in-app-context") == 0) {
185 in_app_context = true;
Yabin Cui569f64a2016-02-05 17:32:08 -0800186 }
187 }
Yabin Cui8f680f62016-03-18 18:47:43 -0700188 android::base::ScopedLogSeverity severity(log_severity);
Yabin Cuibe7ec662016-03-02 13:56:28 -0800189
Yabin Cui6acf8c62016-03-18 13:48:42 -0700190#if defined(__ANDROID__)
Yabin Cuif560a6f2016-12-14 17:43:26 -0800191 std::unique_ptr<ScopedEnablingPerf> scoped_enabling_perf;
192 if (!in_app_context) {
193 // A cts test PerfEventParanoidTest.java is testing if
194 // /proc/sys/kernel/perf_event_paranoid is 3, so restore perf_harden
195 // value after current test to not break that test.
196 scoped_enabling_perf.reset(new ScopedEnablingPerf);
197 }
198
199 if (need_app_context && !in_app_context) {
200 return TestInAppContext(argc, argv) ? 0 : 1;
201 }
202
Yabin Cuibe7ec662016-03-02 13:56:28 -0800203 std::unique_ptr<TemporaryDir> tmp_dir;
204 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
Colin Cross1e8ad072016-12-17 12:38:46 -0800205 testdata_dir = std::string(dirname(argv[0])) + "/testdata";
206 if (!IsDir(testdata_dir)) {
207 tmp_dir.reset(new TemporaryDir);
208 testdata_dir = std::string(tmp_dir->path) + "/";
209 if (!ExtractTestDataFromElfSection()) {
210 LOG(ERROR) << "failed to extract test data from elf section";
211 return 1;
212 }
Yabin Cuibe7ec662016-03-02 13:56:28 -0800213 }
214 }
Yabin Cui58961322016-06-10 12:15:11 -0700215
Yabin Cuibe7ec662016-03-02 13:56:28 -0800216#endif
Yabin Cui6acf8c62016-03-18 13:48:42 -0700217
Yabin Cuif560a6f2016-12-14 17:43:26 -0800218 testing::InitGoogleTest(&argc, argv);
Yabin Cuibe7ec662016-03-02 13:56:28 -0800219 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
220 printf("Usage: %s -t <testdata_dir>\n", argv[0]);
Yabin Cui569f64a2016-02-05 17:32:08 -0800221 return 1;
222 }
223 if (testdata_dir.back() != '/') {
224 testdata_dir.push_back('/');
225 }
Yabin Cuibe7ec662016-03-02 13:56:28 -0800226 LOG(INFO) << "testdata is in " << testdata_dir;
Yabin Cui323e9452015-04-20 18:07:17 -0700227 return RUN_ALL_TESTS();
228}
Yabin Cui569f64a2016-02-05 17:32:08 -0800229
230std::string GetTestData(const std::string& filename) {
231 return testdata_dir + filename;
232}
Yabin Cuib1a885b2016-02-14 19:18:02 -0800233
234const std::string& GetTestDataDir() {
235 return testdata_dir;
236}