• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define TEST_BUFFER_SIZE 4
21 
22 typedef void (*TEST_FUN)();
23 const int FILE_ZERO = 0;
24 const int FILE_NEGA = -1;
25 const int FILE_EIGHT = 8;
26 const int FILE_TEN = 10;
27 const int FILE_TWENTWO = 22;
28 const int FILE_THIRTYTHREE = 33;
29 
30 /**
31  * @tc.name      : lseek64_0100
32  * @tc.desc      : Determine whether the file read position is as expected
33  * @tc.level     : Level 2
34  */
lseek64_0100(void)35 void lseek64_0100(void)
36 {
37     off64_t DataArry[TEST_BUFFER_SIZE] = {1, 2, 4, 8};
38     off64_t offset = 0;
39     int fd = open("lseek64_function_test.c", O_RDWR | O_CREAT, TEST_MODE);
40     if (fd < 0) {
41         t_error("open file failed\n");
42         return;
43     }
44     for (int i = 0; i < TEST_BUFFER_SIZE; i++) {
45         lseek64(fd, 0, SEEK_SET);
46         offset = lseek64(fd, DataArry[i], SEEK_SET);
47         if (offset != DataArry[i]) {
48             EXPECT_FALSE("lseek64_0100", 1);
49         }
50     }
51     close(fd);
52     int ret = access("lseek64_function_test.c", F_OK);
53     if (ret != -1) {
54         ret = remove("./lseek64_function_test.c");
55     }
56 }
57 
58 /**
59  * @tc.name      : lseek_0100
60  * @tc.desc      : Verify that the file pointer is moved to the beginning of the file
61  * @tc.level     : Level 0
62  */
lseek_0100(void)63 void lseek_0100(void)
64 {
65     char *wrstring = "This is a test sample!";
66     int fd = open("/data/readtest.txt", O_RDWR | O_CREAT, TEST_MODE);
67     EXPECT_STRLT("lseek_0100", 0, fd);
68     int retwrite = write(fd, wrstring, sizeof(wrstring));
69     off_t data = lseek(fd, 0L, SEEK_SET);
70     EXPECT_EQ("lseek_0100", (int)data, FILE_ZERO);
71     close(fd);
72     remove("/data/readtest.txt");
73 }
74 
75 /**
76  * @tc.name      : lseek_0200
77  * @tc.desc      : Verify that the file pointer is moved anywhere in the file
78  * @tc.level     : Level 0
79  */
lseek_0200(void)80 void lseek_0200(void)
81 {
82     char *wrstring = "This is a test sample!";
83     int fd = open("/data/readtest.txt", O_RDWR | O_CREAT, TEST_MODE);
84     EXPECT_STRLT("lseek_0200", 0, fd);
85     int retwrite = write(fd, wrstring, sizeof(wrstring));
86     off_t data = lseek(fd, 8L, SEEK_SET);
87     EXPECT_EQ("lseek_0200", (int)data, FILE_EIGHT);
88     close(fd);
89     remove("/data/readtest.txt");
90 }
91 
92 /**
93  * @tc.name      : lseek_0300
94  * @tc.desc      : Verify moves the file pointer to the current file position
95  * @tc.level     : Level 0
96  */
lseek_0300(void)97 void lseek_0300(void)
98 {
99     char *wrstring = "This is a test sample!";
100     int fd = open("/data/readtest.txt", O_RDWR | O_CREAT, TEST_MODE);
101     EXPECT_STRLT("lseek_0300", 0, fd);
102     int retwrite = write(fd, wrstring, sizeof(wrstring));
103     off_t data = lseek(fd, 10L, SEEK_SET);
104     data = lseek(fd, 0L, SEEK_CUR);
105     EXPECT_EQ("lseek_0300", (int)data, FILE_TEN);
106     close(fd);
107     remove("/data/readtest.txt");
108 }
109 
110 /**
111  * @tc.name      : lseek_0400
112  * @tc.desc      : Verify that the file pointer is moved to the end of the file
113  * @tc.level     : Level 0
114  */
lseek_0400(void)115 void lseek_0400(void)
116 {
117     char str[] = "This is a test sample!";
118     int fd = open("/data/readtest.txt", O_RDWR | O_CREAT, TEST_MODE);
119     EXPECT_STRLT("lseek_0400", 0, fd);
120     int retwrite = write(fd, str, sizeof(str));
121     off_t data = lseek(fd, -1L, SEEK_END);
122     EXPECT_EQ("lseek_0400", (int)data, FILE_TWENTWO);
123     close(fd);
124     remove("/data/readtest.txt");
125 }
126 
127 /**
128  * @tc.name      : lseek_0500
129  * @tc.desc      : Verify that the move file pointer position is past the start position
130  * @tc.level     : Level 2
131  */
lseek_0500(void)132 void lseek_0500(void)
133 {
134     char str[] = "This is a test sample!";
135     int fd = open("/data/readtest.txt", O_RDWR | O_CREAT, TEST_MODE);
136     EXPECT_STRLT("lseek_0500", 0, fd);
137     int retwrite = write(fd, str, sizeof(str));
138     off_t data = lseek(fd, -10L, SEEK_SET);
139     EXPECT_EQ("lseek_0500", (int)data, FILE_NEGA);
140     close(fd);
141     remove("/data/readtest.txt");
142 }
143 
144 /**
145  * @tc.name      : lseek_0600
146  * @tc.desc      : Verify that the moved file pointer position is past the end position
147  * @tc.level     : Level 1
148  */
lseek_0600(void)149 void lseek_0600(void)
150 {
151     char str[] = "This is a test sample!";
152     int fd = open("/data/readtest.txt", O_RDWR | O_CREAT, TEST_MODE);
153     EXPECT_STRLT("lseek_0600", 0, fd);
154     int retwrite = write(fd, str, sizeof(str));
155     off_t data = lseek(fd, 10L, SEEK_END);
156     EXPECT_EQ("lseek_0600", (int)data, FILE_THIRTYTHREE);
157     close(fd);
158     remove("/data/readtest.txt");
159 }
160 
161 TEST_FUN G_Fun_Array[] = {
162     lseek64_0100,
163     lseek_0100,
164     lseek_0200,
165     lseek_0300,
166     lseek_0400,
167     lseek_0500,
168     lseek_0600,
169 };
170 
main(void)171 int main(void)
172 {
173     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
174     for (int pos = 0; pos < num; ++pos) {
175         G_Fun_Array[pos]();
176     }
177 
178     return t_status;
179 }