0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===- unittest/Support/ProgramTest.cpp -----------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
147
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8
|
121
|
9 #include "llvm/Support/Program.h"
|
147
|
10 #include "llvm/Config/llvm-config.h"
|
121
|
11 #include "llvm/Support/CommandLine.h"
|
100
|
12 #include "llvm/Support/ConvertUTF.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 #include "llvm/Support/FileSystem.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 #include "llvm/Support/Path.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 #include "gtest/gtest.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include <stdlib.h>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #if defined(__APPLE__)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 # include <crt_externs.h>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 #elif !defined(_MSC_VER)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 // Forward declare environ in case it's not provided by stdlib.h.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 extern char **environ;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 #endif
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 #if defined(LLVM_ON_UNIX)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 #include <unistd.h>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 void sleep_for(unsigned int seconds) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 sleep(seconds);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 }
|
147
|
29 #elif defined(_WIN32)
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 #include <windows.h>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 void sleep_for(unsigned int seconds) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 Sleep(seconds * 1000);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 #else
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 #error sleep_for is not implemented on your platform.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 #endif
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37
|
77
|
38 #define ASSERT_NO_ERROR(x) \
|
|
39 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
|
|
40 SmallString<128> MessageStorage; \
|
|
41 raw_svector_ostream Message(MessageStorage); \
|
|
42 Message << #x ": did not return errc::success.\n" \
|
|
43 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
|
|
44 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
|
|
45 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
|
|
46 } else { \
|
|
47 }
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 // From TestMain.cpp.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 extern const char *TestMainArgv0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 namespace {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 using namespace sys;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 static cl::opt<std::string>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 ProgramTestStringArg1("program-test-string-arg1");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 static cl::opt<std::string>
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 ProgramTestStringArg2("program-test-string-arg2");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60
|
100
|
61 class ProgramEnvTest : public testing::Test {
|
147
|
62 std::vector<StringRef> EnvTable;
|
100
|
63 std::vector<std::string> EnvStorage;
|
|
64
|
|
65 protected:
|
|
66 void SetUp() override {
|
|
67 auto EnvP = [] {
|
147
|
68 #if defined(_WIN32)
|
100
|
69 _wgetenv(L"TMP"); // Populate _wenviron, initially is null
|
|
70 return _wenviron;
|
|
71 #elif defined(__APPLE__)
|
|
72 return *_NSGetEnviron();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 #else
|
100
|
74 return environ;
|
|
75 #endif
|
|
76 }();
|
|
77 ASSERT_TRUE(EnvP);
|
|
78
|
147
|
79 auto prepareEnvVar = [this](decltype(*EnvP) Var) -> StringRef {
|
|
80 #if defined(_WIN32)
|
100
|
81 // On Windows convert UTF16 encoded variable to UTF8
|
|
82 auto Len = wcslen(Var);
|
|
83 ArrayRef<char> Ref{reinterpret_cast<char const *>(Var),
|
|
84 Len * sizeof(*Var)};
|
|
85 EnvStorage.emplace_back();
|
|
86 auto convStatus = convertUTF16ToUTF8String(Ref, EnvStorage.back());
|
|
87 EXPECT_TRUE(convStatus);
|
147
|
88 return EnvStorage.back();
|
100
|
89 #else
|
121
|
90 (void)this;
|
147
|
91 return StringRef(Var);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 #endif
|
100
|
93 };
|
|
94
|
|
95 while (*EnvP != nullptr) {
|
|
96 EnvTable.emplace_back(prepareEnvVar(*EnvP));
|
|
97 ++EnvP;
|
|
98 }
|
|
99 }
|
|
100
|
|
101 void TearDown() override {
|
|
102 EnvTable.clear();
|
|
103 EnvStorage.clear();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 }
|
100
|
105
|
147
|
106 void addEnvVar(StringRef Var) { EnvTable.emplace_back(Var); }
|
100
|
107
|
147
|
108 ArrayRef<StringRef> getEnviron() const { return EnvTable; }
|
100
|
109 };
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110
|
147
|
111 #ifdef _WIN32
|
100
|
112 TEST_F(ProgramEnvTest, CreateProcessLongPath) {
|
83
|
113 if (getenv("LLVM_PROGRAM_TEST_LONG_PATH"))
|
|
114 exit(0);
|
|
115
|
|
116 // getMainExecutable returns an absolute path; prepend the long-path prefix.
|
|
117 std::string MyAbsExe =
|
|
118 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
|
|
119 std::string MyExe;
|
|
120 if (!StringRef(MyAbsExe).startswith("\\\\?\\"))
|
|
121 MyExe.append("\\\\?\\");
|
|
122 MyExe.append(MyAbsExe);
|
|
123
|
147
|
124 StringRef ArgV[] = {MyExe,
|
|
125 "--gtest_filter=ProgramEnvTest.CreateProcessLongPath"};
|
83
|
126
|
|
127 // Add LLVM_PROGRAM_TEST_LONG_PATH to the environment of the child.
|
100
|
128 addEnvVar("LLVM_PROGRAM_TEST_LONG_PATH=1");
|
83
|
129
|
|
130 // Redirect stdout to a long path.
|
|
131 SmallString<128> TestDirectory;
|
|
132 ASSERT_NO_ERROR(
|
|
133 fs::createUniqueDirectory("program-redirect-test", TestDirectory));
|
|
134 SmallString<256> LongPath(TestDirectory);
|
|
135 LongPath.push_back('\\');
|
|
136 // MAX_PATH = 260
|
|
137 LongPath.append(260 - TestDirectory.size(), 'a');
|
|
138
|
|
139 std::string Error;
|
|
140 bool ExecutionFailed;
|
121
|
141 Optional<StringRef> Redirects[] = {None, LongPath.str(), None};
|
100
|
142 int RC = ExecuteAndWait(MyExe, ArgV, getEnviron(), Redirects,
|
83
|
143 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &Error,
|
|
144 &ExecutionFailed);
|
|
145 EXPECT_FALSE(ExecutionFailed) << Error;
|
|
146 EXPECT_EQ(0, RC);
|
|
147
|
|
148 // Remove the long stdout.
|
|
149 ASSERT_NO_ERROR(fs::remove(Twine(LongPath)));
|
|
150 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory)));
|
|
151 }
|
|
152 #endif
|
|
153
|
100
|
154 TEST_F(ProgramEnvTest, CreateProcessTrailingSlash) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
156 if (ProgramTestStringArg1 == "has\\\\ trailing\\" &&
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 ProgramTestStringArg2 == "has\\\\ trailing\\") {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 exit(0); // Success! The arguments were passed and parsed.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160 exit(1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163 std::string my_exe =
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
|
147
|
165 StringRef argv[] = {
|
|
166 my_exe,
|
|
167 "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
|
|
168 "-program-test-string-arg1",
|
|
169 "has\\\\ trailing\\",
|
|
170 "-program-test-string-arg2",
|
|
171 "has\\\\ trailing\\"};
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
|
100
|
174 addEnvVar("LLVM_PROGRAM_TEST_CHILD=1");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 std::string error;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177 bool ExecutionFailed;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
178 // Redirect stdout and stdin to NUL, but let stderr through.
|
147
|
179 #ifdef _WIN32
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
180 StringRef nul("NUL");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
181 #else
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182 StringRef nul("/dev/null");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 #endif
|
121
|
184 Optional<StringRef> redirects[] = { nul, nul, None };
|
100
|
185 int rc = ExecuteAndWait(my_exe, argv, getEnviron(), redirects,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187 &ExecutionFailed);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188 EXPECT_FALSE(ExecutionFailed) << error;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189 EXPECT_EQ(0, rc);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
191
|
100
|
192 TEST_F(ProgramEnvTest, TestExecuteNoWait) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193 using namespace llvm::sys;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
194
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
195 if (getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT")) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
196 sleep_for(/*seconds*/ 1);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
197 exit(0);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
199
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 std::string Executable =
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
|
147
|
202 StringRef argv[] = {Executable,
|
|
203 "--gtest_filter=ProgramEnvTest.TestExecuteNoWait"};
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 // Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
|
100
|
206 addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
208 std::string Error;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
209 bool ExecutionFailed;
|
121
|
210 ProcessInfo PI1 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
|
|
211 &ExecutionFailed);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
212 ASSERT_FALSE(ExecutionFailed) << Error;
|
120
|
213 ASSERT_NE(PI1.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
214
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215 unsigned LoopCount = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 // Test that Wait() with WaitUntilTerminates=true works. In this case,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
218 // LoopCount should only be incremented once.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 while (true) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220 ++LoopCount;
|
120
|
221 ProcessInfo WaitResult = llvm::sys::Wait(PI1, 0, true, &Error);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
222 ASSERT_TRUE(Error.empty());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
223 if (WaitResult.Pid == PI1.Pid)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 break;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
225 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
226
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
227 EXPECT_EQ(LoopCount, 1u) << "LoopCount should be 1";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
228
|
121
|
229 ProcessInfo PI2 = ExecuteNoWait(Executable, argv, getEnviron(), {}, 0, &Error,
|
|
230 &ExecutionFailed);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
231 ASSERT_FALSE(ExecutionFailed) << Error;
|
120
|
232 ASSERT_NE(PI2.Pid, ProcessInfo::InvalidPid) << "Invalid process id";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
233
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
234 // Test that Wait() with SecondsToWait=0 performs a non-blocking wait. In this
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
235 // cse, LoopCount should be greater than 1 (more than one increment occurs).
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
236 while (true) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
237 ++LoopCount;
|
120
|
238 ProcessInfo WaitResult = llvm::sys::Wait(PI2, 0, false, &Error);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
239 ASSERT_TRUE(Error.empty());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
240 if (WaitResult.Pid == PI2.Pid)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
241 break;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
242 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
243
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
244 ASSERT_GT(LoopCount, 1u) << "LoopCount should be >1";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
245 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
246
|
100
|
247 TEST_F(ProgramEnvTest, TestExecuteAndWaitTimeout) {
|
77
|
248 using namespace llvm::sys;
|
|
249
|
|
250 if (getenv("LLVM_PROGRAM_TEST_TIMEOUT")) {
|
|
251 sleep_for(/*seconds*/ 10);
|
|
252 exit(0);
|
|
253 }
|
|
254
|
|
255 std::string Executable =
|
|
256 sys::fs::getMainExecutable(TestMainArgv0, &ProgramTestStringArg1);
|
147
|
257 StringRef argv[] = {
|
|
258 Executable, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout"};
|
77
|
259
|
|
260 // Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
|
100
|
261 addEnvVar("LLVM_PROGRAM_TEST_TIMEOUT=1");
|
77
|
262
|
|
263 std::string Error;
|
|
264 bool ExecutionFailed;
|
|
265 int RetCode =
|
121
|
266 ExecuteAndWait(Executable, argv, getEnviron(), {}, /*secondsToWait=*/1, 0,
|
77
|
267 &Error, &ExecutionFailed);
|
|
268 ASSERT_EQ(-2, RetCode);
|
|
269 }
|
|
270
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
271 TEST(ProgramTest, TestExecuteNegative) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
272 std::string Executable = "i_dont_exist";
|
147
|
273 StringRef argv[] = {Executable};
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
274
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
275 {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
276 std::string Error;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
277 bool ExecutionFailed;
|
147
|
278 int RetCode = ExecuteAndWait(Executable, argv, llvm::None, {}, 0, 0, &Error,
|
121
|
279 &ExecutionFailed);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
280 ASSERT_TRUE(RetCode < 0) << "On error ExecuteAndWait should return 0 or "
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
281 "positive value indicating the result code";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
282 ASSERT_TRUE(ExecutionFailed);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
283 ASSERT_FALSE(Error.empty());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
284 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
285
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
286 {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
287 std::string Error;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
288 bool ExecutionFailed;
|
147
|
289 ProcessInfo PI = ExecuteNoWait(Executable, argv, llvm::None, {}, 0, &Error,
|
121
|
290 &ExecutionFailed);
|
120
|
291 ASSERT_EQ(PI.Pid, ProcessInfo::InvalidPid)
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
292 << "On error ExecuteNoWait should return an invalid ProcessInfo";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
293 ASSERT_TRUE(ExecutionFailed);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
294 ASSERT_FALSE(Error.empty());
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
295 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
296
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
297 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
298
|
147
|
299 #ifdef _WIN32
|
77
|
300 const char utf16le_text[] =
|
|
301 "\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61\x00";
|
|
302 const char utf16be_text[] =
|
|
303 "\x00\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61";
|
|
304 #endif
|
|
305 const char utf8_text[] = "\x6c\x69\x6e\x67\xc3\xbc\x69\xc3\xa7\x61";
|
|
306
|
|
307 TEST(ProgramTest, TestWriteWithSystemEncoding) {
|
|
308 SmallString<128> TestDirectory;
|
|
309 ASSERT_NO_ERROR(fs::createUniqueDirectory("program-test", TestDirectory));
|
|
310 errs() << "Test Directory: " << TestDirectory << '\n';
|
|
311 errs().flush();
|
|
312 SmallString<128> file_pathname(TestDirectory);
|
|
313 path::append(file_pathname, "international-file.txt");
|
|
314 // Only on Windows we should encode in UTF16. For other systems, use UTF8
|
|
315 ASSERT_NO_ERROR(sys::writeFileWithEncoding(file_pathname.c_str(), utf8_text,
|
|
316 sys::WEM_UTF16));
|
|
317 int fd = 0;
|
|
318 ASSERT_NO_ERROR(fs::openFileForRead(file_pathname.c_str(), fd));
|
147
|
319 #if defined(_WIN32)
|
77
|
320 char buf[18];
|
|
321 ASSERT_EQ(::read(fd, buf, 18), 18);
|
|
322 if (strncmp(buf, "\xfe\xff", 2) == 0) { // UTF16-BE
|
|
323 ASSERT_EQ(strncmp(&buf[2], utf16be_text, 16), 0);
|
|
324 } else if (strncmp(buf, "\xff\xfe", 2) == 0) { // UTF16-LE
|
|
325 ASSERT_EQ(strncmp(&buf[2], utf16le_text, 16), 0);
|
|
326 } else {
|
|
327 FAIL() << "Invalid BOM in UTF-16 file";
|
|
328 }
|
|
329 #else
|
|
330 char buf[10];
|
|
331 ASSERT_EQ(::read(fd, buf, 10), 10);
|
|
332 ASSERT_EQ(strncmp(buf, utf8_text, 10), 0);
|
|
333 #endif
|
|
334 ::close(fd);
|
|
335 ASSERT_NO_ERROR(fs::remove(file_pathname.str()));
|
|
336 ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
|
|
337 }
|
|
338
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
339 } // end anonymous namespace
|