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