xref: /llvm-project/lldb/unittests/Host/FileTest.cpp (revision 14735cab655441ba45c4b88ad82f11267e5fe916)
180814287SRaphael Isemann //===-- FileTest.cpp ------------------------------------------------------===//
2a7d186c7SJonas Devlieghere //
3a7d186c7SJonas Devlieghere // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a7d186c7SJonas Devlieghere // See https://llvm.org/LICENSE.txt for license information.
5a7d186c7SJonas Devlieghere // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a7d186c7SJonas Devlieghere //
7a7d186c7SJonas Devlieghere //===----------------------------------------------------------------------===//
8a7d186c7SJonas Devlieghere 
9a7d186c7SJonas Devlieghere #include "lldb/Host/File.h"
10a7d186c7SJonas Devlieghere #include "llvm/ADT/SmallString.h"
11a7d186c7SJonas Devlieghere #include "llvm/Support/FileSystem.h"
12a7d186c7SJonas Devlieghere #include "llvm/Support/FileUtilities.h"
13a7d186c7SJonas Devlieghere #include "llvm/Support/Path.h"
14a7d186c7SJonas Devlieghere #include "llvm/Support/Program.h"
15a7d186c7SJonas Devlieghere #include "gtest/gtest.h"
16a7d186c7SJonas Devlieghere 
17a7d186c7SJonas Devlieghere using namespace lldb;
18a7d186c7SJonas Devlieghere using namespace lldb_private;
19a7d186c7SJonas Devlieghere 
TEST(File,GetWaitableHandleFileno)20a7d186c7SJonas Devlieghere TEST(File, GetWaitableHandleFileno) {
21a7d186c7SJonas Devlieghere   const auto *Info = testing::UnitTest::GetInstance()->current_test_info();
22a7d186c7SJonas Devlieghere 
23a7d186c7SJonas Devlieghere   llvm::SmallString<128> name;
24a7d186c7SJonas Devlieghere   int fd;
25a7d186c7SJonas Devlieghere   llvm::sys::fs::createTemporaryFile(llvm::Twine(Info->test_case_name()) + "-" +
26a7d186c7SJonas Devlieghere                                          Info->name(),
27a7d186c7SJonas Devlieghere                                      "test", fd, name);
28a7d186c7SJonas Devlieghere   llvm::FileRemover remover(name);
29a7d186c7SJonas Devlieghere   ASSERT_GE(fd, 0);
30a7d186c7SJonas Devlieghere 
31a7d186c7SJonas Devlieghere   FILE *stream = fdopen(fd, "r");
32a7d186c7SJonas Devlieghere   ASSERT_TRUE(stream);
33a7d186c7SJonas Devlieghere 
34f913fd6eSLawrence D'Anna   NativeFile file(stream, true);
35a7d186c7SJonas Devlieghere   EXPECT_EQ(file.GetWaitableHandle(), fd);
36a7d186c7SJonas Devlieghere }
37948786c9SJonas Devlieghere 
TEST(File,GetStreamFromDescriptor)38948786c9SJonas Devlieghere TEST(File, GetStreamFromDescriptor) {
39948786c9SJonas Devlieghere   const auto *Info = testing::UnitTest::GetInstance()->current_test_info();
40948786c9SJonas Devlieghere   llvm::SmallString<128> name;
41948786c9SJonas Devlieghere   int fd;
42948786c9SJonas Devlieghere   llvm::sys::fs::createTemporaryFile(llvm::Twine(Info->test_case_name()) + "-" +
43948786c9SJonas Devlieghere                                          Info->name(),
44948786c9SJonas Devlieghere                                      "test", fd, name);
45948786c9SJonas Devlieghere 
46948786c9SJonas Devlieghere   llvm::FileRemover remover(name);
47948786c9SJonas Devlieghere   ASSERT_GE(fd, 0);
48948786c9SJonas Devlieghere 
49*14735cabSMichał Górny   NativeFile file(fd, File::eOpenOptionWriteOnly, true);
50948786c9SJonas Devlieghere   ASSERT_TRUE(file.IsValid());
51948786c9SJonas Devlieghere 
52948786c9SJonas Devlieghere   FILE *stream = file.GetStream();
53948786c9SJonas Devlieghere   ASSERT_TRUE(stream != NULL);
54948786c9SJonas Devlieghere 
55948786c9SJonas Devlieghere   EXPECT_EQ(file.GetDescriptor(), fd);
56948786c9SJonas Devlieghere   EXPECT_EQ(file.GetWaitableHandle(), fd);
57948786c9SJonas Devlieghere }
58