• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "posix_fs_test.h"
32 #include "vfs_config.h"
33 
34 #define TEST_SEEK_WRITE_BUF "AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDD"
35 
36 /* *
37  * @tc.number   SUB_KERNEL_FS_LSEEK_OK001
38  * @tc.name     lseek
39  * @tc.desc     [C- SOFTWARE -0200]
40  */
41 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekOK001, Function | MediumTest | Level1)
42 {
43     off_t off;
44     int32_t ret;
45     const char tmpFileName[TEST_BUF_SIZE] = FILE1;
46 
47     int32_t fd = open(tmpFileName, O_CREAT | O_RDWR);
48     ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd);
49 
50     off = lseek(fd, 0, SEEK_SET);   /* 0, offset distance */
51     ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off);
52 
53     ret = close(fd);
54     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
55 
56     ret = unlink(tmpFileName);
57     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
58 
59     return POSIX_FS_NO_ERROR;
60 }
61 
62 /* *
63  * @tc.number   SUB_KERNEL_FS_LSEEK_OK002
64  * @tc.name     lseek
65  * @tc.desc     [C- SOFTWARE -0200]
66  */
67 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekOK002, Function | MediumTest | Level1)
68 {
69     off_t off;
70     int32_t ret;
71     const char tmpFileName[TEST_BUF_SIZE] = FILE1;
72 
73     int32_t fd = open(tmpFileName, O_CREAT | O_RDWR);
74     ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd);
75 
76     off = lseek(fd, 0, SEEK_CUR);   /* 0, offset distance */
77     ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off);
78 
79     ret = close(fd);
80     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
81 
82     ret = unlink(tmpFileName);
83     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
84 
85     return POSIX_FS_NO_ERROR;
86 }
87 
88 /* *
89  * @tc.number   SUB_KERNEL_FS_LSEEK_OK003
90  * @tc.name     lseek
91  * @tc.desc     [C- SOFTWARE -0200]
92  */
93 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekOK003, Function | MediumTest | Level1)
94 {
95     off_t off;
96     int32_t ret;
97     const char tmpFileName[TEST_BUF_SIZE] = FILE1;
98 
99     int32_t fd = open(tmpFileName, O_CREAT | O_RDWR);
100     ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd);
101 
102     off = lseek(fd, 0, SEEK_END);   /* 0, offset distance */
103     ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off);
104 
105     ret = close(fd);
106     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
107 
108     ret = unlink(tmpFileName);
109     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
110 
111     return POSIX_FS_NO_ERROR;
112 }
113 
114 /* *
115  * @tc.number   SUB_KERNEL_FS_LSEEK_EBADF001
116  * @tc.name     lseek
117  * @tc.desc     [C- SOFTWARE -0200]
118  */
119 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekEBADF001, Function | MediumTest | Level1)
120 {
121     off_t off = lseek(-1, 0, SEEK_SET);   /* -1, bad fd 0, offset distance */
122     ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR);
123     ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off);
124 
125     return POSIX_FS_NO_ERROR;
126 }
127 
128 /* *
129  * @tc.number   SUB_KERNEL_FS_LSEEK_EBADF002
130  * @tc.name     lseek
131  * @tc.desc     [C- SOFTWARE -0200]
132  */
133 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekEBADF002, Function | MediumTest | Level1)
134 {
135     off_t off = lseek(ERROR_CONFIG_NFILE_DESCRIPTORS, 0, SEEK_SET);   /* 0, offset distance */
136     ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR);
137     ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off);
138 
139     return POSIX_FS_NO_ERROR;
140 }
141 
142 /* *
143  * @tc.number   SUB_KERNEL_FS_LSEEK_EBADF003
144  * @tc.name     lseek
145  * @tc.desc     [C- SOFTWARE -0200]
146  */
147 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekEBADF003, Function | MediumTest | Level1)
148 {
149     off_t off = lseek(0, 0, SEEK_SET);    /* 0, used for stdin 0, offet distance */
150     ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR);
151     ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off);
152 
153     off = lseek(1, 0, SEEK_SET);    /* 1, used for stdout 0, offet distance */
154     ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR);
155     ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off);
156 
157     off = lseek(2, 0, SEEK_SET);    /* 2 used for stderr 0, offet distance */
158     ICUNIT_ASSERT_EQUAL(errno, EBADF, POSIX_FS_IS_ERROR);
159     ICUNIT_ASSERT_EQUAL((int32_t)off, POSIX_FS_IS_ERROR, (int32_t)off);
160 
161     return POSIX_FS_NO_ERROR;
162 }
163 
164 /* *
165  * @tc.number   SUB_KERNEL_FS_LSEEK_standard
166  * @tc.name     lseek
167  * @tc.desc     [C- SOFTWARE -0200]
168  */
169 LITE_TEST_CASE(PosixFsFuncTestSuite, TestFsLseekStandard, Function | MediumTest | Level1)
170 {
171     off_t off;
172     int32_t ret;
173     const char tmpFileName[TEST_BUF_SIZE] = { FILE1 };
174     char writeBuf[TEST_BUF_SIZE] = { TEST_SEEK_WRITE_BUF };
175     char readBuf[TEST_BUF_SIZE] = { 0 };
176 
177     int32_t fd = open(tmpFileName, O_CREAT | O_RDWR);
178     ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd);
179 
180     ret = write(fd, writeBuf, TEST_BUF_SIZE);
181     ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
182 
183     off = lseek(fd, 0, SEEK_SET);   /* 0, offet distance */
184     ICUNIT_ASSERT_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off);
185 
186     ret = read(fd, readBuf, 10);    /* 10, common data for test, no special meaning */
187     ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
188 
189     ret = strcmp(readBuf, "AAAAAAAAAA");
190     ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT);
191 
192     off = lseek(fd, 10, SEEK_CUR);  /* 10, common data for test, no special meaning */
193     ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT);
194 
195     ret = read(fd, readBuf, 10);    /* 10, common data for test, no special meaning */
196     ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
197 
198     ret = strcmp(readBuf, "CCCCCCCCCC");
199     ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT);
200 
201     off = lseek(fd, -10, SEEK_END); /* -10, common data for test, no special meaning */
202     ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT);
203 
204     ret = read(fd, readBuf, 10);
205     ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
206 
207     ret = strcmp(readBuf, "DDDDDDDDDD");
208     ICUNIT_GOTO_EQUAL(ret, POSIX_FS_NO_ERROR, ret, EXIT);
209 
210     off = lseek(fd, 10, SEEK_END);  /* 10, common data for test, no special meaning */
211     ICUNIT_GOTO_NOT_EQUAL(off, POSIX_FS_IS_ERROR, off, EXIT);
212 
213     ret = write(fd, "E", 1);    /* 1, common data for test, no special meaning */
214     ICUNIT_GOTO_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret, EXIT);
215 
216     off = lseek(fd, 0, SEEK_END);   /* 0, offet distance */
217     ICUNIT_GOTO_EQUAL(off, 51, off, EXIT);  /* 51, common data for test, no special meaning */
218 
219 EXIT:
220     ret = close(fd);
221     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
222 
223     ret = unlink(tmpFileName);
224     ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret);
225 
226     return POSIX_FS_NO_ERROR;
227 }
228 
PosixFsLseekTest(void)229 void PosixFsLseekTest(void)
230 {
231     RUN_ONE_TESTCASE(TestFsLseekOK001);
232     RUN_ONE_TESTCASE(TestFsLseekOK002);
233     RUN_ONE_TESTCASE(TestFsLseekOK003);
234     RUN_ONE_TESTCASE(TestFsLseekEBADF001);
235     RUN_ONE_TESTCASE(TestFsLseekEBADF002);
236     RUN_ONE_TESTCASE(TestFsLseekEBADF003);
237     RUN_ONE_TESTCASE(TestFsLseekStandard);
238 }
239