• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 fd2 = -1;
39     INT32 fd3 = -1;
40     INT32 ret, scandirCount;
41     CHAR *pret = NULL;
42     CHAR pathname[50] = { JFFS_PATH_NAME0 };
43     CHAR buf[50] = "";
44     DIR *dir = NULL;
45     struct dirent *ptr = NULL;
46     struct stat statfile;
47     struct statfs statfsfile;
48     struct dirent **namelist = NULL;
49 
50     ret = mkdir(pathname, HIGHEST_AUTHORITY);
51     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
52 
53     ret = chdir(pathname);
54     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
55 
56     pret = getcwd(buf, 20); // 20 means path name len
57     ICUNIT_GOTO_NOT_EQUAL(pret, NULL, pret, EXIT);
58     ICUNIT_GOTO_STRING_EQUAL(buf, pathname, buf, EXIT);
59 
60     strcat_s(pathname, sizeof(pathname), "/0test");
61     ret = mkdir(pathname, HIGHEST_AUTHORITY);
62     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
63 
64     JffsStrcat2(pathname, "/0file", sizeof(pathname));
65     fd = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR, HIGHEST_AUTHORITY);
66     ICUNIT_GOTO_NOT_EQUAL(fd, -1, fd, EXIT2);
67 
68     JffsStrcat2(pathname, "/2test", sizeof(pathname));
69     ret = mkdir(pathname, HIGHEST_AUTHORITY);
70     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT3);
71 
72     strcat_s(pathname, sizeof(pathname), "/1file");
73     fd1 = open(pathname, O_NONBLOCK | O_CREAT | O_RDWR, HIGHEST_AUTHORITY);
74     ICUNIT_GOTO_NOT_EQUAL(fd1, -1, fd1, EXIT4);
75 
76     JffsStrcat2(pathname, "/1test", sizeof(pathname));
77     ret = mkdir(pathname, HIGHEST_AUTHORITY);
78     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT5);
79 
80     JffsStrcat2(pathname, "/2test", sizeof(pathname));
81     ret = chdir(pathname);
82     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT5);
83 
84     pret = getcwd(buf, 20); // 20 means path name len
85     ICUNIT_GOTO_NOT_EQUAL(pret, NULL, pret, EXIT5);
86     ICUNIT_GOTO_STRING_EQUAL(buf, pathname, buf, EXIT5);
87 
88     fd2 = open("2file", O_NONBLOCK | O_CREAT | O_RDWR, HIGHEST_AUTHORITY);
89     ICUNIT_GOTO_NOT_EQUAL(fd2, -1, fd2, EXIT6);
90 
91     fd3 = open("2file", O_NONBLOCK | O_CREAT | O_RDWR | O_EXCL, HIGHEST_AUTHORITY);
92     ICUNIT_GOTO_EQUAL(fd3, -1, fd3, EXIT6);
93 
94     ret = mkdir("3dir", HIGHEST_AUTHORITY);
95     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT7);
96 
97     ret = mkdir("3dir", HIGHEST_AUTHORITY);
98     ICUNIT_GOTO_NOT_EQUAL(ret, 0, ret, EXIT7);
99 
100     dir = opendir("3dir");
101     ICUNIT_GOTO_NOT_EQUAL(dir, NULL, dir, EXIT8);
102 
103     scandirCount = scandir(".", &namelist, 0, alphasort);
104     ICUNIT_GOTO_EQUAL(scandirCount, 3, scandirCount, EXIT8); // 3 means scan file num
105     ICUNIT_GOTO_STRING_EQUAL(namelist[0]->d_name, "1file", namelist[0]->d_name, EXIT9); // 0 means first file
106 
107     ret = rename("2file", "file2");
108     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT10);
109 
110     ret = stat("file2", &statfile);
111     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT10);
112     ICUNIT_GOTO_EQUAL(statfile.st_size, 0, statfile.st_size, EXIT10);
113 
114     ret = statfs("file2", &statfsfile);
115     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT10);
116 
117     ret = access("file2", R_OK);
118     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT10);
119 
120     ret = close(fd2);
121     ICUNIT_GOTO_EQUAL(ret, JFFS_NO_ERROR, ret, EXIT10);
122 
123     ret = unlink("file2");
124     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT10);
125 
126     ret = chdir("3dir");
127     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT9);
128     strcat_s(pathname, sizeof(pathname), "/3dir");
129     pret = getcwd(buf, 30); // 30 means path name len
130     ICUNIT_GOTO_NOT_EQUAL(pret, NULL, pret, EXIT9);
131     ICUNIT_GOTO_STRING_EQUAL(buf, pathname, buf, EXIT9);
132 
133     ret = chdir("../");
134     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT9);
135     JffsStrcat2(pathname, "/2test", sizeof(pathname));
136     pret = getcwd(buf, 20); // 20 means path name len
137     ICUNIT_GOTO_NOT_EQUAL(pret, NULL, pret, EXIT9);
138     ICUNIT_GOTO_STRING_EQUAL(buf, pathname, buf, EXIT9);
139 
140 EXIT10:
141     JffsStrcat2(pathname, "/2test/file2", sizeof(pathname));
142     close(fd2);
143     remove(pathname);
144 EXIT9:
145     JffsScandirFree(namelist, scandirCount);
146 EXIT8:
147     ret = closedir(dir);
148     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT8);
149 EXIT7:
150     JffsStrcat2(pathname, "/2test/3dir", sizeof(pathname));
151     rmdir(pathname);
152 EXIT6:
153     JffsStrcat2(pathname, "/2test/2file", sizeof(pathname));
154     close(fd2);
155     close(fd3);
156     remove(pathname);
157 EXIT5:
158     JffsStrcat2(pathname, "/1test", sizeof(pathname));
159     rmdir(pathname);
160 EXIT4:
161     JffsStrcat2(pathname, "/2test/1file", sizeof(pathname));
162     close(fd1);
163     remove(pathname);
164 EXIT3:
165     JffsStrcat2(pathname, "/2test", sizeof(pathname));
166     rmdir(pathname);
167 EXIT2:
168     JffsStrcat2(pathname, "/0file", sizeof(pathname));
169     close(fd);
170     remove(pathname);
171 EXIT1:
172     JffsStrcat2(pathname, "/0test", sizeof(pathname));
173     rmdir(pathname);
174 EXIT:
175     rmdir(JFFS_PATH_NAME0);
176     return JFFS_NO_ERROR;
177 }
178 
179 
ItFsJffs007(VOID)180 VOID ItFsJffs007(VOID)
181 {
182     TEST_ADD_CASE("IT_FS_JFFS_007", Testcase, TEST_VFS, TEST_JFFS, TEST_LEVEL2, TEST_FUNCTION);
183 }
184 
185