• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef __NEED_mode_t
33 #define __NEED_mode_t
34 #endif
35 
36 #include <securec.h>
37 #include <stdio.h>
38 #include <libgen.h>
39 #include "ohos_types.h"
40 #include "posix_test.h"
41 #include "los_config.h"
42 #include "kernel_test.h"
43 #include "log.h"
44 #include <fcntl.h>
45 #include <dirent.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48 
49 /* *
50  * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
51  * @param        : subsystem name is utils
52  * @param        : module name is utilsFile
53  * @param        : test suit name is PosixFsFuncTestSuite
54  */
55 LITE_TEST_SUIT(Posix, PosixFs, PosixFsFuncTestSuite);
56 
57 /* Corresponding to different platforms, only need to modify TEST_ROOT  */
58 #define TEST_ROOT            "/littlefs"
59 
60 #define TEST_FILE_PTAH_RIGHT    TEST_ROOT"/FILE0"   /* file path, to open/rd/close */
61 
62 #define FILE0                   "FILE0"             /* common file name used for testing  */
63 #define FILE1                   TEST_ROOT"/FILE1"   /* common file under test root path name used for testing */
64 #define DIR1                    TEST_ROOT"/DIR1/"   /* common path under test root path name used for testing */
65 
66 #define DIRA                    TEST_ROOT"/a"
67 #define DIRAB                   TEST_ROOT"/a/b"
68 #define DIRAC                   TEST_ROOT"/a/c"
69 
70 
71 #define TEST_BUF_SIZE           40                  /* 40, common data for test, no special meaning */
72 #define TEST_SEEK_SIZE          10                  /* 10, common data for test, no special meaning */
73 #define TEST_RW_SIZE            20                  /* 20, common data for test, no special meaning */
74 #define TEST_LOOPUP_TIME        20                 /* 100, common data for test, no special meaning */
75 /* *
76  * @tc.setup     : setup for all testcases
77  * @return       : setup result, TRUE is success, FALSE is fail
78  */
PosixFsFuncTestSuiteSetUp(void)79 static BOOL PosixFsFuncTestSuiteSetUp(void)
80 {
81     return TRUE;
82 }
83 
84 /* *
85  * @tc.teardown  : teardown for all testcases
86  * @return       : teardown result, TRUE is success, FALSE is fail
87  */
PosixFsFuncTestSuiteTearDown(void)88 static BOOL PosixFsFuncTestSuiteTearDown(void)
89 {
90     printf("+-------------------------------------------+\n");
91     return TRUE;
92 }
93 
94 #if (LOSCFG_LIBC_MUSL == 1)
95 /* *
96  * @tc.number   SUB_KERNEL_FS_DIRNAME_001
97  * @tc.name     dirname basic function test
98  * @tc.desc     [C- SOFTWARE -0200]
99  */
100 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsDirname001, Function | MediumTest | Level1)
101 {
102     char path[] = FILE0;
103     char *workDir = dirname((char *)path);
104     TEST_ASSERT_NOT_NULL(workDir);
105     TEST_ASSERT_EQUAL_STRING(".", workDir);
106     return 0;
107 }
108 
109 /* *
110  * @tc.number   SUB_KERNEL_FS_DIRNAME_002
111  * @tc.name     dirname basic function test
112  * @tc.desc     [C- SOFTWARE -0200]
113  */
114 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsDirname002, Function | MediumTest | Level1)
115 {
116     char path[] = FILE1;
117     char *workDir = dirname((char *)path);
118     TEST_ASSERT_NOT_NULL(workDir);
119     TEST_ASSERT_EQUAL_STRING(TEST_ROOT, workDir);
120     return 0;
121 }
122 
123 /* *
124  * @tc.number   SUB_KERNEL_FS_DIRNAME_003
125  * @tc.name     dirname basic function test
126  * @tc.desc     [C- SOFTWARE -0200]
127  */
128 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsDirname003, Function | MediumTest | Level1)
129 {
130     // get dir
131     char path[] = DIR1;
132     char *workDir = dirname((char *)path);
133     TEST_ASSERT_NOT_NULL(workDir);
134     TEST_ASSERT_EQUAL_STRING(TEST_ROOT, workDir);
135     return 0;
136 }
137 
138 /* *
139  * @tc.number   SUB_KERNEL_FS_DIRNAME_004
140  * @tc.name     dirname basic function test for special input
141  * @tc.desc     [C- SOFTWARE -0200]
142  */
143 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsDirname004, Function | MediumTest | Level1)
144 {
145     // get dir
146     char *workDir = dirname("");
147     TEST_ASSERT_NOT_NULL(workDir);
148     TEST_ASSERT_EQUAL_STRING(".", workDir);
149 
150     workDir = dirname(NULL);
151     TEST_ASSERT_NOT_NULL(workDir);
152     TEST_ASSERT_EQUAL_STRING(".", workDir);
153 
154     workDir = dirname("/");
155     TEST_ASSERT_NOT_NULL(workDir);
156     TEST_ASSERT_EQUAL_STRING("/", workDir);
157 
158     workDir = dirname("..");
159     TEST_ASSERT_NOT_NULL(workDir);
160     TEST_ASSERT_EQUAL_STRING(".", workDir);
161 
162     workDir = dirname(".");
163     TEST_ASSERT_NOT_NULL(workDir);
164     TEST_ASSERT_EQUAL_STRING(".", workDir);
165     return 0;
166 }
167 #endif
168 
169 /* *
170  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_001
171  * @tc.name     fopen and fclose
172  * @tc.desc     [C- SOFTWARE -0200]
173  */
174 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose001, Function | MediumTest | Level1)
175 {
176     int ret = 0;
177     FILE *fp = NULL;
178     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
179     TEST_ASSERT_NOT_NULL(fp);
180 
181     ret = fclose(fp);
182     TEST_ASSERT_EQUAL_INT(ret, 0);
183     return 0;
184 }
185 
186 /* *
187  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_002
188  * @tc.name     fopen and fclose
189  * @tc.desc     [C- SOFTWARE -0200]
190  */
191 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose002, Function | MediumTest | Level1)
192 {
193     int ret = 0;
194     FILE *fp = NULL;
195     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
196     TEST_ASSERT_NOT_NULL(fp);
197 
198     ret = fclose(fp);
199     TEST_ASSERT_EQUAL_INT(ret, 0);
200     return 0;
201 }
202 
203 /* *
204  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_003
205  * @tc.name     fopen and fclose
206  * @tc.desc     [C- SOFTWARE -0200]
207  */
208 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose003, Function | MediumTest | Level1)
209 {
210     int ret = 0;
211     FILE *fp = NULL;
212     fp = fopen(TEST_FILE_PTAH_RIGHT, "r");
213     TEST_ASSERT_NOT_NULL(fp);
214 
215     ret = fclose(fp);
216     TEST_ASSERT_EQUAL_INT(ret, 0);
217     return 0;
218 }
219 
220 /* *
221  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_004
222  * @tc.name     fopen and fclose
223  * @tc.desc     [C- SOFTWARE -0200]
224  */
225 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose004, Function | MediumTest | Level1)
226 {
227     int ret = 0;
228     FILE *fp = NULL;
229     fp = fopen(TEST_FILE_PTAH_RIGHT, "a");
230     TEST_ASSERT_NOT_NULL(fp);
231 
232     ret = fclose(fp);
233     TEST_ASSERT_EQUAL_INT(ret, 0);
234     return 0;
235 }
236 
237 /* *
238  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_005
239  * @tc.name     fopen and fclose
240  * @tc.desc     [C- SOFTWARE -0200]
241  */
242 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose005, Function | MediumTest | Level1)
243 {
244     int ret = 0;
245     FILE *fp = NULL;
246     fp = fopen(TEST_FILE_PTAH_RIGHT, "c");
247     TEST_ASSERT_NULL(fp);
248     return 0;
249 }
250 
251 /* *
252  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_006
253  * @tc.name     remove the path before fopen and fclose
254  * @tc.desc     [C- SOFTWARE -0200]
255  */
256 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose006, Function | MediumTest | Level1)
257 {
258     int ret = 0;
259     FILE *fp = NULL;
260 
261     remove(TEST_FILE_PTAH_RIGHT);
262     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
263     TEST_ASSERT_NOT_NULL(fp);
264 
265     ret = fclose(fp);
266     TEST_ASSERT_EQUAL_INT(ret, 0);
267     return 0;
268 }
269 
270 /* *
271  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_007
272  * @tc.name     remove the path before fopen and fclose
273  * @tc.desc     [C- SOFTWARE -0200]
274  */
275 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose007, Function | MediumTest | Level1)
276 {
277     int ret = 0;
278     FILE *fp = NULL;
279 
280     remove(TEST_FILE_PTAH_RIGHT);
281     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
282     TEST_ASSERT_NOT_NULL(fp);
283 
284     ret = fclose(fp);
285     TEST_ASSERT_EQUAL_INT(ret, 0);
286     return 0;
287 }
288 
289 /* *
290  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_008
291  * @tc.name     remove the path before fopen and fclose
292  * @tc.desc     [C- SOFTWARE -0200]
293  */
294 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose008, Function | MediumTest | Level1)
295 {
296     int ret = 0;
297     FILE *fp = NULL;
298 
299     remove(TEST_FILE_PTAH_RIGHT);
300     fp = fopen(TEST_FILE_PTAH_RIGHT, "a+");
301     TEST_ASSERT_NOT_NULL(fp);
302 
303     ret = fclose(fp);
304     TEST_ASSERT_EQUAL_INT(ret, 0);
305     return 0;
306 }
307 
308 /* *
309  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_009
310  * @tc.name     remove the path before fopen and fclose
311  * @tc.desc     [C- SOFTWARE -0200]
312  */
313 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose009, Function | MediumTest | Level1)
314 {
315     int ret = 0;
316     FILE *fp = NULL;
317 
318     remove(TEST_FILE_PTAH_RIGHT);
319     fp = fopen(TEST_FILE_PTAH_RIGHT, "a");
320     TEST_ASSERT_NOT_NULL(fp);
321 
322     ret = fclose(fp);
323     TEST_ASSERT_EQUAL_INT(ret, 0);
324     return 0;
325 }
326 
327 /* *
328  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_010
329  * @tc.name     remove the path before fopen and fclose
330  * @tc.desc     [C- SOFTWARE -0200]
331  */
332 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose010, Function | MediumTest | Level1)
333 {
334     int ret = 0;
335     FILE *fp = NULL;
336 
337     remove(TEST_FILE_PTAH_RIGHT);
338     fp = fopen(TEST_FILE_PTAH_RIGHT, "wr");
339     TEST_ASSERT_NOT_NULL(fp);
340 
341     ret = fclose(fp);
342     TEST_ASSERT_EQUAL_INT(ret, 0);
343     return 0;
344 }
345 
346 /* *
347  * @tc.number   SUB_KERNEL_FS_FOPEN_FCLOSE_011
348  * @tc.name     wrong input
349  * @tc.desc     [C- SOFTWARE -0200]
350  */
351 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFopenFclose011, Function | MediumTest | Level1)
352 {
353     int ret = 0;
354     FILE *fp = NULL;
355 
356     fp = fopen(TEST_FILE_PTAH_RIGHT, NULL);
357     TEST_ASSERT_NULL(fp);
358     return 0;
359 }
360 
361 /* *
362  * @tc.number   SUB_KERNEL_FS_FDOPEN_001
363  * @tc.name     fdopen
364  * @tc.desc     [C- SOFTWARE -0200]
365  */
366 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFdopen001, Function | MediumTest | Level1)
367 {
368     int ret = 0;
369     FILE *fp = NULL;
370     int fd = 0;
371 
372     remove(TEST_FILE_PTAH_RIGHT);
373     fd = open(TEST_FILE_PTAH_RIGHT, O_CREAT | O_RDWR, 0666);
374     TEST_ASSERT_TRUE(fd >= 0);
375 
376     fp = fdopen(fd, "w");
377     TEST_ASSERT_NOT_NULL(fp);
378 
379     ret = fclose(fp);
380     TEST_ASSERT_EQUAL_INT(ret, 0);
381     return 0;
382 }
383 
384 /* *
385  * @tc.number   SUB_KERNEL_FS_FDOPEN_002
386  * @tc.name     fdopen
387  * @tc.desc     [C- SOFTWARE -0200]
388  */
389 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFdopen002, Function | MediumTest | Level1)
390 {
391     int ret = 0;
392     FILE *fp = NULL;
393     int fd = 0;
394 
395     remove(TEST_FILE_PTAH_RIGHT);
396     fd = open(TEST_FILE_PTAH_RIGHT, O_CREAT | O_RDWR, 0666);
397     TEST_ASSERT_TRUE(fd >= 0);
398 
399     fp = fdopen(fd, "w+");
400     TEST_ASSERT_NOT_NULL(fp);
401 
402     ret = fclose(fp);
403     TEST_ASSERT_EQUAL_INT(ret, 0);
404     return 0;
405 }
406 
407 /* *
408  * @tc.number   SUB_KERNEL_FS_FDOPEN_003
409  * @tc.name     fdopen
410  * @tc.desc     [C- SOFTWARE -0200]
411  */
412 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFdopen003, Function | MediumTest | Level1)
413 {
414     int ret = 0;
415     FILE *fp = NULL;
416     int fd = 0;
417 
418     remove(TEST_FILE_PTAH_RIGHT);
419     fd = open(TEST_FILE_PTAH_RIGHT, O_CREAT | O_RDWR, 0666);
420     TEST_ASSERT_TRUE(fd >= 0);
421 
422     fp = fdopen(fd, "a");
423     TEST_ASSERT_NOT_NULL(fp);
424 
425     ret = fclose(fp);
426     TEST_ASSERT_EQUAL_INT(ret, 0);
427     return 0;
428 }
429 
430 #if (LOSCFG_LIBC_MUSL == 1)
431 /* *
432  * @tc.number   SUB_KERNEL_FS_FDOPEN_004
433  * @tc.name     fdopen
434  * @tc.desc     [C- SOFTWARE -0200]
435  */
436 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFdopen004, Function | MediumTest | Level1)
437 {
438     int ret = 0;
439     FILE *fp = NULL;
440     int fd = 0;
441 
442     remove(TEST_FILE_PTAH_RIGHT);
443     fd = open(TEST_FILE_PTAH_RIGHT, O_CREAT | O_RDWR, 0666);
444     TEST_ASSERT_TRUE(fd >= 0);
445 
446     fp = fdopen(500, "w"); /* 500 is a wrong fd */
447     // in some fs, may return ok, so return null or not is pass.
448     if (NULL == fp) {
449         close (fd);
450         return 0;
451     }
452 
453     fp->fd = fd;
454     ret = fclose(fp);
455     TEST_ASSERT_EQUAL_INT(ret, 0);
456     return 0;
457 }
458 #endif
459 /* *
460  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_001
461  * @tc.name     ftell and fseek
462  * @tc.desc     [C- SOFTWARE -0200]
463  */
464 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek001, Function | MediumTest | Level1)
465 {
466     int ret = 0;
467     FILE *fp = NULL;
468     long off = 0;
469 
470     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
471     TEST_ASSERT_NOT_NULL(fp);
472 
473     off = ftell(fp);
474     TEST_ASSERT_EQUAL_INT((int)off, 0);
475 
476     ret = fclose(fp);
477     TEST_ASSERT_EQUAL_INT(ret, 0);
478     return 0;
479 }
480 
481 /* *
482  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_002
483  * @tc.name     ftell and fseek
484  * @tc.desc     [C- SOFTWARE -0200]
485  */
486 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek002, Function | MediumTest | Level1)
487 {
488     int ret = 0;
489     FILE *fp = NULL;
490     long off = 0;
491 
492     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
493     TEST_ASSERT_NOT_NULL(fp);
494 
495     ret = fseek(fp, 0L, SEEK_SET);
496     TEST_ASSERT_TRUE(ret != -1);
497 
498     off = ftell(fp);
499     TEST_ASSERT_EQUAL_INT((int)off, 0);
500 
501     ret = fclose(fp);
502     TEST_ASSERT_EQUAL_INT(ret, 0);
503     return 0;
504 }
505 
506 /* *
507  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_003
508  * @tc.name     ftell and fseek
509  * @tc.desc     [C- SOFTWARE -0200]
510  */
511 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek003, Function | MediumTest | Level1)
512 {
513     int ret = 0;
514     FILE *fp = NULL;
515     long off = 0;
516 
517     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
518     TEST_ASSERT_NOT_NULL(fp);
519 
520     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_SET);
521     TEST_ASSERT_TRUE(ret != -1);
522 
523     off = ftell(fp);
524     TEST_ASSERT_EQUAL_INT((int)off, TEST_SEEK_SIZE);
525 
526     ret = fclose(fp);
527     TEST_ASSERT_EQUAL_INT(ret, 0);
528     return 0;
529 }
530 
531 /* *
532  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_004
533  * @tc.name     ftell and fseek
534  * @tc.desc     [C- SOFTWARE -0200]
535  */
536 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek004, Function | MediumTest | Level1)
537 {
538     int ret = 0;
539     FILE *fp = NULL;
540     long off = 0;
541 
542     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
543     TEST_ASSERT_NOT_NULL(fp);
544 
545     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_END);
546     TEST_ASSERT_TRUE(ret != -1);
547 
548     off = ftell(fp);
549     TEST_ASSERT_EQUAL_INT((int)off, TEST_SEEK_SIZE);
550 
551     ret = fclose(fp);
552     TEST_ASSERT_EQUAL_INT(ret, 0);
553     return 0;
554 }
555 
556 /* *
557  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_005
558  * @tc.name     ftell and fseek
559  * @tc.desc     [C- SOFTWARE -0200]
560  */
561 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek005, Function | MediumTest | Level1)
562 {
563     int ret = 0;
564     FILE *fp = NULL;
565     long off = 0;
566 
567     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
568     TEST_ASSERT_NOT_NULL(fp);
569 
570     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_CUR);
571     TEST_ASSERT_TRUE(ret != -1);
572 
573     off = ftell(fp);
574     TEST_ASSERT_EQUAL_INT((int)off, TEST_SEEK_SIZE);
575 
576     ret = fclose(fp);
577     TEST_ASSERT_EQUAL_INT(ret, 0);
578     return 0;
579 }
580 
581 /* *
582  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_006
583  * @tc.name     ftell and fseek
584  * @tc.desc     [C- SOFTWARE -0200]
585  */
586 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek006, Function | MediumTest | Level1)
587 {
588     int ret = 0;
589     FILE *fp = NULL;
590     long off = 0;
591 
592     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
593     TEST_ASSERT_NOT_NULL(fp);
594 
595     ret = fseek(fp, -1L, SEEK_SET);
596     TEST_ASSERT_EQUAL_INT(ret, -1);
597 
598     off = ftell(fp);
599     TEST_ASSERT_EQUAL_INT((int)off, 0);
600 
601     ret = fclose(fp);
602     TEST_ASSERT_EQUAL_INT(ret, 0);
603     return 0;
604 }
605 
606 /* *
607  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_007
608  * @tc.name     ftell and fseek
609  * @tc.desc     [C- SOFTWARE -0200]
610  */
611 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek007, Function | MediumTest | Level1)
612 {
613     int ret = 0;
614     FILE *fp = NULL;
615     long off = 0;
616 
617     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
618     TEST_ASSERT_NOT_NULL(fp);
619 
620     ret = fseek(fp, -1L, SEEK_CUR);
621     TEST_ASSERT_EQUAL_INT(ret, -1);
622 
623     off = ftell(fp);
624     TEST_ASSERT_EQUAL_INT((int)off, 0);
625 
626     ret = fclose(fp);
627     TEST_ASSERT_EQUAL_INT(ret, 0);
628     return 0;
629 }
630 
631 /* *
632  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_008
633  * @tc.name     ftell and fseek
634  * @tc.desc     [C- SOFTWARE -0200]
635  */
636 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek008, Function | MediumTest | Level1)
637 {
638     int ret = 0;
639     FILE *fp = NULL;
640     long off = 0;
641 
642     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
643     TEST_ASSERT_NOT_NULL(fp);
644 
645     ret = fseek(fp, -1L, SEEK_END);
646     TEST_ASSERT_EQUAL_INT(ret, -1);
647 
648     off = ftell(fp);
649     TEST_ASSERT_EQUAL_INT((int)off, 0);
650 
651     ret = fclose(fp);
652     TEST_ASSERT_EQUAL_INT(ret, 0);
653     return 0;
654 }
655 
656 /* *
657  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_009
658  * @tc.name     ftell and fseek
659  * @tc.desc     [C- SOFTWARE -0200]
660  */
661 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek009, Function | MediumTest | Level1)
662 {
663     int ret = 0;
664     FILE *fp = NULL;
665     long off = 0;
666 
667     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
668     TEST_ASSERT_NOT_NULL(fp);
669 
670     /* wrong input, fs may return not error, no need to check return value */
671     ret = fseek(fp, TEST_SEEK_SIZE, 5); /* 5, a wrong input */
672 
673     off = ftell(fp);
674     TEST_ASSERT_EQUAL_INT(off, 0);
675 
676     ret = fclose(fp);
677     TEST_ASSERT_EQUAL_INT(ret, 0);
678     return 0;
679 }
680 
681 #if (LOSCFG_LIBC_MUSL == 1)
682 /* *
683  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_010
684  * @tc.name     ftell and fseek
685  * @tc.desc     [C- SOFTWARE -0200]
686  */
687 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek010, Function | MediumTest | Level1)
688 {
689     int ret = 0;
690     FILE *fp = NULL;
691     long off = 0;
692     int fd;
693 
694     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
695     TEST_ASSERT_NOT_NULL(fp);
696 
697     fd = fp->fd;
698 
699     fp->fd = 500; /* 500 is a wrong fd */
700 
701     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_SET);
702     TEST_ASSERT_EQUAL_INT(ret, -1);
703 
704     fp->fd = fd;
705 
706     ret = fclose(fp);
707     TEST_ASSERT_EQUAL_INT(ret, 0);
708     return 0;
709 }
710 #endif
711 /* *
712  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_011
713  * @tc.name     ftell and fseek
714  * @tc.desc     [C- SOFTWARE -0200]
715  */
716 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek011, Function | MediumTest | Level1)
717 {
718     int ret = 0;
719     FILE *fp = NULL;
720     long off = 0;
721 
722     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
723     TEST_ASSERT_NOT_NULL(fp);
724 
725     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_SET);
726     TEST_ASSERT_TRUE(ret != -1);
727 
728     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_CUR);
729     TEST_ASSERT_TRUE(ret != -1);
730 
731     off = ftell(fp);
732     TEST_ASSERT_EQUAL_INT((int)off, (TEST_SEEK_SIZE + TEST_SEEK_SIZE));
733 
734     ret = fclose(fp);
735     TEST_ASSERT_EQUAL_INT(ret, 0);
736     return 0;
737 }
738 
739 
740 /* *
741  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_012
742  * @tc.name     ftell and fseek
743  * @tc.desc     [C- SOFTWARE -0200]
744  */
745 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek012, Function | MediumTest | Level1)
746 {
747     int ret = 0;
748     FILE *fp = NULL;
749     long off = 0;
750 
751     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
752     TEST_ASSERT_NOT_NULL(fp);
753 
754     ret = fseek(fp, (TEST_SEEK_SIZE + TEST_SEEK_SIZE), SEEK_SET);
755     TEST_ASSERT_TRUE(ret != -1);
756 
757     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_SET);
758     TEST_ASSERT_TRUE(ret != -1);
759 
760     off = ftell(fp);
761     TEST_ASSERT_EQUAL_INT((int)off, TEST_SEEK_SIZE);
762 
763     ret = fclose(fp);
764     TEST_ASSERT_EQUAL_INT(ret, 0);
765     return 0;
766 }
767 
768 /* *
769  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_013
770  * @tc.name     ftell and fseek
771  * @tc.desc     [C- SOFTWARE -0200]
772  */
773 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek013, Function | MediumTest | Level1)
774 {
775     int ret = 0;
776     FILE *fp = NULL;
777     long off = 0;
778 
779     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
780     TEST_ASSERT_NOT_NULL(fp);
781 
782     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_SET);
783     TEST_ASSERT_TRUE(ret != -1);
784 
785     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_END);
786     TEST_ASSERT_TRUE(ret != -1);
787 
788     off = ftell(fp);
789     TEST_ASSERT_EQUAL_INT((int)off, TEST_SEEK_SIZE);
790 
791     ret = fclose(fp);
792     TEST_ASSERT_EQUAL_INT(ret, 0);
793     return 0;
794 }
795 
796 /* *
797  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_014
798  * @tc.name     ftell and fseek
799  * @tc.desc     [C- SOFTWARE -0200]
800  */
801 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek014, Function | MediumTest | Level1)
802 {
803     int ret = 0;
804     FILE *fp = NULL;
805     long off = 0;
806 
807     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
808     TEST_ASSERT_NOT_NULL(fp);
809 
810     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_SET);
811     TEST_ASSERT_TRUE(ret != -1);
812 
813     rewind(fp);
814 
815     off = ftell(fp);
816     TEST_ASSERT_EQUAL_INT((int)off, 0);
817 
818     ret = fclose(fp);
819     TEST_ASSERT_EQUAL_INT(ret, 0);
820     return 0;
821 }
822 
823 /* *
824  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_015
825  * @tc.name     ftell and fseek
826  * @tc.desc     [C- SOFTWARE -0200]
827  */
828 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek015, Function | MediumTest | Level1)
829 {
830     int ret = 0;
831     FILE *fp = NULL;
832     long off = 0;
833 
834     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
835     TEST_ASSERT_NOT_NULL(fp);
836 
837     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_CUR);
838     TEST_ASSERT_TRUE(ret != -1);
839 
840     rewind(fp);
841 
842     off = ftell(fp);
843     TEST_ASSERT_EQUAL_INT((int)off, 0);
844 
845     ret = fclose(fp);
846     TEST_ASSERT_EQUAL_INT(ret, 0);
847     return 0;
848 }
849 
850 /* *
851  * @tc.number   SUB_KERNEL_FS_FTELL_FSEEK_016
852  * @tc.name     ftell and fseek
853  * @tc.desc     [C- SOFTWARE -0200]
854  */
855 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFtellFseek016, Function | MediumTest | Level1)
856 {
857     int ret = 0;
858     FILE *fp = NULL;
859     long off = 0;
860 
861     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
862     TEST_ASSERT_NOT_NULL(fp);
863 
864     ret = fseek(fp, TEST_SEEK_SIZE, SEEK_END);
865     TEST_ASSERT_TRUE(ret != -1);
866 
867     rewind(fp);
868 
869     off = ftell(fp);
870     TEST_ASSERT_EQUAL_INT((int)off, 0);
871 
872     ret = fclose(fp);
873     TEST_ASSERT_EQUAL_INT(ret, 0);
874     return 0;
875 }
876 
877 /* *
878  * @tc.number   SUB_KERNEL_FS_FPUTS_001
879  * @tc.name     fputs
880  * @tc.desc     [C- SOFTWARE -0200]
881  */
882 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFputs001, Function | MediumTest | Level1)
883 {
884     int ret = 0;
885     FILE *fp = NULL;
886     const char chr1[TEST_BUF_SIZE] = "hello";
887     char str[TEST_BUF_SIZE] = {0};
888 
889     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
890     TEST_ASSERT_NOT_NULL(fp);
891 
892     ret = fputs(chr1, fp);
893     TEST_ASSERT_TRUE(ret != -1);
894 
895     ret = fclose(fp);
896     TEST_ASSERT_EQUAL_INT(ret, 0);
897     return 0;
898 }
899 
900 /* *
901  * @tc.number   SUB_KERNEL_FS_FPUTS_002
902  * @tc.name     fputs
903  * @tc.desc     [C- SOFTWARE -0200]
904  */
905 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFputs002, Function | MediumTest | Level1)
906 {
907     int ret = 0;
908     FILE *fp = NULL;
909     const char chr1[TEST_BUF_SIZE] = "hello";
910     char str[TEST_BUF_SIZE] = {0};
911 
912     fp = fopen(TEST_FILE_PTAH_RIGHT, "a");
913     TEST_ASSERT_NOT_NULL(fp);
914 
915     ret = fputs(chr1, fp);
916     TEST_ASSERT_TRUE(ret != -1);
917 
918     ret = fclose(fp);
919     TEST_ASSERT_EQUAL_INT(ret, 0);
920     return 0;
921 }
922 
923 /* *
924  * @tc.number   SUB_KERNEL_FS_FPUTS_003
925  * @tc.name     fputs
926  * @tc.desc     [C- SOFTWARE -0200]
927  */
928 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFputs003, Function | MediumTest | Level1)
929 {
930     int ret = 0;
931     FILE *fp = NULL;
932     const char chr1[TEST_BUF_SIZE] = "hello";
933     char str[TEST_BUF_SIZE] = {0};
934 
935     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
936     TEST_ASSERT_NOT_NULL(fp);
937 
938     ret = fputs(chr1, fp);
939     TEST_ASSERT_TRUE(ret != -1);
940 
941     ret = fclose(fp);
942     TEST_ASSERT_EQUAL_INT(ret, 0);
943     return 0;
944 }
945 
946 /* *
947  * @tc.number   SUB_KERNEL_FS_FPUTS_004
948  * @tc.name     fputs
949  * @tc.desc     [C- SOFTWARE -0200]
950  */
951 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFputs004, Function | MediumTest | Level1)
952 {
953     int ret = 0;
954     FILE *fp = NULL;
955     const char chr1[TEST_BUF_SIZE] = "hello";
956     char str[TEST_BUF_SIZE] = {0};
957 
958     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
959     TEST_ASSERT_NOT_NULL(fp);
960 
961     ret = fputs(chr1, fp);
962     TEST_ASSERT_TRUE(ret != -1);
963 
964     (void)fseek (fp, 0L, SEEK_SET);
965     ret = fread(str, strlen(chr1) + 1, 1, fp);
966     TEST_ASSERT_TRUE(ret != -1);
967 
968     ret = strcmp(str, "hello");
969     TEST_ASSERT_EQUAL_INT(ret, 0);
970 
971     ret = fclose(fp);
972     TEST_ASSERT_EQUAL_INT(ret, 0);
973     return 0;
974 }
975 
976 /* *
977  * @tc.number   SUB_KERNEL_FS_FPUTS_005
978  * @tc.name     fputs
979  * @tc.desc     [C- SOFTWARE -0200]
980  */
981 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFputs005, Function | MediumTest | Level1)
982 {
983     int ret = 0;
984     FILE *fp = NULL;
985     const char chr1[TEST_BUF_SIZE] = "hello";
986     char str[TEST_BUF_SIZE] = {0};
987     int i;
988 
989     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
990     TEST_ASSERT_NOT_NULL(fp);
991 
992     for (i = 0; i < TEST_LOOPUP_TIME; i++) {
993         ret = fputs(chr1, fp);
994         TEST_ASSERT_TRUE(ret != -1);
995     }
996 
997     ret = ftell(fp);
998     TEST_ASSERT_EQUAL_INT(ret, TEST_LOOPUP_TIME * strlen(chr1));
999 
1000     ret = fclose(fp);
1001     TEST_ASSERT_EQUAL_INT(ret, 0);
1002     return 0;
1003 }
1004 
1005 /* *
1006  * @tc.number   SUB_KERNEL_FS_FWRITE_001
1007  * @tc.name     fread and fwrite
1008  * @tc.desc     [C- SOFTWARE -0200]
1009  */
1010 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite001, Function | MediumTest | Level1)
1011 {
1012     int ret = 0;
1013     FILE *fp = NULL;
1014     long off = 0;
1015 
1016     const char chr1[TEST_BUF_SIZE] = "hello";
1017     const char chr2[TEST_BUF_SIZE] = "world";
1018 
1019     char str[TEST_BUF_SIZE] = {0};
1020 
1021     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
1022     TEST_ASSERT_NOT_NULL(fp);
1023 
1024     ret = fwrite(chr1, strlen(chr1) + 1, 1, fp);
1025     TEST_ASSERT_TRUE(ret != -1);
1026 
1027     ret = fclose(fp);
1028     TEST_ASSERT_EQUAL_INT(ret, 0);
1029     return 0;
1030 }
1031 
1032 /* *
1033  * @tc.number   SUB_KERNEL_FS_FWRITE_002
1034  * @tc.name     fread and fwrite
1035  * @tc.desc     [C- SOFTWARE -0200]
1036  */
1037 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite002, Function | MediumTest | Level1)
1038 {
1039     int ret = 0;
1040     FILE *fp = NULL;
1041     long off = 0;
1042 
1043     const char chr1[TEST_BUF_SIZE] = "hello";
1044     const char chr2[TEST_BUF_SIZE] = "world";
1045 
1046     char str[TEST_BUF_SIZE] = {0};
1047 
1048     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
1049     TEST_ASSERT_NOT_NULL(fp);
1050 
1051     ret = fwrite(0, 0, 1, fp);
1052     TEST_ASSERT_TRUE(ret != -1);
1053 
1054     ret = fclose(fp);
1055     TEST_ASSERT_EQUAL_INT(ret, 0);
1056     return 0;
1057 }
1058 
1059 /* *
1060  * @tc.number   SUB_KERNEL_FS_FWRITE_003
1061  * @tc.name     fread and fwrite
1062  * @tc.desc     [C- SOFTWARE -0200]
1063  */
1064 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite003, Function | MediumTest | Level1)
1065 {
1066     int ret = 0;
1067     FILE *fp = NULL;
1068     long off = 0;
1069 
1070     const char chr1[TEST_BUF_SIZE] = "hello";
1071     const char chr2[TEST_BUF_SIZE] = "world";
1072 
1073     char str[TEST_BUF_SIZE] = {0};
1074 
1075     fp = fopen(TEST_FILE_PTAH_RIGHT, "r");
1076     TEST_ASSERT_NOT_NULL(fp);
1077 
1078     ret = fread((void *)chr1, strlen(chr1) + 1, 1, fp);
1079     TEST_ASSERT_TRUE(ret != -1);
1080 
1081     ret = fclose(fp);
1082     TEST_ASSERT_EQUAL_INT(ret, 0);
1083     return 0;
1084 }
1085 
1086 /* *
1087  * @tc.number   SUB_KERNEL_FS_FWRITE_004
1088  * @tc.name     fread and fwrite
1089  * @tc.desc     [C- SOFTWARE -0200]
1090  */
1091 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite004, Function | MediumTest | Level1)
1092 {
1093     int ret = 0;
1094     FILE *fp = NULL;
1095     long off = 0;
1096 
1097     const char chr1[TEST_BUF_SIZE] = "hello";
1098     const char chr2[TEST_BUF_SIZE] = "world";
1099 
1100     char str[TEST_BUF_SIZE] = {0};
1101 
1102     fp = fopen(TEST_FILE_PTAH_RIGHT, "r");
1103     TEST_ASSERT_NOT_NULL(fp);
1104 
1105     ret = fread(0, 0, 1, fp);
1106     TEST_ASSERT_TRUE(ret != -1);
1107 
1108     ret = fclose(fp);
1109     TEST_ASSERT_EQUAL_INT(ret, 0);
1110     return 0;
1111 }
1112 
1113 /* *
1114  * @tc.number   SUB_KERNEL_FS_FWRITE_005
1115  * @tc.name     fread and fwrite
1116  * @tc.desc     [C- SOFTWARE -0200]
1117  */
1118 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite005, Function | MediumTest | Level1)
1119 {
1120     int ret = 0;
1121     FILE *fp = NULL;
1122     long off = 0;
1123 
1124     const char chr1[TEST_BUF_SIZE] = "hello";
1125     const char chr2[TEST_BUF_SIZE] = "world";
1126 
1127     char str[TEST_BUF_SIZE] = {0};
1128 
1129     fp = fopen(TEST_FILE_PTAH_RIGHT, "a");
1130     TEST_ASSERT_NOT_NULL(fp);
1131 
1132     ret = fwrite(chr1, strlen(chr1) + 1, 1, fp);
1133     TEST_ASSERT_TRUE(ret != -1);
1134 
1135     ret = fclose(fp);
1136     TEST_ASSERT_EQUAL_INT(ret, 0);
1137     return 0;
1138 }
1139 
1140 /* *
1141  * @tc.number   SUB_KERNEL_FS_FWRITE_006
1142  * @tc.name     fread and fwrite
1143  * @tc.desc     [C- SOFTWARE -0200]
1144  */
1145 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite006, Function | MediumTest | Level1)
1146 {
1147     int ret = 0;
1148     FILE *fp = NULL;
1149     long off = 0;
1150 
1151     const char chr1[TEST_BUF_SIZE] = "hello";
1152     const char chr2[TEST_BUF_SIZE] = "world";
1153 
1154     char str[TEST_BUF_SIZE] = {0};
1155 
1156     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
1157     TEST_ASSERT_NOT_NULL(fp);
1158 
1159     ret = fwrite(chr1, strlen(chr1), 1, fp);
1160     TEST_ASSERT_TRUE(ret != -1);
1161 
1162     (void)fseek(fp, 0L, SEEK_SET);
1163 
1164     ret = fread(str, TEST_RW_SIZE, 1, fp);
1165     TEST_ASSERT_TRUE(ret != -1);
1166 
1167     ret = fwrite(chr2, strlen(chr2), 1, fp);
1168     TEST_ASSERT_TRUE(ret != -1);
1169 
1170     (void)fseek(fp, 0L, SEEK_SET);
1171     ret = fread(str, TEST_RW_SIZE, 1, fp);
1172     TEST_ASSERT_TRUE(ret != -1);
1173 
1174     ret = strcmp(str, "helloworld");
1175     TEST_ASSERT_EQUAL_INT(ret, 0);
1176 
1177     ret = fclose(fp);
1178     TEST_ASSERT_EQUAL_INT(ret, 0);
1179     return 0;
1180 
1181 }
1182 
1183 /* *
1184  * @tc.number   SUB_KERNEL_FS_FWRITE_007
1185  * @tc.name     fread and fwrite
1186  * @tc.desc     [C- SOFTWARE -0200]
1187  */
1188 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite007, Function | MediumTest | Level1)
1189 {
1190     int ret = 0;
1191     FILE *fp = NULL;
1192     long off = 0;
1193 
1194     const char chr1[TEST_BUF_SIZE] = "hello";
1195     const char chr2[TEST_BUF_SIZE] = "world";
1196 
1197     char str[TEST_BUF_SIZE] = {0};
1198 
1199     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
1200     TEST_ASSERT_NOT_NULL(fp);
1201 
1202     ret = fwrite(chr1, strlen(chr1), 1, fp);
1203     TEST_ASSERT_TRUE(ret != -1);
1204 
1205     ret = fwrite(chr2, strlen(chr2), 1, fp);
1206     TEST_ASSERT_TRUE(ret != -1);
1207 
1208     (void)fseek(fp, 0L, SEEK_SET);
1209 
1210     ret = fread(str, TEST_RW_SIZE, 1, fp);
1211     TEST_ASSERT_TRUE(ret != -1);
1212 
1213     ret = strcmp(str, "helloworld");
1214     TEST_ASSERT_EQUAL_INT(ret, 0);
1215 
1216     ret = fclose(fp);
1217     TEST_ASSERT_EQUAL_INT(ret, 0);
1218     return 0;
1219 }
1220 
1221 /* *
1222  * @tc.number   SUB_KERNEL_FS_FWRITE_008
1223  * @tc.name     fread and fwrite
1224  * @tc.desc     [C- SOFTWARE -0200]
1225  */
1226 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite008, Function | MediumTest | Level1)
1227 {
1228     int ret = 0;
1229     FILE *fp = NULL;
1230     int i;
1231 
1232     const char chr1[TEST_BUF_SIZE] = "hello";
1233     const char chr2[TEST_BUF_SIZE] = "world";
1234 
1235     char str[TEST_BUF_SIZE] = {0};
1236 
1237     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
1238     TEST_ASSERT_NOT_NULL(fp);
1239 
1240     for (i = 0; i < TEST_LOOPUP_TIME; i++) {
1241         ret = fwrite(chr1, strlen(chr1), 1, fp);
1242         TEST_ASSERT_TRUE(ret != -1);
1243     }
1244 
1245     for (i = 0; i < TEST_LOOPUP_TIME; i++) {
1246         ret = fwrite(chr2, strlen(chr2), 1, fp);
1247         TEST_ASSERT_TRUE(ret != -1);
1248     }
1249 
1250     (void)fseek(fp, (TEST_LOOPUP_TIME - 2) * strlen(chr1), SEEK_SET); /* 2, means will read chr1 2times */
1251 
1252     ret = fread(str, TEST_RW_SIZE, 1, fp);
1253     TEST_ASSERT_TRUE(ret != -1);
1254 
1255     ret = strncmp(str, "hellohelloworldworld", TEST_RW_SIZE);
1256     TEST_ASSERT_EQUAL_INT(ret, 0);
1257 
1258     ret = fclose(fp);
1259     TEST_ASSERT_EQUAL_INT(ret, 0);
1260     return 0;
1261 }
1262 
1263 /* *
1264  * @tc.number   SUB_KERNEL_FS_FWRITE_009
1265  * @tc.name     fread and fwrite
1266  * @tc.desc     [C- SOFTWARE -0200]
1267  */
1268 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFreadFwrite009, Function | MediumTest | Level1)
1269 {
1270     int ret = 0;
1271     FILE *fp = NULL;
1272     int i;
1273 
1274     const char chr1[TEST_BUF_SIZE] = "12345";
1275     char str[TEST_BUF_SIZE] = {0};
1276 
1277     fp = fopen(TEST_FILE_PTAH_RIGHT, "w+");
1278     TEST_ASSERT_NOT_NULL(fp);
1279 
1280     for (i = 0; i < TEST_LOOPUP_TIME; i++) {
1281         ret = fwrite(chr1, strlen(chr1), 1, fp);
1282         TEST_ASSERT_TRUE(ret != -1);
1283     }
1284 
1285     (void)fseek(fp, TEST_SEEK_SIZE, SEEK_SET);
1286 
1287     ret = fread(str, TEST_RW_SIZE, 1, fp);
1288     TEST_ASSERT_TRUE(ret != -1);
1289 
1290     ret = strncmp(str, "1234512345123451234512345", TEST_RW_SIZE);
1291     TEST_ASSERT_EQUAL_INT(ret, 0);
1292 
1293     ret = fclose(fp);
1294     TEST_ASSERT_EQUAL_INT(ret, 0);
1295     return 0;
1296 }
1297 
1298 /* *
1299  * @tc.number   SUB_KERNEL_FS_READDIR_001
1300  * @tc.name     readdir
1301  * @tc.desc     [C- SOFTWARE -0200]
1302  */
1303 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsReaddir001, Function | MediumTest | Level1)
1304 {
1305     DIR *dirp;
1306     struct dirent *dResult;
1307     int ret = 0;
1308 
1309     ret = mkdir(DIRA, 0777);
1310     dirp = opendir(DIRA);
1311     TEST_ASSERT_NOT_NULL(dirp);
1312 
1313     dResult = readdir(dirp);
1314     TEST_ASSERT_NOT_NULL(dResult);
1315 
1316     ret = closedir(dirp);
1317     TEST_ASSERT_EQUAL_INT(ret, 0);
1318     ret = rmdir(DIRA);
1319     return 0;
1320 }
1321 
1322 /* *
1323  * @tc.number   SUB_KERNEL_FS_READDIR_002
1324  * @tc.name     readdir
1325  * @tc.desc     [C- SOFTWARE -0200]
1326  */
1327 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsReaddir002, Function | MediumTest | Level1)
1328 {
1329     DIR *dirp;
1330     struct dirent *dResult;
1331     off_t tellDir0;
1332     off_t tellDir1;
1333     int ret = 0;
1334 
1335     ret = mkdir(DIRA, 0777);
1336     dirp = opendir((DIRA));
1337     TEST_ASSERT_NOT_NULL(dirp);
1338 
1339     dResult = readdir(dirp);
1340     TEST_ASSERT_NOT_NULL(dirp);
1341     TEST_ASSERT_NOT_NULL(dResult);
1342     tellDir0 = dResult->d_off;
1343 
1344     dResult = readdir(dirp);
1345     TEST_ASSERT_NOT_NULL(dirp);
1346     TEST_ASSERT_NOT_NULL(dResult);
1347     tellDir1 = dResult->d_off;
1348 
1349     TEST_ASSERT_TRUE(tellDir0 == tellDir1);
1350     ret = rmdir(DIRA);
1351     return 0;
1352 }
1353 
1354 /* *
1355  * @tc.number   SUB_KERNEL_FS_REMOVE_001
1356  * @tc.name     remove
1357  * @tc.desc     [C- SOFTWARE -0200]
1358  */
1359 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsRemove001, Function | MediumTest | Level1)
1360 {
1361     FILE *fp = NULL;
1362     int ret = 0;
1363 
1364     fp = fopen(TEST_FILE_PTAH_RIGHT, "w");
1365     fclose(fp);
1366     ret = remove(TEST_FILE_PTAH_RIGHT);
1367     TEST_ASSERT_EQUAL_INT(ret, 0);
1368     return 0;
1369 }
1370 
1371 /* *
1372  * @tc.number   SUB_KERNEL_FS_REMOVE_002
1373  * @tc.name     remove
1374  * @tc.desc     [C- SOFTWARE -0200]
1375  */
1376 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsRemove002, Function | MediumTest | Level1)
1377 {
1378     int ret = 0;
1379 
1380     ret = mkdir(DIRA, 0777);
1381     TEST_ASSERT_EQUAL_INT(ret, 0);
1382     ret = remove(DIRA);
1383     TEST_ASSERT_EQUAL_INT(ret, 0);
1384     return 0;
1385 }
1386 
1387 /* *
1388  * @tc.number   SUB_KERNEL_FS_RMDIR_001
1389  * @tc.name     rmdir
1390  * @tc.desc     [C- SOFTWARE -0200]
1391  */
1392 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsRmdir001, Function | MediumTest | Level1)
1393 {
1394     int ret = 0;
1395 
1396     ret = mkdir(DIRA, 0777);
1397     TEST_ASSERT_EQUAL_INT(ret, 0);
1398 
1399     ret = mkdir(DIRAB, 0777);
1400     TEST_ASSERT_EQUAL_INT(ret, 0);
1401 
1402     ret = rmdir(DIRAB);
1403     TEST_ASSERT_EQUAL_INT(ret, 0);
1404 
1405     ret = rmdir(DIRA);
1406     TEST_ASSERT_EQUAL_INT(ret, 0);
1407     return 0;
1408 }
1409 
1410 /* *
1411  * @tc.number   SUB_KERNEL_FS_RMDIR_002
1412  * @tc.name     rmdir
1413  * @tc.desc     [C- SOFTWARE -0200]
1414  */
1415 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsRmdir002, Function | MediumTest | Level1)
1416 {
1417     int ret = 0;
1418 
1419     ret = mkdir(DIRA, 0777);
1420     TEST_ASSERT_EQUAL_INT(ret, 0);
1421 
1422     ret = mkdir(DIRAB, 0777);
1423     TEST_ASSERT_EQUAL_INT(ret, 0);
1424 
1425     ret = mkdir(DIRAC, 0777);
1426     TEST_ASSERT_EQUAL_INT(ret, 0);
1427 
1428     ret = rmdir(DIRAB);
1429     TEST_ASSERT_EQUAL_INT(ret, 0);
1430 
1431     ret = rmdir(DIRAC);
1432     TEST_ASSERT_EQUAL_INT(ret, 0);
1433 
1434     ret = rmdir(DIRA);
1435     TEST_ASSERT_EQUAL_INT(ret, 0);
1436     return 0;
1437 }
1438 
1439 /* *
1440  * @tc.number   SUB_KERNEL_FS_UNLINK_001
1441  * @tc.name     unlink
1442  * @tc.desc     [C- SOFTWARE -0200]
1443  */
1444 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsUnlink001, Function | MediumTest | Level1)
1445 {
1446     int ret = 0;
1447     int fd = 0;
1448     char tmpFileName[]= FILE1;
1449 
1450     fd = open(tmpFileName, O_CREAT | O_RDWR, 0777);
1451     TEST_ASSERT_TRUE(fd != -1);
1452 
1453     (void)close(fd);
1454 
1455     ret = unlink(tmpFileName);
1456     TEST_ASSERT_TRUE(ret != -1);
1457     return 0;
1458 }
1459 
1460 /* *
1461  * @tc.number   SUB_KERNEL_FS_STAT_001
1462  * @tc.name     stat
1463  * @tc.desc     [C- SOFTWARE -0200]
1464  */
1465 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsStat001, Function | MediumTest | Level1)
1466 {
1467     struct stat buf;
1468     int fd = 0;
1469     char tmpFileName[]= FILE1;
1470     int ret = 0;
1471 
1472     remove(FILE1);
1473     fd = open(tmpFileName, O_CREAT | O_RDWR, 0777);
1474     TEST_ASSERT_TRUE(fd != -1);
1475 
1476     (void)close(fd);
1477 
1478     ret = stat(tmpFileName, &buf);
1479     TEST_ASSERT_TRUE(ret != -1);
1480     return 0;
1481 }
1482 
1483 /* *
1484  * @tc.number   SUB_KERNEL_FS_STAT_002
1485  * @tc.name     stat
1486  * @tc.desc     [C- SOFTWARE -0200]
1487  */
1488 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsStat002, Function | MediumTest | Level1)
1489 {
1490     struct stat buf;
1491     int fd = 0;
1492     char tmpFileName[]= FILE1;
1493     int ret = 0;
1494     ssize_t size = 0;
1495     char writeBuf[TEST_BUF_SIZE] = "write test";
1496 
1497     remove(FILE1);
1498     fd = open(tmpFileName, O_CREAT | O_RDWR, 0777);
1499     TEST_ASSERT_TRUE(fd != -1);
1500 
1501     size = write(fd, writeBuf, sizeof(writeBuf));
1502     (void)close(fd);
1503     TEST_ASSERT_TRUE(size != -1);
1504 
1505     ret = stat(tmpFileName, &buf);
1506     TEST_ASSERT_TRUE(ret != -1);
1507 
1508     TEST_ASSERT_TRUE(buf.st_size == sizeof(writeBuf));
1509     return 0;
1510 }
1511 
1512 /* *
1513  * @tc.number   SUB_KERNEL_FS_STAT_003
1514  * @tc.name     stat
1515  * @tc.desc     [C- SOFTWARE -0200]
1516  */
1517 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsStat003, Function | MediumTest | Level1)
1518 {
1519     struct stat buf;
1520     int fd;
1521     char tmpFileName[]= FILE1;
1522     int ret = 0;
1523     ssize_t size;
1524     char writeBuf[TEST_BUF_SIZE] = "write test";
1525 
1526     (void)memset_s(&buf, sizeof(buf), 0, sizeof(buf));
1527     fd = open(tmpFileName, O_CREAT | O_RDWR, 0777);
1528     TEST_ASSERT_TRUE(fd != -1);
1529 
1530     size = write(fd, writeBuf, sizeof(writeBuf));
1531     (void)close(fd);
1532     TEST_ASSERT_TRUE(size != -1);
1533 
1534     ret = stat(tmpFileName, &buf);
1535     TEST_ASSERT_TRUE(ret != -1);
1536 
1537     TEST_ASSERT_EQUAL_INT(buf.st_rdev, 0);
1538     return 0;
1539 }
1540 
1541 /* *
1542  * extern lseek is necessary, Otherwise it will cause stacking errors
1543  */
1544 extern off_t lseek(int fd, off_t offset, int whence);
1545 
1546 /* *
1547  * @tc.number   SUB_KERNEL_FS_WRITE_001
1548  * @tc.name     write
1549  * @tc.desc     [C- SOFTWARE -0200]
1550  */
1551 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsWrite001, Function | MediumTest | Level1)
1552 {
1553     off_t reLseek;
1554     int fd = 0;
1555     char writeBuf[TEST_BUF_SIZE];
1556     int ret = 0;
1557     char tmpFileName[]= FILE1;
1558 
1559     for (int i = 0; i < TEST_BUF_SIZE; i++) {
1560         writeBuf[i] = '1';
1561     }
1562 
1563     fd = open(tmpFileName, O_CREAT | O_RDWR, 0777);
1564     TEST_ASSERT_TRUE(fd != -1);
1565 
1566     ret = write(fd, writeBuf, TEST_RW_SIZE);
1567     if (ret == -1) {
1568         (void)close(fd);
1569     }
1570     TEST_ASSERT_TRUE(ret != -1);
1571 
1572     reLseek = lseek(fd, 0, SEEK_CUR);
1573 
1574     ret = write(fd, writeBuf, TEST_RW_SIZE);
1575     if (ret == -1) {
1576         (void)close(fd);
1577     }
1578     TEST_ASSERT_TRUE(ret != -1);
1579 
1580     reLseek = lseek(fd, 0, SEEK_CUR);
1581     (void)close(fd);
1582 
1583     TEST_ASSERT_TRUE((TEST_RW_SIZE + TEST_RW_SIZE) == (unsigned int)reLseek);
1584 
1585     return 0;
1586 }
1587 
1588 /* *
1589  * @tc.number   SUB_KERNEL_FS_WRITE_002
1590  * @tc.name     write
1591  * @tc.desc     [C- SOFTWARE -0200]
1592  */
1593 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsWrite002, Function | MediumTest | Level1)
1594 {
1595     int fd = 0;
1596     char writeBuf[TEST_BUF_SIZE] = "0123456789";
1597     int ret = 0;
1598     struct stat statbuf;
1599     char tmpFileName[]= FILE1;
1600     int i;
1601 
1602     remove(FILE1);
1603     fd = open(tmpFileName, O_CREAT | O_RDWR, 0777);
1604     TEST_ASSERT_TRUE(fd != -1);
1605 
1606     for (i = 0; i < TEST_LOOPUP_TIME; i++) {
1607         ret = write(fd, writeBuf, sizeof(writeBuf));
1608     }
1609     (void)close(fd);
1610     TEST_ASSERT_TRUE(ret != -1);
1611 
1612     ret = stat(tmpFileName, &statbuf);
1613     TEST_ASSERT_TRUE(ret != -1);
1614 
1615     TEST_ASSERT_TRUE(statbuf.st_size == sizeof(writeBuf) * TEST_LOOPUP_TIME);
1616     return 0;
1617 }
1618 
1619 #if (LOSCFG_LIBC_MUSL == 1)
1620 /* *
1621  * @tc.number   SUB_KERNEL_FS_FCNTL_001
1622  * @tc.name     fcntl
1623  * @tc.desc     [C- SOFTWARE -0200]
1624  */
1625 LITE_TEST_CASE(PosixFsFuncTestSuite, testFsFcntl001, Function | MediumTest | Level1)
1626 {
1627     int fd = 0;
1628     int flags = 0;
1629     char tmpFileName[]= FILE1;
1630 
1631     fd = open(tmpFileName, O_CREAT | O_RDWR, 0777);
1632     TEST_ASSERT_TRUE(fd != -1);
1633 
1634     flags = fcntl(fd, F_GETFL);
1635     TEST_ASSERT_TRUE(flags == O_CREAT | O_RDWR);
1636 
1637     fcntl(fd, F_SETFL, flags | O_APPEND);
1638     flags = fcntl(fd, F_GETFL);
1639     TEST_ASSERT_TRUE(flags == O_CREAT | O_RDWR | O_APPEND);
1640 
1641     (void)close(fd);
1642 
1643     return 0;
1644 }
1645 #endif
1646 
1647 RUN_TEST_SUITE(PosixFsFuncTestSuite);
1648 
PosixFsFuncTest()1649 void PosixFsFuncTest()
1650 {
1651     LOG("begin PosixFsFuncTest....\r\n");
1652 
1653     RUN_ONE_TESTCASE(testFsUnlink001);
1654 
1655     RUN_ONE_TESTCASE(testFsStat001);
1656     RUN_ONE_TESTCASE(testFsStat002);
1657     RUN_ONE_TESTCASE(testFsStat003);
1658 
1659     RUN_ONE_TESTCASE(testFsWrite001);
1660     RUN_ONE_TESTCASE(testFsWrite002);
1661 #if (LOSCFG_LIBC_MUSL == 1)
1662     RUN_ONE_TESTCASE(testFsDirname001);
1663     RUN_ONE_TESTCASE(testFsDirname002);
1664     RUN_ONE_TESTCASE(testFsDirname003);
1665     RUN_ONE_TESTCASE(testFsDirname004);
1666     RUN_ONE_TESTCASE(testFsFcntl001);
1667 #endif
1668     RUN_ONE_TESTCASE(testFsReaddir001);
1669     RUN_ONE_TESTCASE(testFsReaddir002);
1670 
1671     RUN_ONE_TESTCASE(testFsRemove001);
1672     RUN_ONE_TESTCASE(testFsRemove002);
1673 
1674     RUN_ONE_TESTCASE(testFsRmdir001);
1675     RUN_ONE_TESTCASE(testFsRmdir002);
1676 
1677     RUN_ONE_TESTCASE(testFsFopenFclose001);
1678     RUN_ONE_TESTCASE(testFsFopenFclose002);
1679     RUN_ONE_TESTCASE(testFsFopenFclose003);
1680     RUN_ONE_TESTCASE(testFsFopenFclose004);
1681     RUN_ONE_TESTCASE(testFsFopenFclose005);
1682     RUN_ONE_TESTCASE(testFsFopenFclose006);
1683     RUN_ONE_TESTCASE(testFsFopenFclose007);
1684     RUN_ONE_TESTCASE(testFsFopenFclose008);
1685     RUN_ONE_TESTCASE(testFsFopenFclose009);
1686     RUN_ONE_TESTCASE(testFsFopenFclose010);
1687     RUN_ONE_TESTCASE(testFsFopenFclose011);
1688 
1689     RUN_ONE_TESTCASE(testFsFdopen001);
1690     RUN_ONE_TESTCASE(testFsFdopen002);
1691     RUN_ONE_TESTCASE(testFsFdopen003);
1692 #if (LOSCFG_LIBC_MUSL == 1)
1693     RUN_ONE_TESTCASE(testFsFdopen004);
1694 #endif
1695     RUN_ONE_TESTCASE(testFsFtellFseek001);
1696     RUN_ONE_TESTCASE(testFsFtellFseek002);
1697     RUN_ONE_TESTCASE(testFsFtellFseek003);
1698     RUN_ONE_TESTCASE(testFsFtellFseek004);
1699     RUN_ONE_TESTCASE(testFsFtellFseek005);
1700     RUN_ONE_TESTCASE(testFsFtellFseek006);
1701     RUN_ONE_TESTCASE(testFsFtellFseek007);
1702     RUN_ONE_TESTCASE(testFsFtellFseek008);
1703     RUN_ONE_TESTCASE(testFsFtellFseek009);
1704 #if (LOSCFG_LIBC_MUSL == 1)
1705     RUN_ONE_TESTCASE(testFsFtellFseek010);
1706 #endif
1707     RUN_ONE_TESTCASE(testFsFtellFseek011);
1708     RUN_ONE_TESTCASE(testFsFtellFseek012);
1709     RUN_ONE_TESTCASE(testFsFtellFseek013);
1710     RUN_ONE_TESTCASE(testFsFtellFseek014);
1711     RUN_ONE_TESTCASE(testFsFtellFseek015);
1712     RUN_ONE_TESTCASE(testFsFtellFseek016);
1713 
1714     RUN_ONE_TESTCASE(testFsFputs001);
1715     RUN_ONE_TESTCASE(testFsFputs002);
1716     RUN_ONE_TESTCASE(testFsFputs003);
1717     RUN_ONE_TESTCASE(testFsFputs004);
1718     RUN_ONE_TESTCASE(testFsFputs005);
1719 
1720     RUN_ONE_TESTCASE(testFsFreadFwrite001);
1721     RUN_ONE_TESTCASE(testFsFreadFwrite002);
1722     RUN_ONE_TESTCASE(testFsFreadFwrite003);
1723     RUN_ONE_TESTCASE(testFsFreadFwrite004);
1724     RUN_ONE_TESTCASE(testFsFreadFwrite005);
1725     RUN_ONE_TESTCASE(testFsFreadFwrite006);
1726     RUN_ONE_TESTCASE(testFsFreadFwrite007);
1727     RUN_ONE_TESTCASE(testFsFreadFwrite008);
1728     RUN_ONE_TESTCASE(testFsFreadFwrite009);
1729     return;
1730 }
1731