1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 /* BEGIN_HEADER */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "securec.h"
21 #include "bsl_errno.h"
22 #include "list_base.h"
23 #include "bsl_hash.h"
24 #include "bsl_hash_list.h"
25 #include "bsl_sal.h"
26
27 #define BACKET_SIZE 64
28 #define MAX_NAME_LEN 64
29
30 typedef struct userData {
31 int id;
32 const char name[MAX_NAME_LEN];
33 } UserData;
34
UserHashKeyDupFunc(void * src,size_t size)35 void *UserHashKeyDupFunc(void *src, size_t size)
36 {
37 char *retKey;
38 char *tmpKey = (char *)src;
39
40 if (size > MAX_NAME_LEN) {
41 return NULL;
42 }
43
44 retKey = (char *)BSL_SAL_Calloc(1, size);
45 ASSERT_TRUE((char *)retKey != (char *)NULL);
46 ASSERT_TRUE(strcpy_s(retKey, size, tmpKey) == EOK);
47
48 EXIT:
49 return (void *)retKey;
50 }
51
UserHashDataDupFunc(void * src,size_t size)52 void *UserHashDataDupFunc(void *src, size_t size)
53 {
54 UserData *ret = NULL;
55 UserData *tmpSrc = (UserData *)src;
56
57 ret = (UserData *)BSL_SAL_Calloc(1, sizeof(UserData));
58 ASSERT_TRUE(ret != (UserData *)NULL);
59 ASSERT_TRUE(memcpy_s(ret, size + 1, tmpSrc, size) == EOK);
60
61 EXIT:
62 return ret;
63 }
64
UserDataCmpFunc(uintptr_t data1,uintptr_t data2)65 int UserDataCmpFunc(uintptr_t data1, uintptr_t data2)
66 {
67 return strcmp((const char*)data1, (const char*)data2);
68 }
69
BslListFreeFunc(void * data)70 void BslListFreeFunc(void *data)
71 {
72 if (data != NULL) {
73 BSL_SAL_Free(data);
74 }
75 }
76
ListNodeCmpFunc(const void * node,uintptr_t data)77 bool ListNodeCmpFunc(const void *node, uintptr_t data)
78 {
79 const ListRawNode *t = (ListRawNode *)node;
80 return (uintptr_t)(t->prev) == data;
81 }
82
83 /* END_HEADER */
84
85 /**
86 * @test SDV_BSL_HASH_LIST_FUNC_TC001
87 * @title Hash list normal capability test
88 * @precon nan
89 * @brief
90 * 1. Call BSL_HASH_Create to create a hash list header. Expected result 1 is obtained.
91 * 2. Call BSL_HASH_Insert to add data to the hash list. Expected result 2 is obtained.
92 * 3. Call BSL_HASH_Size get list size. Expected result 3 is obtained.
93 * 4. Call BSL_CstlHashErase BSL_CstlHashClear BSL_CstlHashDestory delete data,
94 * Expected result 4 is obtained.
95 * @expect
96 * 1. success
97 * 2. BSL_SUCCESS
98 * 3. size is 6
99 * 4. success
100 */
101 /* BEGIN_CASE */
SDV_BSL_HASH_LIST_FUNC_TC001(void)102 void SDV_BSL_HASH_LIST_FUNC_TC001(void)
103 {
104 int i;
105 BSL_HASH_Hash *hash;
106 uintptr_t tmpValue;
107 UserData *userValue;
108 BSL_HASH_Iterator it;
109 uintptr_t tmpKey;
110 uint8_t key[6] = {28, 29, 30, 31, 32, 33};
111 UserData value[6] = {{16, "bsl_cstl001"},
112 {18, "bsl_cstl002"},
113 {15, "bsl_cstl003"},
114 {17, "bsl_cstl004"},
115 {17, "bsl_cstl005"},
116 {16, "bsl_cstl001"}};
117 ListDupFreeFuncPair valueFunc = {UserHashDataDupFunc, BSL_SAL_Free};
118 TestMemInit();
119 hash = BSL_HASH_Create(BACKET_SIZE, NULL, NULL, NULL, &valueFunc);
120 ASSERT_TRUE(hash != (BSL_HASH_Hash *)NULL);
121
122 for (i = 0; i < 5; i++) {
123 ASSERT_TRUE(BSL_HASH_Insert(hash, key[i], 0, (uintptr_t)&value[i], sizeof(UserData)) == BSL_SUCCESS);
124 }
125 ASSERT_TRUE(BSL_HASH_Put(hash, key[i], 0, (uintptr_t)&value[i], sizeof(UserData), NULL) == BSL_SUCCESS);
126
127 ASSERT_TRUE(BSL_HASH_Size(hash) == (size_t)6);
128
129 ASSERT_TRUE(BSL_HASH_At(hash, key[4], &tmpValue) == BSL_SUCCESS);
130 userValue = (UserData *)tmpValue;
131 ASSERT_TRUE(userValue->id == value[4].id);
132 ASSERT_TRUE(strcmp(userValue->name, value[4].name) == 0);
133
134 it = BSL_HASH_Find(hash, key[4]);
135 ASSERT_TRUE(it != BSL_HASH_IterEnd(hash));
136 ASSERT_TRUE(BSL_HASH_HashIterKey(hash, it) == (uintptr_t)key[4]);
137
138 userValue = (UserData *)BSL_HASH_IterValue(hash, it);
139 ASSERT_TRUE(userValue != (UserData *)NULL);
140 ASSERT_TRUE(userValue->id == value[4].id);
141 ASSERT_TRUE(strcmp(userValue->name, value[4].name) == 0);
142
143 (void)BSL_HASH_Erase(hash, key[4]);
144 ASSERT_TRUE(BSL_HASH_Size(hash) == (size_t)5);
145
146 for (it = BSL_HASH_IterBegin(hash); it != BSL_HASH_IterEnd(hash);) {
147 tmpKey = BSL_HASH_HashIterKey(hash, it);
148 it = BSL_HASH_Erase(hash, tmpKey);
149 }
150
151 ASSERT_TRUE(BSL_HASH_Size(hash) == (size_t)0);
152 BSL_HASH_Destory(hash);
153 EXIT:
154 return;
155 }
156 /* END_CASE */
157
158 /**
159 * @test SDV_BSL_HASH_LIST_FUNC_TC002
160 * @title Hash list normal capability test, key type is string.
161 * @precon nan
162 * @brief
163 * 1. Call BSL_HASH_Create to create a hash list header. Expected result 1 is obtained.
164 * 2. Call BSL_HASH_Insert to add data to the hash list. Expected result 2 is obtained.
165 * 3. Call BSL_HASH_Size get list size. Expected result 3 is obtained.
166 * 4. Call BSL_CstlHashErase BSL_CstlHashClear BSL_CstlHashDestory delete data,
167 * Expected result 4 is obtained.
168 * @expect
169 * 1. success
170 * 2. BSL_SUCCESS
171 * 3. size is 6
172 * 4. success
173 */
174 /* BEGIN_CASE */
SDV_BSL_HASH_LIST_FUNC_TC002(void)175 void SDV_BSL_HASH_LIST_FUNC_TC002(void)
176 {
177 uint32_t i;
178 BSL_HASH_Hash *hash;
179 const char *key[6] = {"7201028", "7201029", "7201030", "7201031", "7201032", "7201033"};
180 UserData value[6] = {{16, "bsl_cstl001"},
181 {18, "bsl_cstl002"},
182 {15, "bsl_cstl003"},
183 {17, "bsl_cstl004"},
184 {17, "bsl_cstl005"},
185 {16, "bsl_cstl001"}};
186 char *tmpKey;
187 uintptr_t tmpValue;
188 UserData *userValue;
189 BSL_HASH_Iterator it;
190 ListDupFreeFuncPair keyFunc = {UserHashKeyDupFunc, BSL_SAL_Free};
191 ListDupFreeFuncPair valueFunc = {UserHashDataDupFunc, BSL_SAL_Free};
192
193 hash = BSL_HASH_Create(BACKET_SIZE, BSL_HASH_CodeCalcStr, BSL_HASH_MatchStr, &keyFunc, &valueFunc);
194 ASSERT_TRUE(hash != (BSL_HASH_Hash *)NULL);
195
196 for (i = 0; i < 6; i++) {
197 ASSERT_TRUE(
198 BSL_HASH_Insert(hash, (uintptr_t)key[i], strlen(key[i]) + 1, (uintptr_t)&value[i], sizeof(UserData)) ==
199 BSL_SUCCESS);
200 }
201
202 ASSERT_TRUE(BSL_HASH_Put(hash, (uintptr_t)key[1], strlen(key[1]) + 1, (uintptr_t)&value[1], sizeof(UserData), NULL)
203 == BSL_SUCCESS);
204 ASSERT_TRUE(BSL_HASH_Empty(hash) == false);
205 ASSERT_TRUE(BSL_HASH_Size(hash) == (size_t)6);
206 ASSERT_TRUE(BSL_HASH_At(hash, (uintptr_t)key[4], &tmpValue) == BSL_SUCCESS);
207 userValue = (UserData *)tmpValue;
208 ASSERT_TRUE(userValue->id == value[4].id);
209 ASSERT_TRUE(strcmp(userValue->name, value[4].name) == 0);
210
211 it = BSL_HASH_Find(hash, (uintptr_t)key[4]);
212 ASSERT_TRUE(it != BSL_HASH_IterEnd(hash));
213 ASSERT_TRUE(strcmp((const char *)BSL_HASH_HashIterKey(hash, it), key[4]) == 0);
214
215 userValue = (UserData *)BSL_HASH_IterValue(hash, it);
216 ASSERT_TRUE(userValue != (UserData *)NULL);
217 ASSERT_TRUE(userValue->id == value[4].id);
218 ASSERT_TRUE(strcmp(userValue->name, value[4].name) == 0);
219
220 (void)BSL_HASH_Erase(hash, (uintptr_t)key[4]);
221 ASSERT_TRUE(BSL_HASH_Size(hash) == (size_t)5);
222
223 for (it = BSL_HASH_IterBegin(hash); it != BSL_HASH_IterEnd(hash);) {
224 tmpKey = (char *)BSL_HASH_HashIterKey(hash, it);
225 it = BSL_HASH_Erase(hash, (uintptr_t)tmpKey);
226 }
227
228 ASSERT_TRUE(BSL_HASH_Size(hash) == (size_t)0);
229 BSL_HASH_Destory(hash);
230 EXIT:
231 return;
232 }
233 /* END_CASE */
234
235 /**
236 * @test SDV_BSL_HASH_LIST_FUNC_TC003
237 * @title bsl list normal capability test, key type is string.
238 * @precon nan
239 * @brief
240 * 1. Call BSL_ListInit to create a hash list header. Expected result 1 is obtained.
241 * 2. Call BSL_ListPushFront and BSL_ListPushBack to add data to the hash list. Expected result 2 is obtained.
242 * 3. Perform find and delete. Expected result 3 is obtained.
243 * 4. Call BSL_ListIterErase delete data. Expected result 4 is obtained.
244 * @expect
245 * 1. success
246 * 2. BSL_SUCCESS
247 * 3. success
248 * 4. success
249 */
250 /* BEGIN_CASE */
SDV_BSL_HASH_LIST_FUNC_TC003(void)251 void SDV_BSL_HASH_LIST_FUNC_TC003(void)
252 {
253 uint32_t i;
254 BSL_List *list = BSL_SAL_Calloc(1, sizeof(BSL_List));
255 ASSERT_TRUE(list != NULL);
256 const char *data[7] = {"7201028", "7201029", "7201030", "7201031", "7201032", "7201033", "777888"};
257
258 BSL_ListIterator it = NULL;
259 ListDupFreeFuncPair valueFunc = {UserHashKeyDupFunc, BSL_SAL_Free};
260
261 ASSERT_EQ(BSL_ListInit(list, &valueFunc), BSL_SUCCESS);
262
263 for (i = 0; i < 5; i++) {
264 ASSERT_EQ( BSL_ListPushFront(list, (uintptr_t)data[i], strlen(data[i]) + 1), BSL_SUCCESS);
265 }
266
267 ASSERT_EQ(BSL_ListPushBack(list, (uintptr_t)data[i], strlen(data[i]) + 1), BSL_SUCCESS);
268
269 ASSERT_TRUE(BSL_ListSize(list) == (size_t)i + 1);
270
271 ASSERT_TRUE(strcmp((const char* )BSL_ListFront(list), data[4]) == 0);
272 ASSERT_TRUE(strcmp((const char* )BSL_ListBack(list), data[5]) == 0);
273
274 it = BSL_ListIterFind(list, UserDataCmpFunc, (uintptr_t)data[1]);
275 ASSERT_TRUE(it != BSL_ListIterEnd(list));
276
277 ASSERT_TRUE(strcmp((const char* )BSL_ListIterData(it), data[1]) == 0);
278 ASSERT_TRUE(BSL_ListInsert(list, it, (uintptr_t)data[6], strlen(data[6]) + 1) == BSL_SUCCESS);
279
280 ASSERT_TRUE(BSL_ListIterPrev(list, it) != NULL);
281 ASSERT_TRUE(BSL_ListIterNext(list, it) != NULL);
282 ASSERT_TRUE(BSL_ListIterBegin(list) != NULL);
283 ASSERT_TRUE(BSL_ListIterEnd(list) != NULL);
284
285 ASSERT_TRUE(BSL_ListPopFront(list) == BSL_SUCCESS);
286 ASSERT_TRUE(BSL_ListPopBack(list) == BSL_SUCCESS);
287
288 ASSERT_TRUE(BSL_ListIterErase(list, it) != NULL);
289
290 EXIT:
291 BSL_ListDeinit(list);
292 BSL_SAL_Free(list);
293 return;
294 }
295 /* END_CASE */
296
297 /**
298 * @test SDV_BSL_HASH_LIST_FUNC_TC004
299 * @title raw list normal capability test.
300 * @precon nan
301 * @brief
302 * 1. Call ListRawInit to create a raw list header. Expected result 1 is obtained.
303 * 2. Call ListRawPushFront to add node to the list. Expected result 2 is obtained.
304 * 3. Perform find and delete. Expected result 3 is obtained.
305 * 4. Call ListRawDeinit delete list. Expected result 4 is obtained.
306 * @expect
307 * 1. create success
308 * 2. add success
309 * 3. success
310 * 4. success
311 */
312 /* BEGIN_CASE */
SDV_BSL_HASH_LIST_FUNC_TC004(void)313 void SDV_BSL_HASH_LIST_FUNC_TC004(void)
314 {
315 uint32_t i;
316 ListRawNode *node = BSL_SAL_Calloc(6, sizeof(ListRawNode));
317 ASSERT_TRUE(node != NULL);
318 RawList *list = BSL_SAL_Calloc(1, sizeof(RawList));
319 ASSERT_TRUE(list != NULL);
320
321 ASSERT_EQ(ListRawInit(list, NULL), BSL_SUCCESS);
322
323 for (i = 0; i < 6; i++) {
324 ASSERT_EQ(ListRawPushFront(list, &node[i]), BSL_SUCCESS);
325 }
326
327 ListRawNode *it = ListRawBack(list);
328 ASSERT_TRUE(it != NULL);
329 ASSERT_TRUE(it->prev != NULL);
330
331 it = ListRawGetPrev(list, &node[1]);
332 ASSERT_TRUE(it != NULL);
333 ASSERT_TRUE(it->next != NULL);
334
335 it = ListRawFindNode(list, ListNodeCmpFunc, (uintptr_t)&node[1]);
336 ASSERT_TRUE(it != NULL);
337
338 ASSERT_EQ(ListRawPopFront(list), BSL_SUCCESS);
339 ASSERT_EQ(ListRawPopBack(list), BSL_SUCCESS);
340
341 ASSERT_EQ(ListRawDeinit(list), BSL_SUCCESS);
342 EXIT:
343 BSL_SAL_Free(list);
344 BSL_SAL_Free(node);
345 return;
346 }
347 /* END_CASE */
348