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 <string.h>
33 #include <stdlib.h>
34 #include "ohos_types.h"
35 #include "posix_test.h"
36 #include "los_config.h"
37 #include "kernel_test.h"
38 #include "log.h"
39
40 /* *
41 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
42 * @param : subsystem name is utils
43 * @param : module name is utilsFile
44 * @param : test suit name is PosixMemFuncTestSuite
45 */
46 LITE_TEST_SUIT(Posix, PosixMemory, PosixMemFuncTestSuite);
47
48
49 /* *
50 * @tc.setup : setup for all testcases
51 * @return : setup result, TRUE is success, FALSE is fail
52 */
PosixMemFuncTestSuiteSetUp(void)53 static BOOL PosixMemFuncTestSuiteSetUp(void)
54 {
55 return TRUE;
56 }
57
58 /* *
59 * @tc.teardown : teardown for all testcases
60 * @return : teardown result, TRUE is success, FALSE is fail
61 */
PosixMemFuncTestSuiteTearDown(void)62 static BOOL PosixMemFuncTestSuiteTearDown(void)
63 {
64 printf("+Hello this is a memory function test+\n");
65 return TRUE;
66 }
67
68 /* *
69 * @tc.number : SUB_KERNEL_POSIX_MEMCPY_OPERATION_001
70 * @tc.name : Memony operation for memcpy test
71 * @tc.desc : [C- SOFTWARE -0200]
72 */
73 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemMemcpy001, Function | MediumTest | Level1)
74 {
75 void *retValue = NULL;
76 char source[] = {"This File is About Memony Operation Test , Please Carefully Check Result As Below\r\n"};
77 char dest[1024] = {0};
78 retValue = memcpy(dest, source, sizeof(source) / sizeof(source[0]));
79
80 TEST_ASSERT_NOT_NULL(retValue);
81
82 retValue = memcpy(source, dest, sizeof(source) / sizeof(source[0]));
83 TEST_ASSERT_NOT_NULL(retValue);
84
85 TEST_ASSERT_EQUAL_STRING(dest, source);
86
87 int len;
88 char chr = 'A';
89 char buf[1024];
90
91 len = sizeof(buf) / sizeof(buf[0]);
92 for (int i = 0; i < len; i++) {
93 buf[i] = chr + rand() % 26;
94 }
95
96 memcpy(&buf[16], &buf[0], 16);
97 for (int i = 0; i < 16; i++) {
98 TEST_ASSERT_EQUAL_INT((int)buf[i + 16], (int)buf[i]);
99 if (buf[i + 16] != buf[i]) {
100 printf("String Copy error \r\n");
101 break;
102 }
103 }
104 return 0;
105 };
106
107
108 /* *
109 * @tc.number : SUB_KERNEL_POSIX_MEMCPY_OPERATION_002
110 * @tc.name : Memony operation for memcpy test
111 * @tc.desc : [C- SOFTWARE -0200]
112 */
113 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemMemcpy002, Function | MediumTest | Level1)
114 {
115 void *retValue = NULL;
116
117 char source[] = {"memory refers to the computer hardware devices used to store information for"
118 " immediate use in a computer\r\n"};
119 char dest[1024] = {0};
120 retValue = memcpy(dest, source, sizeof(source) / sizeof(source[0]));
121 TEST_ASSERT_NOT_NULL(retValue);
122
123 retValue = memcpy(source, dest, sizeof(source) / sizeof(source[0]));
124 TEST_ASSERT_NOT_NULL(retValue);
125 TEST_ASSERT_EQUAL_STRING(source, dest);
126
127 char chr = 'A';
128 int i, len, failure;
129 char src[1024];
130 char dst[1024];
131
132 len = rand() % 1024;
133 for (i = 0; i < len; i++) {
134 src[i] = chr + i % 26;
135 }
136
137 memcpy(dst, src, len);
138 failure = 0;
139 for (i = 0; i < len; i++) {
140 if (dst[i] != src[i]) {
141 failure = 1;
142 break;
143 }
144 }
145 TEST_ASSERT_EQUAL_INT(0, failure);
146 return 0;
147 };
148
149
150 /* *
151 * @tc.number : SUB_KERNEL_POSIX_MEMSET_OPERATION_001
152 * @tc.name : Memony operation for memset test
153 * @tc.desc : [C- SOFTWARE -0200]
154 */
155 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemMemset001, Function | MediumTest | Level1)
156 {
157 void *retValue = NULL;
158 char source[1024] = {"memory refers to the computer hardware devices used to store information for"
159 " immediate use in a computer\r\n"};
160 char ch = rand() % 26 + 'A';
161 retValue = memset(source, ch, sizeof(source) / sizeof(source[0]));
162 TEST_ASSERT_NOT_NULL(retValue);
163 TEST_ASSERT_EQUAL_PTR(retValue, source);
164
165 for (int i = 0; i < (sizeof(source) / sizeof(source[0])); i++) {
166 TEST_ASSERT_EQUAL_INT((int)source[i], (int)ch);
167 }
168 return 0;
169 };
170
171
172 /* *
173 * @tc.number : SUB_KERNEL_POSIX_MEMCMP_OPERATION_001
174 * @tc.name : Memony operation for memcmp test
175 * @tc.desc : [C- SOFTWARE -0200]
176 */
177 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemMemcmp001, Function | MediumTest | Level1)
178 {
179 int retValue = 0;
180 char source[] = {"memory refers to the computer hardware devices used to store information for "
181 "immediate use in a computer\r\n"};
182 char dest[] = {"memory refers to the computer hardware devices used to store information for "
183 "immediate use in a computer\r\n"};
184
185 retValue = memcmp(source, dest, sizeof(source) / sizeof(source[0]));
186 TEST_ASSERT_EQUAL_INT(0, retValue);
187
188
189 char orign[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
190 char lt[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x77};
191 char eq[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
192 char gt[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x99};
193
194 int ret;
195 int len = sizeof(orign);
196
197 ret = memcmp(orign, lt, len);
198 TEST_ASSERT_GREATER_THAN(0, ret);
199
200 ret = memcmp(eq, orign, len);
201 TEST_ASSERT_EQUAL_INT(0, ret);
202
203 ret = memcmp(orign, gt, len);
204 TEST_ASSERT_LESS_THAN(0, ret);
205
206 ret = memcmp(gt, orign, 0);
207 TEST_ASSERT_EQUAL_INT(0, ret);
208 return 0;
209 };
210
211
212 /* *
213 * @tc.number : SUB_KERNEL_POSIX_MEMCMP_OPERATION_002
214 * @tc.name : Memony operation for memcmp test
215 * @tc.desc : [C- SOFTWARE -0200]
216 */
217 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemMemcmp002, Function | MediumTest | Level1)
218 {
219 int retValue = 0;
220 char source[] = {"memory refers to the computer hardware devices used to store information for "
221 "immediate use in a computer\r\n"};
222 char dest[] = {"Hello, Richard, how are you?\r\n"};
223 retValue = memcmp(source, dest, sizeof(dest) / sizeof(dest[0]));
224 TEST_ASSERT_GREATER_THAN(0, retValue);
225
226 int ret = memcmp(L"CBCDEFG", L"BBCDEFG", 7);
227 TEST_ASSERT_GREATER_THAN(0, ret);
228
229 ret = memcmp(L"ABCDEFG", L"abcdefg", 2);
230 TEST_ASSERT_LESS_THAN(0, ret);
231
232 ret = memcmp(L"ABCDEFG", L"ABCDEFG", 6);
233 TEST_ASSERT_EQUAL_INT(0, ret);
234 return 0;
235 };
236
237
238 /* *
239 * @tc.number : SUB_KERNEL_POSIX_MEMCMP_OPERATION_003
240 * @tc.name : Memony operation for memcmp test
241 * @tc.desc : [C- SOFTWARE -0200]
242 */
243 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemMemcmp003, Function | MediumTest | Level1)
244 {
245 int retValue = 0;
246 char source[] = {"00000\r\n"};
247 char dest[] = {"memory refers to the computer hardware devices used to store information for "
248 "immediate use in a computer\r\n"};
249 retValue = memcmp(source, dest, sizeof(source) / sizeof(source[0]));
250 TEST_ASSERT_LESS_THAN(0, retValue);
251 return 0;
252 };
253
254
255 /* *
256 * @tc.number : SUB_KERNEL_POSIX_REALLOC_OPERATION_001
257 * @tc.name : Memony operation for realloc test
258 * @tc.desc : [C- SOFTWARE -0200]
259 */
260 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemRealloc001, Function | MediumTest | Level1)
261 {
262 char *source = (char *)malloc(20);
263 source = (char *)realloc(source, 0);
264 TEST_ASSERT_NULL(source);
265
266 size_t k, len, mlen, rlen;
267 for (int i = 0; i < 5; i++) {
268 char *mem = NULL;
269 char *data = NULL;
270 int failure = 0;
271 const char testChar = 0x36;
272
273 mlen = 10;
274 mem = malloc(mlen);
275 TEST_ASSERT_NOT_NULL(mem);
276
277 (void)memset_s(mem, mlen, testChar, mlen);
278 rlen = rand() % (1024) + mlen;
279 char *mem1 = realloc(mem, rlen);
280 if (mem1 == NULL) {
281 free(mem);
282 }
283 TEST_ASSERT_NOT_NULL(mem1);
284
285 len = mlen <= rlen ? mlen : rlen;
286
287 data = (char *)mem1;
288 for (k = 0; k < len; k++) {
289 if (data[k] != testChar) {
290 failure = 1;
291 }
292 }
293
294 free(mem1);
295 TEST_ASSERT_EQUAL_INT(failure, 0);
296 }
297 return 0;
298 };
299
300
301 /* *
302 * @tc.number : SUB_KERNEL_POSIX_REALLOC_OPERATION_002
303 * @tc.name : Memony operation for realloc test
304 * @tc.desc : [C- SOFTWARE -0200]
305 */
306 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemRealloc002, Function | MediumTest | Level1)
307 {
308 char *source = (char *)malloc(20);
309
310 char *newData = (char *)realloc(source, 40);
311 TEST_ASSERT_NOT_NULL(newData);
312 if (newData != NULL) {
313 source = newData;
314 }
315 free(source);
316
317 const size_t len = 1024;
318 const size_t large = 4096;
319
320 void *mem = malloc(len);
321 TEST_ASSERT_NOT_NULL(mem);
322
323 void *reMem = realloc(mem, large);
324 TEST_ASSERT_NOT_NULL(reMem);
325
326 if (reMem != NULL) {
327 mem = reMem;
328 }
329 free(mem);
330 return 0;
331 };
332
333
334 /* *
335 * @tc.number : SUB_KERNEL_POSIX_REALLOC_OPERATION_003
336 * @tc.name : Memony operation for realloc test
337 * @tc.desc : [C- SOFTWARE -0200]
338 */
339 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemRealloc003, Function | MediumTest | Level1)
340 {
341 char *retValue = NULL;
342
343 retValue = (char *)realloc(retValue, 20);
344 TEST_ASSERT_NOT_NULL(retValue);
345 return 0;
346 };
347
348 /* *
349 * @tc.number : SUB_KERNEL_POSIX_REALLOC_OPERATION_004
350 * @tc.name : Memony operation for realloc test
351 * @tc.desc : [C- SOFTWARE -0200]
352 */
353 LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemRealloc004, Function | MediumTest | Level1)
354 {
355 char *source = (char *)malloc(20);
356
357 char *newData = (char *)realloc(source, 10);
358 TEST_ASSERT_NOT_NULL(newData);
359
360 if (newData != NULL) {
361 source = newData;
362 }
363 free(source);
364 return 0;
365 };
366
367
368 RUN_TEST_SUITE(PosixMemFuncTestSuite);
369
PosixStringMemTest03()370 void PosixStringMemTest03()
371 {
372 LOG("begin PosixStringMemTest03....");
373 RUN_ONE_TESTCASE(testOsMemMemcpy001);
374 RUN_ONE_TESTCASE(testOsMemMemcpy002);
375 RUN_ONE_TESTCASE(testOsMemMemset001);
376 RUN_ONE_TESTCASE(testOsMemMemcmp001);
377 RUN_ONE_TESTCASE(testOsMemMemcmp002);
378 RUN_ONE_TESTCASE(testOsMemMemcmp003);
379 RUN_ONE_TESTCASE(testOsMemRealloc001);
380 RUN_ONE_TESTCASE(testOsMemRealloc002);
381 RUN_ONE_TESTCASE(testOsMemRealloc003);
382 RUN_ONE_TESTCASE(testOsMemRealloc004);
383
384 return;
385 }
386