Home | History | Annotate | Download | only in runtime
      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 
     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   if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
     40     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
     41     EXPECT_TRUE(Exec(command, &error_msg));
     42   }
     43   EXPECT_EQ(0U, error_msg.size()) << error_msg;
     44 }
     45 
     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   if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
     54     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
     55     EXPECT_FALSE(Exec(command, &error_msg));
     56     EXPECT_FALSE(error_msg.empty());
     57   }
     58 }
     59 
     60 TEST_F(ExecUtilsTest, EnvSnapshotAdditionsAreNotVisible) {
     61   static constexpr const char* kModifiedVariable = "EXEC_SHOULD_NOT_EXPORT_THIS";
     62   static constexpr int kOverwrite = 1;
     63   // Set an variable in the current environment.
     64   EXPECT_EQ(setenv(kModifiedVariable, "NEVER", kOverwrite), 0);
     65   // Test that it is not exported.
     66   std::vector<std::string> command;
     67   if (kIsTargetBuild) {
     68     std::string android_root(GetAndroidRoot());
     69     command.push_back(android_root + "/bin/printenv");
     70   } else {
     71     command.push_back("/usr/bin/printenv");
     72   }
     73   command.push_back(kModifiedVariable);
     74   std::string error_msg;
     75   if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
     76     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
     77     EXPECT_FALSE(Exec(command, &error_msg));
     78     EXPECT_NE(0U, error_msg.size()) << error_msg;
     79   }
     80 }
     81 
     82 TEST_F(ExecUtilsTest, EnvSnapshotDeletionsAreNotVisible) {
     83   static constexpr const char* kDeletedVariable = "PATH";
     84   static constexpr int kOverwrite = 1;
     85   // Save the variable's value.
     86   const char* save_value = getenv(kDeletedVariable);
     87   EXPECT_NE(save_value, nullptr);
     88   // Delete the variable.
     89   EXPECT_EQ(unsetenv(kDeletedVariable), 0);
     90   // Test that it is not exported.
     91   std::vector<std::string> command;
     92   if (kIsTargetBuild) {
     93     std::string android_root(GetAndroidRoot());
     94     command.push_back(android_root + "/bin/printenv");
     95   } else {
     96     command.push_back("/usr/bin/printenv");
     97   }
     98   command.push_back(kDeletedVariable);
     99   std::string error_msg;
    100   if (!(RUNNING_ON_MEMORY_TOOL && kMemoryToolDetectsLeaks)) {
    101     // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
    102     EXPECT_TRUE(Exec(command, &error_msg));
    103     EXPECT_EQ(0U, error_msg.size()) << error_msg;
    104   }
    105   // Restore the variable's value.
    106   EXPECT_EQ(setenv(kDeletedVariable, save_value, kOverwrite), 0);
    107 }
    108 
    109 }  // namespace art
    110