blob: f132c033045eb22e62f0a233dc3e5e665174dfad [file] [log] [blame]
Reid Kleckner0b675d82013-04-22 19:03:55 +00001//===- 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>
Reid Kleckner268adf22013-04-24 17:50:30 +000016#if defined(__APPLE__)
Reid Kleckner8eca6772013-04-23 13:15:51 +000017# include <crt_externs.h>
Reid Kleckner268adf22013-04-24 17:50:30 +000018#elif !defined(_MSC_VER)
Reid Kleckner8eca6772013-04-23 13:15:51 +000019// Forward declare environ in case it's not provided by stdlib.h.
20extern char **environ;
21#endif
Reid Kleckner0b675d82013-04-22 19:03:55 +000022
Reid Kleckner2a839432013-04-30 04:30:41 +000023// From TestMain.cpp.
24extern const char *TestMainArgv0;
25
Reid Kleckner0b675d82013-04-22 19:03:55 +000026namespace {
27
28using namespace llvm;
29using namespace sys;
30
31static cl::opt<std::string>
32ProgramTestStringArg1("program-test-string-arg1");
33static cl::opt<std::string>
34ProgramTestStringArg2("program-test-string-arg2");
35
Reid Kleckner8eca6772013-04-23 13:15:51 +000036static void CopyEnvironment(std::vector<const char *> &out) {
37#ifdef __APPLE__
Reid Kleckner8eca6772013-04-23 13:15:51 +000038 char **envp = *_NSGetEnviron();
39#else
40 // environ seems to work for Windows and most other Unices.
41 char **envp = environ;
42#endif
43 while (*envp != 0) {
44 out.push_back(*envp);
45 ++envp;
46 }
47}
48
Reid Kleckner0b675d82013-04-22 19:03:55 +000049TEST(ProgramTest, CreateProcessTrailingSlash) {
50 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
51 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
52 ProgramTestStringArg2 == "has\\\\ trailing\\") {
53 exit(0); // Success! The arguments were passed and parsed.
54 }
55 exit(1);
56 }
57
Reid Kleckner2a839432013-04-30 04:30:41 +000058 Path my_exe = Path::GetMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
Reid Kleckner0b675d82013-04-22 19:03:55 +000059 const char *argv[] = {
60 my_exe.c_str(),
61 "--gtest_filter=ProgramTest.CreateProcessTrailingSlashChild",
62 "-program-test-string-arg1", "has\\\\ trailing\\",
63 "-program-test-string-arg2", "has\\\\ trailing\\",
64 0
65 };
Reid Kleckner8eca6772013-04-23 13:15:51 +000066
67 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
68 std::vector<const char *> envp;
69 CopyEnvironment(envp);
70 envp.push_back("LLVM_PROGRAM_TEST_CHILD=1");
71 envp.push_back(0);
72
Reid Kleckner0b675d82013-04-22 19:03:55 +000073 std::string error;
74 bool ExecutionFailed;
75 // Redirect stdout and stdin to NUL, but let stderr through.
76#ifdef LLVM_ON_WIN32
77 Path nul("NUL");
78#else
79 Path nul("/dev/null");
80#endif
81 const Path *redirects[] = { &nul, &nul, 0 };
Rafael Espindola9f1d9fd2013-06-12 20:58:35 +000082 int rc =
83 ExecuteAndWait(my_exe, argv, &envp[0], redirects, /*secondsToWait=*/ 10,
84 /*memoryLimit=*/ 0, &error, &ExecutionFailed);
Reid Kleckner0b675d82013-04-22 19:03:55 +000085 EXPECT_FALSE(ExecutionFailed) << error;
86 EXPECT_EQ(0, rc);
87}
88
89} // end anonymous namespace