1 /*
2 * Copyright (C) 2011 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 "exec_utils.h"
18
19 #include "android-base/stringprintf.h"
20 #include "base/file_utils.h"
21 #include "base/memory_tool.h"
22 #include "common_runtime_test.h"
23
24 namespace art {
25
26 std::string PrettyArguments(const char* signature);
27 std::string PrettyReturnType(const char* signature);
28
29 class ExecUtilsTest : public CommonRuntimeTest {};
30
TEST_F(ExecUtilsTest,ExecSuccess)31 TEST_F(ExecUtilsTest, ExecSuccess) {
32 std::vector<std::string> command;
33 if (kIsTargetBuild) {
34 std::string android_root(GetAndroidRoot());
35 command.push_back(android_root + "/bin/id");
36 } else {
37 command.push_back("/usr/bin/id");
38 }
39 std::string error_msg;
40 // Historical note: Running on Valgrind failed due to some memory
41 // that leaks in thread alternate signal stacks.
42 EXPECT_TRUE(Exec(command, &error_msg));
43 EXPECT_EQ(0U, error_msg.size()) << error_msg;
44 }
45
TEST_F(ExecUtilsTest,ExecError)46 TEST_F(ExecUtilsTest, ExecError) {
47 // This will lead to error messages in the log.
48 ScopedLogSeverity sls(LogSeverity::FATAL);
49
50 std::vector<std::string> command;
51 command.push_back("bogus");
52 std::string error_msg;
53 // Historical note: Running on Valgrind failed due to some memory
54 // that leaks in thread alternate signal stacks.
55 EXPECT_FALSE(Exec(command, &error_msg));
56 EXPECT_FALSE(error_msg.empty());
57 }
58
TEST_F(ExecUtilsTest,EnvSnapshotAdditionsAreNotVisible)59 TEST_F(ExecUtilsTest, EnvSnapshotAdditionsAreNotVisible) {
60 static constexpr const char* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS";
61 static constexpr int kOverwrite = 1;
62 // Set an variable in the current environment.
63 EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0);
64 // Test that it is not exported.
65 std::vector<std::string> command;
66 if (kIsTargetBuild) {
67 std::string android_root(GetAndroidRoot());
68 command.push_back(android_root + "/bin/printenv");
69 } else {
70 command.push_back("/usr/bin/printenv");
71 }
72 command.push_back(kModifiedVariable);
73 std::string error_msg;
74 // Historical note: Running on Valgrind failed due to some memory
75 // that leaks in thread alternate signal stacks.
76 EXPECT_FALSE(Exec(command, &error_msg));
77 EXPECT_NE(0U, error_msg.size()) << error_msg;
78 }
79
TEST_F(ExecUtilsTest,EnvSnapshotDeletionsAreNotVisible)80 TEST_F(ExecUtilsTest, EnvSnapshotDeletionsAreNotVisible) {
81 static constexpr const char* kDeletedVariable = "PATH";
82 static constexpr int kOverwrite = 1;
83 // Save the variable's value.
84 const char* save_value = getenv(kDeletedVariable);
85 EXPECT_NE(save_value, nullptr);
86 // Delete the variable.
87 EXPECT_EQ(unsetenv(kDeletedVariable), 0);
88 // Test that it is not exported.
89 std::vector<std::string> command;
90 if (kIsTargetBuild) {
91 std::string android_root(GetAndroidRoot());
92 command.push_back(android_root + "/bin/printenv");
93 } else {
94 command.push_back("/usr/bin/printenv");
95 }
96 command.push_back(kDeletedVariable);
97 std::string error_msg;
98 // Historical note: Running on Valgrind failed due to some memory
99 // that leaks in thread alternate signal stacks.
100 EXPECT_TRUE(Exec(command, &error_msg));
101 EXPECT_EQ(0U, error_msg.size()) << error_msg;
102 // Restore the variable's value.
103 EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
104 }
105
SleepCommand(int sleep_seconds)106 static std::vector<std::string> SleepCommand(int sleep_seconds) {
107 std::vector<std::string> command;
108 if (kIsTargetBuild) {
109 command.push_back(GetAndroidRoot() + "/bin/sleep");
110 } else {
111 command.push_back("/bin/sleep");
112 }
113 command.push_back(android::base::StringPrintf("%d", sleep_seconds));
114 return command;
115 }
116
TEST_F(ExecUtilsTest,ExecTimeout)117 TEST_F(ExecUtilsTest, ExecTimeout) {
118 static constexpr int kSleepSeconds = 5;
119 static constexpr int kWaitSeconds = 1;
120 std::vector<std::string> command = SleepCommand(kSleepSeconds);
121 std::string error_msg;
122 bool timed_out;
123 ASSERT_EQ(ExecAndReturnCode(command, kWaitSeconds, &timed_out, &error_msg), -1);
124 EXPECT_TRUE(timed_out);
125 }
126
TEST_F(ExecUtilsTest,ExecNoTimeout)127 TEST_F(ExecUtilsTest, ExecNoTimeout) {
128 static constexpr int kSleepSeconds = 1;
129 static constexpr int kWaitSeconds = 5;
130 std::vector<std::string> command = SleepCommand(kSleepSeconds);
131 std::string error_msg;
132 bool timed_out;
133 ASSERT_EQ(ExecAndReturnCode(command, kWaitSeconds, &timed_out, &error_msg), 0);
134 EXPECT_FALSE(timed_out);
135 }
136
137 } // namespace art
138