1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 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 #include "It_vfs_jffs.h"
33
ScandirF01(const struct dirent * ent)34 static INT32 ScandirF01(const struct dirent *ent)
35 {
36 // read out a file while the name begined with "test"
37 return (strncmp(ent->d_name, "test", 4) == 0);
38 }
Testcase(VOID)39 static UINT32 Testcase(VOID)
40 {
41 INT32 fd = -1;
42 INT32 fd1 = -1;
43 INT32 ret, scandirCount;
44 CHAR *pret = NULL;
45 CHAR pathname[50] = { JFFS_PATH_NAME0 };
46 DIR *dir = NULL;
47 struct dirent *ptr = NULL;
48 struct dirent **namelist = NULL;
49 INT32 offset, offset1, offset2, offset3, offset4;
50
51 ret = mkdir(pathname, HIGHEST_AUTHORITY);
52 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
53
54 strcat_s(pathname, sizeof(pathname), "/test");
55 ret = mkdir(pathname, HIGHEST_AUTHORITY);
56 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
57
58 JffsStrcat2(pathname, "/testfile", 50); // 50 means name length
59 fd = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR, HIGHEST_AUTHORITY);
60 ICUNIT_GOTO_NOT_EQUAL(fd, -1, fd, EXIT2);
61
62 JffsStrcat2(pathname, "/0test1", 50); // 50 means name length
63 ret = mkdir(pathname, HIGHEST_AUTHORITY);
64 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT3);
65
66 strcat_s(pathname, sizeof(pathname), "/test1file");
67 fd1 = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR, HIGHEST_AUTHORITY);
68 ICUNIT_GOTO_NOT_EQUAL(fd, -1, fd, EXIT4);
69
70 JffsStrcat2(pathname, "/test2", 50); // 50 means name length
71 ret = mkdir(pathname, HIGHEST_AUTHORITY);
72 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT5);
73
74 dir = opendir(JFFS_PATH_NAME0);
75 ICUNIT_GOTO_NOT_EQUAL(dir, NULL, dir, EXIT6);
76
77 offset = telldir(dir);
78 ptr = readdir(dir);
79 offset1 = ptr->d_off;
80 ICUNIT_GOTO_EQUAL(offset, offset1 - 1, offset, EXIT6);
81
82 offset = telldir(dir);
83 ptr = readdir(dir);
84 offset2 = ptr->d_off;
85 ICUNIT_GOTO_EQUAL(offset, offset2 - 1, offset, EXIT6);
86
87 offset = telldir(dir);
88 ptr = readdir(dir);
89 offset3 = ptr->d_off;
90 ICUNIT_GOTO_EQUAL(offset, offset3 - 1, offset, EXIT6);
91
92 offset = telldir(dir);
93 ptr = readdir(dir);
94 offset4 = ptr->d_off;
95 ICUNIT_GOTO_EQUAL(offset, offset4 - 1, offset, EXIT6);
96
97 scandirCount = scandir(JFFS_PATH_NAME0, &namelist, ScandirF01, alphasort);
98 ICUNIT_GOTO_EQUAL(scandirCount, 3, scandirCount, EXIT6); // 3 means total scaned file
99 ICUNIT_GOTO_STRING_EQUAL(namelist[0]->d_name, "test", namelist[0]->d_name, EXIT7); // 0 means first file
100 ICUNIT_GOTO_STRING_EQUAL(namelist[1]->d_name, "test2", namelist[1]->d_name, EXIT7); // 1 means second file
101 ICUNIT_GOTO_STRING_EQUAL(namelist[2]->d_name, "testfile", namelist[2]->d_name, EXIT7); // 2 means third file
102 ICUNIT_GOTO_EQUAL(namelist[0]->d_type, DT_DIR, namelist[0]->d_name, EXIT7); // 0 means first file
103
104 EXIT7:
105 JffsScandirFree(namelist, scandirCount);
106 EXIT6:
107 ret = closedir(dir);
108 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT6);
109 EXIT5:
110 JffsStrcat2(pathname, "/test2", sizeof(pathname));
111 rmdir(pathname);
112 EXIT4:
113 JffsStrcat2(pathname, "/0test1/test1file", sizeof(pathname));
114 close(fd1);
115 remove(pathname);
116 EXIT3:
117 JffsStrcat2(pathname, "/0test1", sizeof(pathname));
118 rmdir(pathname);
119 EXIT2:
120 JffsStrcat2(pathname, "/testfile", sizeof(pathname));
121 close(fd);
122 remove(pathname);
123 EXIT1:
124 JffsStrcat2(pathname, "/test", sizeof(pathname));
125 rmdir(pathname);
126 EXIT:
127 rmdir(JFFS_PATH_NAME0);
128 return JFFS_NO_ERROR;
129 }
130
ItFsJffs009(VOID)131 VOID ItFsJffs009(VOID)
132 {
133 TEST_ADD_CASE("IT_FS_JFFS_009", Testcase, TEST_VFS, TEST_JFFS, TEST_LEVEL2, TEST_FUNCTION);
134 }
135
136