• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/file_utils.h"
20 #include "base/memory_tool.h"
21 #include "common_runtime_test.h"
22 
23 namespace art {
24 
25 std::string PrettyArguments(const char* signature);
26 std::string PrettyReturnType(const char* signature);
27 
28 class ExecUtilsTest : public CommonRuntimeTest {};
29 
TEST_F(ExecUtilsTest,ExecSuccess)30 TEST_F(ExecUtilsTest, ExecSuccess) {
31   std::vector<std::string> command;
32   if (kIsTargetBuild) {
33     std::string android_root(GetAndroidRoot());
34     command.push_back(android_root + "/bin/id");
35   } else {
36     command.push_back("/usr/bin/id");
37   }
38   std::string error_msg;
39   // Historical note: Running on Valgrind failed due to some memory
40   // that leaks in thread alternate signal stacks.
41   EXPECT_TRUE(Exec(command, &error_msg));
42   EXPECT_EQ(0U, error_msg.size()) << error_msg;
43 }
44 
TEST_F(ExecUtilsTest,ExecError)45 TEST_F(ExecUtilsTest, ExecError) {
46   // This will lead to error messages in the log.
47   ScopedLogSeverity sls(LogSeverity::FATAL);
48 
49   std::vector<std::string> command;
50   command.push_back("bogus");
51   std::string error_msg;
52   // Historical note: Running on Valgrind failed due to some memory
53   // that leaks in thread alternate signal stacks.
54   EXPECT_FALSE(Exec(command, &error_msg));
55   EXPECT_FALSE(error_msg.empty());
56 }
57 
TEST_F(ExecUtilsTest,EnvSnapshotAdditionsAreNotVisible)58 TEST_F(ExecUtilsTest, EnvSnapshotAdditionsAreNotVisible) {
59   static constexpr const char* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS";
60   static constexpr int kOverwrite = 1;
61   // Set an variable in the current environment.
62   EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0);
63   // Test that it is not exported.
64   std::vector<std::string> command;
65   if (kIsTargetBuild) {
66     std::string android_root(GetAndroidRoot());
67     command.push_back(android_root + "/bin/printenv");
68   } else {
69     command.push_back("/usr/bin/printenv");
70   }
71   command.push_back(kModifiedVariable);
72   std::string error_msg;
73   // Historical note: Running on Valgrind failed due to some memory
74   // that leaks in thread alternate signal stacks.
75   EXPECT_FALSE(Exec(command, &error_msg));
76   EXPECT_NE(0U, error_msg.size()) << error_msg;
77 }
78 
TEST_F(ExecUtilsTest,EnvSnapshotDeletionsAreNotVisible)79 TEST_F(ExecUtilsTest, EnvSnapshotDeletionsAreNotVisible) {
80   static constexpr const char* kDeletedVariable = "PATH";
81   static constexpr int kOverwrite = 1;
82   // Save the variable's value.
83   const char* save_value = getenv(kDeletedVariable);
84   EXPECT_NE(save_value, nullptr);
85   // Delete the variable.
86   EXPECT_EQ(unsetenv(kDeletedVariable), 0);
87   // Test that it is not exported.
88   std::vector<std::string> command;
89   if (kIsTargetBuild) {
90     std::string android_root(GetAndroidRoot());
91     command.push_back(android_root + "/bin/printenv");
92   } else {
93     command.push_back("/usr/bin/printenv");
94   }
95   command.push_back(kDeletedVariable);
96   std::string error_msg;
97   // Historical note: Running on Valgrind failed due to some memory
98   // that leaks in thread alternate signal stacks.
99   EXPECT_TRUE(Exec(command, &error_msg));
100   EXPECT_EQ(0U, error_msg.size()) << error_msg;
101   // Restore the variable's value.
102   EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
103 }
104 
105 }  // namespace art
106