• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <gtest/gtest.h>
4 #include <string.h>
5 #include <sys/utsname.h>
6 #include <sys/vfs.h>
7 #include <unistd.h>
8 
9 using namespace testing::ext;
10 
11 class FcntlFcntlTest : public testing::Test {
SetUp()12     void SetUp() override {}
TearDown()13     void TearDown() override {}
14 };
15 
16 /**
17  * @tc.name: fcntl_001
18  * @tc.desc: Verify the correctness of the fcntl interface in retrieving file descriptor flags and checks if the file
19  *           descriptor has the FD_CLOEXEC flag set.
20  * @tc.type: FUNC
21  */
22 HWTEST_F(FcntlFcntlTest, fcntl_001, TestSize.Level1)
23 {
24     int fd = open("test_fcntl_001.txt", O_RDONLY | O_CREAT, 0644);
25     ASSERT_NE(fd, -1);
26     int fdFlags = fcntl(fd, F_GETFD);
27     ASSERT_NE(fdFlags, -1);
28     EXPECT_EQ(fdFlags & FD_CLOEXEC, 0);
29     close(fd);
30     EXPECT_EQ(0, remove("test_fcntl_001.txt"));
31 }
32 
33 /**
34  * @tc.name: fcntl_002
35  * @tc.desc: Verify that the fcntl interface, when used with the F_DUPFD parameter, correctly creates a new file
36  *           descriptor that points to the same file table entry as the original file descriptor.
37  * @tc.type: FUNC
38  */
39 HWTEST_F(FcntlFcntlTest, fcntl_002, TestSize.Level1)
40 {
41     int fd = open("test_fcntl_002.txt", O_RDONLY | O_CREAT, 0644);
42     ASSERT_NE(fd, -1);
43     int newFd = fcntl(fd, F_DUPFD, 0);
44     EXPECT_GE(newFd, 0);
45     close(fd);
46     close(newFd);
47     EXPECT_EQ(0, remove("test_fcntl_002.txt"));
48 }
49 
50 /**
51  * @tc.name: fcntl_003
52  * @tc.desc: Verify that the fcntl interface, when used with the F_GETLK parameter, retrieves the lock information of
53  *           the file associated with the file descriptor, and the expected result is that the file is not locked.
54  * @tc.type: FUNC
55  */
56 HWTEST_F(FcntlFcntlTest, fcntl_003, TestSize.Level1)
57 {
58     int fd = open("test_fcntl_003.txt", O_RDWR | O_CREAT, 0644);
59     ASSERT_NE(fd, -1);
60     struct flock fileLock;
61     fileLock.l_type = F_WRLCK;
62     fileLock.l_whence = SEEK_SET;
63     fileLock.l_start = 0;
64     fileLock.l_len = 0;
65     int lockResult = fcntl(fd, F_GETLK, &fileLock);
66     ASSERT_NE(lockResult, -1);
67     close(fd);
68     EXPECT_EQ(0, remove("test_fcntl_003.txt"));
69 }
70 
71 /**
72  * @tc.name: fcntl_004
73  * @tc.desc: Verify that the fcntl interface, when used with the F_GETLK64 parameter, retrieves the lock information of
74  *           the file associated with the file descriptor, and the expected result is that the file is not locked.
75  * @tc.type: FUNC
76  */
77 HWTEST_F(FcntlFcntlTest, fcntl_004, TestSize.Level1)
78 {
79     int fd = open("test_fcntl_004.txt", O_RDWR | O_CREAT, 0644);
80     ASSERT_NE(fd, -1);
81     struct flock64 fileLock;
82     fileLock.l_type = F_WRLCK;
83     fileLock.l_whence = SEEK_SET;
84     fileLock.l_start = 0;
85     fileLock.l_len = 0;
86     int lockResult = fcntl(fd, F_GETLK64, &fileLock);
87     ASSERT_NE(lockResult, -1);
88     close(fd);
89     EXPECT_EQ(0, remove("test_fcntl_004.txt"));
90 }
91 
92 /**
93  * @tc.name: fcntl_005
94  * @tc.desc: Verify that the fcntl interface, when used with the F_GETFL parameter, retrieves the flag information of
95  *           the file descriptor, and the expected result is that the file descriptor does not have the O_APPEND flag
96  *           set, but has the O_RDWR flag set.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(FcntlFcntlTest, fcntl_005, TestSize.Level1)
100 {
101     int fd = open("test_fcntl_005.txt", O_RDWR | O_CREAT, 0666);
102     ASSERT_NE(-1, fd);
103     int fdFlags = fcntl(fd, F_GETFL);
104     ASSERT_NE(fdFlags, -1);
105     EXPECT_FALSE(fdFlags & O_APPEND);
106     EXPECT_EQ(fdFlags & O_RDWR, O_RDWR);
107     close(fd);
108     EXPECT_EQ(0, remove("test_fcntl_005.txt"));
109 }
110 
111 /**
112  * @tc.name: fcntl_006
113  * @tc.desc: Verify that the fcntl interface, when used with the F_SETOWN and F_GETOWN parameters, can set and retrieve
114  *           the process ownership of the file descriptor, and the expected result is that the set process ID matches
115  *           the retrieved process ID.
116  * @tc.type: FUNC
117  */
118 HWTEST_F(FcntlFcntlTest, fcntl_006, TestSize.Level1)
119 {
120     int fd = open("test_fcntl_006.txt", O_RDWR | O_CREAT, 0666);
121     ASSERT_NE(-1, fd);
122     pid_t pid = getpid();
123     int setPidResult = fcntl(fd, F_SETOWN, pid);
124     ASSERT_NE(setPidResult, -1);
125     int id = fcntl(fd, F_GETOWN);
126     ASSERT_NE(id, -1);
127     EXPECT_EQ(pid, id);
128     close(fd);
129     EXPECT_EQ(0, remove("test_fcntl_006.txt"));
130 }
131 
132 /**
133  * @tc.name: fcntl_007
134  * @tc.desc: Verify that the fcntl interface, when used with the F_SETFD and F_GETFD parameters, can set and retrieve
135  *           the close-on-exec flag of the file descriptor, and the expected result is that the set flag matches the
136  *           retrieved flag.
137  * @tc.type: FUNC
138  */
139 HWTEST_F(FcntlFcntlTest, fcntl_007, TestSize.Level1)
140 {
141     int fd = open("test_fcntl_007.txt", O_CREAT | O_RDWR, S_IRWXU);
142     ASSERT_NE(-1, fd);
143     int setFlagResult = fcntl(fd, F_SETFD, FD_CLOEXEC);
144     ASSERT_NE(setFlagResult, -1);
145     int getFlagResult = fcntl(fd, F_GETFD);
146     ASSERT_NE(getFlagResult, -1);
147     EXPECT_EQ(getFlagResult & FD_CLOEXEC, FD_CLOEXEC);
148     close(fd);
149     EXPECT_EQ(0, remove("test_fcntl_007.txt"));
150 }
151 
152 /**
153  * @tc.name: fcntl_008
154  * @tc.desc: Verify that the fcntl interface, when used with the F_GETFL and F_SETFL parameters, can retrieve and set
155  *           the flag information of the file descriptor, and the expected result is to add the O_NONBLOCK flag on top
156  *           of the original flags.
157  * @tc.type: FUNC
158  */
159 HWTEST_F(FcntlFcntlTest, fcntl_008, TestSize.Level1)
160 {
161     int fd = open("test_fcntl_008.txt", O_RDONLY | O_CREAT, 0644);
162     ASSERT_NE(fd, -1);
163     int fdFlags = fcntl(fd, F_GETFL);
164     ASSERT_NE(fdFlags, -1);
165     EXPECT_EQ(fdFlags & O_NONBLOCK, 0);
166     int newFlags = fdFlags | O_NONBLOCK;
167     int setResult = fcntl(fd, F_SETFL, newFlags);
168     ASSERT_NE(setResult, -1);
169     int updatedFlags = fcntl(fd, F_GETFL);
170     ASSERT_NE(updatedFlags, -1);
171     EXPECT_EQ(updatedFlags, newFlags);
172     close(fd);
173     EXPECT_EQ(0, remove("test_fcntl_008.txt"));
174 }
175 
176 /**
177  * @tc.name: fcntl_009
178  * @tc.desc: Verify that the fcntl interface, when used with the F_SETLK parameter, can lock the file descriptor for
179  *           writing, and the expected result is a successful setting of the write lock.
180  * @tc.type: FUNC
181  */
182 HWTEST_F(FcntlFcntlTest, fcntl_009, TestSize.Level1)
183 {
184     int fd = open("test_fcntl_009.txt", O_RDWR | O_CREAT, 0644);
185     ASSERT_NE(fd, -1);
186     struct flock fileLock;
187     fileLock.l_type = F_WRLCK;
188     fileLock.l_whence = SEEK_SET;
189     fileLock.l_start = 0;
190     fileLock.l_len = 0;
191     int setResult = fcntl(fd, F_SETLK, &fileLock);
192     ASSERT_NE(setResult, -1);
193     close(fd);
194     EXPECT_EQ(0, remove("test_fcntl_009.txt"));
195 }
196 
197 /**
198  * @tc.name: fcntl_010
199  * @tc.desc: Verify that the fcntl interface, when used with the F_SETLKW parameter, can lock the file descriptor for
200  *           writing and wait until the lock is released, and the expected result is a successful setting and waiting
201  *           for the write lock.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(FcntlFcntlTest, fcntl_010, TestSize.Level1)
205 {
206     int fd = open("test_fcntl_010.txt", O_RDWR | O_CREAT, 0644);
207     ASSERT_NE(fd, -1);
208     struct flock fileLock;
209     fileLock.l_type = F_WRLCK;
210     fileLock.l_whence = SEEK_SET;
211     fileLock.l_start = 0;
212     fileLock.l_len = 0;
213     int setResult = fcntl(fd, F_SETLKW, &fileLock);
214     ASSERT_NE(setResult, -1);
215     close(fd);
216     EXPECT_EQ(0, remove("test_fcntl_010.txt"));
217 }
218