• 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 <stdbool.h>
18 #include <stdio.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include "functionalext.h"
23 
24 const int32_t INIT_LEN = 0;
25 const int32_t INCREASE_LEN = 1;
26 const int32_t CREAT_MODE = 666;
27 
28 /**
29  * @tc.name       : fopen_0100
30  * @tc.desc       : Verify fopen("r") and fclose fread and fwrite, fopen success, fread success, fwrite failed
31  * @tc.level      : level 0.
32  */
fopen_0100(void)33 void fopen_0100(void)
34 {
35     char abc[100] = {0};
36     const char *add = "this is tempory test!";
37     const char *wrstring = "to write";
38     const char *wstring = "this is tempory test!";
39     int ret = creat("tempory_test.txt", CREAT_MODE);
40     if (ret < 0) {
41         EXPECT_MT("fopen_0100", ret, 0);
42         return;
43     }
44     FILE *fptr = fopen("tempory_test.txt", "w");
45     if (fptr != NULL) {
46         size_t wrsize = fwrite(wstring, sizeof(char), strlen(wstring), fptr);
47         if (wrsize == INIT_LEN) {
48             EXPECT_EQ("fopen_0100", wrsize, INIT_LEN);
49         }
50         fclose(fptr);
51     }
52     fptr = fopen("tempory_test.txt", "r");
53     bool writeFailedFlag = false;
54     bool consistentFlag = false;
55     if (fptr != NULL) {
56         while (!feof(fptr)) {
57             int32_t rsize = fread(abc, sizeof(abc), 1, fptr);
58             EXPECT_EQ("pread_0100", rsize, 0);
59         }
60         if (!strncmp(abc, add, strlen(add))) {
61             consistentFlag = true;
62         }
63 
64         size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
65         if (wrsize == INIT_LEN) {
66             writeFailedFlag = true;
67         }
68         fclose(fptr);
69     }
70 
71     EXPECT_TRUE("fopen_0100", writeFailedFlag);
72     EXPECT_TRUE("fopen_0100", consistentFlag);
73     remove("tempory_test.txt");
74 }
75 
76 /**
77  * @tc.name       : fopen_0200
78  * @tc.desc       : Verify fopen("r") and fclose and fopen failed
79  * @tc.level      : level 2.
80  */
fopen_0200(void)81 void fopen_0200(void)
82 {
83     FILE *fptr = fopen("tempory_test1.txt", "r");
84     EXPECT_EQ("fopen_0200", fptr, NULL);
85 }
86 
87 /**
88  * @tc.name       : fopen_0300
89  * @tc.desc       : Verify fopen("w") and fclose and fread and fwrite and fopen success, fread failed, fwrite success
90  * @tc.level      : level 0.
91  */
fopen_0300(void)92 void fopen_0300(void)
93 {
94     int32_t rsize = 0;
95     char abc[100] = {0};
96     const char *wrstring = "to write";
97     FILE *fptr = fopen("tempory_test3.txt", "w");
98     bool writeSuccessFlag = false;
99 
100     if (fptr != NULL) {
101         rsize = fread(abc, sizeof(abc), 1, fptr);
102         size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
103         if (wrsize == INIT_LEN) {
104             EXPECT_EQ("fopen_0300", wrsize, INIT_LEN);
105             ;
106         } else if (wrsize == strlen(wrstring)) {
107             writeSuccessFlag = true;
108         }
109         fclose(fptr);
110     }
111 
112     EXPECT_EQ("fopen_0300", rsize, INIT_LEN);
113     EXPECT_TRUE("fopen_0300", writeSuccessFlag);
114     remove("tempory_test3.txt");
115 }
116 
117 /**
118  * @tc.name       : fopen_0400
119  * @tc.desc       : Verify fopen("w") and fclose and fread and fwrite and fopen success, fread failed, fwrite success
120  * @tc.level      : level 1.
121  */
fopen_0400(void)122 void fopen_0400(void)
123 {
124     int32_t rsize = 0;
125     char abc[100] = {0};
126     const char *wrstring = "to write";
127     FILE *fptr = fopen("tempory_test2.txt", "w");
128     bool writeSuccessFlag = false;
129 
130     if (fptr != NULL) {
131         rsize = fread(abc, sizeof(abc), 1, fptr);
132 
133         size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
134         if (wrsize == INIT_LEN) {
135             EXPECT_EQ("fopen_0400", wrsize, INIT_LEN);
136         } else if (wrsize == strlen(wrstring)) {
137             writeSuccessFlag = true;
138         }
139         fclose(fptr);
140     }
141 
142     EXPECT_EQ("fopen_0400", rsize, INIT_LEN);
143     EXPECT_TRUE("fopen_0400", writeSuccessFlag);
144     remove("tempory_test2.txt");
145 }
146 
147 /**
148  * @tc.name       : fopen_0500
149  * @tc.desc       : Verify fopen("a") and fclose and fread and fwrite and fopen success, fread success, fwrite success
150  * @tc.level      : level 0.
151  */
fopen_0500(void)152 void fopen_0500(void)
153 {
154     char abc[100] = {0};
155     const char *add = "this is tempory test!to write";
156     const char *wrstring = "to write";
157     const char *wstring = "this is tempory test!";
158     int ret = creat("tempory_test5.txt", CREAT_MODE);
159     if (ret < 0) {
160         EXPECT_MT("fopen_0500", ret, 0);
161         return;
162     }
163     FILE *fptr = fopen("tempory_test5.txt", "w");
164     if (fptr != NULL) {
165         size_t wrsize = fwrite(wstring, sizeof(char), strlen(wstring), fptr);
166         if (wrsize == INIT_LEN) {
167             EXPECT_EQ("fopen_0500", wrsize, INIT_LEN);
168         }
169         fclose(fptr);
170     }
171 
172     fptr = fopen("tempory_test5.txt", "a");
173     bool writeSuccessFlag = false;
174     bool consistentFlag = false;
175     if (fptr != NULL) {
176         fseek(fptr, 0, SEEK_SET);
177         size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
178         if (wrsize == INIT_LEN) {
179             EXPECT_EQ("fopen_0500", wrsize, INIT_LEN);
180         } else if (wrsize == strlen(wrstring)) {
181             writeSuccessFlag = true;
182         }
183         fclose(fptr);
184     }
185 
186     fptr = fopen("tempory_test5.txt", "r");
187     if (fptr != NULL) {
188         while (!feof(fptr)) {
189             int32_t rsize = fread(abc, sizeof(abc), 1, fptr);
190             EXPECT_EQ("pread_0500", rsize, 0);
191         }
192         if (!strncmp(abc, add, strlen(add))) {
193             consistentFlag = true;
194         }
195         fclose(fptr);
196     }
197 
198     EXPECT_TRUE("fopen_0500", writeSuccessFlag);
199     EXPECT_TRUE("fopen_0500", consistentFlag);
200     remove("tempory_test5.txt");
201 }
202 
203 /**
204  * @tc.name       : fopen_0600
205  * @tc.desc       : Verify fopen("a") and fclose and fread and fwrite and fopen success, fread success, fwrite success
206  * @tc.level      : level 1.
207  */
fopen_0600(void)208 void fopen_0600(void)
209 {
210     int32_t rsize = 0;
211     char abc[100] = {0};
212     const char *wrstring = "to write";
213     FILE *fptr = fopen("tempory_test6.txt", "w");
214     bool writeSuccessFlag = false;
215 
216     if (fptr != NULL) {
217         rsize = fread(abc, sizeof(abc), 1, fptr);
218 
219         size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
220         if (wrsize == INIT_LEN) {
221             EXPECT_EQ("fopen_0600", wrsize, INIT_LEN);
222             ;
223         } else if (wrsize == strlen(wrstring)) {
224             writeSuccessFlag = true;
225         }
226         fclose(fptr);
227     }
228 
229     EXPECT_EQ("fopen_0600", rsize, INIT_LEN);
230     EXPECT_TRUE("fopen_0600", writeSuccessFlag);
231     remove("tempory_test6.txt");
232 }
233 
234 /**
235  * @tc.name       : fopen_0700
236  * @tc.desc       : Verify fopen("b") and fclose and fread and fwrite and fopen failed
237  * @tc.level      : level 2.
238  */
fopen_0700(void)239 void fopen_0700(void)
240 {
241     FILE *fptr = fopen("./tempory_test.txt", "b");
242     int32_t flag = INIT_LEN;
243     if (fptr == NULL) {
244         EXPECT_EQ("fopen_0700", errno, EINVAL);
245     } else {
246         flag++;
247         EXPECT_EQ("fopen_0700", flag, INCREASE_LEN);
248     }
249 }
250 
251 /**
252  * @tc.name       : fopen_0800
253  * @tc.desc       : Verify fopen("a") and fclose and fread and fwrite and fopen failed
254  * @tc.level      : level 2.
255  */
fopen_0800(void)256 void fopen_0800(void)
257 {
258     struct stat info;
259     if(stat("/data/local/tmp/libc-test-empty/", &info) == 0 && S_ISDIR(info.st_mode) ) {
260         system("rm -rf /data/local/tmp/libc-test-empty/");
261     }
262     FILE *fptr = fopen("/data/local/tmp/libc-test-empty/tempory_test.txt", "a");
263     EXPECT_EQ("fopen_0800", fptr, NULL);
264 }
265 
266 /**
267  * @tc.name       : fopen_0900
268  * @tc.desc       : Verify fopen("ae") and fclose and fread and fwrite and fopen success, fread success, fwrite success
269  * @tc.level      : level 0.
270  */
fopen_0900(void)271 void fopen_0900(void)
272 {
273     char abc[100] = {0};
274     const char *add = "this is tempory test!to write";
275     const char *wrstring = "to write";
276     const char *wstring = "this is tempory test!";
277     int ret = creat("tempory_test9.txt", CREAT_MODE);
278     if (ret < 0) {
279         EXPECT_MT("fopen_0900", ret, 0);
280         return;
281     }
282     FILE *fptr = fopen("tempory_test9.txt", "w");
283     if (fptr != NULL) {
284         size_t wrsize = fwrite(wstring, sizeof(char), strlen(wstring), fptr);
285         if (wrsize == INIT_LEN) {
286             EXPECT_EQ("fopen_0900", wrsize, INIT_LEN);
287             ;
288         }
289         fclose(fptr);
290     }
291 
292     fptr = fopen("tempory_test9.txt", "ae");
293     bool writeSuccessFlag = false;
294     bool consistentFlag = false;
295     if (fptr != NULL) {
296         size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
297         if (wrsize == INIT_LEN) {
298             EXPECT_EQ("fopen_0900", wrsize, INIT_LEN);
299             ;
300         } else if (wrsize == strlen(wrstring)) {
301             writeSuccessFlag = true;
302         }
303         fclose(fptr);
304     }
305 
306     fptr = fopen("tempory_test9.txt", "r");
307     if (fptr != NULL) {
308         while (!feof(fptr)) {
309             int32_t rsize = fread(abc, sizeof(abc), 1, fptr);
310             EXPECT_EQ("pread_0900", rsize, 0);
311         }
312         if (!strncmp(abc, add, strlen(add))) {
313             consistentFlag = true;
314         }
315         fclose(fptr);
316     }
317     remove("tempory_test9.txt");
318 
319     EXPECT_TRUE("fopen_0900", writeSuccessFlag);
320     EXPECT_TRUE("fopen_0900", consistentFlag);
321 }
322 
main(void)323 int main(void)
324 {
325     fopen_0100();
326     fopen_0200();
327     fopen_0300();
328     fopen_0400();
329     fopen_0500();
330     fopen_0600();
331     fopen_0700();
332     fopen_0800();
333     fopen_0900();
334     return t_status;
335 }
336