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>
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/test_utils.h>
26 #include <ziparchive/zip_archive.h>
28 #if defined(__ANDROID__)
29 #include <sys/system_properties.h>
32 #include "get_test_data.h"
36 static std::string testdata_dir;
38 #if defined(__ANDROID__)
39 static const std::string testdata_section = ".testzipdata";
41 static bool ExtractTestDataFromElfSection() {
42 if (!MkdirWithParents(testdata_dir)) {
43 PLOG(ERROR) << "failed to create testdata_dir " << testdata_dir;
47 ElfStatus result = ReadSectionFromElfFile("/proc/self/exe", testdata_section, &content);
48 if (result != ElfStatus::NO_ERROR) {
49 LOG(ERROR) << "failed to read section " << testdata_section
53 TemporaryFile tmp_file;
54 if (!android::base::WriteStringToFile(content, tmp_file.path)) {
55 PLOG(ERROR) << "failed to write file " << tmp_file.path;
58 ArchiveHelper ahelper(tmp_file.fd, tmp_file.path);
60 LOG(ERROR) << "failed to open archive " << tmp_file.path;
63 ZipArchiveHandle& handle = ahelper.archive_handle();
65 int ret = StartIteration(handle, &cookie, nullptr, nullptr);
67 LOG(ERROR) << "failed to start iterating zip entries";
70 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
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;
77 if (path.back() == '/') {
80 if (!MkdirWithParents(path)) {
81 LOG(ERROR) << "failed to create dir for " << path;
84 FileHelper fhelper = FileHelper::OpenWriteOnly(path);
86 PLOG(ERROR) << "failed to create file " << path;
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;
94 if (!android::base::WriteFully(fhelper.fd(), data.data(), data.size())) {
95 LOG(ERROR) << "failed to write file " << path;
102 class ScopedEnablingPerf {
104 ScopedEnablingPerf() {
105 memset(prop_value_, '\0', sizeof(prop_value_));
106 __system_property_get("security.perf_harden", prop_value_);
110 ~ScopedEnablingPerf() {
111 if (strlen(prop_value_) != 0) {
112 SetProp(prop_value_);
117 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.
124 char prop_value_[PROP_VALUE_MAX];
127 static 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";
135 std::string copy_cmd = android::base::StringPrintf("run-as com.android.simpleperf cp %s .",
137 if (system(copy_cmd.c_str()) == -1) {
138 PLOG(ERROR) << "system(" << copy_cmd << ") failed";
142 arg_str += basename(argv[0]);
143 for (int i = 1; i < argc; ++i) {
144 arg_str.push_back(' ');
147 std::string test_cmd = android::base::StringPrintf("run-as com.android.simpleperf ./%s",
149 test_cmd += " --in-app-context";
150 if (system(test_cmd.c_str()) == -1) {
151 PLOG(ERROR) << "system(" << test_cmd << ") failed";
157 #endif // defined(__ANDROID__)
159 int main(int argc, char** argv) {
160 android::base::InitLogging(argv, android::base::StderrLogger);
161 android::base::LogSeverity log_severity = android::base::WARNING;
162 bool need_app_context __attribute__((unused)) = false;
163 bool in_app_context __attribute__((unused)) = false;
165 #if defined(RUN_IN_APP_CONTEXT)
166 need_app_context = true;
169 for (int i = 1; i < argc; ++i) {
170 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
171 testdata_dir = argv[i + 1];
173 } else if (strcmp(argv[i], "--log") == 0) {
176 if (!GetLogSeverity(argv[i], &log_severity)) {
177 LOG(ERROR) << "Unknown log severity: " << argv[i];
181 LOG(ERROR) << "Missing argument for --log option.\n";
184 } else if (strcmp(argv[i], "--in-app-context") == 0) {
185 in_app_context = true;
188 android::base::ScopedLogSeverity severity(log_severity);
190 #if defined(__ANDROID__)
191 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);
199 if (need_app_context && !in_app_context) {
200 return TestInAppContext(argc, argv) ? 0 : 1;
203 std::unique_ptr<TemporaryDir> tmp_dir;
204 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
205 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";
218 testing::InitGoogleTest(&argc, argv);
219 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
220 printf("Usage: %s -t <testdata_dir>\n", argv[0]);
223 if (testdata_dir.back() != '/') {
224 testdata_dir.push_back('/');
226 LOG(INFO) << "testdata is in " << testdata_dir;
227 return RUN_ALL_TESTS();
230 std::string GetTestData(const std::string& filename) {
231 return testdata_dir + filename;
234 const std::string& GetTestDataDir() {