| 1 | //===-- FileTest.cpp ------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Host/File.h" |
| 10 | #include "llvm/ADT/SmallString.h" |
| 11 | #include "llvm/Support/FileSystem.h" |
| 12 | #include "llvm/Support/FileUtilities.h" |
| 13 | #include "llvm/Support/Path.h" |
| 14 | #include "llvm/Support/Program.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | #ifdef _WIN32 |
| 18 | #include "lldb/Host/windows/windows.h" |
| 19 | #endif |
| 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | TEST(File, GetWaitableHandleFileno) { |
| 25 | const auto *Info = testing::UnitTest::GetInstance()->current_test_info(); |
| 26 | |
| 27 | llvm::SmallString<128> name; |
| 28 | int fd; |
| 29 | llvm::sys::fs::createTemporaryFile(Prefix: llvm::Twine(Info->test_case_name()) + "-" + |
| 30 | Info->name(), |
| 31 | Suffix: "test" , ResultFD&: fd, ResultPath&: name); |
| 32 | llvm::FileRemover remover(name); |
| 33 | ASSERT_GE(fd, 0); |
| 34 | |
| 35 | FILE *stream = fdopen(fd: fd, modes: "r" ); |
| 36 | ASSERT_TRUE(stream); |
| 37 | |
| 38 | NativeFile file(stream, true); |
| 39 | #ifdef _WIN32 |
| 40 | EXPECT_EQ(file.GetWaitableHandle(), (HANDLE)_get_osfhandle(fd)); |
| 41 | #else |
| 42 | EXPECT_EQ(file.GetWaitableHandle(), (file_t)fd); |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | TEST(File, GetStreamFromDescriptor) { |
| 47 | const auto *Info = testing::UnitTest::GetInstance()->current_test_info(); |
| 48 | llvm::SmallString<128> name; |
| 49 | int fd; |
| 50 | llvm::sys::fs::createTemporaryFile(Prefix: llvm::Twine(Info->test_case_name()) + "-" + |
| 51 | Info->name(), |
| 52 | Suffix: "test" , ResultFD&: fd, ResultPath&: name); |
| 53 | |
| 54 | llvm::FileRemover remover(name); |
| 55 | ASSERT_GE(fd, 0); |
| 56 | |
| 57 | NativeFile file(fd, File::eOpenOptionWriteOnly, true); |
| 58 | ASSERT_TRUE(file.IsValid()); |
| 59 | |
| 60 | FILE *stream = file.GetStream(); |
| 61 | ASSERT_TRUE(stream != NULL); |
| 62 | |
| 63 | EXPECT_EQ(file.GetDescriptor(), fd); |
| 64 | #ifdef _WIN32 |
| 65 | EXPECT_EQ(file.GetWaitableHandle(), (HANDLE)_get_osfhandle(fd)); |
| 66 | #else |
| 67 | EXPECT_EQ(file.GetWaitableHandle(), (file_t)fd); |
| 68 | #endif |
| 69 | } |
| 70 | |