1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include "functionalext.h"
19
20 const char *path = "lockf.txt";
21 const int SIZE = 3;
22
23 /**
24 * @tc.name : lockf_0100
25 * @tc.desc : Verify lockf process success. When cmd is F_LOCK.
26 * @tc.level : Level 0
27 */
lockf_0100(void)28 void lockf_0100(void)
29 {
30 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
31 if (fd < 0) {
32 t_error("%s failed: fd = %d\n", __func__, fd);
33 return;
34 }
35
36 int ret = lockf(fd, F_LOCK, 0);
37 EXPECT_EQ("lockf_0100", ret, 0);
38
39 ret = lockf(fd, F_ULOCK, 0);
40 EXPECT_EQ("lockf_0100", ret, 0);
41
42 ret = remove(path);
43 EXPECT_EQ("lockf_0100", ret, 0);
44 }
45
46 /**
47 * @tc.name : lockf_0200
48 * @tc.desc : Verify lockf process success. When cmd is F_TLOCK.
49 * @tc.level : Level 0
50 */
lockf_0200(void)51 void lockf_0200(void)
52 {
53 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
54 if (fd < 0) {
55 t_error("%s failed: fd = %d\n", __func__, fd);
56 return;
57 }
58
59 int ret = lockf(fd, F_TLOCK, 0);
60 EXPECT_EQ("lockf_0200", ret, 0);
61
62 ret = lockf(fd, F_ULOCK, 0);
63 EXPECT_EQ("lockf_0200", ret, 0);
64
65 ret = remove(path);
66 EXPECT_EQ("lockf_0200", ret, 0);
67 }
68
69 /**
70 * @tc.name : lockf_0300
71 * @tc.desc : Verify lockf process success. When cmd is F_TEST.
72 * @tc.level : Level 0
73 */
lockf_0300(void)74 void lockf_0300(void)
75 {
76 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
77 if (fd < 0) {
78 t_error("%s failed: fd = %d\n", __func__, fd);
79 }
80
81 int ret = lockf(fd, F_LOCK, 0);
82 EXPECT_EQ("lockf_0300", ret, 0);
83
84 ret = lockf(fd, F_TEST, 0);
85 EXPECT_EQ("lockf_0300", ret, 0);
86
87 ret = lockf(fd, F_ULOCK, 0);
88 EXPECT_EQ("lockf_0300", ret, 0);
89
90 ret = remove(path);
91 EXPECT_EQ("lockf_0300", ret, 0);
92 }
93
94 /**
95 * @tc.name : lockf_0400
96 * @tc.desc : Verify lockf process fail bacause lock mode is error.
97 * @tc.level : Level 0
98 */
lockf_0400(void)99 void lockf_0400(void)
100 {
101 int fd = open(path, O_RDWR | O_CREAT, TEST_MODE);
102 if (fd < 0) {
103 t_error("%s failed: fd = %d\n", __func__, fd);
104 }
105
106 int ret = lockf(fd, -1, 0);
107 EXPECT_EQ("lockf_0400", ret, -1);
108
109 ret = remove(path);
110 EXPECT_EQ("lockf_0400", ret, 0);
111 }
112
main(void)113 int main(void)
114 {
115 lockf_0100();
116 lockf_0200();
117 lockf_0300();
118 lockf_0400();
119 return t_status;
120 }
121