1 //===- unittest/Support/ProgramTest.cpp -----------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Program.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Support/CommandLine.h"
12 #include "llvm/Support/ConvertUTF.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Signals.h"
16 #include "gtest/gtest.h"
19 #if defined(__APPLE__)
20 # include <crt_externs.h>
21 #elif !defined(_MSC_VER)
22 // Forward declare environ in case it's not provided by stdlib.h.
23 extern char **environ
;
26 #if defined(LLVM_ON_UNIX)
28 void sleep_for(unsigned int seconds
) {
33 void sleep_for(unsigned int seconds
) {
34 Sleep(seconds
* 1000);
37 #error sleep_for is not implemented on your platform.
40 #define ASSERT_NO_ERROR(x) \
41 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
42 SmallString<128> MessageStorage; \
43 raw_svector_ostream Message(MessageStorage); \
44 Message << #x ": did not return errc::success.\n" \
45 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
46 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
47 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
51 extern const char *TestMainArgv0
;
58 static cl::opt
<std::string
>
59 ProgramTestStringArg1("program-test-string-arg1");
60 static cl::opt
<std::string
>
61 ProgramTestStringArg2("program-test-string-arg2");
63 class ProgramEnvTest
: public testing::Test
{
64 std::vector
<StringRef
> EnvTable
;
65 std::vector
<std::string
> EnvStorage
;
68 void SetUp() override
{
71 _wgetenv(L
"TMP"); // Populate _wenviron, initially is null
73 #elif defined(__APPLE__)
74 return *_NSGetEnviron();
81 auto prepareEnvVar
= [this](decltype(*EnvP
) Var
) -> StringRef
{
83 // On Windows convert UTF16 encoded variable to UTF8
84 auto Len
= wcslen(Var
);
85 ArrayRef
<char> Ref
{reinterpret_cast<char const *>(Var
),
87 EnvStorage
.emplace_back();
88 auto convStatus
= convertUTF16ToUTF8String(Ref
, EnvStorage
.back());
89 EXPECT_TRUE(convStatus
);
90 return EnvStorage
.back();
93 return StringRef(Var
);
97 while (*EnvP
!= nullptr) {
98 auto S
= prepareEnvVar(*EnvP
);
99 if (!StringRef(S
).startswith("GTEST_"))
100 EnvTable
.emplace_back(S
);
105 void TearDown() override
{
110 void addEnvVar(StringRef Var
) { EnvTable
.emplace_back(Var
); }
112 ArrayRef
<StringRef
> getEnviron() const { return EnvTable
; }
116 void checkSeparators(StringRef Path
) {
117 char UndesiredSeparator
= sys::path::get_separator()[0] == '/' ? '\\' : '/';
118 ASSERT_EQ(Path
.find(UndesiredSeparator
), StringRef::npos
);
121 TEST_F(ProgramEnvTest
, CreateProcessLongPath
) {
122 if (getenv("LLVM_PROGRAM_TEST_LONG_PATH"))
125 // getMainExecutable returns an absolute path; prepend the long-path prefix.
126 SmallString
<128> MyAbsExe(
127 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
));
128 checkSeparators(MyAbsExe
);
129 // Force a path with backslashes, when we are going to prepend the \\?\
131 sys::path::native(MyAbsExe
, sys::path::Style::windows_backslash
);
133 if (!StringRef(MyAbsExe
).startswith("\\\\?\\"))
134 MyExe
.append("\\\\?\\");
135 MyExe
.append(std::string(MyAbsExe
.begin(), MyAbsExe
.end()));
137 StringRef ArgV
[] = {MyExe
,
138 "--gtest_filter=ProgramEnvTest.CreateProcessLongPath"};
140 // Add LLVM_PROGRAM_TEST_LONG_PATH to the environment of the child.
141 addEnvVar("LLVM_PROGRAM_TEST_LONG_PATH=1");
143 // Redirect stdout to a long path.
144 SmallString
<128> TestDirectory
;
146 fs::createUniqueDirectory("program-redirect-test", TestDirectory
));
147 SmallString
<256> LongPath(TestDirectory
);
148 LongPath
.push_back('\\');
150 LongPath
.append(260 - TestDirectory
.size(), 'a');
153 bool ExecutionFailed
;
154 std::optional
<StringRef
> Redirects
[] = {std::nullopt
, LongPath
.str(),
156 int RC
= ExecuteAndWait(MyExe
, ArgV
, getEnviron(), Redirects
,
157 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &Error
,
159 EXPECT_FALSE(ExecutionFailed
) << Error
;
162 // Remove the long stdout.
163 ASSERT_NO_ERROR(fs::remove(Twine(LongPath
)));
164 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory
)));
168 TEST_F(ProgramEnvTest
, CreateProcessTrailingSlash
) {
169 if (getenv("LLVM_PROGRAM_TEST_CHILD")) {
170 if (ProgramTestStringArg1
== "has\\\\ trailing\\" &&
171 ProgramTestStringArg2
== "has\\\\ trailing\\") {
172 exit(0); // Success! The arguments were passed and parsed.
178 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
);
181 "--gtest_filter=ProgramEnvTest.CreateProcessTrailingSlash",
182 "-program-test-string-arg1",
183 "has\\\\ trailing\\",
184 "-program-test-string-arg2",
185 "has\\\\ trailing\\"};
187 // Add LLVM_PROGRAM_TEST_CHILD to the environment of the child.
188 addEnvVar("LLVM_PROGRAM_TEST_CHILD=1");
191 bool ExecutionFailed
;
192 // Redirect stdout and stdin to NUL, but let stderr through.
194 StringRef
nul("NUL");
196 StringRef
nul("/dev/null");
198 std::optional
<StringRef
> redirects
[] = {nul
, nul
, std::nullopt
};
199 int rc
= ExecuteAndWait(my_exe
, argv
, getEnviron(), redirects
,
200 /*secondsToWait=*/ 10, /*memoryLimit=*/ 0, &error
,
202 EXPECT_FALSE(ExecutionFailed
) << error
;
206 TEST_F(ProgramEnvTest
, TestExecuteNoWait
) {
207 using namespace llvm::sys
;
209 if (getenv("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT")) {
210 sleep_for(/*seconds*/ 1);
214 std::string Executable
=
215 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
);
216 StringRef argv
[] = {Executable
,
217 "--gtest_filter=ProgramEnvTest.TestExecuteNoWait"};
219 // Add LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT to the environment of the child.
220 addEnvVar("LLVM_PROGRAM_TEST_EXECUTE_NO_WAIT=1");
223 bool ExecutionFailed
;
224 ProcessInfo PI1
= ExecuteNoWait(Executable
, argv
, getEnviron(), {}, 0, &Error
,
226 ASSERT_FALSE(ExecutionFailed
) << Error
;
227 ASSERT_NE(PI1
.Pid
, ProcessInfo::InvalidPid
) << "Invalid process id";
229 unsigned LoopCount
= 0;
231 // Test that Wait() with SecondsToWait=std::nullopt works. In this case,
232 // LoopCount should only be incremented once.
235 ProcessInfo WaitResult
=
236 llvm::sys::Wait(PI1
, /*SecondsToWait=*/std::nullopt
, &Error
);
237 ASSERT_TRUE(Error
.empty());
238 if (WaitResult
.Pid
== PI1
.Pid
)
242 EXPECT_EQ(LoopCount
, 1u) << "LoopCount should be 1";
244 ProcessInfo PI2
= ExecuteNoWait(Executable
, argv
, getEnviron(),
245 /*Redirects*/ {}, /*MemoryLimit*/ 0, &Error
,
247 ASSERT_FALSE(ExecutionFailed
) << Error
;
248 ASSERT_NE(PI2
.Pid
, ProcessInfo::InvalidPid
) << "Invalid process id";
250 // Test that Wait() with SecondsToWait=0 performs a non-blocking wait. In this
251 // case, LoopCount should be greater than 1 (more than one increment occurs).
254 ProcessInfo WaitResult
= llvm::sys::Wait(PI2
, /*SecondsToWait=*/0, &Error
);
255 ASSERT_TRUE(Error
.empty());
256 if (WaitResult
.Pid
== PI2
.Pid
)
260 ASSERT_GT(LoopCount
, 1u) << "LoopCount should be >1";
263 TEST_F(ProgramEnvTest
, TestExecuteAndWaitTimeout
) {
264 using namespace llvm::sys
;
266 if (getenv("LLVM_PROGRAM_TEST_TIMEOUT")) {
267 sleep_for(/*seconds*/ 10);
271 std::string Executable
=
272 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
);
274 Executable
, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitTimeout"};
276 // Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
277 addEnvVar("LLVM_PROGRAM_TEST_TIMEOUT=1");
280 bool ExecutionFailed
;
282 ExecuteAndWait(Executable
, argv
, getEnviron(), {}, /*SecondsToWait=*/1,
283 /*MemoryLimit*/ 0, &Error
, &ExecutionFailed
);
284 ASSERT_EQ(-2, RetCode
);
287 TEST_F(ProgramEnvTest
, TestExecuteNoWaitTimeoutPolling
) {
288 using namespace llvm::sys
;
290 if (getenv("LLVM_PROGRAM_TEST_TIMEOUT")) {
291 sleep_for(/*seconds*/ 5);
295 std::string Executable
=
296 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
);
299 "--gtest_filter=ProgramEnvTest.TestExecuteNoWaitTimeoutPolling"};
301 // Add LLVM_PROGRAM_TEST_TIMEOUT to the environment of the child.
302 addEnvVar("LLVM_PROGRAM_TEST_TIMEOUT=1");
305 bool ExecutionFailed
;
306 ProcessInfo PI0
= ExecuteNoWait(Executable
, argv
, getEnviron(),
307 /*Redirects=*/{}, /*MemoryLimit=*/0, &Error
,
309 ASSERT_FALSE(ExecutionFailed
) << Error
;
310 ASSERT_NE(PI0
.Pid
, ProcessInfo::InvalidPid
) << "Invalid process id";
312 // Check that we don't kill the process with a non-0 SecondsToWait if Polling.
313 unsigned LoopCount
= 0;
314 ProcessInfo WaitResult
;
317 WaitResult
= llvm::sys::Wait(PI0
, /*SecondsToWait=*/1, &Error
,
318 /*ProcStats=*/nullptr,
320 ASSERT_TRUE(Error
.empty()) << Error
;
321 } while (WaitResult
.Pid
!= PI0
.Pid
);
323 ASSERT_GT(LoopCount
, 1u) << "LoopCount should be >1";
326 TEST(ProgramTest
, TestExecuteNegative
) {
327 std::string Executable
= "i_dont_exist";
328 StringRef argv
[] = {Executable
};
332 bool ExecutionFailed
;
333 int RetCode
= ExecuteAndWait(Executable
, argv
, std::nullopt
, {}, 0, 0,
334 &Error
, &ExecutionFailed
);
335 ASSERT_LT(RetCode
, 0) << "On error ExecuteAndWait should return 0 or "
336 "positive value indicating the result code";
337 ASSERT_TRUE(ExecutionFailed
);
338 ASSERT_FALSE(Error
.empty());
343 bool ExecutionFailed
;
344 ProcessInfo PI
= ExecuteNoWait(Executable
, argv
, std::nullopt
, {}, 0,
345 &Error
, &ExecutionFailed
);
346 ASSERT_EQ(PI
.Pid
, ProcessInfo::InvalidPid
)
347 << "On error ExecuteNoWait should return an invalid ProcessInfo";
348 ASSERT_TRUE(ExecutionFailed
);
349 ASSERT_FALSE(Error
.empty());
355 const char utf16le_text
[] =
356 "\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61\x00";
357 const char utf16be_text
[] =
358 "\x00\x6c\x00\x69\x00\x6e\x00\x67\x00\xfc\x00\x69\x00\xe7\x00\x61";
360 const char utf8_text
[] = "\x6c\x69\x6e\x67\xc3\xbc\x69\xc3\xa7\x61";
362 TEST(ProgramTest
, TestWriteWithSystemEncoding
) {
363 SmallString
<128> TestDirectory
;
364 ASSERT_NO_ERROR(fs::createUniqueDirectory("program-test", TestDirectory
));
365 errs() << "Test Directory: " << TestDirectory
<< '\n';
367 SmallString
<128> file_pathname(TestDirectory
);
368 path::append(file_pathname
, "international-file.txt");
369 // Only on Windows we should encode in UTF16. For other systems, use UTF8
370 ASSERT_NO_ERROR(sys::writeFileWithEncoding(file_pathname
.c_str(), utf8_text
,
373 ASSERT_NO_ERROR(fs::openFileForRead(file_pathname
.c_str(), fd
));
376 ASSERT_EQ(::read(fd
, buf
, 18), 18);
377 const char *utf16_text
;
378 if (strncmp(buf
, "\xfe\xff", 2) == 0) { // UTF16-BE
379 utf16_text
= utf16be_text
;
380 } else if (strncmp(buf
, "\xff\xfe", 2) == 0) { // UTF16-LE
381 utf16_text
= utf16le_text
;
383 FAIL() << "Invalid BOM in UTF-16 file";
385 ASSERT_EQ(strncmp(&buf
[2], utf16_text
, 16), 0);
388 ASSERT_EQ(::read(fd
, buf
, 10), 10);
389 ASSERT_EQ(strncmp(buf
, utf8_text
, 10), 0);
392 ASSERT_NO_ERROR(fs::remove(file_pathname
.str()));
393 ASSERT_NO_ERROR(fs::remove(TestDirectory
.str()));
396 TEST_F(ProgramEnvTest
, TestExecuteAndWaitStatistics
) {
397 using namespace llvm::sys
;
399 if (getenv("LLVM_PROGRAM_TEST_STATISTICS"))
402 std::string Executable
=
403 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
);
405 Executable
, "--gtest_filter=ProgramEnvTest.TestExecuteAndWaitStatistics"};
407 // Add LLVM_PROGRAM_TEST_STATISTICS to the environment of the child.
408 addEnvVar("LLVM_PROGRAM_TEST_STATISTICS=1");
411 bool ExecutionFailed
;
412 std::optional
<ProcessStatistics
> ProcStat
;
413 int RetCode
= ExecuteAndWait(Executable
, argv
, getEnviron(), {}, 0, 0, &Error
,
414 &ExecutionFailed
, &ProcStat
);
415 ASSERT_EQ(0, RetCode
);
416 ASSERT_TRUE(ProcStat
);
417 ASSERT_GE(ProcStat
->UserTime
, std::chrono::microseconds(0));
418 ASSERT_GE(ProcStat
->TotalTime
, ProcStat
->UserTime
);
421 TEST_F(ProgramEnvTest
, TestLockFile
) {
422 using namespace llvm::sys
;
424 if (const char *LockedFile
= getenv("LLVM_PROGRAM_TEST_LOCKED_FILE")) {
427 ASSERT_NO_ERROR(fs::openFileForReadWrite(LockedFile
, FD2
,
428 fs::CD_OpenExisting
, fs::OF_None
));
430 std::error_code ErrC
= fs::tryLockFile(FD2
, std::chrono::seconds(5));
431 ASSERT_NO_ERROR(ErrC
);
432 ASSERT_NO_ERROR(fs::unlockFile(FD2
));
437 // Create file that will be locked.
438 SmallString
<64> LockedFile
;
441 fs::createTemporaryFile("TestLockFile", "temp", FD1
, LockedFile
));
443 std::string Executable
=
444 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
);
445 StringRef argv
[] = {Executable
, "--gtest_filter=ProgramEnvTest.TestLockFile"};
447 // Add LLVM_PROGRAM_TEST_LOCKED_FILE to the environment of the child.
448 std::string EnvVar
= "LLVM_PROGRAM_TEST_LOCKED_FILE=";
449 EnvVar
+= LockedFile
.str();
453 ASSERT_NO_ERROR(fs::tryLockFile(FD1
));
456 bool ExecutionFailed
;
457 ProcessInfo PI2
= ExecuteNoWait(Executable
, argv
, getEnviron(), {}, 0, &Error
,
459 ASSERT_FALSE(ExecutionFailed
) << Error
;
460 ASSERT_TRUE(Error
.empty());
461 ASSERT_NE(PI2
.Pid
, ProcessInfo::InvalidPid
) << "Invalid process id";
463 // Wait some time to give the child process a chance to start.
464 std::this_thread::sleep_for(std::chrono::milliseconds(100));
466 ASSERT_NO_ERROR(fs::unlockFile(FD1
));
467 ProcessInfo WaitResult
= llvm::sys::Wait(PI2
, /*SecondsToWait=*/5, &Error
);
468 ASSERT_TRUE(Error
.empty());
469 ASSERT_EQ(0, WaitResult
.ReturnCode
);
470 ASSERT_EQ(WaitResult
.Pid
, PI2
.Pid
);
471 sys::fs::remove(LockedFile
);
474 TEST_F(ProgramEnvTest
, TestExecuteWithNoStacktraceHandler
) {
475 using namespace llvm::sys
;
477 if (getenv("LLVM_PROGRAM_TEST_NO_STACKTRACE_HANDLER")) {
478 sys::PrintStackTrace(errs());
482 std::string Executable
=
483 sys::fs::getMainExecutable(TestMainArgv0
, &ProgramTestStringArg1
);
486 "--gtest_filter=ProgramEnvTest.TestExecuteWithNoStacktraceHandler"};
488 addEnvVar("LLVM_PROGRAM_TEST_NO_STACKTRACE_HANDLER=1");
491 bool ExecutionFailed
;
492 int RetCode
= ExecuteAndWait(Executable
, argv
, getEnviron(), {}, 0, 0, &Error
,
494 EXPECT_FALSE(ExecutionFailed
) << Error
;
495 ASSERT_EQ(0, RetCode
);
498 } // end anonymous namespace