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 <sys/file.h> 18#include "functionalext.h" 19 20/** 21 * @tc.name : flock_0100 22 * @tc.desc : Each parameter is valid, the op parameter is LOCK_SH, which can lock the file 23 * @tc.level : Level 0 24 */ 25void flock_0100(void) 26{ 27 int result = -1; 28 int fd = open("/data/test.txt", O_RDWR | O_CREAT, TEST_MODE); 29 EXPECT_NE("flock_0100", fd, -1); 30 lseek(fd, 2, SEEK_SET); 31 32 result = flock(fd, LOCK_SH); 33 EXPECT_EQ("flock_0100", result, 0); 34 close(fd); 35 remove("/data/test.txt"); 36} 37 38/** 39 * @tc.name : flock_0200 40 * @tc.desc : Each parameter is valid, the op parameter is LOCK_SH|LOCK_NB, which can lock the file 41 * @tc.level : Level 1 42 */ 43void flock_0200(void) 44{ 45 int result = -1; 46 int fd = open("/data/test.txt", O_RDWR | O_CREAT, TEST_MODE); 47 EXPECT_NE("flock_0200", fd, -1); 48 lseek(fd, 2, SEEK_SET); 49 50 result = flock(fd, LOCK_SH | LOCK_NB); 51 EXPECT_EQ("flock_0200", result, 0); 52 close(fd); 53 remove("/data/test.txt"); 54} 55 56/** 57 * @tc.name : flock_0300 58 * @tc.desc : Each parameter is valid, the op parameter is LOCK_EX, which can lock the file 59 * @tc.level : Level 0 60 */ 61void flock_0300(void) 62{ 63 int result = -1; 64 int fd = open("/data/test.txt", O_RDWR | O_CREAT, TEST_MODE); 65 EXPECT_NE("flock_0300", fd, -1); 66 lseek(fd, 2, SEEK_SET); 67 68 result = flock(fd, LOCK_EX); 69 EXPECT_EQ("flock_0300", result, 0); 70 close(fd); 71 remove("/data/test.txt"); 72} 73 74/** 75 * @tc.name : flock_0400 76 * @tc.desc : Each parameter is valid, the op parameter is LOCK_EX|LOCK_NB, which can lock the file 77 * @tc.level : Level 1 78 */ 79void flock_0400(void) 80{ 81 int result = -1; 82 int fd = open("/data/test.txt", O_RDWR | O_CREAT, TEST_MODE); 83 EXPECT_NE("flock_0400", fd, -1); 84 lseek(fd, 2, SEEK_SET); 85 86 result = flock(fd, LOCK_EX | LOCK_NB); 87 EXPECT_EQ("flock_0400", result, 0); 88 close(fd); 89 remove("/data/test.txt"); 90} 91 92/** 93 * @tc.name : flock_0500 94 * @tc.desc : Each parameter is valid, the op parameter is LOCK_UN, which can release the file lock state 95 * @tc.level : Level 0 96 */ 97void flock_0500(void) 98{ 99 int result = -1; 100 int fd = open("/data/test.txt", O_RDWR | O_CREAT, TEST_MODE); 101 EXPECT_NE("flock_0500", fd, -1); 102 lseek(fd, 2, SEEK_SET); 103 104 result = flock(fd, LOCK_UN); 105 EXPECT_EQ("flock_0500", result, 0); 106 close(fd); 107 remove("/data/test.txt"); 108} 109 110/** 111 * @tc.name : flock_0600 112 * @tc.desc : The fd parameter is invalid, the file cannot be locked 113 * @tc.level : Level 2 114 */ 115void flock_0600(void) 116{ 117 int result = flock(-1, LOCK_EX); 118 EXPECT_EQ("flock_0600", result, -1); 119} 120 121/** 122 * @tc.name : flock_0700 123 * @tc.desc : The op parameter is invalid, the file cannot be locked 124 * @tc.level : Level 2 125 */ 126void flock_0700() 127{ 128 int result = 0; 129 int fd = open("/data/test.txt", O_RDWR | O_CREAT, TEST_MODE); 130 EXPECT_NE("flock_0700", fd, -1); 131 lseek(fd, 2, SEEK_SET); 132 133 result = flock(fd, 0); 134 EXPECT_EQ("flock_0700", result, -1); 135 close(fd); 136 remove("/data/test.txt"); 137} 138 139int main(int argc, char *argv[]) 140{ 141 flock_0100(); 142 flock_0200(); 143 flock_0300(); 144 flock_0400(); 145 flock_0500(); 146 flock_0600(); 147 flock_0700(); 148 return t_status; 149}