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
17using namespace lldb;
18using namespace lldb_private;
19
20TEST(File, GetWaitableHandleFileno) {
21 const auto *Info = testing::UnitTest::GetInstance()->current_test_info();
22
23 llvm::SmallString<128> name;
24 int fd;
25 llvm::sys::fs::createTemporaryFile(Prefix: llvm::Twine(Info->test_case_name()) + "-" +
26 Info->name(),
27 Suffix: "test", ResultFD&: fd, ResultPath&: name);
28 llvm::FileRemover remover(name);
29 ASSERT_GE(fd, 0);
30
31 FILE *stream = fdopen(fd: fd, modes: "r");
32 ASSERT_TRUE(stream);
33
34 NativeFile file(stream, true);
35 EXPECT_EQ(file.GetWaitableHandle(), fd);
36}
37
38TEST(File, GetStreamFromDescriptor) {
39 const auto *Info = testing::UnitTest::GetInstance()->current_test_info();
40 llvm::SmallString<128> name;
41 int fd;
42 llvm::sys::fs::createTemporaryFile(Prefix: llvm::Twine(Info->test_case_name()) + "-" +
43 Info->name(),
44 Suffix: "test", ResultFD&: fd, ResultPath&: name);
45
46 llvm::FileRemover remover(name);
47 ASSERT_GE(fd, 0);
48
49 NativeFile file(fd, File::eOpenOptionWriteOnly, true);
50 ASSERT_TRUE(file.IsValid());
51
52 FILE *stream = file.GetStream();
53 ASSERT_TRUE(stream != NULL);
54
55 EXPECT_EQ(file.GetDescriptor(), fd);
56 EXPECT_EQ(file.GetWaitableHandle(), fd);
57}
58

source code of lldb/unittests/Host/FileTest.cpp