• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <unistd.h>
21 #include <sched.h>
22 #include <malloc.h>
23 #include <sys/time.h>
24 #include <sys/resource.h>
25 
26 #include "gtest/gtest.h"
27 #include "XtsActsUtil.h"
28 #include "KernelConstants.h"
29 
30 using namespace testing::ext;
31 
32 class ActsMemApiTest : public testing::Test {
33 protected:
34     // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)35     static void SetUpTestCase(void)
36     {
37     }
38     // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)39     static void TearDownTestCase(void)
40     {
41     }
42     // Testcase setup
SetUp()43     virtual void SetUp()
44     {
45         errno = 0;
46     }
47     // Testcase teardown
TearDown()48     virtual void TearDown()
49     {
50     }
51 };
52 
53 /**
54 * @tc.number     SUB_KERNEL_NDKAPI_MEM_BCMP_0100
55 * @tc.name       test bcmp api
56 * @tc.desc       [C- SOFTWARE -0200]
57 */
58 HWTEST_F(ActsMemApiTest, testMemBcmp_0100, Function | MediumTest | Level1) {
59     char str1[] = "this is string1";
60     char str2[] = "this is string2";
61     int returnVal;
62 
63     returnVal = bcmp(str1, str2, 7);
64     LogPrint("    bcmp *s1:='%s' *s2:='%s' size:='7',   --> returnVal:='%d'\n", str1, str2, returnVal);
65     EXPECT_EQ(0, returnVal)
66         << "ErrInfo: bcmp *s1:='" << str1 << "' *s2:='" << str2
67         << "' size:='7',   --> returnVal:='" << returnVal << "'";
68 }
69 
70 /**
71 * @tc.number     SUB_KERNEL_NDKAPI_MEM_BCMP_1000
72 * @tc.name       test bcmp api NE
73 * @tc.desc       [C- SOFTWARE -0200]
74 */
75 HWTEST_F(ActsMemApiTest, testMemBcmp_1000, Function | MediumTest | Level1) {
76     char str1[] = "this is string1";
77     char str2[] = "this is string2";
78     int returnVal;
79 
80     returnVal = bcmp(str1, str2, 15);
81     LogPrint("    bcmp *s1:='%s' *s2:='%s' size:='15',   --> returnVal:='%d'\n", str1, str2, returnVal);
82     EXPECT_NE(0, returnVal)
83         << "ErrInfo: bcmp *s1:='" << str1 << "' *s2:='" << str2
84         << "' size:='15',   --> returnVal:='" << returnVal << "'";
85 }
86 
87 /**
88 * @tc.number     SUB_KERNEL_NDKAPI_MEM_BCOPY_0100
89 * @tc.name       test bcopy api
90 * @tc.desc       [C- SOFTWARE -0200]
91 */
92 HWTEST_F(ActsMemApiTest, testMemBcopy_0100, Function | MediumTest | Level1) {
93     char srcStr[] = "this";
94     char destStr[] = "00000000";
95     int iRtn = 1;
96 
97     LogPrint("    bcopy *src:='%s' *dest:='%s', size:='4', \n", srcStr, destStr);
98     bcopy(srcStr, destStr, 4);
99     LogPrint("    --> *dest:='%s'\n", destStr);
100 
101     for (int iLoop = 0; iLoop < 4; iLoop++)
102     {
103         if (srcStr[iLoop] != destStr[iLoop]) {
104             iRtn = 0;
105             break;
106         }
107     }
108 
109     EXPECT_EQ(1, iRtn)
110         << "ErrInfo: bcopy *src:='" << srcStr << "' *dest:='"
111         << "' size:='4',   --> *dest:='" << destStr << "'";
112 }
113 
114 /**
115 * @tc.number     SUB_KERNEL_NDKAPI_MEM_BZERO_0100
116 * @tc.name       test bzero api
117 * @tc.desc       [C- SOFTWARE -0200]
118 */
119 HWTEST_F(ActsMemApiTest, testMemBzero_0100, Function | MediumTest | Level1) {
120     char srcStr[] = "this is string1";
121     char destStr[] = "this is string1";
122     int iFlag = 1;
123 
124     LogPrint("    bzero *s:='%s' size:='4',   ", srcStr);
125     bzero(destStr, 4);
126     LogPrint("--> afterrun s:='%s', ", destStr);
127 
128     for(int iLoop = 0; iLoop < 4; iLoop++)
129     {
130         if (destStr[iLoop] != '\0') {
131             iFlag = 0;
132             break;
133         } else {
134             destStr[iLoop] = '0';
135         }
136     }
137     LogPrint("  for_print s:='%s'\n", destStr);
138 
139     EXPECT_EQ(1, iFlag)
140         << "ErrInfo: bzero *:='" << srcStr << "' size:='4',   --> afterrun s:='" << destStr << "'";
141 }
142 
143 /**
144 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MALLOC_USABLE_SIZE_0100
145 * @tc.name       test malloc_usable_size api
146 * @tc.desc       [C- SOFTWARE -0200]
147 */
148 HWTEST_F(ActsMemApiTest, testMallocUsableSize_0100, Function | MediumTest | Level1) {
149     char *pt = nullptr;
150     size_t size;
151 
152     pt = (char *)malloc(128);
153     if (pt) {
154         LogPrint("    Mem allocated at: %x\n", pt);
155     } else {
156         LogPrint("    No enough Mem!\n");
157     }
158 
159     size = malloc_usable_size(pt);
160     LogPrint("    malloc_usable_size *ptr:='%x',   --> return size:='%d'\n", pt, size);
161 
162     EXPECT_GT((int)size, 1)
163         << "ErrInfo: malloc_usable_size *ptr:='" << pt << "',   --> return size:='" << size << "'";
164     free(pt);
165 }
166 
167 /**
168 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MALLOC_USABLE_SIZE_1000
169 * @tc.name       test malloc_usable_size api exception null
170 * @tc.desc       [C- SOFTWARE -0200]
171 */
172 HWTEST_F(ActsMemApiTest, testMallocUsableSize_1000, Function | MediumTest | Level1) {
173     size_t size;
174 
175     size = malloc_usable_size(nullptr);
176     LogPrint("    malloc_usable_size *ptr:='nullptr',   --> return size:='%d'\n", size);
177 
178     EXPECT_EQ((int)size, 0)
179         << "ErrInfo: malloc_usable_size *ptr:='nullptr',   --> return size:='" << size << "'";
180 }
181 
182 /**
183 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCCPY_0100
184 * @tc.name       test memccpy api
185 * @tc.desc       [C- SOFTWARE -0200]
186 */
187 HWTEST_F(ActsMemApiTest, testMemCcpy_0100, Function | MediumTest | Level1) {
188     char srcStr[] = "this is str1:stop here";
189     char destStr[] = "000000000000000000000000";
190     char *pt = nullptr;
191 
192     LogPrint("    bcopy *dest:='%s' *src:='%s' c:=':' size:='18', \n", destStr, srcStr);
193     pt = (char *)memccpy(destStr, srcStr, ':', 18);
194     LogPrint("    --> return pt:='%x', dest:='%s'\n", pt, destStr);
195     ASSERT_TRUE(pt)
196         << "ErrInfo: bcopy *dest:='destStr' *src:='"
197         << srcStr << "' c:=':' size:='18',   --> afterrun dest:='" << destStr << "'";
198 }
199 
200 /**
201 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCCPY_0200
202 * @tc.name       test memccpy api not find c
203 * @tc.desc       [C- SOFTWARE -0200]
204 */
205 HWTEST_F(ActsMemApiTest, testMemCcpy_0200, Function | MediumTest | Level1) {
206     char srcStr[] = "this is str1:stop here";
207     char destStr[] = "000000000000000000000000";
208     char *pt = nullptr;
209 
210     LogPrint("    bcopy *dest:='%s' *src:='%s' c:=';' size:='18', \n", destStr, srcStr);
211     pt = (char *)memccpy(destStr, srcStr, ';', 18);
212     LogPrint("    --> return pt:='%x', *dest:='%s'\n", pt, destStr);
213     ASSERT_FALSE(pt)
214         << "ErrInfo: bcopy *dest:='destStr' *src:='"
215         << srcStr << "' c:=':' size:='18',   --> return pt:='" << pt << "', dest:='" << destStr << "'";
216 }
217 
218 /**
219 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMPCPY_0100
220 * @tc.name       test mempcpy api
221 * @tc.desc       [C- SOFTWARE -0200]
222 */
223 HWTEST_F(ActsMemApiTest, testMempcpy_0100, Function | MediumTest | Level1) {
224     char srcStr[] = "this is str1";
225     char destStr[] = "00000000000000000000";
226     char *pt = nullptr;
227 
228     LogPrint("    mempcpy *dest:='%s' *src:='%s' size:='16',", destStr, srcStr);
229     pt = (char *)mempcpy(destStr, srcStr, 16);
230     LogPrint("    --> return pt:='%x', *dest:='%s'\n", pt, destStr);
231     ASSERT_TRUE(pt)
232         << "ErrInfo: mempcpy *dest:='destStr' *src:='"
233         << srcStr << "' size:='16',   --> return pt:='" << pt << "', dest:='" << destStr << "'";
234 }
235 
236 /**
237 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCHR_0100
238 * @tc.name       test memchr api
239 * @tc.desc       [C- SOFTWARE -0200]
240 */
241 HWTEST_F(ActsMemApiTest, testMemchr_0100, Function | MediumTest | Level1) {
242     char srcStr[] = "this is str a;";
243     char *pos = nullptr;
244 
245     LogPrint("    memchr *s:='%s' c:='a' size:='14',", srcStr);
246     pos = (char *)memchr(srcStr, 'a', 14);
247     LogPrint("    --> return pos:='%x'\n", pos);
248 
249     ASSERT_TRUE(pos)
250         << "ErrInfo: memchr *s:='" << srcStr << "' c:='a' size:='14',"
251         << "   --> return pos:='" << pos << "'";
252 }
253 
254 /**
255 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCHR_1000
256 * @tc.name       test memchr api para len not enough
257 * @tc.desc       [C- SOFTWARE -0200]
258 */
259 HWTEST_F(ActsMemApiTest, testMemchr_1000, Function | MediumTest | Level1) {
260     char srcStr[] = "this is str a;";
261     char *pos = nullptr;
262 
263     LogPrint("    memchr *s:='%s' c:='a' size:='4',", srcStr);
264     pos = (char *)memchr(srcStr, 'a', 4);
265     LogPrint("    --> return pos:='%x'\n", pos);
266     ASSERT_FALSE(pos)
267         << "ErrInfo: memchr *s:='" << srcStr << "' c:='a' size:='4',"
268         << "   --> return pos:='" << pos << "'";
269 }
270 
271 /**
272 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMCHR_1100
273 * @tc.name       test memchr api para c not found
274 * @tc.desc       [C- SOFTWARE -0200]
275 */
276 HWTEST_F(ActsMemApiTest, testMemchr_1100, Function | MediumTest | Level1) {
277     char srcStr[] = "this is str a;";
278     char *pos = nullptr;
279 
280     LogPrint("    memchr *s:='%s' c:='b' size:='14',", srcStr);
281     pos = (char *)memchr(srcStr, 'b', 14);
282     LogPrint("    --> return pos:='%x'\n", pos);
283     ASSERT_FALSE(pos)
284         << "ErrInfo: memchr *s:='" << srcStr << "' c:='b' size:='14',"
285         << "   --> return pos:='" << pos << "'";
286 }
287 
288 /**
289 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMMEM_0100
290 * @tc.name       test memmem api
291 * @tc.desc       [C- SOFTWARE -0200]
292 */
293 HWTEST_F(ActsMemApiTest, testMemmem_0100, Function | MediumTest | Level1) {
294     char srcStr[] = "this is str a;";
295     char srcSubStr[] = "str";
296     char *pos = nullptr;
297 
298     LogPrint("    memmem *haystack:='%s' size_haystack:='%d' ", srcStr, strlen(srcStr));
299     LogPrint("*needle:='%s' size_needle:='%d',\n", srcSubStr, strlen(srcSubStr));
300     if (strlen(srcStr) >= strlen(srcSubStr)) {
301     pos = (char *)memmem(srcStr, strlen(srcStr), srcSubStr, strlen(srcSubStr));
302     LogPrint("    --> return pos:='%x'\n", pos);
303     }
304 
305     ASSERT_TRUE(pos)
306         << "ErrInfo: memmem *haystack:='" << srcStr << " size_haystack:='"
307         << strlen(srcStr) << "' *needle:='" << srcSubStr << "' size_needle:='" << strlen(srcSubStr)
308         << "   --> return pos:='" << pos << "'";
309 }
310 
311 /**
312 * @tc.number     SUB_KERNEL_NDKAPI_MEM_MEMMEM_1000
313 * @tc.name       test memmem api para not found
314 * @tc.desc       [C- SOFTWARE -0200]
315 */
316 HWTEST_F(ActsMemApiTest, testMemmem_1000, Function | MediumTest | Level1) {
317     char srcStr[] = "this is str a;";
318     char srcSubStr[] = "strb";
319     char *pos = nullptr;
320 
321     if (strlen(srcStr) < strlen(srcSubStr)) {
322         FAIL();
323     }
324     pos = (char *)memmem(srcStr, strlen(srcStr), srcSubStr, strlen(srcSubStr));
325     LogPrint("    memmem *haystack:='%s' size_haystack:='%d' ", srcStr, strlen(srcStr));
326     LogPrint("*needle:='%s' size_needle:='%d',\n", srcSubStr, strlen(srcSubStr));
327     LogPrint("    --> return pos:='%x'\n", pos);
328 
329     ASSERT_FALSE(pos)
330         << "ErrInfo: memmem *haystack:='" << srcStr << " size_haystack:='"
331         << strlen(srcStr) << "' *needle:='" << srcSubStr << "' size_needle:='" << strlen(srcSubStr)
332         << "   --> return pos:='" << pos << "'";
333 }
334 
335 /**
336 * @tc.number     SUB_KERNEL_NDKAPI_MEM_OPEN_MEMSTREAM_0100
337 * @tc.name       test open_memstream api
338 * @tc.desc       [C- SOFTWARE -0200]
339 */
340 HWTEST_F(ActsMemApiTest, testOpenMemStream_0100, Function | MediumTest | Level1) {
341     FILE *memStream;
342     char *ptr = nullptr;
343     size_t sizeLoc;
344 
345     memStream = open_memstream(&ptr, &sizeLoc);
346     LogPrint("    open_memstream **ptr:='*ptr' *sizeloc:='*sizeLoc', ");
347     LogPrint("    --> return memStream:='%x'\n", memStream);
348     LogPrint("    ptr:='%x', sizeLoc:='%d'\n", ptr, sizeLoc);
349 
350     ASSERT_TRUE(memStream)
351         << "ErrInfo: open_memstream **ptr:='*ptr' *sizeloc:='*sizeLoc', "
352         << "   --> return memStream:='" << memStream << "'";
353 
354     LogPrint("    fprintf(memStream, 'Hello!');\n");
355     fprintf(memStream, "Hello!");
356     fflush(memStream);
357     LogPrint("    ptr:='%s', sizeloc:='%zu'\n", ptr, sizeLoc);
358 
359     LogPrint("    fclose(memStream);\n");
360     fclose(memStream);
361     LogPrint("    ptr:='%s', sizeloc:='%zu'\n", ptr, sizeLoc);
362     free(ptr);
363 }
364 
365 /**
366 * @tc.number     SUB_KERNEL_NDKAPI_MEM_GETPAGESIZE_0100
367 * @tc.name       test getpagesize api
368 * @tc.desc       [C- SOFTWARE -0200]
369 */
370 HWTEST_F(ActsMemApiTest, testMemGetpagesize_0100, Function | MediumTest | Level1) {
371     size_t returnVal;
372 
373     returnVal = getpagesize();
374     LogPrint("    getpagesize,   --> returnVal:='%d'\n", returnVal);
375     EXPECT_GE(returnVal, 0U);
376     EXPECT_EQ(returnVal, PAGE_SIZE)
377         << "ErrInfo: getpagesize,   --> returnVal:='" << returnVal << "'";
378 }
379