blob: dc789aa292ca91161b44ead3f09ac2e87479f061 [file] [log] [blame]
David Sehrb2ec9f52018-02-21 13:20:31 -08001/*
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
Orion Hodsondd732cc2021-01-15 16:31:30 +000019#include "android-base/stringprintf.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080020#include "base/file_utils.h"
21#include "base/memory_tool.h"
22#include "common_runtime_test.h"
23
24namespace art {
25
26std::string PrettyArguments(const char* signature);
27std::string PrettyReturnType(const char* signature);
28
29class ExecUtilsTest : public CommonRuntimeTest {};
30
31TEST_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;
Roland Levillain0b0d3b42018-06-14 13:55:49 +010040 // 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));
David Sehrb2ec9f52018-02-21 13:20:31 -080043 EXPECT_EQ(0U, error_msg.size()) << error_msg;
44}
45
46TEST_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;
Roland Levillain0b0d3b42018-06-14 13:55:49 +010053 // 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());
David Sehrb2ec9f52018-02-21 13:20:31 -080057}
58
59TEST_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;
Roland Levillain0b0d3b42018-06-14 13:55:49 +010074 // 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;
David Sehrb2ec9f52018-02-21 13:20:31 -080078}
79
80TEST_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;
Roland Levillain0b0d3b42018-06-14 13:55:49 +010098 // 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;
David Sehrb2ec9f52018-02-21 13:20:31 -0800102 // Restore the variable's value.
103 EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
104}
105
Orion Hodsondd732cc2021-01-15 16:31:30 +0000106static 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
117TEST_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
127TEST_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
David Sehrb2ec9f52018-02-21 13:20:31 -0800137} // namespace art