lwip_func_test.c 36.1 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "hctest.h"
#include "lwip/sockets.h"
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "securec.h"
#include "cmsis_os2.h"
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
M
mamingshuai 已提交
26
#include <stdio.h>
W
wenjun 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40

#define LOCAL_HOST "127.0.0.1"
#define STACK_PORT 2277
#define STACK_IP LOCAL_HOST
#define PEER_PORT STACK_PORT
#define PEER_IP LOCAL_HOST
#define UDP_MSG "Hi, I am UDP"
#define SRV_MSG "Hi, I am TCP server"
#define CLI_MSG "Hi, I am TCP client"
#define BUF_SIZE (1024 * 8)
#define LWIP_TEST_SUCCESS 0
#define LWIP_TEST_FAIL (-1)
#define DEF_TASK_STACK 2000
#define DEF_TASK_PRIORITY 20
41
#define ONE_SECOND 1
W
wenjun 已提交
42
#define TIMEOUT 4
M
mamingshuai 已提交
43
#define TEST_FD_COUNT 10
W
wenjun 已提交
44 45 46 47 48 49 50 51

static char g_buf[BUF_SIZE + 1] = {0};
static int g_clientWait = 0;
static int g_clientWaitOver = 0;
static int g_serverWait = 0;
static int g_serverWaitOver = 0;
static int g_clientResult = 0;
static int g_serverResult = 0;
M
mamingshuai 已提交
52 53 54
static int g_selectResult = 0;
static int g_selectFlag = 1;
static int g_selectTimeout = 2;
W
wenjun 已提交
55

M
mamingshuai 已提交
56
static void WaitClient(void)
W
wenjun 已提交
57 58
{
    while (1) {
59
        sleep(ONE_SECOND);
W
wenjun 已提交
60 61 62 63 64 65 66 67
        if (g_clientWait) {
            break;
        }
    }
    g_clientWaitOver = 1;
    g_clientWait = 0;
}

M
mamingshuai 已提交
68
static void WaitServer(void)
W
wenjun 已提交
69 70
{
    while (1) {
71
        sleep(ONE_SECOND);
W
wenjun 已提交
72 73 74 75 76 77 78 79
        if (g_serverWait) {
            break;
        }
    }
    g_serverWaitOver = 1;
    g_serverWait = 0;
}

M
mamingshuai 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static int CommInitTcpServer(int port)
{
    int srvFd = socket(AF_INET, SOCK_STREAM, 0);
    if (srvFd == -1) {
        printf("CommInitTcpServer]socket fail!, errinfo[%s]\n", strerror(errno));
        return -1;
    }
    int flag = 1;
    int ret = setsockopt(srvFd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
    if (ret != 0) {
        printf("[CommInitTcpServer]setsockopt fail, ret[%d]!\n", ret);
    }
    struct sockaddr_in srvAddr = {0};
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(LOCAL_HOST);
    srvAddr.sin_port = htons(port);
    ret = bind(srvFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    if (ret != 0) {
        printf("[CommInitTcpServer]::bind fail!\n");
        lwip_close(srvFd);
        return -1;
    }
    int backlog = 5;
    ret = listen(srvFd, backlog);
    if (ret != 0) {
        printf("[CommInitTcpServer]listen fail!\n");
        lwip_close(srvFd);
        return -1;
    }
    printf("[CommInitTcpServer]success,Fd[%d]\n", srvFd);
    return srvFd;
}

static void CommTcpClientTask(void)
{
    int clnFd = socket(AF_INET, SOCK_STREAM, 0);
    TEST_ASSERT_NOT_EQUAL(-1, clnFd);
    if (clnFd == -1) {
        printf("[comm client]socket fail, errinfo[%s]\n", strerror(errno));
        return;
    }
    struct sockaddr_in srvAddr = {0};
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(LOCAL_HOST);
    srvAddr.sin_port = htons(PEER_PORT+1);
    int rst = connect(clnFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    TEST_ASSERT_EQUAL_INT(0, rst);
    if (rst != 0) {
        printf("[comm client]connect fail\n");
        lwip_close(clnFd);
        return;
    }
    int count = 4;
    char dataBuf[50] = {0};
    char sendMsgList[5][50] = {"Hi, I'm client,FD:[%d]", "client:123456789abcdefg,FD:[%d]",
        "client:!!@@##$$%%^^&&**(()),FD:[%d]", "client:(((112233445566778899))),FD:[%d]", "bye"};
    for (int i = 0; i < count; i++) {
137
        sleep(ONE_SECOND);
M
mamingshuai 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
        rst = sprintf_s(dataBuf, sizeof(dataBuf), sendMsgList[i], clnFd);
        if (rst < 0) {
            printf("[comm client]sprintf_s error!\n");
            continue;
        }
        rst = send(clnFd, dataBuf, strlen(dataBuf), 0);
        TEST_ASSERT_TRUE(rst > 0);
        if (rst <= 0) {
            printf("[comm client][%d]send fail\n", clnFd);
        }
        memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
        rst = recv(clnFd, dataBuf, sizeof(dataBuf), 0);
        if (rst > 0) {
            printf("[comm client][%d]recv msg[%s]\n", clnFd, dataBuf);
        } else {
            printf("[comm client][%d]recv no data\n", clnFd);
        }
    }
    // send 'bye', finish
    rst = send(clnFd, sendMsgList[count], strlen(sendMsgList[count]), 0);
    TEST_ASSERT_TRUE(rst > 0);
    if (rst <= 0) {
        printf("[comm client][%d]send fail\n", clnFd);
    }
    rst = lwip_close(clnFd);
    TEST_ASSERT_EQUAL_INT(0, rst);
    if (rst != 0) {
        printf("[comm client][%d]close fd fail\n", clnFd);
    }
}

W
wenjun 已提交
170 171 172 173 174 175 176 177 178
static void SampleTcpServerTask(void)
{
    int ret;
    struct msghdr msg = {0};
    struct iovec iov[2] = {};
    struct sockaddr_in clnAddr = {0};
    socklen_t clnAddrLen = sizeof(clnAddr);
    static char buf[BUF_SIZE + 1] = {0};

M
mamingshuai 已提交
179 180
    int srvFd = CommInitTcpServer(STACK_PORT);
    TEST_ASSERT_NOT_EQUAL(-1, srvFd);
W
wenjun 已提交
181

M
mamingshuai 已提交
182 183
    WaitServer();
    int clientFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
W
wenjun 已提交
184
    printf("[tcp server]accept <%s:%d>\n", inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
M
mamingshuai 已提交
185
    TEST_ASSERT_NOT_EQUAL(LWIP_TEST_FAIL, clientFd);
W
wenjun 已提交
186 187 188 189

    int srvMsgLen = strlen(SRV_MSG);
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, SRV_MSG);
M
mamingshuai 已提交
190 191
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
    ret = send(clientFd, buf, srvMsgLen, 0);
W
wenjun 已提交
192
    printf("[tcp server]send, ret=%d\n", ret);
M
mamingshuai 已提交
193
    TEST_ASSERT_EQUAL_INT(srvMsgLen, ret);
W
wenjun 已提交
194 195

    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
196
    ret = recv(clientFd, buf, sizeof(buf), 0);
W
wenjun 已提交
197
    printf("[tcp server]recv, ret=%d\n", ret);
M
mamingshuai 已提交
198
    TEST_ASSERT_EQUAL_INT(strlen(CLI_MSG), ret);
W
wenjun 已提交
199

M
mamingshuai 已提交
200
    WaitServer();
W
wenjun 已提交
201 202 203 204 205 206
    int len = 2;
    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
    clnAddr.sin_port = htons(PEER_PORT);
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, SRV_MSG);
M
mamingshuai 已提交
207
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
W
wenjun 已提交
208 209 210 211 212 213 214 215
    msg.msg_name = &clnAddr;
    msg.msg_namelen = sizeof(clnAddr);
    msg.msg_iov = iov;
    msg.msg_iovlen = len;
    iov[0].iov_base = buf;
    iov[0].iov_len = srvMsgLen;
    iov[1].iov_base = buf;
    iov[1].iov_len = srvMsgLen;
M
mamingshuai 已提交
216
    ret = sendmsg(clientFd, &msg, 0);
W
wenjun 已提交
217
    printf("[tcp server]sendmsg, ret=%d\n", ret);
M
mamingshuai 已提交
218
    TEST_ASSERT_EQUAL_INT(len * srvMsgLen, ret);
W
wenjun 已提交
219

M
mamingshuai 已提交
220
    WaitServer();
W
wenjun 已提交
221
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
222
    int recvSum = 0;
223 224
    int recvTimes = 50;
    while (recvTimes > 0) {
M
mamingshuai 已提交
225
        ret = recv(clientFd, buf, sizeof(buf), 0);
226
        recvSum += ret;
M
mamingshuai 已提交
227
        if ((unsigned int)recvSum == (len * strlen(SRV_MSG)) || ret <= 0) {
228 229 230 231 232 233
            break;
        }
        recvTimes--;
    }
    printf("[tcp server]recv, recvSum=%d,recvTimes=%d\n", recvSum, recvTimes);
    TEST_ASSERT_EQUAL_INT(len * strlen(CLI_MSG), recvSum);
W
wenjun 已提交
234

M
mamingshuai 已提交
235
    ret = shutdown(clientFd, SHUT_RDWR);
W
wenjun 已提交
236
    printf("[tcp server]shutdown, ret=%d\n", ret);
M
mamingshuai 已提交
237
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
W
wenjun 已提交
238

M
mamingshuai 已提交
239 240
    lwip_close(clientFd);
    lwip_close(srvFd);
W
wenjun 已提交
241
    g_serverResult = 1;
M
mamingshuai 已提交
242
    WaitServer();
W
wenjun 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255
}

static void SampleTcpClientTask(void)
{
    int ret;
    struct msghdr msg = {0};
    struct iovec iov[2] = {};
    struct sockaddr addr;
    socklen_t addrLen = sizeof(addr);
    struct sockaddr_in srvAddr = {0};
    struct sockaddr_in clnAddr = {0};
    static char buf[BUF_SIZE+1] = {0};

M
mamingshuai 已提交
256 257 258 259 260
    int clientFd = socket(AF_INET, SOCK_STREAM, 0);
    TEST_ASSERT_NOT_EQUAL(LWIP_TEST_FAIL, clientFd);
    if (clientFd == -1) {
        printf("[tcp client]socket fail, errinfo[%s]\n", strerror(errno));
    }
W
wenjun 已提交
261

M
mamingshuai 已提交
262
    WaitClient();
W
wenjun 已提交
263 264 265
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(PEER_IP);
    srvAddr.sin_port = htons(PEER_PORT);
M
mamingshuai 已提交
266
    ret = connect(clientFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
W
wenjun 已提交
267
    printf("[tcp client]connect %s:%d, ret=%d\n", inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), ret);
M
mamingshuai 已提交
268
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
W
wenjun 已提交
269

M
mamingshuai 已提交
270
    ret = getpeername(clientFd, &addr, &addrLen);
W
wenjun 已提交
271 272
    printf("[tcp client]getpeername %s:%d, ret=%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
        ntohs(((struct sockaddr_in*)&addr)->sin_port), ret);
M
mamingshuai 已提交
273 274 275
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
    TEST_ASSERT_EQUAL_INT(sizeof(struct sockaddr_in), addrLen);
    TEST_ASSERT_EQUAL_INT(inet_addr(PEER_IP), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
W
wenjun 已提交
276

M
mamingshuai 已提交
277
    ret = getsockname(clientFd, &addr, &addrLen);
W
wenjun 已提交
278 279
    printf("[tcp client]getsockname %s:%d, ret=%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
        ntohs(((struct sockaddr_in*)&addr)->sin_port), ret);
M
mamingshuai 已提交
280 281 282
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
    TEST_ASSERT_EQUAL_INT(sizeof(struct sockaddr_in), addrLen);
    TEST_ASSERT_EQUAL_INT(inet_addr(STACK_IP), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
W
wenjun 已提交
283 284 285 286

    int cliMsgLen = strlen(CLI_MSG);
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, CLI_MSG);
M
mamingshuai 已提交
287 288
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
    ret = send(clientFd, buf, cliMsgLen, 0);
W
wenjun 已提交
289
    printf("[tcp client]send, ret=%d\n", ret);
M
mamingshuai 已提交
290
    TEST_ASSERT_EQUAL_INT(cliMsgLen, ret);
W
wenjun 已提交
291 292

    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
293
    ret = recv(clientFd, buf, sizeof(buf), 0);
W
wenjun 已提交
294
    printf("[tcp client]recv,ret=%d\n", ret);
M
mamingshuai 已提交
295
    TEST_ASSERT_EQUAL_INT(strlen(SRV_MSG), ret);
W
wenjun 已提交
296

M
mamingshuai 已提交
297
    WaitClient();
W
wenjun 已提交
298 299 300 301 302 303
    int len = 2;
    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
    clnAddr.sin_port = htons(PEER_PORT);
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, CLI_MSG);
M
mamingshuai 已提交
304
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
W
wenjun 已提交
305 306 307 308 309 310 311 312
    msg.msg_name = &clnAddr;
    msg.msg_namelen = sizeof(clnAddr);
    msg.msg_iov = iov;
    msg.msg_iovlen = len;
    iov[0].iov_base = buf;
    iov[0].iov_len = cliMsgLen;
    iov[1].iov_base = buf;
    iov[1].iov_len = cliMsgLen;
M
mamingshuai 已提交
313
    ret = sendmsg(clientFd, &msg, 0);
W
wenjun 已提交
314
    printf("[tcp client]sendmsg, ret=%d\n", ret);
M
mamingshuai 已提交
315
    TEST_ASSERT_EQUAL_INT(len * cliMsgLen, ret);
W
wenjun 已提交
316

M
mamingshuai 已提交
317
    WaitClient();
W
wenjun 已提交
318
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
319
    int recvSum = 0;
320 321
    int recvTimes = 50;
    while (recvTimes > 0) {
M
mamingshuai 已提交
322
        ret = recv(clientFd, buf, sizeof(buf), 0);
323
        recvSum += ret;
M
mamingshuai 已提交
324
        if ((unsigned int)recvSum == (len * strlen(SRV_MSG)) || ret <= 0) {
325 326 327 328 329 330
            break;
        }
        recvTimes--;
    }
    printf("[tcp client]recv, recvSum=%d,recvTimes=%d\n", recvSum, recvTimes);
    TEST_ASSERT_EQUAL_INT(len * strlen(SRV_MSG), recvSum);
W
wenjun 已提交
331

M
mamingshuai 已提交
332
    ret = shutdown(clientFd, SHUT_RDWR);
W
wenjun 已提交
333
    printf("[tcp client]shutdown, ret=%d\n", ret);
M
mamingshuai 已提交
334
    TEST_ASSERT_EQUAL_INT(0, ret);
W
wenjun 已提交
335

M
mamingshuai 已提交
336
    lwip_close(clientFd);
W
wenjun 已提交
337
    g_clientResult = 1;
M
mamingshuai 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    WaitClient();
}

static void SelectServerTask(void)
{
    int srvFd = CommInitTcpServer(STACK_PORT + 1);
    if (srvFd == -1) {
        g_selectResult = -1;
        g_selectFlag = 0;
        return;
    }
    int ret;
    int cliCount = 0;
    int maxFd = srvFd;
    fd_set readSet;
    char dataBuf[50] = {0};
    int fds[TEST_FD_COUNT] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
    fds[0] = srvFd;
    struct timeval timev = {
        .tv_sec = g_selectTimeout,
        .tv_usec = 0
    };
    struct sockaddr_in clnAddr = {0};
    socklen_t clnAddrLen = sizeof(clnAddr);
    while (1) {
        FD_ZERO(&readSet);
        for (int i = 0; i < TEST_FD_COUNT; i++) {
            if (fds[i] != -1) {
                FD_SET(fds[i], &readSet);
                if (maxFd < fds[i]) {
                    maxFd = fds[i];
                }
                printf("[select process]fd info[%d:%d]\n", i, fds[i]);
            }
        }
        ret = select(maxFd + 1, &readSet, NULL, NULL, &timev);
        if (ret == 0) {
            printf("[select process]select timeout!\n");
            int retTimeout  = -2;
            g_selectResult = retTimeout;
            break;
        }
        if (ret == -1) {
            g_selectResult = -1;
            printf("[select process]select fail, errinfo[%s]!\n", strerror(errno));
            break;
        }
        if (FD_ISSET(srvFd, &readSet)) {
            int cliFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
            if (cliFd == -1) {
                printf("[select process]accept fail!\n");
                g_selectResult = -1;
                break;
            } else {
                for (int i = 0; i < TEST_FD_COUNT; i++) {
                    if (fds[i] == -1) {
                        fds[i] = cliFd;
                        break;
                    }
                }
                cliCount++;
                printf("[select process]accept success, cliCount[%d]\n", cliCount);
            }
            continue;
        }
        for (int i = 0; i < TEST_FD_COUNT; i++) {
            if (fds[i] == -1) {
                continue;
            }
            if (FD_ISSET(fds[i], &readSet)) {
                memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
                int len = recv(fds[i], dataBuf, sizeof(dataBuf), 0);
                if (len > 0) {
                    printf("[select process][%d]recv msg[%s]\n", fds[i], dataBuf);
                    if (strcmp(dataBuf, "bye") == 0) {
                        printf("[select process][%d]client bye,cliCount[%d]\n", fds[i], cliCount);
                        FD_CLR(fds[i], &readSet);
                        lwip_close(fds[i]);
                        fds[i] = -1;
                        cliCount--;
                        continue;
                    }
                    len = send(fds[i], dataBuf, strlen(dataBuf), 0);
                    if (len > 0) {
                        printf("[select process][%d]send success\n", fds[i]);
                    } else {
                        printf("[select process][%d]send fail\n", fds[i]);
                    }
                }
            }
        }
        if (cliCount == 0) {
            g_selectResult = 0;
            printf("[select process]cliCount=0, over!\n");
            break;
        }
    }
    lwip_close(srvFd);
    g_selectFlag = 0;
W
wenjun 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
}

/**
 * @tc.desc      : register a test suite, this suite is used to test function
 * @param        : subsystem name is communication
 * @param        : module name is lwip
 * @param        : test suit name is LwipFuncTestSuite
 */
LITE_TEST_SUIT(communication, lwip, LwipFuncTestSuite);

/**
 * @tc.setup     : setup for every testcase
 * @return       : setup result, TRUE is success, FALSE is fail
 */
static BOOL LwipFuncTestSuiteSetUp(void)
{
    return TRUE;
}

/**
 * @tc.teardown  : teardown for every testcase
 * @return       : teardown result, TRUE is success, FALSE is fail
 */
static BOOL LwipFuncTestSuiteTearDown(void)
{
    printf("+----------------------------------------------------------+\n");
    return TRUE;
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0100
M
mamingshuai 已提交
468
 * @tc.name      : sample test tcp
W
wenjun 已提交
469 470
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
LITE_TEST_CASE(LwipFuncTestSuite, testTcp, Function | MediumTest | Level2)
{
    osThreadAttr_t attrServer;
    attrServer.name = "SampleTcpServerTask";
    attrServer.attr_bits = 0U;
    attrServer.cb_mem = NULL;
    attrServer.cb_size = 0U;
    attrServer.stack_mem = NULL;
    attrServer.stack_size = DEF_TASK_STACK;
    attrServer.priority = DEF_TASK_PRIORITY;

    osThreadAttr_t attrClient;
    attrClient.name = "SampleTcpClientTask";
    attrClient.attr_bits = 0U;
    attrClient.cb_mem = NULL;
    attrClient.cb_size = 0U;
    attrClient.stack_mem = NULL;
    attrClient.stack_size = DEF_TASK_STACK;
    attrClient.priority = DEF_TASK_PRIORITY;

    osThreadId_t serverTaskId = osThreadNew((osThreadFunc_t)SampleTcpServerTask, NULL, &attrServer);
    TEST_ASSERT_NOT_NULL(serverTaskId);
    if (serverTaskId == NULL) {
        printf("[testTcp]create server task fail!\n");
    }
    osThreadId_t clientTaskId = osThreadNew((osThreadFunc_t)SampleTcpClientTask, NULL, &attrClient);
    TEST_ASSERT_NOT_NULL(clientTaskId);
    if (clientTaskId == NULL) {
        printf("[testTcp]create client task fail!\n");
    }

502
    sleep(ONE_SECOND);
M
mamingshuai 已提交
503 504 505 506 507 508 509 510 511
    int timeout = TIMEOUT;
    g_serverWait = 1;
    g_clientWait = 1;
    while (timeout > 0) {
        if (g_serverWaitOver == 1 && g_clientWaitOver == 1) {
            printf("[testTcp] wait success[1]!\n");
            break;
        }
        timeout--;
512
        sleep(ONE_SECOND);
M
mamingshuai 已提交
513 514 515
        printf("[testTcp] wait[1]...\n");
    }

516
    sleep(ONE_SECOND);
M
mamingshuai 已提交
517 518 519 520 521 522 523 524 525
    timeout = TIMEOUT;
    g_serverWait = 1;
    g_clientWait = 1;
    while (timeout > 0) {
        if (g_serverWaitOver == 1 && g_clientWaitOver == 1) {
            printf("[testTcp] wait success[2]!\n");
            break;
        }
        timeout--;
526
        sleep(ONE_SECOND);
M
mamingshuai 已提交
527 528 529
        printf("[testTcp] wait[2]...\n");
    }

530
    sleep(ONE_SECOND);
M
mamingshuai 已提交
531 532 533 534 535 536 537 538 539
    timeout = TIMEOUT;
    g_serverWait = 1;
    g_clientWait = 1;
    while (timeout > 0) {
        if (g_serverWaitOver == 1 && g_clientWaitOver == 1) {
            printf("[testTcp] wait success[3]!\n");
            break;
        }
        timeout--;
540
        sleep(ONE_SECOND);
M
mamingshuai 已提交
541 542
        printf("[testTcp] wait[3]...\n");
    }
543
    sleep(ONE_SECOND);
M
mamingshuai 已提交
544 545 546 547 548 549 550 551 552
    timeout = TIMEOUT;
    g_serverWait = 1;
    g_clientWait = 1;
    while (timeout > 0) {
        if (g_serverWaitOver == 1 && g_clientWaitOver == 1) {
            printf("[testTcp] wait success[4]!\n");
            break;
        }
        timeout--;
553
        sleep(ONE_SECOND);
M
mamingshuai 已提交
554 555 556
        printf("[testTcp] wait[4]...\n");
    }
    TEST_ASSERT_EQUAL_INT(1, g_clientResult);
557 558
    TEST_ASSERT_EQUAL_INT(1, g_serverResult);
    
559
    sleep(ONE_SECOND);
560 561
    g_serverWait = 1;
    g_clientWait = 1;
M
mamingshuai 已提交
562 563 564 565 566 567 568 569
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0200
 * @tc.name      : sample test udp
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testUdp, Function | MediumTest | Level2)
W
wenjun 已提交
570 571 572 573 574 575 576 577 578
{
    int ret;
    int len = 2;
    struct msghdr msg = {0};
    struct iovec iov[2] = {0};
    struct sockaddr_in srvAddr = {0};
    struct sockaddr_in clnAddr = {0};
    socklen_t clnAddrLen = sizeof(clnAddr);

M
mamingshuai 已提交
579
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
580
    TEST_ASSERT_NOT_EQUAL(LWIP_TEST_FAIL, fd);
W
wenjun 已提交
581 582 583 584

    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(STACK_IP);
    srvAddr.sin_port = htons(STACK_PORT);
M
mamingshuai 已提交
585 586
    ret = bind(fd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
W
wenjun 已提交
587 588 589 590 591 592

    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
    clnAddr.sin_port = htons(PEER_PORT);
    memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(g_buf, BUF_SIZE, UDP_MSG);
M
mamingshuai 已提交
593 594 595
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
    ret = sendto(fd, g_buf, strlen(UDP_MSG), 0, (struct sockaddr*)&clnAddr, (socklen_t)sizeof(clnAddr));
    TEST_ASSERT_NOT_EQUAL(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
596 597

    memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
598 599
    ret = recvfrom(fd, g_buf, sizeof(g_buf), 0, (struct sockaddr*)&clnAddr, &clnAddrLen);
    TEST_ASSERT_EQUAL_INT(strlen(UDP_MSG), ret);
W
wenjun 已提交
600 601 602 603 604 605

    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(PEER_IP);
    clnAddr.sin_port = htons(PEER_PORT);
    memset_s(g_buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(g_buf, BUF_SIZE, UDP_MSG);
M
mamingshuai 已提交
606
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_SUCCESS, ret);
W
wenjun 已提交
607 608 609 610 611 612 613 614
    msg.msg_name = &clnAddr;
    msg.msg_namelen = sizeof(clnAddr);
    msg.msg_iov = iov;
    msg.msg_iovlen = len;
    iov[0].iov_base = g_buf;
    iov[0].iov_len = strlen(UDP_MSG);
    iov[1].iov_base = g_buf;
    iov[1].iov_len = strlen(UDP_MSG);
M
mamingshuai 已提交
615 616
    ret = sendmsg(fd, &msg, 0);
    TEST_ASSERT_EQUAL_INT(len * strlen(UDP_MSG), ret);
W
wenjun 已提交
617

M
mamingshuai 已提交
618 619
    ret = lwip_close(fd);
    TEST_ASSERT_NOT_EQUAL(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
620 621 622
}

/**
M
mamingshuai 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0400
 * @tc.name      : test select timeout
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testSelectTimeout, Function | MediumTest | Level2)
{
    osThreadAttr_t tSelect;
    tSelect.name = "SelectServerTask";
    tSelect.attr_bits = 0U;
    tSelect.cb_mem = NULL;
    tSelect.cb_size = 0U;
    tSelect.stack_mem = NULL;
    tSelect.stack_size = DEF_TASK_STACK;
    tSelect.priority = DEF_TASK_PRIORITY;

    g_selectTimeout = 2;
    osThreadId_t serverTaskId = osThreadNew((osThreadFunc_t)SelectServerTask, NULL, &tSelect);
    TEST_ASSERT_NOT_NULL(serverTaskId);
    if (serverTaskId == NULL) {
        printf("create select server task fail!\n");
    } else {
        while (g_selectFlag) {
645
            sleep(ONE_SECOND);
M
mamingshuai 已提交
646 647 648 649 650 651 652
            printf("wait select server finish...\n");
        }
        TEST_ASSERT_EQUAL_INT(-2, g_selectResult);
    }
}

/**
653
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0500
654
 * @tc.name      : test select with one clients
M
mamingshuai 已提交
655 656
 * @tc.desc      : [C- SOFTWARE -0200]
 */
657
LITE_TEST_CASE(LwipFuncTestSuite, testSelectOneClient, Function | MediumTest | Level2)
M
mamingshuai 已提交
658
{
659 660 661 662 663 664 665 666 667 668 669 670
    osThreadAttr_t tSelect;
    tSelect.name = "SelectServerTask";
    tSelect.attr_bits = 0U;
    tSelect.cb_mem = NULL;
    tSelect.cb_size = 0U;
    tSelect.stack_mem = NULL;
    tSelect.stack_size = DEF_TASK_STACK;
    tSelect.priority = DEF_TASK_PRIORITY;

    g_selectTimeout = 5;
    osThreadId_t serverTaskId = osThreadNew((osThreadFunc_t)SelectServerTask, NULL, &tSelect);
    TEST_ASSERT_NOT_NULL(serverTaskId);
671
    sleep(ONE_SECOND);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    if (serverTaskId == NULL) {
        printf("create select server task fail!\n");
    } else {
        osThreadAttr_t tClient;
        osThreadId_t clientTaskId;
        tClient.name = "CommTcpClientTask";
        tClient.attr_bits = 0U;
        tClient.cb_mem = NULL;
        tClient.cb_size = 0U;
        tClient.stack_mem = NULL;
        tClient.stack_size = DEF_TASK_STACK;
        tClient.priority = DEF_TASK_PRIORITY;
        clientTaskId = osThreadNew((osThreadFunc_t)CommTcpClientTask, NULL, &tClient);
        TEST_ASSERT_NOT_NULL(clientTaskId);
        }
M
mamingshuai 已提交
687

688 689
        g_selectFlag = 1;
        while (g_selectFlag) {
690
            sleep(ONE_SECOND);
691 692 693
            printf("wait select server finish...\n");
        }
        TEST_ASSERT_EQUAL_INT(0, g_selectResult);
M
mamingshuai 已提交
694 695 696 697
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0700
W
wenjun 已提交
698 699 700
 * @tc.name      : test socket operation
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
701
LITE_TEST_CASE(LwipFuncTestSuite, testSocketOpt, Function | MediumTest | Level2)
W
wenjun 已提交
702 703
{
    socklen_t len;
M
mamingshuai 已提交
704 705 706
    struct timeval timeout;
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    TEST_ASSERT_NOT_EQUAL(-1, fd);
W
wenjun 已提交
707 708 709

    int error = -1;
    len = sizeof(error);
M
mamingshuai 已提交
710 711 712 713
    int ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
    TEST_ASSERT_EQUAL_INT(0, ret);
    TEST_ASSERT_EQUAL_INT(0, error);

W
wenjun 已提交
714 715
    len = sizeof(timeout);
    ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
M
mamingshuai 已提交
716
    TEST_ASSERT_EQUAL_INT(0, ret);
W
wenjun 已提交
717

M
mamingshuai 已提交
718
    timeout.tv_sec = 1000;
W
wenjun 已提交
719 720
    len = sizeof(timeout);
    ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, len);
M
mamingshuai 已提交
721
    TEST_ASSERT_EQUAL_INT(0, ret);
W
wenjun 已提交
722 723 724

    memset_s(&timeout, len, 0, len);
    ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
M
mamingshuai 已提交
725 726 727 728 729 730 731 732 733 734 735 736
    TEST_ASSERT_EQUAL_INT(0, ret);
    TEST_ASSERT_EQUAL_INT(1000, timeout.tv_sec);

    int flag = 1;
    ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
    TEST_ASSERT_EQUAL_INT(0, ret);

    flag = 0;
    len = sizeof(flag);
    ret = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, &len);
    TEST_ASSERT_EQUAL_INT(0, ret);
    TEST_ASSERT_EQUAL_INT(1, flag);
W
wenjun 已提交
737 738 739 740

    error = -1;
    len = sizeof(error);
    ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
M
mamingshuai 已提交
741 742
    TEST_ASSERT_EQUAL_INT(0, ret);
    TEST_ASSERT_EQUAL_INT(0, error);
W
wenjun 已提交
743 744

    ret = lwip_close(fd);
M
mamingshuai 已提交
745
    TEST_ASSERT_EQUAL_INT(0, ret);
W
wenjun 已提交
746 747 748
}

/**
M
mamingshuai 已提交
749 750
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0800
 * @tc.name      : test getsockname and getpeername invalid input
W
wenjun 已提交
751 752
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
LITE_TEST_CASE(LwipFuncTestSuite, testGetSocketNameInvalidInput, Function | MediumTest | Level3)
{
    struct sockaddr addr;
    socklen_t addrLen = sizeof(addr);
    int ret = getsockname(-1, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getpeername(-1, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getsockname(0, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getpeername(0, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getsockname(1, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getpeername(1, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getsockname(130, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getpeername(130, &addr, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getsockname(10, NULL, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getpeername(10, NULL, &addrLen);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getsockname(10, &addr, NULL);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = getpeername(10, &addr, NULL);
    TEST_ASSERT_EQUAL_INT(-1, ret);
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0900
 * @tc.name      : test inet_pton IPv4 normal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetPtonIpv4Normal, Function | MediumTest | Level2)
W
wenjun 已提交
789 790
{
    int ret;
M
mamingshuai 已提交
791 792 793 794 795 796 797
    struct in_addr rst;
    char cpAddrs[4][16] = {"10.58.212.100", "0.0.0.0", "255.0.0.0", "255.255.255.255"};
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    unsigned int expectLittle[4] = {1691630090, 0, 255, 4294967295};
#else
    unsigned int expectBig[4] = {171627620, 0, 4278190080, 4294967295};
#endif
W
wenjun 已提交
798

M
mamingshuai 已提交
799 800 801
    for (int i = 0; i < 4; i++) {
        ret = inet_pton(AF_INET, cpAddrs[i], &rst);
        TEST_ASSERT_EQUAL_INT(1, ret);
W
wenjun 已提交
802
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
M
mamingshuai 已提交
803
        TEST_ASSERT_EQUAL_INT(expectLittle[i], rst.s_addr);
W
wenjun 已提交
804
#else
M
mamingshuai 已提交
805
        TEST_ASSERT_EQUAL_INT(expectBig[i], rst.s_addr);
W
wenjun 已提交
806
#endif
M
mamingshuai 已提交
807 808 809
        printf("inet_pton %s: un[%u],s[%d],hex[%x]\n", cpAddrs[i], rst.s_addr, rst.s_addr, rst.s_addr);
    }
}
W
wenjun 已提交
810

M
mamingshuai 已提交
811 812 813 814 815 816 817 818 819
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1000
 * @tc.name      : test inet_pton IPv4 abnormal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetPtonIpv4Abnormal, Function | MediumTest | Level2)
{
    int ret;
    struct in_addr rst;
M
mlinlmcc 已提交
820 821
    char cpAddrs[6][16] = {"256.0.0.1", "a.a.a.a", "....", "#", "127.0.0.f", "0:0:0:0:0:0:0:1"};
    for (int i = 0; i < 6; i++) {
M
mamingshuai 已提交
822 823 824 825 826
        ret = inet_pton(AF_INET, cpAddrs[i], &rst);
        TEST_ASSERT_EQUAL_INT(0, ret);
        printf("inet_pton error, cpAddr[%s]\n", cpAddrs[i]);
    }
}
W
wenjun 已提交
827

M
mamingshuai 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1100
 * @tc.name      : test inet_pton IPv6 normal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetPtonIpv6Normal, Function | MediumTest | Level2)
{
    int ret;
    struct in6_addr rst;
    char cpAddrs[6][40] = {"fc00:0101:0011:0011:0011:0011:0011:0011", "0:0:0:0:0:0:0:0", 
        "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "::", "1::", "fc00:0011:0011:0011:11:11:11:11"};
    for (int i = 0; i < 6; i++) {
        ret = inet_pton(AF_INET6, cpAddrs[i], &rst);
        TEST_ASSERT_EQUAL_INT(1, ret);
        printf("inet_pton error, cpAddr[%s]\n", cpAddrs[i]);
    }
W
wenjun 已提交
844 845 846
}

/**
M
mamingshuai 已提交
847 848
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1200
 * @tc.name      : test inet_pton IPv6 abnormal
W
wenjun 已提交
849 850
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
851
LITE_TEST_CASE(LwipFuncTestSuite, testInetPtonIpv6Abnormal, Function | MediumTest | Level2)
W
wenjun 已提交
852
{
M
mamingshuai 已提交
853 854 855 856 857 858 859 860 861 862
    int ret;
    struct in6_addr rst;
    char cpAddrs[7][40] = {"127.0.0.1", "f", ":", "0:0", "1:::", ":::::::",
        "1111:1111:1111:1111:1111:1111:1111:111G"};
    for (int i = 0; i < 7; i++) {
        ret = inet_pton(AF_INET6, cpAddrs[i], &rst);
        TEST_ASSERT_EQUAL_INT(0, ret);
        printf("inet_pton error, cpAddr[%s]\n", cpAddrs[i]);
    }
}
W
wenjun 已提交
863

M
mamingshuai 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1300
 * @tc.name      : test inet_pton with invalid family
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetPtonInvalidFamily, Function | MediumTest | Level2)
{
    struct in_addr rst;
    int ret = inet_pton(AF_IPX, "127.0.0.1", &rst);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    ret = inet_pton(-1, "127.0.0.1", &rst);
    TEST_ASSERT_EQUAL_INT(-1, ret);
}
W
wenjun 已提交
877

M
mamingshuai 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1400
 * @tc.name      : test inet_ntop IPv4 normal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetNtopIpv4Normal, Function | MediumTest | Level2)
{
    const char* ret = NULL;
    struct in_addr inputAddr;
    char rstBuff[INET_ADDRSTRLEN];
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    unsigned int inputLittle[4] = {0x64d43a0a, 0, 255, 4294967295};
#else
    unsigned int inputBig[4] = {171627620, 0, 4278190080, 4294967295};
#endif
W
wenjun 已提交
893

M
mamingshuai 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
    char expectAddrs[4][16] = {"10.58.212.100", "0.0.0.0", "255.0.0.0", "255.255.255.255"};
    for (int i = 0; i < 4; i++) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
        inputAddr.s_addr = inputLittle[i];
#else
        inputAddr.s_addr = inputBig[i];
#endif
        ret = inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
        if (ret == NULL) {
            TEST_ASSERT_TRUE(ret != NULL);
            printf("inet_ntop error, cpAddr[%s]\n", expectAddrs[i]);
        } else {
            printf("inet_ntop expect [%s]: ret[%s], buf[%s]\n", expectAddrs[i], ret, rstBuff);
            TEST_ASSERT_EQUAL_STRING(expectAddrs[i], ret);
            TEST_ASSERT_EQUAL_STRING(expectAddrs[i], rstBuff);
W
wenjun 已提交
909 910
        }
    }
M
mamingshuai 已提交
911
}
W
wenjun 已提交
912

M
mamingshuai 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1500
 * @tc.name      : test inet_ntop IPv4 boundary input
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetNtopIpv4Abnormal, Function | MediumTest | Level2)
{
    const char* ret = NULL;
    struct in_addr inputAddr;
    char rstBuff[INET_ADDRSTRLEN];
    char expectStr[2][16] = {"255.255.255.255", "0.0.0.0"};
    for (int i = 0; i < 2; i++) {
        inputAddr.s_addr = (i == 0 ? -1 : 4294967296);
        ret = inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
        TEST_ASSERT_TRUE(ret != NULL);
        TEST_ASSERT_EQUAL_STRING(expectStr[i], ret);
        TEST_ASSERT_EQUAL_STRING(expectStr[i], rstBuff);
W
wenjun 已提交
930
    }
M
mamingshuai 已提交
931
}
W
wenjun 已提交
932

M
mamingshuai 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1600
 * @tc.name      : test inet_ntop IPv6 normal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetNtopIpv6Normal, Function | MediumTest | Level2)
{
    int iret;
    const char* ret = NULL;
    struct in6_addr inputAddr;
    char rstBuff[INET6_ADDRSTRLEN];
    char inputAddrs[6][40] = {"fc00:0101:0011:0011:0011:0011:0011:0011", "0:0:0:0:0:0:0:0", 
        "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "::", "1::", "fc00:0011:0011:0011:1100:11:11:11"};
    char expectAddrs[6][40] = {"FC00:101:11:11:11:11:11:11", "::", 
            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "::", "1::", "FC00:11:11:11:1100:11:11:11"};
    for (int i = 0; i < 6; i++) {
        iret = inet_pton(AF_INET6, inputAddrs[i], &inputAddr);
        TEST_ASSERT_EQUAL_INT(1, iret);
        ret = inet_ntop(AF_INET6, &inputAddr, rstBuff, sizeof(rstBuff));
        if (ret == NULL) {
            TEST_ASSERT_TRUE(ret != NULL);
            printf("inet_ntop error, cpAddr[%s]\n", expectAddrs[i]);
        } else {
            printf("inet_ntop expect [%s]: ret[%s], buf[%s]\n", expectAddrs[i], ret, rstBuff);
            TEST_ASSERT_EQUAL_STRING(expectAddrs[i], ret);
            TEST_ASSERT_EQUAL_STRING(expectAddrs[i], rstBuff);
W
wenjun 已提交
959 960
        }
    }
M
mamingshuai 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1700
 * @tc.name      : test inet_ntop IPv6 boundary input
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetNtopIpv6Abnormal, Function | MediumTest | Level2)
{
    const char* ret = NULL;
    struct in6_addr inputAddr;
    char rstBuff[INET6_ADDRSTRLEN];
    char expectStr[2][40] = {"FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "::"};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 16; j++) {
            inputAddr.s6_addr[j] = (i == 0 ? -1 : 256);
W
wenjun 已提交
977
        }
M
mamingshuai 已提交
978 979 980 981
        ret = inet_ntop(AF_INET6, &inputAddr, rstBuff, sizeof(rstBuff));
        TEST_ASSERT_TRUE(ret != NULL);
        TEST_ASSERT_EQUAL_STRING(expectStr[i], ret);
        TEST_ASSERT_EQUAL_STRING(expectStr[i], rstBuff);
W
wenjun 已提交
982 983 984
    }
}

M
mamingshuai 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1800
 * @tc.name      : test inet_ntop with invalid family
 * @tc.desc      : [C- SOFTWARE -0200]
 */
LITE_TEST_CASE(LwipFuncTestSuite, testInetNtopInvalidFamily, Function | MediumTest | Level2)
{
    int iret;
    const char* ret = NULL;
    struct in6_addr inputAddr;
    char rstBuff[INET6_ADDRSTRLEN];

    iret = inet_pton(AF_INET6, "1::", &inputAddr);
    TEST_ASSERT_EQUAL_INT(1, iret);
    ret = inet_ntop(AF_IPX, &inputAddr, rstBuff, sizeof(rstBuff));
    TEST_ASSERT_TRUE(ret == NULL);
    ret = inet_ntop(-1, &inputAddr, rstBuff, sizeof(rstBuff));
    TEST_ASSERT_TRUE(ret == NULL);
}
W
wenjun 已提交
1004 1005 1006 1007 1008
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0500
 * @tc.name      : test invalid parameter
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
1009
LITE_TEST_CASE(LwipFuncTestSuite, testInvalidParameter, Function | MediumTest | Level2)
W
wenjun 已提交
1010 1011 1012 1013 1014 1015
{
    int ret;
    int fdFail = -1;
    int fdSuccess = -1;

    fdFail = socket(0, 0, 0);
M
mamingshuai 已提交
1016
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, fdFail);
W
wenjun 已提交
1017
    fdSuccess = socket(AF_INET, SOCK_STREAM, 0);
M
mamingshuai 已提交
1018
    TEST_ASSERT_NOT_EQUAL(LWIP_TEST_FAIL, fdSuccess);
W
wenjun 已提交
1019 1020

    ret = bind(fdFail, NULL, sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1021
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1022
    ret = bind(fdSuccess, NULL, sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1023
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1024 1025

    ret = connect(fdFail, NULL, sizeof(struct sockaddr));
M
mamingshuai 已提交
1026
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1027
    ret = connect(fdSuccess, NULL, sizeof(struct sockaddr));
M
mamingshuai 已提交
1028
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1029 1030

    ret = accept(fdFail, NULL, NULL);
M
mamingshuai 已提交
1031
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1032
    ret = accept(fdSuccess, NULL, NULL);
M
mamingshuai 已提交
1033
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1034 1035

    ret = getsockname(fdFail, NULL, NULL);
M
mamingshuai 已提交
1036
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1037
    ret = getsockname(fdSuccess, NULL, NULL);
M
mamingshuai 已提交
1038
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1039 1040

    ret = getpeername(fdFail, NULL, NULL);
M
mamingshuai 已提交
1041
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1042
    ret = getpeername(fdSuccess, NULL, NULL);
M
mamingshuai 已提交
1043
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1044 1045

    ret = send(fdFail, NULL, strlen(SRV_MSG), 0);
M
mamingshuai 已提交
1046
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1047
    ret = send(fdSuccess, NULL, strlen(SRV_MSG), 0);
M
mamingshuai 已提交
1048
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1049 1050

    ret = sendto(fdFail, NULL, strlen(SRV_MSG), 0, NULL, (socklen_t)sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1051
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1052
    ret = sendto(fdSuccess, NULL, strlen(SRV_MSG), 0, NULL, (socklen_t)sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1053
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1054 1055

    ret = recv(fdFail, NULL, sizeof(SRV_MSG), 0);
M
mamingshuai 已提交
1056
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1057
    ret = recv(fdSuccess, NULL, sizeof(SRV_MSG), 0);
M
mamingshuai 已提交
1058
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1059 1060

    ret = recvfrom(fdFail, NULL, sizeof(SRV_MSG), 0, NULL, NULL);
M
mamingshuai 已提交
1061
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1062
    ret = recvfrom(fdSuccess, NULL, sizeof(SRV_MSG), 0, NULL, NULL);
M
mamingshuai 已提交
1063
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1064 1065

    ret = setsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, NULL, (socklen_t)sizeof(struct timeval));
M
mamingshuai 已提交
1066
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1067
    ret = setsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, NULL, (socklen_t)sizeof(struct timeval));
M
mamingshuai 已提交
1068
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1069 1070

    ret = getsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, NULL, (socklen_t*)sizeof(struct timeval));
M
mamingshuai 已提交
1071
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1072
    ret = getsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, NULL, (socklen_t*)sizeof(struct timeval));
M
mamingshuai 已提交
1073
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1074 1075

    ret = sendmsg(fdFail, NULL, 0);
M
mamingshuai 已提交
1076
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1077
    ret = sendmsg(fdSuccess, NULL, 0);
M
mamingshuai 已提交
1078
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1079 1080

    ret = listen(fdFail, 0);
M
mamingshuai 已提交
1081
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1082
    ret = listen(fdSuccess, -1);
M
mamingshuai 已提交
1083
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1084 1085

    ret = select(fdFail, NULL, NULL, NULL, NULL);
M
mamingshuai 已提交
1086
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1087 1088

    ret = shutdown(fdFail, SHUT_RD);
M
mamingshuai 已提交
1089
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1090
    ret = shutdown(fdSuccess, -1);
M
mamingshuai 已提交
1091
    TEST_ASSERT_EQUAL_INT(LWIP_TEST_FAIL, ret);
W
wenjun 已提交
1092 1093

    ret = lwip_close(fdSuccess);
M
mamingshuai 已提交
1094
    TEST_ASSERT_EQUAL_INT(0, ret);
W
wenjun 已提交
1095 1096
}

S
shuxiong 已提交
1097
RUN_TEST_SUITE(LwipFuncTestSuite);