xref: /llvm-project/lldb/unittests/Host/FileActionTest.cpp (revision 3bc58fc7f79a9b0cbf931573cb257344bfeaca1e)
1 //===-- FileActionTest.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 <fcntl.h>
10 
11 #include "lldb/Host/FileAction.h"
12 #include "gtest/gtest.h"
13 #if defined(_WIN32)
14 #include "lldb/Host/windows/PosixApi.h"
15 #endif
16 
17 using namespace lldb_private;
18 
19 TEST(FileActionTest, Open) {
20   FileAction Action;
21   Action.Open(47, FileSpec("/tmp"), /*read*/ true, /*write*/ false);
22   EXPECT_EQ(Action.GetAction(), FileAction::eFileActionOpen);
23   EXPECT_EQ(Action.GetFileSpec(), FileSpec("/tmp"));
24 }
25 
26 TEST(FileActionTest, OpenReadWrite) {
27   FileAction Action;
28   Action.Open(48, FileSpec("/tmp_0"), /*read*/ true, /*write*/ true);
29   EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_CREAT | O_RDWR));
30   EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
31   EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
32 }
33 
34 TEST(FileActionTest, OpenReadOnly) {
35   FileAction Action;
36   Action.Open(49, FileSpec("/tmp_1"), /*read*/ true, /*write*/ false);
37 #ifndef _WIN32
38   EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_RDONLY));
39 #endif
40   EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
41 }
42 
43 TEST(FileActionTest, OpenWriteOnly) {
44   FileAction Action;
45   Action.Open(50, FileSpec("/tmp_2"), /*read*/ false, /*write*/ true);
46   EXPECT_TRUE(Action.GetActionArgument() &
47               (O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC));
48   EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
49 }
50