• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/select.h>
30 #include <sys/time.h>
31 #include <linux/ioctl.h>
32 #include "securec.h"
33 #include "stub_replace.h"
34 #include "bsl_sal.h"
35 #include "sal_net.h"
36 #include "bsl_errno.h"
37 #include "bsl_uio.h"
38 #include "uio_base.h"
39 #include "sal_atomic.h"
40 #include "uio_abstraction.h"
41 
42 /* END_HEADER */
43 
44 #define MAX_BUF_SIZE 255
45 #define IP_V4_LEN 4
46 #define IP_V6_LEN 16
47 
48 static int32_t g_writeRet;
49 static uint32_t g_writeLen;
STUB_Write(BSL_UIO * uio,const void * buf,uint32_t len,uint32_t * writeLen)50 static int32_t STUB_Write(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen)
51 {
52     (void)uio;
53     (void)buf;
54     (void)len;
55 
56     *writeLen = g_writeLen;
57     return g_writeRet;
58 }
59 
60 static int32_t g_readRet;
61 static uint32_t g_readLen;
STUB_Read(BSL_UIO * uio,void * buf,uint32_t len,uint32_t * readLen)62 static int32_t STUB_Read(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen)
63 {
64     (void)uio;
65     (void)buf;
66     (void)len;
67 
68     *readLen = g_readLen;
69     return g_readRet;
70 }
71 
72 static int32_t g_ctrlRet1;
73 static int32_t g_ctrlRet2;
74 static int32_t g_ctrlRet3;
75 static BSL_UIO_CtrlParameter g_ctrlCmd1;
76 static BSL_UIO_CtrlParameter g_ctrlCmd2;
77 static BSL_UIO_CtrlParameter g_ctrlCmd3;
78 static uint16_t g_shareKeyId;
79 static uint16_t g_delShareKeyId;
80 static uint8_t g_isEmpty;
STUB_Ctrl(BSL_UIO * uio,int32_t cmd,int32_t larg,void * param)81 static int32_t STUB_Ctrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *param)
82 {
83     (void)larg;
84     (void)uio;
85     (void)param;
86     if (cmd == BSL_UIO_SCTP_ADD_AUTH_SHARED_KEY) {
87         BSL_UIO_SctpAuthKey *key = (BSL_UIO_SctpAuthKey *)param;
88         g_shareKeyId = key->shareKeyId;
89     }
90 
91     if (cmd == BSL_UIO_SCTP_DEL_PRE_AUTH_SHARED_KEY) {
92         g_delShareKeyId = *(uint16_t*)param;
93     }
94 
95     if (cmd == BSL_UIO_SCTP_SND_BUFF_IS_EMPTY) {
96         *(uint8_t *)param = g_isEmpty;
97     }
98 
99     if ((int32_t)g_ctrlCmd1 == cmd) {
100         return g_ctrlRet1;
101     }
102 
103     if ((int32_t)g_ctrlCmd2 == cmd) {
104         return g_ctrlRet2;
105     }
106 
107     if ((int32_t)g_ctrlCmd3 == cmd) {
108         return g_ctrlRet3;
109     }
110 
111     return BSL_UIO_FAIL;
112 }
113 
GetUioMethodByType(int uioType)114 const BSL_UIO_Method * GetUioMethodByType(int uioType)
115 {
116     switch (uioType) {
117 #ifdef HITLS_BSL_UIO_TCP
118         case BSL_UIO_TCP:
119             return BSL_UIO_TcpMethod();
120 #endif
121 #ifdef HITLS_BSL_UIO_UDP
122         case BSL_UIO_UDP:
123             return BSL_UIO_UdpMethod();
124 #endif
125         case BSL_UIO_BUFFER:
126             return BSL_UIO_BufferMethod();
127         default:
128             return NULL;
129     }
130 }
131 
132 typedef struct {
133     int32_t len;
134     uint8_t index;
135     uint8_t *buff;
136 } CustomLowCtx;
137 
BslUioCreate(BSL_UIO * uio)138 static int32_t BslUioCreate(BSL_UIO *uio)
139 {
140     int32_t len = 20;
141     int32_t ret;
142     CustomLowCtx *lowCtx = BSL_SAL_Calloc(1, sizeof(CustomLowCtx));
143     if (lowCtx == NULL) {
144         ret = BSL_MALLOC_FAIL;
145         goto EXIT;
146     }
147     lowCtx->buff = BSL_SAL_Malloc(len);
148     if (lowCtx->buff == NULL) {
149         ret = BSL_MALLOC_FAIL;
150         goto EXIT;
151     }
152     lowCtx->len = len;
153     BSL_UIO_SetCtx(uio, (void *)lowCtx);
154     BSL_UIO_SetInit(uio, 1);
155     return BSL_SUCCESS;
156 EXIT:
157     if(lowCtx != NULL) {
158         BSL_SAL_FREE(lowCtx->buff);
159         BSL_SAL_FREE(lowCtx);
160     }
161     return ret;
162 }
163 
BslUioDestroy(BSL_UIO * uio)164 static int32_t BslUioDestroy(BSL_UIO *uio)
165 {
166     CustomLowCtx *lowCtx = BSL_UIO_GetCtx(uio);
167     if (lowCtx == NULL) {
168         return BSL_INVALID_ARG;
169     }
170     BSL_SAL_FREE(lowCtx->buff);
171     BSL_SAL_FREE(lowCtx);
172     BSL_UIO_SetCtx(uio, NULL);
173     return BSL_SUCCESS;
174 }
175 
BslUioWrite(BSL_UIO * uio,const void * buf,uint32_t len,uint32_t * writeLen)176 static int32_t BslUioWrite(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen)
177 {
178     CustomLowCtx *lowCtx = BSL_UIO_GetCtx(uio);
179     if (lowCtx == NULL) {
180         return BSL_INVALID_ARG;
181     }
182     uint32_t reslen = lowCtx->len - lowCtx->index;
183     if (reslen < len) {
184         return BSL_INVALID_ARG;
185     }
186     memcpy_s(lowCtx->buff + lowCtx->index, len, buf, len);
187     lowCtx->index += len;
188     *writeLen = len;
189     return BSL_SUCCESS;
190 }
191 
BslUioRead(BSL_UIO * uio,void * buf,uint32_t len,uint32_t * readLen)192 static int32_t BslUioRead(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen)
193 {
194     CustomLowCtx *lowCtx = BSL_UIO_GetCtx(uio);
195     if (lowCtx == NULL) {
196         return BSL_INVALID_ARG;
197     }
198     if (lowCtx->index == 0) {
199         return BSL_INVALID_ARG;
200     }
201 
202     int copyLen = (lowCtx->index > len) ? len : lowCtx->index;
203     (void)memcpy_s(buf, copyLen, lowCtx->buff, copyLen);
204     *readLen = copyLen;
205     lowCtx->index -= copyLen;
206     return BSL_SUCCESS;
207 }
208 
209 #define BSL_CUSTOM_UIO_GET_INDEX 0x100
210 
BslUioCtrl(BSL_UIO * uio,int32_t cmd,int32_t larg,void * parg)211 static int32_t BslUioCtrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg)
212 {
213     (void)larg;
214     CustomLowCtx *lowCtx = BSL_UIO_GetCtx(uio);
215     if (lowCtx == NULL) {
216         return BSL_INVALID_ARG;
217     }
218     if (cmd == BSL_CUSTOM_UIO_GET_INDEX) {
219         *(uint32_t *)parg = lowCtx->index;
220         return BSL_SUCCESS;
221     }
222     return BSL_INVALID_ARG;
223 }
224 
BslUioPuts(BSL_UIO * uio,const char * buf,uint32_t * writeLen)225 static int32_t BslUioPuts(BSL_UIO *uio, const char *buf, uint32_t *writeLen)
226 {
227     (void) uio;
228     (void) buf;
229     (void) writeLen;
230     return BSL_INVALID_ARG;
231 }
232 
BslUioGets(BSL_UIO * uio,char * buf,uint32_t * readLen)233 static int32_t BslUioGets(BSL_UIO *uio, char *buf, uint32_t *readLen)
234 {
235     (void) uio;
236     (void) buf;
237     (void) readLen;
238     return BSL_INVALID_ARG;
239 }
240 
241 /**
242  * @test  SDV_BSL_UIO_NEW_API_TC001
243  * @title  Input parameter test
244  * @precon  nan
245  * @brief
246  *    1. Construct the tcp/sctp/udp method structure, and invoke BSL_UIO_New.
247  *    2. Invoke the BSL_UIO_GetTransportType interface.
248  * @expect
249  *    1. Expected the uio is not NULL, and transport type is the target type
250  */
251 /* BEGIN_CASE */
SDV_BSL_UIO_NEW_API_TC001(void)252 void SDV_BSL_UIO_NEW_API_TC001(void)
253 {
254 #if defined(HITLS_BSL_UIO_TCP) || defined(HITLS_BSL_UIO_UDP)
255     TestMemInit();
256     /* Set method to NULL */
257     BSL_UIO *uio = BSL_UIO_New(NULL);
258     ASSERT_TRUE(uio == NULL);
259 #ifdef HITLS_BSL_UIO_TCP
260     /* Set transportType to tcp and construct the method structure. */
261     {
262         const BSL_UIO_Method *ori = BSL_UIO_TcpMethod();
263         BSL_UIO_Method method = {0};
264         memcpy(&method, ori, sizeof(method));
265         method.uioWrite = STUB_Write;
266         method.uioRead = STUB_Read;
267         method.uioCtrl = STUB_Ctrl;
268         uio = BSL_UIO_New(&method);
269         ASSERT_TRUE(uio != NULL && BSL_UIO_GetTransportType(uio) == BSL_UIO_TCP);
270         BSL_UIO_Free(uio);
271     }
272 #endif
273 #ifdef HITLS_BSL_UIO_UDP
274     /* Set transportType to udp and construct the method structure. */
275     {
276         const BSL_UIO_Method *ori = BSL_UIO_UdpMethod();
277         BSL_UIO_Method method = {0};
278         memcpy(&method, ori, sizeof(method));
279         method.uioWrite = STUB_Write;
280         method.uioRead = STUB_Read;
281         method.uioCtrl = STUB_Ctrl;
282         uio = BSL_UIO_New(&method);
283         ASSERT_TRUE(uio != NULL && BSL_UIO_GetTransportType(uio) == BSL_UIO_UDP);
284         BSL_UIO_Free(uio);
285     }
286 #endif
287 EXIT:
288     return;
289 #else
290     SKIP_TEST();
291 #endif
292 }
293 /* END_CASE */
294 
295 /**
296  * @test  SDV_BSL_UIO_NEW_API_TC002
297  * @title BSL uio and meth parameter test
298  * @precon Registering memory-related functions.
299  */
300 /* BEGIN_CASE */
SDV_BSL_UIO_NEW_API_TC002(void)301 void SDV_BSL_UIO_NEW_API_TC002(void)
302 {
303     TestMemInit();
304 
305     BSL_UIO *uio = BSL_UIO_New(NULL);
306     ASSERT_EQ(uio, NULL);
307     BSL_UIO_Method *ori = BSL_UIO_NewMethod();
308     ASSERT_NE(ori, NULL);
309     int32_t customType = BSL_UIO_EXTEND + 3;
310 
311     ASSERT_EQ(BSL_UIO_SetMethodType(ori, customType), BSL_SUCCESS);
312     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_READ_CB, BslUioRead), BSL_SUCCESS);
313     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_WRITE_CB, BslUioWrite), BSL_SUCCESS);
314     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_CTRL_CB, BslUioCtrl), BSL_SUCCESS);
315     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_CREATE_CB, BslUioCreate), BSL_SUCCESS);
316     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_DESTROY_CB, BslUioDestroy), BSL_SUCCESS);
317     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_PUTS_CB, BslUioPuts), BSL_SUCCESS);
318     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_GETS_CB, BslUioGets), BSL_SUCCESS);
319 
320     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_READ_CB, NULL), BSL_NULL_INPUT);
321     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_GETS_CB + 1, BslUioGets), BSL_INVALID_ARG);
322 
323     uio = BSL_UIO_New(ori);
324     ASSERT_NE(uio, NULL);
325 
326     ASSERT_EQ(BSL_UIO_GetTransportType(uio), customType);
327 
328     char *test = "test ";
329     uint32_t len = 0;
330     ASSERT_EQ(BSL_UIO_Gets(uio, test, &len), BSL_INVALID_ARG);
331 
332     ASSERT_EQ(BSL_UIO_Puts(uio, test, &len), BSL_INVALID_ARG);
333 
334 EXIT:
335     BSL_UIO_Free(uio);
336     BSL_UIO_FreeMethod(ori);
337 }
338 /* END_CASE */
339 
340 
341 /**
342  * @test  SDV_BSL_UIO_NEW_API_TC001
343  * @title BSL uio and meth functional test
344  * @precon Registering memory-related functions.
345  */
346 /* BEGIN_CASE */
SDV_BSL_UIO_NEW_FUNC_TC001(void)347 void SDV_BSL_UIO_NEW_FUNC_TC001(void)
348 {
349     uint8_t test[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
350     TestMemInit();
351 
352     BSL_UIO_Method *ori = BSL_UIO_NewMethod();
353     ASSERT_NE(ori, NULL);
354     int32_t customType = BSL_UIO_EXTEND + 3;
355 
356     ASSERT_EQ(BSL_UIO_SetMethodType(ori, customType), BSL_SUCCESS);
357     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_READ_CB, BslUioRead), BSL_SUCCESS);
358     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_WRITE_CB, BslUioWrite), BSL_SUCCESS);
359     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_CTRL_CB, BslUioCtrl), BSL_SUCCESS);
360     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_CREATE_CB, BslUioCreate), BSL_SUCCESS);
361     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_DESTROY_CB, BslUioDestroy), BSL_SUCCESS);
362     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_PUTS_CB, BslUioPuts), BSL_SUCCESS);
363     ASSERT_EQ(BSL_UIO_SetMethod(ori, BSL_UIO_GETS_CB, BslUioGets), BSL_SUCCESS);
364     BSL_UIO *uio = BSL_UIO_New(ori);
365     ASSERT_NE(uio, NULL);
366 
367     ASSERT_EQ(BSL_UIO_GetTransportType(uio), customType);
368 
369     uint32_t len = 0;
370     ASSERT_EQ(BSL_UIO_Write(uio, test, 3, &len), BSL_SUCCESS);
371     ASSERT_EQ(len, 3);
372 
373     ASSERT_EQ(BSL_UIO_Write(uio, &test[3], 7, &len), BSL_SUCCESS);
374     ASSERT_EQ(len, 7);
375 
376     uint32_t index = 0;
377     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_CUSTOM_UIO_GET_INDEX, sizeof(index), &index), BSL_SUCCESS);
378     ASSERT_EQ(index, 10);
379 
380     uint8_t readBuff[20] = {0};
381     ASSERT_EQ(BSL_UIO_Read(uio, readBuff, 8, &len), BSL_SUCCESS);
382     ASSERT_EQ(len, 8);
383 
384     ASSERT_EQ(BSL_UIO_Read(uio, &readBuff[7], 8, &len), BSL_SUCCESS);
385     ASSERT_EQ(len, 2);
386 
387     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_CUSTOM_UIO_GET_INDEX, sizeof(index), &index), BSL_SUCCESS);
388     ASSERT_EQ(index, 0);
389 
390 EXIT:
391     BSL_UIO_Free(uio);
392     BSL_UIO_FreeMethod(ori);
393 }
394 /* END_CASE */
395 
396 /**
397  * @test  SDV_BSL_UIO_INIT_FUNC_TC001
398  * @title  Init status value test: The uio type of the FD can be set.
399  * @precon  nan
400  * @brief
401  *    1.BSL_UIO_New,Expected result 1 is obtained.
402  *    2.BSL_UIO_GET_INIT,Expected result 2 is obtained.
403  *    3.BSL_UIO_GET_FD,Expected result 3 is obtained.
404  *    4.BSL_UIO_SET_FD,Expected result 4 is obtained.
405  *    5.BSL_UIO_GET_INIT,Expected result 5 is obtained.
406  *    6.BSL_UIO_GET_FD,Expected result 6 is obtained.
407  * @expect
408  *    1.Expected success
409  *    2.The value of init is 0.
410  *    3.Expected failure, The error code is BSL_UIO_UNINITIALIZED,fd is - 1.
411  *    4.Expected success
412  *    5.The value of init is 1.
413  *    6.Expected success
414  */
415 /* BEGIN_CASE */
SDV_BSL_UIO_INIT_FUNC_TC001(int uioType)416 void SDV_BSL_UIO_INIT_FUNC_TC001(int uioType)
417 {
418     BSL_UIO *uio = NULL;
419     int32_t fd = 5;
420     uint8_t init = 0;
421     int32_t getFd = 0;
422 
423     const BSL_UIO_Method *ori = NULL;
424     switch (uioType) {
425         case BSL_UIO_TCP:
426         case BSL_UIO_UDP:
427             ori = GetUioMethodByType(uioType);
428             break;
429         default: // The uio of the FD cannot be set.
430             ASSERT_TRUE(false);
431     }
432     ASSERT_TRUE(ori != NULL);
433 
434     uio = BSL_UIO_New(ori);
435     ASSERT_TRUE(uio != NULL);
436     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_UIO_GET_INIT, (int32_t)sizeof(init), &init), BSL_SUCCESS);
437     ASSERT_EQ(init, 0);
438 
439     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_UIO_SET_FD, (int32_t)sizeof(fd), &fd), BSL_SUCCESS);
440     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_UIO_GET_INIT, (int32_t)sizeof(init), &init), BSL_SUCCESS);
441     ASSERT_EQ(init, 1);
442 
443     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_UIO_GET_FD, (int32_t)sizeof(fd), &getFd), BSL_SUCCESS);
444     ASSERT_EQ(getFd, fd);
445 
446 EXIT:
447     BSL_UIO_Free(uio);
448 }
449 /* END_CASE */
450 
451 /**
452  * @test  SDV_BSL_UIO_INIT_FUNC_TC002
453  * @title  Init status value test: Test the UIO type whose init is set to 1 during create.
454  * @precon  nan
455  * @brief
456  *    1.Call BSL_UIO_New. Expected result 1 is obtained.
457  *    2.Call BSL_UIO_GET_INIT. Expected result 1 is obtained.
458  * @expect
459  *    1.Success
460  *    2.init is 1
461  */
462 /* BEGIN_CASE */
SDV_BSL_UIO_INIT_FUNC_TC002(int uioType)463 void SDV_BSL_UIO_INIT_FUNC_TC002(int uioType)
464 {
465     BSL_UIO *uio = NULL;
466     uint8_t init = 0;
467 
468     const BSL_UIO_Method *ori = NULL;
469     switch (uioType) {
470         case BSL_UIO_BUFFER:
471             ori = GetUioMethodByType(uioType);
472             break;
473         default:
474             ASSERT_TRUE(false);
475     }
476     ASSERT_TRUE(ori != NULL);
477 
478     uio = BSL_UIO_New(ori);
479     ASSERT_TRUE(uio != NULL);
480     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_UIO_GET_INIT, (int32_t)sizeof(init), &init), BSL_SUCCESS);
481     ASSERT_EQ(init, 1);
482 
483 EXIT:
484     BSL_UIO_Free(uio);
485 }
486 /* END_CASE */
487 
488 /**
489  * @test  UT_BSL_UIO_FREE_API_TC001
490  * @title  Input parameter test
491  * @precon  nan
492  * @brief
493  *    1. Set UIO to null and invoke BSL_UIO_Free. Expected result 1 is obtained.
494  * @expect
495  *    1. It is expected that the program does not dump code. No memory leakage occurs.
496  */
497 /* BEGIN_CASE */
UT_BSL_UIO_FREE_API_TC001(void)498 void UT_BSL_UIO_FREE_API_TC001(void)
499 {
500     /* The test UIO is empty. */
501     BSL_UIO_Free(NULL);
502 }
503 /* END_CASE */
504 
505 /**
506  * @test  SDV_BSL_UIO_SETUSERDATA_API_TC001
507  * @title  Input parameter test
508  * @precon  nan
509  * @brief
510  *    1.The specified uio is empty, Invoke BSL_UIO_SetUserData,Expected result 1 is obtained.
511  *    2.Construct the uio object,  the user data is empty, invoke the BSL_UIO_SetUserData interface.
512         Expected result 2 is obtained.
513  *    3.Invoke BSL_UIO_SetUserData to specify that data1 is not null, Expected result 3 is obtained.
514  *    4.User data2 is not empty, call BSL_UIO_SetUserData again, Expected result 4 is obtained.
515  *    5.Releasing a UIO Object, Expected result 5 is obtained.
516  * @expect
517  *    1.Expected return failure
518  *    2.Expected success
519  *    3.Expected success
520  *    4.Expected success
521  *    5.It is expected that the program does not have code dump and no memory leakage occurs.
522  */
523 /* BEGIN_CASE */
SDV_BSL_UIO_SETUSERDATA_API_TC001(void)524 void SDV_BSL_UIO_SETUSERDATA_API_TC001(void)
525 {
526     BSL_UIO *uio = NULL;
527     void *userData1 = (void *)STUB_Write;
528     void *userData2 = (void *)STUB_Read;
529     /* The test UIO is empty. */
530     int32_t ret = BSL_UIO_SetUserData(NULL, userData1);
531     ASSERT_TRUE(ret == BSL_NULL_INPUT);
532 
533     uio = BSL_UIO_New(BSL_UIO_TcpMethod());
534     ASSERT_TRUE(uio != NULL);
535 
536     ret = BSL_UIO_SetUserData(uio, NULL);
537     ASSERT_TRUE(ret == BSL_SUCCESS);
538 
539     ret = BSL_UIO_SetUserData(uio, userData1);
540     ASSERT_TRUE(ret == BSL_SUCCESS);
541 
542     ret = BSL_UIO_SetUserData(uio, userData2);
543     ASSERT_TRUE(ret == BSL_SUCCESS);
544 
545 EXIT:
546     BSL_UIO_Free(uio);
547 }
548 /* END_CASE */
549 
550 /**
551  * @test  SDV_BSL_UIO_GETUSERDATA_API_TC001
552  * @title  Input parameter test
553  * @precon  nan
554  * @brief
555  *    1.The specified uio is empty., invoke BSL_UIO_GetUserData. Expected result 1 is obtained.
556  *    2.Construct the uio object, invoke BSL_UIO_GetUserData. Expected result 2 is obtained.
557  *    3.Construct user data, invoke BSL_UIO_SetUserData. Expected result 3 is obtained.
558  *    4.Invoke BSL_UIO_GetUserData. Expected result 4 is obtained.
559  *    5.Invoke BSL_UIO_Free. Expected result 5 is obtained.
560  * @expect
561  *    1.Expected return NULL
562  *    2.Expected return NULL
563  *    3.Expected success
564  *    4.The expected return value is the same as the data pointer.
565  *    5.It is expected that the program does not have code dump and no memory leakage occurs.
566  */
567 /* BEGIN_CASE */
SDV_BSL_UIO_GETUSERDATA_API_TC001(void)568 void SDV_BSL_UIO_GETUSERDATA_API_TC001(void)
569 {
570     BSL_UIO *uio = NULL;
571     void *userData = (void *)STUB_Write;
572     /* The test UIO is empty. */
573     void *data = BSL_UIO_GetUserData(NULL);
574     ASSERT_TRUE(data == NULL);
575 
576     uio = BSL_UIO_New(BSL_UIO_TcpMethod());
577     ASSERT_TRUE(uio != NULL);
578 
579     int32_t ret = BSL_UIO_SetUserData(uio, userData);
580     ASSERT_TRUE(ret == BSL_SUCCESS);
581 
582     data = BSL_UIO_GetUserData(uio);
583     ASSERT_TRUE(data == userData);
584 EXIT:
585     BSL_UIO_Free(uio);
586 }
587 /* END_CASE */
588 
589 /**
590  * @test  SDV_BSL_UIO_FLAGS_FUNC_TC001
591  * @title  UIO Setting the Status Test
592  * @precon  nan
593  * @brief
594  *    1.BSL_UIO_New. Expected result 1 is obtained.
595  *    2.BSL_UIO_SetFlags. Expected result 2 is obtained.
596  * @expect
597  *    1.Expected success
598  *    2.If the flags are invalid, BSL_INVALID_ARG is returned. If the flags are valid, BSL_SUCCESS is returned.
599  */
600 /* BEGIN_CASE */
SDV_BSL_UIO_FLAGS_FUNC_TC001(int uioType)601 void SDV_BSL_UIO_FLAGS_FUNC_TC001(int uioType)
602 {
603     BSL_UIO *uio = NULL;
604 
605     const BSL_UIO_Method *ori = GetUioMethodByType(uioType);
606     ASSERT_TRUE(ori != NULL);
607 
608     uio = BSL_UIO_New(ori);
609     ASSERT_TRUE(uio != NULL);
610 
611     // 0000 0001
612     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_READ), BSL_SUCCESS);
613     // 0000 0010
614     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_WRITE), BSL_SUCCESS);
615     // 0000 0100
616     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_IO_SPECIAL), BSL_SUCCESS);
617     // 0000 0111
618     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_RWS), BSL_SUCCESS);
619     // 0000 1000
620     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_SHOULD_RETRY), BSL_SUCCESS);
621     // 0001 0000
622     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_MEM_READ_ONLY), BSL_INVALID_ARG);
623     // 0010 0000
624     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_BASE64_NO_NEWLINE), BSL_SUCCESS);
625     // 0100 0000
626     ASSERT_EQ(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_BASE64_PEM), BSL_SUCCESS);
627     // 0111 1111
628     ASSERT_EQ(BSL_UIO_SetFlags(uio, 0b01111111), BSL_INVALID_ARG);
629     // 0110 1111
630     ASSERT_EQ(BSL_UIO_SetFlags(uio, 0b01101111), BSL_SUCCESS);
631     // 1110 1111
632     ASSERT_EQ(BSL_UIO_SetFlags(uio, 0b11101111), BSL_INVALID_ARG);
633 
634     ASSERT_EQ(BSL_UIO_SetFlags(uio, -1), BSL_INVALID_ARG);
635     ASSERT_EQ(BSL_UIO_SetFlags(uio, INT_MAX), BSL_INVALID_ARG);
636     ASSERT_EQ(BSL_UIO_SetFlags(uio, 0), BSL_INVALID_ARG);
637 
638 EXIT:
639     BSL_UIO_Free(uio);
640 }
641 /* END_CASE */
642 
643 /**
644  * @test  SDV_BSL_UIO_FLAGS_FUNC_TC002
645  * @title  UIO flags interface test
646  * @precon
647 * @brief
648 *    1. uio new. Expected result 1 is obtained.
649 *    2. Set two flags A and B. Expected result 2 is obtained.
650 *    3. Detection mark A. Expected result 3 is obtained.
651 *    4. Clear mark A. Expected result 4 is obtained.
652 *    5. Detection mark A. Expected result 5 is obtained.
653 *    6. Detection mark B. Expected result 6 is obtained.
654 * @expect
655 *    1. The success is not null.
656 *    2. Success
657 *    3. Successful and flag A detected
658 *    4. Success
659 *    5. Succeeded and Flag A Not Detected
660 *    6. Successful and Mark B detected
661  */
662 /* BEGIN_CASE */
SDV_BSL_UIO_FLAGS_FUNC_TC002(void)663 void SDV_BSL_UIO_FLAGS_FUNC_TC002(void)
664 {
665     BSL_UIO *uio = BSL_UIO_New(BSL_UIO_TcpMethod());
666     ASSERT_TRUE(uio != NULL);
667 
668     ASSERT_TRUE(BSL_UIO_SetFlags(uio, BSL_UIO_FLAGS_WRITE | BSL_UIO_FLAGS_SHOULD_RETRY) == BSL_SUCCESS);
669 
670     uint32_t out = 0;
671     ASSERT_TRUE(BSL_UIO_TestFlags(uio, BSL_UIO_FLAGS_WRITE, &out) == BSL_SUCCESS);
672     ASSERT_TRUE(out == BSL_UIO_FLAGS_WRITE);
673 
674     ASSERT_TRUE(BSL_UIO_ClearFlags(uio, BSL_UIO_FLAGS_WRITE) == BSL_SUCCESS);
675 
676     ASSERT_TRUE(BSL_UIO_TestFlags(uio, BSL_UIO_FLAGS_WRITE, &out) == BSL_SUCCESS);
677     ASSERT_TRUE(out == 0);
678 
679     ASSERT_TRUE(BSL_UIO_TestFlags(uio, BSL_UIO_FLAGS_SHOULD_RETRY, &out) == BSL_SUCCESS);
680     ASSERT_TRUE(out == BSL_UIO_FLAGS_SHOULD_RETRY);
681 EXIT:
682     BSL_UIO_Free(uio);
683 }
684 /* END_CASE */
685 
686 /**
687  * @test  SDV_BSL_UIO_UPREF_API_TC001
688  * @title  Input Parameter test
689  * @precon  nan
690  * @brief
691  *    1.The specified uio is empty, invoke UIO_UpRef. Expected result 1 is obtained.
692  *    2.Construct the uio object(BSL_UIO_New), invoke UIO_UpRef. Expected result 2 is obtained.
693  *    3.BSL_UIO_Free is invoked twice. Expected result 3 is obtained.
694  * @expect
695  *    1.Expected return failure
696  *    2.Expected success
697  *    3.It is expected that the program does not have code dump and no memory leakage occurs.
698  */
699 /* BEGIN_CASE */
SDV_BSL_UIO_UPREF_API_TC001(void)700 void SDV_BSL_UIO_UPREF_API_TC001(void)
701 {
702 #ifndef HITLS_BSL_UIO_SCTP
703     SKIP_TEST();
704 #else
705     BSL_UIO *uio = NULL;
706     /* The test UIO is empty. */
707     int32_t ret = BSL_UIO_UpRef(NULL);
708     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
709 
710     uio = BSL_UIO_New(BSL_UIO_SctpMethod());
711     ASSERT_TRUE(uio != NULL);
712 
713     ret = BSL_UIO_UpRef(uio);
714     ASSERT_TRUE(ret == BSL_SUCCESS);
715 
716 EXIT:
717     BSL_UIO_Free(uio);
718     BSL_UIO_Free(uio);
719 #endif
720 }
721 /* END_CASE */
722 
723 /**
724  * @test  SDV_BSL_UIO_WRITE_API_TC001
725  * @title  Input parameter test
726  * @precon  nan
727  * @brief
728  *    1. Call BSL_UIO_TcpMethod to create a tcp method. Expected result 1 is obtained.
729  *    2. The value of the input parameter UIO is NULL when BSL_UIO_Write is invoked. Expected result 2 is obtained.
730  *    3. The value of the input parameter data is NULL when BSL_UIO_Write is invoked. Expected result 2 is obtained.
731  *    4. The value of the input parameter data len is 0 when BSL_UIO_Write is invoked. Expected result 2 is obtained.
732  *    5. The value of the input parameter write len is NULL when BSL_UIO_Write is invoked. Expected result 2 is obtained.
733  * @expect
734  *    1. The TCP method is successfully created.
735  *    2. Return HITLS_INTERNAL_EXCEPTION
736  */
737 /* BEGIN_CASE */
SDV_BSL_UIO_WRITE_API_TC001(void)738 void SDV_BSL_UIO_WRITE_API_TC001(void)
739 {
740 #ifdef HITLS_BSL_UIO_TCP
741     BSL_UIO *uio = NULL;
742     uint8_t data[MAX_BUF_SIZE] = {0};
743     const uint32_t len = 1;
744     uint32_t writeLen;
745 
746     const BSL_UIO_Method *ori = BSL_UIO_TcpMethod();
747     BSL_UIO_Method method = {0};
748     memcpy(&method, ori, sizeof(method));
749     method.uioWrite = STUB_Write;
750     method.uioRead = STUB_Read;
751 
752     /* The test UIO is empty. */
753     int32_t ret = BSL_UIO_Write(NULL, data, len, &writeLen);
754     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
755 
756     uio = BSL_UIO_New(&method);
757     ASSERT_TRUE(uio != NULL);
758 
759     /* The test data is null. */
760     ret = BSL_UIO_Write(uio, NULL, len, &writeLen);
761     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
762 
763     /* Test the case when the write length is 0. */
764     ret = BSL_UIO_Write(uio, data, 0, &writeLen);
765     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
766 
767     /* Test that writeLen is NULL. */
768     ret = BSL_UIO_Write(uio, data, len, NULL);
769     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
770 EXIT:
771     BSL_UIO_Free(uio);
772 #else
773     SKIP_TEST();
774 #endif
775 }
776 /* END_CASE */
777 
778 /**
779  * @test  SDV_BSL_UIO_READ_API_TC001
780  * @title  Input parameter test
781  * @precon  nan
782  * @brief
783  *    1. Call BSL_UIO_TcpMethod to create a tcp method. Expected result 1 is obtained.
784  *    2. The value of the input parameter UIO is NULL when BSL_UIO_Read is invoked. Expected result 2 is obtained.
785  *    3. The value of the input parameter data is NULL when BSL_UIO_Read is invoked. Expected result 3 is obtained.
786  *    4. The value of the input parameter data len is 0 when BSL_UIO_Read is invoked. Expected result 4 is obtained.
787  *    5. The value of the input parameter write len is NULL when BSL_UIO_Read is invoked. Expected result 4 is obtained.
788  * @expect
789  *    1. The TCP method is successfully created.
790  *    2. Return BSL_INTERNAL_EXCEPTION
791  *    3. Return BSL_INTERNAL_EXCEPTION
792  *    4. Return BSL_INTERNAL_EXCEPTION
793  *    5. Return BSL_INTERNAL_EXCEPTION
794  */
795 /* BEGIN_CASE */
SDV_BSL_UIO_READ_API_TC001(void)796 void SDV_BSL_UIO_READ_API_TC001(void)
797 {
798 #ifdef HITLS_BSL_UIO_TCP
799     BSL_UIO *uio = NULL;
800     uint8_t data[MAX_BUF_SIZE] = {0};
801     const uint32_t len = 1;
802     uint32_t readLen;
803 
804     const BSL_UIO_Method *ori = BSL_UIO_TcpMethod();
805     BSL_UIO_Method method = {0};
806     memcpy(&method, ori, sizeof(method));
807     method.uioWrite = STUB_Write;
808     method.uioRead = STUB_Read;
809 
810     /* The test UIO is empty. */
811     int32_t ret = BSL_UIO_Read(NULL, data, len, &readLen);
812     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
813 
814     uio = BSL_UIO_New(&method);
815     ASSERT_TRUE(uio != NULL);
816 
817     /* The test data is null. */
818     ret = BSL_UIO_Read(uio, NULL, len, &readLen);
819     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
820 
821     /* Test the case when the write length is 0. */
822     ret = BSL_UIO_Read(uio, data, 0, &readLen);
823     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
824 
825     /* Test that writeLen is NULL. */
826     ret = BSL_UIO_Read(uio, data, len, NULL);
827     ASSERT_TRUE(ret == BSL_INTERNAL_EXCEPTION);
828 EXIT:
829     BSL_UIO_Free(uio);
830 #else
831     SKIP_TEST();
832 #endif
833 }
834 /* END_CASE */
835 
836 /**
837  * @test  SDV_BSL_UIO_SET_USERDATA_FREE_TC001
838  * @title  Set userData free test
839  * @precon  nan
840  * @brief
841  *    1. Call BSL_UIO_New to create a tcp uio. Expected result 1 is obtained.
842  *    2. Apply for space for userData. Expected result 2 is obtained.
843  *    3. Call BSL_UIO_SetUserData to set userData for UIO. Expected result 3 is obtained.
844  *    4. Call BSL_UIO_SetUserDataFreeFunc when uio is NULL. Expected result 4 is obtained.
845  *    5. Call BSL_UIO_SetUserDataFreeFunc when uio is not null. Expected result 5 is obtained.
846  * @expect
847  *    1. The TCP UIO is successfully created.
848  *    2. The value of userData is not empty.
849  *    3. Return BSL_SUCCESS and the userdata of the UIO is not empty.
850  *    4. Return BSL_NULL_INPUT
851  *    5. Return BSL_SUCCESS
852  */
853 /* BEGIN_CASE */
SDV_BSL_UIO_SET_USERDATA_FREE_TC001(void)854 void SDV_BSL_UIO_SET_USERDATA_FREE_TC001(void)
855 {
856     BSL_UIO *uio = BSL_UIO_New(BSL_UIO_TcpMethod());
857     ASSERT_TRUE(uio != NULL);
858     void *userData = BSL_SAL_Malloc(MAX_BUF_SIZE);
859     ASSERT_TRUE(userData != NULL);
860 
861     int32_t ret = BSL_UIO_SetUserData(uio, userData);
862     ASSERT_TRUE(ret == BSL_SUCCESS);
863     ASSERT_TRUE(uio->userData != NULL);
864 
865     ret = BSL_UIO_SetUserDataFreeFunc(NULL, BSL_SAL_Free);
866     ASSERT_TRUE(ret == BSL_NULL_INPUT);
867 
868     ret = BSL_UIO_SetUserDataFreeFunc(uio, BSL_SAL_Free);
869     ASSERT_TRUE(ret == BSL_SUCCESS);
870 EXIT:
871     BSL_UIO_Free(uio);
872 }
873 /* END_CASE */
874 
875 /**
876  * @test  SDV_BSL_UIO_GET_METHOD_TC001
877  * @title  Get uio method test
878  * @precon  nan
879  * @brief
880  *    1. Call BSL_UIO_New to create a tcp uio. Expected result 1 is obtained.
881  *    2. Call BSL_UIO_TcpMethod to create a tcp method. Call BSL_UIO_GetMethod to obtain the method of TCP_UIO.
882  *       Compare the two methods. Expected result 2 is obtained.
883  * @expect
884  *    1. The TCP UIO is successfully created.
885  *    2. The two methods are equal.
886  */
887 /* BEGIN_CASE */
SDV_BSL_UIO_GET_METHOD_TC001(void)888 void SDV_BSL_UIO_GET_METHOD_TC001(void)
889 {
890 #ifdef HITLS_BSL_UIO_TCP
891     const BSL_UIO_Method *ori = BSL_UIO_TcpMethod();
892     BSL_UIO *uio = BSL_UIO_New(ori);
893     ASSERT_TRUE(uio != NULL);
894 
895     const BSL_UIO_Method *method = BSL_UIO_GetMethod(uio);
896     int ret = memcmp(method, ori, sizeof(BSL_UIO_Method));
897     ASSERT_TRUE(ret == 0);
898 EXIT:
899     BSL_UIO_Free(uio);
900 #else
901     SKIP_TEST();
902 #endif
903 }
904 /* END_CASE */
905 
906 /**
907  * @test  SDV_BSL_UIO_GET_READANDWRITE_NUM_TC001
908  * @title  Get read and written num test
909  * @precon  nan
910  * @brief
911  *    1. Call BSL_UIO_TcpMethod to create a tcp method. Expected result 1 is obtained.
912  *    2. Call BSL_UIO_New to create a tcp uio. Expected result 2 is obtained.
913  *    3. Call BSL_UIO_Ctrl and transfer the BSL_UIO_GET_WRITE_NUM parameter to obtain the number of written
914  *       bytes. Expected result 3 is obtained.
915  *    4. Call BSL_UIO_Ctrl and transfer the BSL_UIO_GET_READ_NUM parameter to obtain the number of read bytes.
916  *       Expected result 4 is obtained.
917  * @expect
918  *    1. The TCP METHOD is successfully created.
919  *    2. The TCP UIO is successfully created.
920  *    3. The value of writeNum is the same as the number of written bytes.
921  *    4. The value of readNum is the same as the number of read bytes.
922  */
923 /* BEGIN_CASE */
SDV_BSL_UIO_GET_READANDWRITE_NUM_TC001(void)924 void SDV_BSL_UIO_GET_READANDWRITE_NUM_TC001(void)
925 {
926 #ifdef HITLS_BSL_UIO_TCP
927     BSL_UIO *uio = NULL;
928     uint8_t data[10] = {'0', '1', '2', '3', '4'};
929     uint8_t readBuf[10] = {0};
930     const uint32_t dataLen = 5;
931     uint32_t writeLen = 0;
932     uint32_t readLen = 0;
933     int64_t writeNum = 0;
934     int64_t readNum = 0;
935 
936     const BSL_UIO_Method *ori = BSL_UIO_TcpMethod();
937     BSL_UIO_Method method = {0};
938     memcpy(&method, ori, sizeof(method));
939     method.uioWrite = STUB_Write;
940     method.uioRead = STUB_Read;
941 
942     uio = BSL_UIO_New(&method);
943     ASSERT_TRUE(uio != NULL);
944     BSL_UIO_SetInit(uio, 1);
945 
946     ASSERT_TRUE(BSL_UIO_Write(uio, data, dataLen, &writeLen) == BSL_SUCCESS);
947     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_UIO_GET_WRITE_NUM, (int32_t)sizeof(writeNum), &writeNum), BSL_SUCCESS);
948     ASSERT_EQ(writeNum, writeLen);
949 
950     ASSERT_TRUE(BSL_UIO_Read(uio, readBuf, dataLen, &readLen) == BSL_SUCCESS);
951     ASSERT_EQ(BSL_UIO_Ctrl(uio, BSL_UIO_GET_READ_NUM, (int32_t)sizeof(readNum), &readNum), BSL_SUCCESS);
952     ASSERT_EQ(readNum, readLen);
953 EXIT:
954     BSL_UIO_Free(uio);
955 #else
956     SKIP_TEST();
957 #endif
958 }
959 /* END_CASE */
960 
961 /**
962  * @test  SDV_BSL_UIO_SET_FD_TC001
963  * @title  Set fd test
964  * @precon  nan
965  * @brief
966  *    1. Call BSL_UIO_New to create a tcp uio. Expected result 1 is obtained.
967  *    2. Call BSL_UIO_SetFD to set fd to uio. Call BSL_UIO_Ctrl and transfer the BSL_UIO_GET_FD parameter to obtain
968  *       the fd1 of uio. Compare the two fds. Expected result 2 is obtained.
969  * @expect
970  *    1. The TCP UIO is successfully created.
971  *    2. The two fds are equal.
972  */
973 /* BEGIN_CASE */
SDV_BSL_UIO_SET_FD_TC001(void)974 void SDV_BSL_UIO_SET_FD_TC001(void)
975 {
976     char *filename = "fd_test_file.txt";
977     int fd = open(filename, O_RDWR | O_CREAT, 0060);
978     const char *data = "012345678\nabcdef";
979     write(fd, data, strlen(data));
980     BSL_UIO *uio = BSL_UIO_New(BSL_UIO_TcpMethod());
981     ASSERT_TRUE(uio != NULL);
982     BSL_UIO_SetIsUnderlyingClosedByUio(uio, true);
983 
984     BSL_UIO_SetFD(uio, fd);
985     int32_t fd1 = -1;
986     ASSERT_TRUE(BSL_UIO_Ctrl(uio, BSL_UIO_GET_FD, (int32_t)sizeof(fd1), &fd1) == BSL_SUCCESS);
987     ASSERT_TRUE(fd == fd1);
988 EXIT:
989     BSL_UIO_Free(uio);
990     remove(filename);
991 }
992 /* END_CASE */
993 
994 /**
995  * @test  SDV_BSL_UIO_NEXT_TC001
996  * @title  Uio next test
997  * @precon  nan
998  * @brief
999  *    1. Call BSL_UIO_New to create a tcp uio named tcp1. Expected result 1 is obtained.
1000  *    2. Call BSL_UIO_New to create a tcp uio named tcp2. Expected result 2 is obtained.
1001  *    3. Append tcp2 to tcp1. Expected result 3 is obtained.
1002  *    4. Check the next uio of tcp1 and the next uio of tcp2. Expected result 4 is obtained.
1003  * @expect
1004  *    1. The tcp1 is successfully created.
1005  *    2. The tcp2 is successfully created.
1006  *    3. Return BSL_SUCCESS.
1007  *    4. The next uio of tcp1 is tcp2 and the next uio of tcp2 is NULL.
1008  */
1009 /* BEGIN_CASE */
SDV_BSL_UIO_NEXT_TC001(void)1010 void SDV_BSL_UIO_NEXT_TC001(void)
1011 {
1012     BSL_UIO *tcp1 = BSL_UIO_New(BSL_UIO_TcpMethod());
1013     ASSERT_TRUE(tcp1 != NULL);
1014     BSL_UIO *tcp2 = BSL_UIO_New(BSL_UIO_TcpMethod());
1015     ASSERT_TRUE(tcp2 != NULL);
1016 
1017     ASSERT_TRUE(BSL_UIO_Append(tcp1, tcp2) == BSL_SUCCESS);
1018     ASSERT_TRUE(BSL_UIO_Next(tcp1) == tcp2);
1019     ASSERT_TRUE(BSL_UIO_Next(tcp2) == NULL);
1020 EXIT:
1021     BSL_UIO_Free(tcp1);
1022     BSL_UIO_Free(tcp2);
1023 }
1024 /* END_CASE */
1025 
1026 typedef union {
1027     struct sockaddr addr;
1028     struct sockaddr_in6 addrIn6;
1029     struct sockaddr_in addrIn;
1030     struct sockaddr_un addrUn;
1031 } UIO_Addr;
1032 /**
1033  * @test  SDV_BSL_UIO_UDP_API_TC001
1034  * @title  UDP ctrl test
1035  * @precon  nan
1036  * @brief
1037  *    1. Call BSL_UIO_UdpMethod to create a UDP method. Expected result 1 is obtained.
1038  *    2. The input cmd is BSL_UIO_SET_PEER_IP_ADDR when BSL_UIO_Ctrl is invoked. Expected result 2 is obtained.
1039  *    3. The input cmd is BSL_UIO_GET_PEER_IP_ADDR when BSL_UIO_Ctrl is invoked. Expected result 3 is obtained.
1040  *    4. The input cmd is BSL_UIO_UDP_SET_CONNECTED when BSL_UIO_Ctrl is invoked. Expected result 3 is obtained.
1041  * @expect
1042  *    1. The UDP method is successfully created.
1043  *    2. Return BSL_SUCCESS
1044  *    3. Return BSL_SUCCESS
1045  */
1046 /* BEGIN_CASE */
SDV_BSL_UIO_UDP_API_TC001(void)1047 void SDV_BSL_UIO_UDP_API_TC001(void)
1048 {
1049     BSL_UIO *uio = NULL;
1050     int ret;
1051     UIO_Addr peerAddr = { 0 };
1052     uint8_t ipv4[IP_V4_LEN] = {0x11, 0x22, 0x33, 0x44};
1053     peerAddr.addr.sa_family = AF_INET;
1054     ASSERT_TRUE(memcpy_s(peerAddr.addr.sa_data, sizeof(UIO_Addr), ipv4, IP_V4_LEN) == EOK);
1055 
1056     const BSL_UIO_Method *ori = BSL_UIO_UdpMethod();
1057     BSL_UIO_Method method = {0};
1058     memcpy_s(&method, sizeof(method), ori, sizeof(method));
1059     method.uioWrite = STUB_Write;
1060     method.uioRead = STUB_Read;
1061 
1062     uio = BSL_UIO_New(&method);
1063     ASSERT_TRUE(uio != NULL);
1064 
1065     ret = BSL_UIO_Ctrl(uio, BSL_UIO_SET_PEER_IP_ADDR, sizeof(peerAddr), &peerAddr);
1066     ASSERT_TRUE(ret == BSL_SUCCESS);
1067 
1068     UIO_Addr getAddr = { 0 };
1069     ret = BSL_UIO_Ctrl(uio, BSL_UIO_GET_PEER_IP_ADDR, sizeof(getAddr), &getAddr);
1070     ASSERT_TRUE(ret == BSL_SUCCESS);
1071 
1072     ASSERT_TRUE(memcmp(getAddr.addr.sa_data, peerAddr.addr.sa_data, IP_V4_LEN) == 0);
1073 
1074     ret = BSL_UIO_Ctrl(uio, BSL_UIO_UDP_SET_CONNECTED, sizeof(peerAddr.addr), &peerAddr);
1075     ASSERT_TRUE(ret == BSL_SUCCESS);
1076 
1077     ret = BSL_UIO_Ctrl(uio, BSL_UIO_UDP_SET_CONNECTED, 0, NULL);
1078     ASSERT_TRUE(ret == BSL_SUCCESS);
1079 EXIT:
1080     BSL_UIO_Free(uio);
1081 }
1082 /* END_CASE */
1083 
1084 /**
1085  * @test  SDV_BSL_UIO_SCTP_API_TC001
1086  * @title  SCTP ctrl test
1087  * @precon  nan
1088  * @brief
1089  *    1. Call BSL_UIO_SctpMethod to create a SCTP method. Expected result 1 is obtained.
1090  *    2. The input cmd is BSL_UIO_SET_PEER_IP_ADDR when BSL_UIO_Ctrl is invoked. Expected result 2 is obtained.
1091  *    3. The input cmd is BSL_UIO_GET_PEER_IP_ADDR when BSL_UIO_Ctrl is invoked. Expected result 3 is obtained.
1092  *    4. The input cmd is BSL_UIO_SCTP_SET_APP_STREAM_ID when BSL_UIO_Ctrl is invoked. Expected result 4 is obtained.
1093  * @expect
1094  *    1. The SCTP method is successfully created.
1095  *    2. Return BSL_SUCCESS
1096  *    3. Return BSL_SUCCESS
1097  *    4. Return BSL_SUCCESS
1098  */
1099 /* BEGIN_CASE */
SDV_BSL_UIO_SCTP_API_TC001(void)1100 void SDV_BSL_UIO_SCTP_API_TC001(void)
1101 {
1102 #ifndef HITLS_BSL_UIO_SCTP
1103     SKIP_TEST();
1104 #else
1105     BSL_UIO *uio = NULL;
1106     int ret;
1107     uint8_t ipAddr[256] = {0};
1108     BSL_UIO_CtrlGetPeerIpAddrParam param = {ipAddr, sizeof(ipAddr)};
1109     uint8_t data[IP_ADDR_V4_LEN] = {0};
1110     uint16_t sendAppStreamId = 1;
1111 
1112     const BSL_UIO_Method *ori = BSL_UIO_SctpMethod();
1113     BSL_UIO_Method method = {0};
1114     memcpy(&method, ori, sizeof(method));
1115     method.uioWrite = STUB_Write;
1116     method.uioRead = STUB_Read;
1117 
1118     uio = BSL_UIO_New(&method);
1119     ASSERT_TRUE(uio != NULL);
1120 
1121     ret = BSL_UIO_Ctrl(uio, BSL_UIO_SET_PEER_IP_ADDR, sizeof(data), data);
1122     ASSERT_TRUE(ret == BSL_SUCCESS);
1123 
1124     ret = BSL_UIO_Ctrl(uio, BSL_UIO_GET_PEER_IP_ADDR, sizeof(param), &param);
1125     ASSERT_TRUE(ret == BSL_SUCCESS);
1126 
1127     ret = BSL_UIO_Ctrl(uio, BSL_UIO_SCTP_SET_APP_STREAM_ID, sizeof(uint16_t), &sendAppStreamId);
1128     ASSERT_TRUE(ret == BSL_SUCCESS);
1129 EXIT:
1130     BSL_UIO_Free(uio);
1131 #endif
1132 }
1133 /* END_CASE */
1134 
1135 /**
1136  * @test  SDV_BSL_UIO_BUFFER_RESET_TC001
1137  * @title  Buffer reset test
1138  * @precon  nan
1139  * @brief
1140  *    1. Call BSL_UIO_New to create a buffer uio named buffer. Expected result 1 is obtained.
1141  *    2. Call BSL_UIO_New to create a tcp uio named tcp. Expected result 2 is obtained.
1142  *    3. Append tcp to buffer. Expected result 3 is obtained.
1143  *    4. Write 2048-length data to the buffer uio. Expected result 4 is obtained.
1144  *    5. Call BSL_UIO_Ctrl and transfer the BSL_UIO_RESET parameter to reset the buffer uio. Expected result 5 is
1145  *       obtained.
1146  * @expect
1147  *    1. The buffer is successfully created.
1148  *    2. The tcp is successfully created.
1149  *    3. Return BSL_SUCCESS and the next uio of buffer is tcp.
1150  *    4. Return BSL_SUCCESS and writeLen is 2048.
1151  *    5. Return BSL_UIO_FAIL.
1152  */
1153 /* BEGIN_CASE */
SDV_BSL_UIO_BUFFER_RESET_TC001(void)1154 void SDV_BSL_UIO_BUFFER_RESET_TC001(void)
1155 {
1156     BSL_UIO *buffer = BSL_UIO_New(BSL_UIO_BufferMethod());
1157     ASSERT_TRUE(buffer != NULL);
1158     BSL_UIO *tcp = BSL_UIO_New(BSL_UIO_TcpMethod());
1159     ASSERT_TRUE(tcp != NULL);
1160 
1161     int32_t ret = BSL_UIO_Append(buffer, tcp);
1162     ASSERT_TRUE(ret == BSL_SUCCESS);
1163     ASSERT_TRUE(BSL_UIO_Next(buffer) == tcp);
1164     ASSERT_TRUE(BSL_UIO_Next(tcp) == NULL);
1165 
1166     uint8_t buf[8192];
1167     uint32_t writeLen = 0;
1168     ret = BSL_UIO_Write(buffer, buf, 2048, &writeLen);
1169     ASSERT_TRUE(ret == BSL_SUCCESS);
1170     ASSERT_TRUE(writeLen == 2048);
1171 
1172     ret = BSL_UIO_Ctrl(buffer, BSL_UIO_RESET, 0, NULL);
1173     ASSERT_TRUE(ret = BSL_UIO_FAIL);
1174 EXIT:
1175     BSL_UIO_Free(buffer);
1176     BSL_UIO_Free(tcp);
1177 }
1178 /* END_CASE */
1179 
1180 /* BEGIN_CASE */
SDV_BSL_UIO_MEM_BASIC_TC001(void)1181 void SDV_BSL_UIO_MEM_BASIC_TC001(void)
1182 {
1183     TestMemInit();
1184 
1185     // Create memory UIO
1186     BSL_UIO *uio = BSL_UIO_New(BSL_UIO_MemMethod());
1187     ASSERT_TRUE(uio != NULL);
1188 
1189     // Test write operation
1190     const char testData[] = "Hello World";
1191     uint32_t writeLen = 0;
1192     int32_t ret = BSL_UIO_Write(uio, testData, strlen(testData), &writeLen);
1193     ASSERT_TRUE(ret == BSL_SUCCESS);
1194     ASSERT_TRUE(writeLen == strlen(testData));
1195 
1196     // Test read operation
1197     char readBuf[20] = {0};
1198     uint32_t readLen = 0;
1199     ret = BSL_UIO_Read(uio, readBuf, sizeof(readBuf), &readLen);
1200     ASSERT_TRUE(ret == BSL_SUCCESS);
1201     ASSERT_TRUE(readLen == strlen(testData));
1202     ASSERT_TRUE(memcmp(readBuf, testData, readLen) == 0);
1203 
1204     // Test pending data length
1205     int64_t pendingLen = 0;
1206     ret = BSL_UIO_Ctrl(uio, BSL_UIO_PENDING, sizeof(size_t), &pendingLen);
1207     ASSERT_TRUE(ret == BSL_SUCCESS);
1208     ASSERT_TRUE(pendingLen == 0); // All data has been read
1209 
1210 EXIT:
1211     BSL_UIO_Free(uio);
1212 }
1213 /* END_CASE */
1214 
1215 /* BEGIN_CASE */
SDV_BSL_UIO_MEM_NEW_BUF_TC001(void)1216 void SDV_BSL_UIO_MEM_NEW_BUF_TC001(void)
1217 {
1218     TestMemInit();
1219 
1220     BSL_UIO *uio = BSL_UIO_New(BSL_UIO_MemMethod());
1221     ASSERT_TRUE(uio != NULL);
1222 
1223     // Test MemNewBuf with invalid parameters
1224     int32_t ret = BSL_UIO_Ctrl(uio, BSL_UIO_MEM_NEW_BUF, -1, NULL);
1225     ASSERT_TRUE(ret == BSL_NULL_INPUT);
1226 
1227     // Test MemNewBuf with valid parameters
1228     char testBuf[] = "Test Buffer";
1229     ret = BSL_UIO_Ctrl(uio, BSL_UIO_MEM_NEW_BUF, strlen(testBuf), testBuf);
1230     ASSERT_TRUE(ret == BSL_SUCCESS);
1231 
1232     // Verify buffer is in read-only mode
1233     uint32_t writeLen = 0;
1234     ret = BSL_UIO_Write(uio, "data", 4, &writeLen);
1235     ASSERT_TRUE(ret == BSL_UIO_WRITE_NOT_ALLOWED);
1236 
1237     // Test reading from new buffer
1238     char readBuf[20] = {0};
1239     uint32_t readLen = 0;
1240     ret = BSL_UIO_Read(uio, readBuf, sizeof(readBuf), &readLen);
1241     ASSERT_TRUE(ret == BSL_SUCCESS);
1242     ASSERT_TRUE(readLen == strlen(testBuf));
1243     ASSERT_TRUE(memcmp(readBuf, testBuf, readLen) == 0);
1244 EXIT:
1245     BSL_UIO_Free(uio);
1246 }
1247 /* END_CASE */
1248 
1249 /* BEGIN_CASE */
SDV_BSL_UIO_MEM_EOF_TC001(void)1250 void SDV_BSL_UIO_MEM_EOF_TC001(void)
1251 {
1252     TestMemInit();
1253 
1254     BSL_UIO *uio = BSL_UIO_New(BSL_UIO_MemMethod());
1255     ASSERT_TRUE(uio != NULL);
1256 
1257     // Test setting EOF behavior
1258     int32_t eofValue = 1;
1259     int32_t ret = BSL_UIO_Ctrl(uio, BSL_UIO_MEM_SET_EOF, sizeof(int32_t), &eofValue);
1260     ASSERT_TRUE(ret == BSL_SUCCESS);
1261 
1262     // Verify EOF value
1263     int32_t readEof = 0;
1264     ret = BSL_UIO_Ctrl(uio, BSL_UIO_MEM_GET_EOF, sizeof(int32_t), &readEof);
1265     ASSERT_TRUE(ret == BSL_SUCCESS);
1266     ASSERT_TRUE(readEof == eofValue);
1267 
1268     // Test read behavior with EOF set
1269     char readBuf[10];
1270     uint32_t readLen = 0;
1271     ret = BSL_UIO_Read(uio, readBuf, sizeof(readBuf), &readLen);
1272     ASSERT_TRUE(ret == BSL_SUCCESS);
1273     ASSERT_TRUE(readLen == 0);
1274 
1275     // Verify retry flag is set due to EOF
1276     uint32_t flags = 0;
1277     BSL_UIO_TestFlags(uio, BSL_UIO_FLAGS_SHOULD_RETRY, &flags);
1278     ASSERT_TRUE((flags & BSL_UIO_FLAGS_SHOULD_RETRY) != 0);
1279 
1280 EXIT:
1281     BSL_UIO_Free(uio);
1282 }
1283 /* END_CASE */
1284