1 //===- unittest/Support/ProgramTest.cpp -----------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/CommandLine.h" 11 #include "llvm/Support/Path.h" 12 #include "llvm/Support/Program.h" 13 #include "gtest/gtest.h" 14 15 #include <stdlib.h> 16 17 namespace { 18 19 using namespace llvm; 20 using namespace sys; 21 22 static cl::opt<std::string> 23 ProgramTestStringArg1("program-test-string-arg1"); 24 static cl::opt<std::string> 25 ProgramTestStringArg2("program-test-string-arg2"); 26 27 static void CopyEnvironment(std::vector<const char *> out) { 28 // environ appears to be pretty portable. 29 char **envp = environ; 30 while (*envp != 0) { 31 out.push_back(*envp); 32 ++envp; 33 } 34 } 35 36 TEST(ProgramTest, CreateProcessTrailingSlash) { 37 if (getenv("LLVM_PROGRAM_TEST_CHILD")) { 38 if (ProgramTestStringArg1 == "has\\\\ trailing\\" && 39 ProgramTestStringArg2 == "has\\\\ trailing\\") { 40 exit(0); // Success! The arguments were passed and parsed. 41 } 42 exit(1); 43 } 44 45 // FIXME: Hardcoding argv0 here since I don't know a good cross-platform way 46 // to get it. Maybe ParseCommandLineOptions() should save it? 47 Path my_exe = Path::GetMainExecutable("SupportTests", &ProgramTestStringArg1); 48 const char *argv[] = { 49 my_exe.c_str(), 50 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild", 51 "-program-test-string-arg1", "has\\\\ trailing\\", 52 "-program-test-string-arg2", "has\\\\ trailing\\", 53 0 54 }; 55 56 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child. 57 std::vector<const char *> envp; 58 CopyEnvironment(envp); 59 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1"); 60 envp.push_back(0); 61 62 std::string error; 63 bool ExecutionFailed; 64 // Redirect stdout and stdin to NUL, but let stderr through. 65 #ifdef LLVM_ON_WIN32 66 Path nul("NUL"); 67 #else 68 Path nul("/dev/null"); 69 #endif 70 const Path *redirects[] = { &nul, &nul, 0 }; 71 int rc = Program::ExecuteAndWait(my_exe, argv, &envp[0], redirects, 72 /*secondsToWait=*/10, /*memoryLimit=*/0, 73 &error, &ExecutionFailed); 74 EXPECT_FALSE(ExecutionFailed) << error; 75 EXPECT_EQ(0, rc); 76 } 77 78 } // end anonymous namespace 79