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
19 #include <libgen.h>
20
21 #include <memory>
22
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/strings.h>
26
27 #if defined(__ANDROID__)
28 #include <android-base/properties.h>
29 #endif
30
31 #include "command.h"
32 #include "environment.h"
33 #include "get_test_data.h"
34 #include "read_elf.h"
35 #include "test_util.h"
36 #include "utils.h"
37 #include "workload.h"
38
39 static std::string testdata_dir;
40
41 #if defined(__ANDROID__)
42
43 class ScopedEnablingPerf {
44 public:
ScopedEnablingPerf()45 ScopedEnablingPerf() {
46 prop_value_ = android::base::GetProperty("security.perf_harden", "");
47 SetProp("0");
48 }
49
~ScopedEnablingPerf()50 ~ScopedEnablingPerf() {
51 if (!prop_value_.empty()) {
52 SetProp(prop_value_);
53 }
54 }
55
56 private:
SetProp(const std::string & value)57 void SetProp(const std::string& value) {
58 android::base::SetProperty("security.perf_harden", value);
59
60 // Sleep one second to wait for security.perf_harden changing
61 // /proc/sys/kernel/perf_event_paranoid.
62 sleep(1);
63 }
64
65 std::string prop_value_;
66 };
67
68 #endif // defined(__ANDROID__)
69
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71 android::base::InitLogging(argv, android::base::StderrLogger);
72 android::base::LogSeverity log_severity = android::base::WARNING;
73 testdata_dir = std::string(dirname(argv[0])) + "/testdata";
74 for (int i = 1; i < argc; ++i) {
75 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
76 testdata_dir = argv[i + 1];
77 i++;
78 } else if (strcmp(argv[i], "--log") == 0) {
79 if (i + 1 < argc) {
80 ++i;
81 if (!GetLogSeverity(argv[i], &log_severity)) {
82 LOG(ERROR) << "Unknown log severity: " << argv[i];
83 return 1;
84 }
85 } else {
86 LOG(ERROR) << "Missing argument for --log option.\n";
87 return 1;
88 }
89 }
90 }
91 android::base::ScopedLogSeverity severity(log_severity);
92
93 #if defined(__ANDROID__)
94 // A cts test PerfEventParanoidTest.java is testing if
95 // /proc/sys/kernel/perf_event_paranoid is 3, so restore perf_harden
96 // value after current test to not break that test.
97 ScopedEnablingPerf scoped_enabling_perf;
98 #endif
99
100 testing::InitGoogleTest(&argc, argv);
101 if (!::testing::GTEST_FLAG(list_tests)) {
102 if (!IsDir(testdata_dir)) {
103 LOG(ERROR) << "testdata wasn't found. Use \"" << argv[0] << " -t <testdata_dir>\"";
104 return 1;
105 }
106 }
107 if (!android::base::EndsWith(testdata_dir, OS_PATH_SEPARATOR)) {
108 testdata_dir += OS_PATH_SEPARATOR;
109 }
110 LOG(INFO) << "testdata is in " << testdata_dir;
111 return RUN_ALL_TESTS();
112 }
113
GetTestData(const std::string & filename)114 std::string GetTestData(const std::string& filename) {
115 return testdata_dir + filename;
116 }
117
GetTestDataDir()118 const std::string& GetTestDataDir() {
119 return testdata_dir;
120 }
121