brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 85697c4 Raw
90 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Host/File.h"10#include "llvm/ADT/SmallString.h"11#include "llvm/ADT/StringRef.h"12#include "llvm/Support/FileSystem.h"13#include "llvm/Support/FileUtilities.h"14#include "llvm/Support/Path.h"15#include "llvm/Support/Program.h"16#include "gtest/gtest.h"17 18#ifdef _WIN3219#include "lldb/Host/windows/windows.h"20#endif21 22using namespace lldb;23using namespace lldb_private;24 25TEST(File, GetWaitableHandleFileno) {26  const auto *Info = testing::UnitTest::GetInstance()->current_test_info();27 28  llvm::SmallString<128> name;29  int fd;30  llvm::sys::fs::createTemporaryFile(llvm::Twine(Info->test_case_name()) + "-" +31                                         Info->name(),32                                     "test", fd, name);33  llvm::FileRemover remover(name);34  ASSERT_GE(fd, 0);35 36  FILE *stream = fdopen(fd, "r");37  ASSERT_TRUE(stream);38 39  NativeFile file(stream, File::eOpenOptionReadWrite, true);40#ifdef _WIN3241  EXPECT_EQ(file.GetWaitableHandle(), (HANDLE)_get_osfhandle(fd));42#else43  EXPECT_EQ(file.GetWaitableHandle(), (file_t)fd);44#endif45}46 47TEST(File, GetStreamFromDescriptor) {48  const auto *Info = testing::UnitTest::GetInstance()->current_test_info();49  llvm::SmallString<128> name;50  int fd;51  llvm::sys::fs::createTemporaryFile(llvm::Twine(Info->test_case_name()) + "-" +52                                         Info->name(),53                                     "test", fd, name);54 55  llvm::FileRemover remover(name);56  ASSERT_GE(fd, 0);57 58  NativeFile file(fd, File::eOpenOptionWriteOnly, true);59  ASSERT_TRUE(file.IsValid());60 61  FILE *stream = file.GetStream();62  ASSERT_TRUE(stream != NULL);63 64  EXPECT_EQ(file.GetDescriptor(), fd);65#ifdef _WIN3266  EXPECT_EQ(file.GetWaitableHandle(), (HANDLE)_get_osfhandle(fd));67#else68  EXPECT_EQ(file.GetWaitableHandle(), (file_t)fd);69#endif70}71 72TEST(File, ReadOnlyModeNotWritable) {73  const auto *Info = testing::UnitTest::GetInstance()->current_test_info();74  llvm::SmallString<128> name;75  int fd;76  llvm::sys::fs::createTemporaryFile(llvm::Twine(Info->test_case_name()) + "-" +77                                         Info->name(),78                                     "test", fd, name);79 80  llvm::FileRemover remover(name);81  ASSERT_GE(fd, 0);82 83  NativeFile file(fd, File::eOpenOptionReadOnly, true);84  ASSERT_TRUE(file.IsValid());85  llvm::StringLiteral buf = "Hello World";86  size_t bytes_written = buf.size();87  Status error = file.Write(buf.data(), bytes_written);88  EXPECT_EQ(error.Fail(), true);89}90