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 #define _GNU_SOURCE
33
34 #include "posix_test.h"
35 #include "los_config.h"
36 #include "kernel_test.h"
37 #include "log.h"
38 #include <string.h>
39 #include <stdlib.h>
40
41 #define RET_OK 0
42
43 /* *
44 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
45 * @param : subsystem name is utils
46 * @param : module name is utilsFile
47 * @param : test suit name is CmsisTaskFuncTestSuite
48 */
49 LITE_TEST_SUIT(Posix, Posixtimer, PosixStringFuncTestSuite);
50
51 /* *
52 * @tc.setup : setup for all testcases
53 * @return : setup result, TRUE is success, FALSE is fail
54 */
PosixStringFuncTestSuiteSetUp(void)55 static BOOL PosixStringFuncTestSuiteSetUp(void)
56 {
57 return TRUE;
58 }
59
60 /* *
61 * @tc.teardown : teardown for all testcases
62 * @return : teardown result, TRUE is success, FALSE is fail
63 */
PosixStringFuncTestSuiteTearDown(void)64 static BOOL PosixStringFuncTestSuiteTearDown(void)
65 {
66 printf("==== [ String TEST ] ====\n\n");
67 return TRUE;
68 }
69
70 /* *
71 * @tc.number TEST_STRING_STRLEN_001
72 * @tc.name string_strlen error test with not exist pid
73 * @tc.desc [C- SOFTWARE -0200]
74 */
75 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrlen001, Function | MediumTest | Level1)
76 {
77 char src[] = "helloworld";
78 int ret = strlen(src);
79 ICUNIT_ASSERT_EQUAL(ret, 10, ret);
80 return 0;
81 }
82
83 /* *
84 * @tc.number TEST_STRING_STRLEN_002
85 * @tc.name string_strlen error test with not exist pid
86 * @tc.desc [C- SOFTWARE -0200]
87 */
88 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrlen002, Function | MediumTest | Level1)
89 {
90 char src[] = "hello world";
91 int ret = strlen(src);
92 ICUNIT_ASSERT_EQUAL(ret, 11, ret);
93 return 0;
94 }
95
96 /* *
97 * @tc.number TEST_STRING_STRLEN_003
98 * @tc.name string_strlen error test with not exist pid
99 * @tc.desc [C- SOFTWARE -0200]
100 */
101 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrlen003, Function | MediumTest | Level1)
102 {
103 int ret = strlen("");
104 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
105 return 0;
106 }
107
108 /* *
109 * @tc.number TEST_STRING_STRLEN_004
110 * @tc.name string_strlen error test with not exist pid
111 * @tc.desc [C- SOFTWARE -0200]
112 */
113 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrlen004, Function | MediumTest | Level1)
114 {
115 char src[] = "hello\0world";
116 int ret = strlen(src);
117 ICUNIT_ASSERT_EQUAL(ret, 5, ret);
118 return 0;
119 }
120
121 /* *
122 * @tc.number TEST_STRING_STRLEN_005
123 * @tc.name string_strlen error test with not exist pid
124 * @tc.desc [C- SOFTWARE -0200]
125 */
126 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrlen005, Function | MediumTest | Level1)
127 {
128 char src[] = "\0helloworld";
129 int ret = strlen(src);
130 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
131 return 0;
132 }
133
134
135 /* *
136 * @tc.number TEST_STRING_STRNCASECMP_001
137 * @tc.name string_strncasecmp error test with not exist pid
138 * @tc.desc [C- SOFTWARE -0200]
139 */
140 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncasecmp001, Function | MediumTest | Level1)
141 {
142 char *src[] = {"helloworld", "HElloworld"};
143 int ret = strncasecmp(src[0], src[1], 2);
144 ICUNIT_ASSERT_EQUAL(ret, RET_OK, ret);
145 return 0;
146 }
147
148 /* *
149 * @tc.number TEST_STRING_STRNCASECMP_002
150 * @tc.name string_strncasecmp error test with not exist pid
151 * @tc.desc [C- SOFTWARE -0200]
152 */
153 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncasecmp002, Function | MediumTest | Level1)
154 {
155 char *src[] = {"helloworld", "he\0lloworld"};
156 int ret = strncasecmp(src[0], src[1], 3);
157 ICUNIT_ASSERT_WITHIN_EQUAL(ret, RET_OK + 1, ret, 0);
158 return 0;
159 }
160
161 /* *
162 * @tc.number TEST_STRING_STRNCASECMP_003
163 * @tc.name string_strncasecmp error test with not exist pid
164 * @tc.desc [C- SOFTWARE -0200]
165 */
166 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncasecmp003, Function | MediumTest | Level1)
167 {
168 char *src[] = {"helloworld", "he lloworld"};
169 int ret = strncasecmp(src[0], src[1], 3);
170 ICUNIT_ASSERT_WITHIN_EQUAL(ret, RET_OK + 1, ret, 0);
171 return 0;
172 }
173
174 /* *
175 * @tc.number TEST_STRING_STRNCASECMP_004
176 * @tc.name string_strncasecmp error test with not exist pid
177 * @tc.desc [C- SOFTWARE -0200]
178 */
179 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncasecmp004, Function | MediumTest | Level1)
180 {
181 char *src[] = {"helloworld", "hello World"};
182 int ret = strncasecmp(src[0], src[1], 3);
183 ICUNIT_ASSERT_EQUAL(ret, RET_OK, ret);
184 return 0;
185 }
186
187 /* *
188 * @tc.number TEST_STRING_STRNCASECMP_005
189 * @tc.name string_strncasecmp error test with not exist pid
190 * @tc.desc [C- SOFTWARE -0200]
191 */
192 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncasecmp005, Function | MediumTest | Level1)
193 {
194 char *src[] = {"helloworld", "\0"};
195 int ret = strncasecmp(src[0], src[1], 1);
196 ICUNIT_ASSERT_WITHIN_EQUAL(ret, RET_OK + 1, ret, 0);
197 return 0;
198 }
199
200 /* *
201 * @tc.number TEST_STRING_STRNCMP_001
202 * @tc.name string_strncmp error test with not exist pid
203 * @tc.desc [C- SOFTWARE -0200]
204 */
205 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncmp001, Function | MediumTest | Level1)
206 {
207 char *src[] = {"helloworld", "HELloworld"};
208 int ret = strncmp(src[0], src[1], 3);
209 ICUNIT_ASSERT_WITHIN_EQUAL(ret, RET_OK + 1, ret, 0);
210 return 0;
211 }
212
213 /* *
214 * @tc.number TEST_STRING_STRNCMP_002
215 * @tc.name string_strncmp error test with not exist pid
216 * @tc.desc [C- SOFTWARE -0200]
217 */
218 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncmp002, Function | MediumTest | Level1)
219 {
220 char *src[] = {"helloworld", "he\0lloworld"};
221 int ret = strncmp(src[0], src[1], 3);
222 ICUNIT_ASSERT_WITHIN_EQUAL(ret, RET_OK + 1, ret, 0);
223 return 0;
224 }
225
226 /* *
227 * @tc.number TEST_STRING_STRNCMP_003
228 * @tc.name string_strncmp error test with not exist pid
229 * @tc.desc [C- SOFTWARE -0200]
230 */
231 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncmp003, Function | MediumTest | Level1)
232 {
233 char *src[] = {"helloworld", "he lloworld"};
234 int ret = strncmp(src[0], src[1], 3);
235 ICUNIT_ASSERT_WITHIN_EQUAL(ret, RET_OK + 1, ret, 0);
236 return 0;
237 }
238
239 /* *
240 * @tc.number TEST_STRING_STRNCMP_004
241 * @tc.name string_strncmp error test with not exist pid
242 * @tc.desc [C- SOFTWARE -0200]
243 */
244 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncmp004, Function | MediumTest | Level1)
245 {
246 char *src[] = {"helloworld", "hello World"};
247 int ret = strncmp(src[0], src[1], 3);
248 ICUNIT_ASSERT_EQUAL(ret, RET_OK, ret);
249 return 0;
250 }
251
252 /* *
253 * @tc.number TEST_STRING_STRNCMP_005
254 * @tc.name string_strncmp error test with not exist pid
255 * @tc.desc [C- SOFTWARE -0200]
256 */
257 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrncmp005, Function | MediumTest | Level1)
258 {
259 char *src[] = {"helloworld", "\0"};
260 int ret = strncmp(src[0], src[1], 3);
261 ICUNIT_ASSERT_WITHIN_EQUAL(ret, RET_OK + 1, ret, 0);
262 return 0;
263 }
264
265
266 /* *
267 * @tc.number TEST_STRING_STRRCHR_001
268 * @tc.name string_strrchr error test with not exist pid
269 * @tc.desc [C- SOFTWARE -0200]
270 */
271 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrrchr001, Function | MediumTest | Level1)
272 {
273 char src[] = "hello world";
274 char *ret = strrchr(src, '!');
275 ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
276 return 0;
277 }
278
279 /* *
280 * @tc.number TEST_STRING_STRRCHR_002
281 * @tc.name string_strrchr error test with not exist pid
282 * @tc.desc [C- SOFTWARE -0200]
283 */
284 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrrchr002, Function | MediumTest | Level1)
285 {
286 char src[] = "hello world";
287 char *ret = strrchr(src, '\0');
288 ICUNIT_ASSERT_EQUAL(ret, src + 11, 0);
289 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, 0);
290 return 0;
291 }
292
293 /* *
294 * @tc.number TEST_STRING_STRRCHR_003
295 * @tc.name string_strrchr error test with not exist pid
296 * @tc.desc [C- SOFTWARE -0200]
297 */
298 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrrchr003, Function | MediumTest | Level1)
299 {
300 char src[] = "hello\0world";
301 char *ret = strrchr(src, '\0');
302 ICUNIT_ASSERT_EQUAL(ret, src + 5, 0);
303 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, 0);
304 return 0;
305 }
306
307 /* *
308 * @tc.number TEST_STRING_STRRCHR_004
309 * @tc.name string_strrchr error test with not exist pid
310 * @tc.desc [C- SOFTWARE -0200]
311 */
312 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrrchr004, Function | MediumTest | Level1)
313 {
314 char src[] = "hello world";
315 char *ret = strrchr(src, ' ');
316 ICUNIT_ASSERT_EQUAL(ret, src + 5, 0);
317 ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, 0);
318 return 0;
319 }
320
321 /* *
322 * @tc.number TEST_STRING_STRRCHR_005
323 * @tc.name string_strrchr error test with not exist pid
324 * @tc.desc [C- SOFTWARE -0200]
325 */
326 LITE_TEST_CASE(PosixStringFuncTestSuite, testStringStrrchr005, Function | MediumTest | Level1)
327 {
328 char src[] = "hello\0world";
329 char *ret = strrchr(src, ' ');
330 ICUNIT_ASSERT_EQUAL(ret, NULL, 0);
331 return 0;
332 }
333
334 RUN_TEST_SUITE(PosixStringFuncTestSuite);
335
PosixStringFuncTest02()336 void PosixStringFuncTest02()
337 {
338 LOG("begin PosixStringFuncTest02....");
339 RUN_ONE_TESTCASE(testStringStrlen001);
340 RUN_ONE_TESTCASE(testStringStrlen002);
341 RUN_ONE_TESTCASE(testStringStrlen003);
342 RUN_ONE_TESTCASE(testStringStrlen004);
343 RUN_ONE_TESTCASE(testStringStrlen005);
344 RUN_ONE_TESTCASE(testStringStrncasecmp001);
345 RUN_ONE_TESTCASE(testStringStrncasecmp002);
346 RUN_ONE_TESTCASE(testStringStrncasecmp003);
347 RUN_ONE_TESTCASE(testStringStrncasecmp004);
348 RUN_ONE_TESTCASE(testStringStrncasecmp005);
349 RUN_ONE_TESTCASE(testStringStrncmp001);
350 RUN_ONE_TESTCASE(testStringStrncmp002);
351 RUN_ONE_TESTCASE(testStringStrncmp003);
352 RUN_ONE_TESTCASE(testStringStrncmp004);
353 RUN_ONE_TESTCASE(testStringStrncmp005);
354 RUN_ONE_TESTCASE(testStringStrrchr001);
355 RUN_ONE_TESTCASE(testStringStrrchr002);
356 RUN_ONE_TESTCASE(testStringStrrchr003);
357 RUN_ONE_TESTCASE(testStringStrrchr004);
358 RUN_ONE_TESTCASE(testStringStrrchr005);
359
360 return;
361 }