ActsLwipTest.cpp 36.0 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
 * 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 "gtest/gtest.h"
#include "sys/socket.h"
#include "arpa/inet.h"
#include "netinet/tcp.h"
M
mamingshuai 已提交
20
#include <sys/wait.h>
W
wenjun 已提交
21 22 23
#include <string.h>
#include <errno.h>
#include <stdlib.h>
24
#include "securec.h"
W
wenjun 已提交
25 26 27 28

using namespace std;
using namespace testing::ext;

M
mamingshuai 已提交
29
static const int TEST_FD_COUNT = 10;
W
wenjun 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static const int STACK_PORT = 2277;
static const int PEER_PORT = 2277;
static const int BUF_SIZE = (1024 * 8);
static const char* g_udpMsg = "Hi, I am UDP";
static const char* g_srvMsg = "Hi, I am TCP server";
static const char* g_cliMsg = "Hi, I am TCP client";
static const char* g_localHost = "127.0.0.1";

static pthread_barrier_t g_barrier;
#define WAIT() pthread_barrier_wait(&g_barrier)

class ActsLwipTest : public testing::Test {
protected:
    // SetUpTestCase: Testsuit setup, run before 1st testcase
    static void SetUpTestCase(void) {}
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void) {}
    // Testcase setup
    virtual void SetUp() {}
    // Testcase teardown
    virtual void TearDown() {}
};

M
mamingshuai 已提交
53
static int CommInitTcpServer(int port)
W
wenjun 已提交
54
{
M
mamingshuai 已提交
55 56 57 58 59 60 61 62 63 64
    int srvFd = socket(AF_INET, SOCK_STREAM, 0);
    if (srvFd == -1) {
        printf("CommInitTcpServer]socket fail!\n");
        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);
    }
W
wenjun 已提交
65
    struct sockaddr_in srvAddr = {0};
M
mamingshuai 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
    srvAddr.sin_port = htons(port);
    ret = ::bind(srvFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    if (ret != 0) {
        printf("[CommInitTcpServer]::bind fail!\n");
        close(srvFd);
        return -1;
    }
    int backlog = 5;
    ret = listen(srvFd, backlog);
    if (ret != 0) {
        printf("[CommInitTcpServer]listen fail!\n");
        close(srvFd);
        return -1;
    }
    printf("[CommInitTcpServer]success,Fd[%d]\n", srvFd);
    return srvFd;
}
W
wenjun 已提交
85

M
mamingshuai 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static void* CommTcpClientTask(void* param)
{
    int* ret = (int *)malloc(sizeof(int));
    if (ret == nullptr) {
        printf("[comm client]malloc fail\n");
        return nullptr;
    }
    *ret = 0;
    int clnFd = socket(AF_INET, SOCK_STREAM, 0);
    if (clnFd == -1) {
        printf("[comm client]socket fail\n");  
        *ret = -1;
        return ret;
    }
    struct sockaddr_in srvAddr = {0};
W
wenjun 已提交
101 102
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
M
mamingshuai 已提交
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 137 138 139 140 141 142 143 144 145 146 147 148 149
    srvAddr.sin_port = htons(PEER_PORT + 1);
    int rst = connect(clnFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    if (rst != 0) {
        printf("[comm client]connect fail\n");
        close(clnFd);
        *ret = -1;
        return ret;
    }
    int sleepSecond = *((int *)param);
    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++) {
        sleep(sleepSecond);
        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);
        if (rst <= 0) {
            *ret = -1;
            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);
    if (rst <= 0) {
        *ret = -1;
        printf("[comm client][%d]send fail\n", clnFd);
    }
    rst = close(clnFd);
    if (rst != 0) {
        *ret = -1;
        printf("[comm client][%d]close fd fail\n", clnFd);
    }
    return ret;
}
W
wenjun 已提交
150

M
mamingshuai 已提交
151 152 153 154
static void* SampleTcpServerTask(void* p)
{
    int srvFd = CommInitTcpServer(STACK_PORT);
    EXPECT_NE(-1, srvFd);
W
wenjun 已提交
155 156

    WAIT();
M
mamingshuai 已提交
157 158 159
    struct sockaddr_in clnAddr = {0};
    socklen_t clnAddrLen = sizeof(clnAddr);
    int clnFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
W
wenjun 已提交
160
    printf("[tcp server]accept <%s:%d>\n", inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
M
mamingshuai 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    EXPECT_NE(-1, clnFd);
    printf("[tcp server]lsfd[%d], sfd[%d]\n", srvFd, clnFd);

    struct sockaddr addr = {0};
    socklen_t addrLen = sizeof(addr);
    int ret = getsockname(clnFd, &addr, &addrLen);
    printf("[tcp server]getsockname %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
        ntohs(((struct sockaddr_in*)&addr)->sin_port));
    EXPECT_EQ(0, ret);
    EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);

    ret = getpeername(clnFd, &addr, &addrLen);
    printf("[tcp server]getpeername %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
        ntohs(((struct sockaddr_in*)&addr)->sin_port));
    EXPECT_EQ(0, ret);
    EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);
W
wenjun 已提交
177

M
mamingshuai 已提交
178 179
    static char buf[BUF_SIZE + 1] = {0};
    unsigned int srvMsgLen = strlen(g_srvMsg);
W
wenjun 已提交
180 181
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, g_srvMsg);
M
mamingshuai 已提交
182 183
    EXPECT_EQ(0, ret);
    ret = send(clnFd, buf, srvMsgLen, 0);
W
wenjun 已提交
184
    printf("[tcp server]send, ret=%d\n", ret);
M
mamingshuai 已提交
185
    EXPECT_EQ(srvMsgLen, (unsigned int)ret);
W
wenjun 已提交
186 187

    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
188 189 190
    ret = recv(clnFd, buf, sizeof(buf), 0);
    printf("[tcp server]recv, ret=%d, msg[%s]\n", ret, buf);
    EXPECT_EQ(strlen(g_cliMsg), (unsigned int)ret);
W
wenjun 已提交
191 192 193

    WAIT();
    int len = 2;
M
mamingshuai 已提交
194 195
    struct msghdr msg = {0};
    struct iovec iov[2] = {0};
W
wenjun 已提交
196 197 198 199 200
    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
    clnAddr.sin_port = htons(PEER_PORT);
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, g_srvMsg);
M
mamingshuai 已提交
201
    EXPECT_EQ(0, ret);
W
wenjun 已提交
202 203 204 205 206 207 208 209
    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 已提交
210
    ret = sendmsg(clnFd, &msg, 0);
W
wenjun 已提交
211
    printf("[tcp server]sendmsg, ret=%d\n", ret);
M
mamingshuai 已提交
212
    EXPECT_EQ(len * srvMsgLen, (unsigned int)ret);
W
wenjun 已提交
213 214 215 216 217 218 219 220 221 222

    WAIT();
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    memset_s(&msg, sizeof(msg), 0, sizeof(msg));
    msg.msg_name = &clnAddr;
    msg.msg_namelen = sizeof(clnAddr);
    msg.msg_iov = iov;
    msg.msg_iovlen = 1;
    iov[0].iov_base = buf;
    iov[0].iov_len = sizeof(buf);
M
mamingshuai 已提交
223 224 225 226 227 228 229 230 231 232 233
    ret = recvmsg(clnFd, &msg, 0);
    printf("[tcp server]recvmsg on socket %d:%d, msg[%s]\n", clnFd, ret, buf);
    EXPECT_EQ(len * strlen(g_cliMsg), (unsigned int)ret);

    ret = shutdown(clnFd, SHUT_RDWR);
    printf("[tcp server]shutdown\n");
    EXPECT_EQ(0, ret);
    ret = close(clnFd);
    EXPECT_NE(-1, ret);
    ret = close(srvFd);
    EXPECT_NE(-1, ret);
W
wenjun 已提交
234 235 236
    return nullptr;
}

M
mamingshuai 已提交
237
static void* SampleTcpClientTask(void* p)
W
wenjun 已提交
238
{
M
mamingshuai 已提交
239 240 241
    int clnFd = socket(AF_INET, SOCK_STREAM, 0);
    printf("[tcp client]create socket\n");
    EXPECT_NE(-1, clnFd);
W
wenjun 已提交
242 243

    WAIT();
M
mamingshuai 已提交
244
    struct sockaddr_in srvAddr = {0};
W
wenjun 已提交
245 246 247
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
    srvAddr.sin_port = htons(PEER_PORT);
M
mamingshuai 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    int ret = connect(clnFd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    printf("[tcp client]connect %s:%d, fd[%d]\n", inet_ntoa(srvAddr.sin_addr), ntohs(srvAddr.sin_port), clnFd);
    EXPECT_EQ(0, ret);

    struct sockaddr addr = {0};
    socklen_t addrLen = sizeof(addr);
    ret = getpeername(clnFd, &addr, &addrLen);
    printf("[tcp client]getpeername %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
        ntohs(((struct sockaddr_in*)&addr)->sin_port));
    EXPECT_EQ(0, ret);
    EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);

    ret = getsockname(clnFd, &addr, &addrLen);
    printf("[tcp client]getsockname %s:%d\n", inet_ntoa(((struct sockaddr_in*)&addr)->sin_addr),
        ntohs(((struct sockaddr_in*)&addr)->sin_port));
    EXPECT_EQ(0, ret);
    EXPECT_EQ(inet_addr(g_localHost), ((struct sockaddr_in*)&addr)->sin_addr.s_addr);

    static char buf[BUF_SIZE + 1] = {0};
    unsigned int cliMsgLen = strlen(g_cliMsg);
W
wenjun 已提交
268 269
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, g_cliMsg);
M
mamingshuai 已提交
270 271
    EXPECT_EQ(0, ret);
    ret = send(clnFd, buf, cliMsgLen, 0);
W
wenjun 已提交
272
    printf("[tcp client]send, ret=%d\n", ret);
M
mamingshuai 已提交
273
    EXPECT_EQ(cliMsgLen, (unsigned int)ret);
W
wenjun 已提交
274 275

    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
276 277 278
    ret = recv(clnFd, buf, sizeof(buf), 0);
    printf("[tcp client]recv, ret=%d, msg[%s]\n", ret, buf);
    EXPECT_EQ(strlen(g_srvMsg), (unsigned int)ret);
W
wenjun 已提交
279 280 281

    WAIT();
    int len = 2;
M
mamingshuai 已提交
282 283 284
    struct msghdr msg = {0};
    struct iovec iov[2] = {0};
    struct sockaddr_in clnAddr = {0};
W
wenjun 已提交
285 286 287 288 289
    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
    clnAddr.sin_port = htons(PEER_PORT);
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, g_cliMsg);
M
mamingshuai 已提交
290
    EXPECT_EQ(0, ret);
W
wenjun 已提交
291 292 293 294 295 296 297 298
    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 已提交
299
    ret = sendmsg(clnFd, &msg, 0);
W
wenjun 已提交
300
    printf("[tcp client]sendmsg, ret=%d\n", ret);
M
mamingshuai 已提交
301
    EXPECT_EQ(len * cliMsgLen, (unsigned int)ret);
W
wenjun 已提交
302 303 304 305 306 307 308 309 310 311

    WAIT();
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    memset_s(&msg, sizeof(msg), 0, sizeof(msg));
    msg.msg_name = &clnAddr;
    msg.msg_namelen = sizeof(clnAddr);
    msg.msg_iov = iov;
    msg.msg_iovlen = 1;
    iov[0].iov_base = buf;
    iov[0].iov_len = sizeof(buf);
M
mamingshuai 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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
    ret = recvmsg(clnFd, &msg, 0);
    printf("[tcp client]recvmsg, ret=%d, msg[%s]\n", ret, buf);
    EXPECT_EQ(len * strlen(g_srvMsg), (unsigned int)ret);

    ret = shutdown(clnFd, SHUT_RDWR);
    printf("[tcp client]shutdown\n");
    EXPECT_EQ(0, ret);
    ret = close(clnFd);
    EXPECT_NE(-1, ret);
    return nullptr;
}

static int TcpServerForFork()
{
    int timeout = 10;
    char dataBuf[50] = {0};
    int srvFd = CommInitTcpServer(STACK_PORT + 1);
    if (srvFd == -1) {
        return -1;
    }
    printf("[server process]wait client...\n");
    int ret = -1;
    struct sockaddr_in clnAddr = {0};
    socklen_t clnAddrLen = sizeof(clnAddr);
    int cliFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
    if (cliFd == -1) {
        printf("[server process]accept fail!\n");
        goto FINALLY;
    }
    while (timeout > 0) {
        memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
        ret = recv(cliFd, dataBuf, sizeof(dataBuf), 0);
        if (ret > 0) {
            printf("[server process]recv msg[%s]\n", dataBuf);
            if (strcmp(dataBuf, "bye") == 0) {
                printf("[server process]break\n");
                break;
            }
        } else {
            printf("[server process]recv no data\n");
        }
        memset_s(dataBuf, sizeof(dataBuf), 0, sizeof(dataBuf));
        sprintf_s(dataBuf, sizeof(dataBuf), "server timeout:%d", timeout);
        ret = send(cliFd, dataBuf, strlen(dataBuf), 0);
        if (ret > 0) {
            printf("[server process]send success, msg[%s]\n", dataBuf);
        } else {
            printf("[server process]send fail!\n");
        }
        timeout--;
    }
    close(cliFd);
    printf("[server process]over\n");

FINALLY:
    close(srvFd);
    return ret;
}
W
wenjun 已提交
370

M
mamingshuai 已提交
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
static int WaitProcessExit(int pid, int* code)
{
    int status;
    int rt = waitpid(pid, &status, 0);
    errno = 0;
    int retError = -2;
    if (rt == -1) {
        printf("[WaitProcessExit]waitpid return -1, errno=%d:%s\n", errno, strerror(errno));
        return -1;
    } else if (rt == 0) {
        return 0;
    } else if (rt != pid) { // waitpid return error
        printf("[WaitProcessExit]waitpid error\n");
        return retError;
    }

    int retWterm = 2;
    int retWstop = 3;
    int other = 4;
    if (WIFEXITED(status)) {
        *code = WEXITSTATUS(status);
        return 1;
    } else if (WIFSIGNALED(status)) {
        *code = WTERMSIG(status);
        return retWterm;
    } else if (WIFSTOPPED(status)) {
        *code = WSTOPSIG(status);
        return retWstop;
    }
    return other;
}
W
wenjun 已提交
402

M
mamingshuai 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415
static int SelectServerForFork(unsigned int timeoutSec)
{
    int srvFd = CommInitTcpServer(STACK_PORT + 1);
    if (srvFd == -1) {
        return -1;
    }
    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;
416 417
    long timeout = timeoutSec;
    struct timeval timev = {.tv_sec = timeout, .tv_usec = 0};
M
mamingshuai 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 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 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    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");
            ret = -2;
            break;
        }
        if (ret == -1) {
            fprintf(stderr, "[select process]select fail[%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");
                ret = -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);
                        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) {
            ret = 0;
            printf("[select process]cliCount=0, over!\n");
            break;
        }
    }
    close(srvFd);
    return ret;
W
wenjun 已提交
493 494 495 496
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0100
M
mamingshuai 已提交
497
 * @tc.name      : sample test tcp
W
wenjun 已提交
498 499
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
HWTEST_F(ActsLwipTest, testTcpSample, Function | MediumTest | Level1)
{
    pthread_t srv;
    pthread_t cli;

    int ret = pthread_barrier_init(&g_barrier, 0, 2);
    EXPECT_EQ(0, ret);

    ret = pthread_create(&srv, nullptr, SampleTcpServerTask, nullptr);
    EXPECT_EQ(0, ret);
    ret = pthread_create(&cli, nullptr, SampleTcpClientTask, nullptr);
    EXPECT_EQ(0, ret);

    ret = pthread_join(cli, nullptr);
    EXPECT_EQ(0, ret);
    ret = pthread_join(srv, nullptr);
    EXPECT_EQ(0, ret);

    ret = pthread_barrier_destroy(&g_barrier);
    EXPECT_EQ(0, ret);
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0200
 * @tc.name      : sample test udp
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testUdpSample, Function | MediumTest | Level2)
W
wenjun 已提交
528 529
{
    int len = 2;
M
mamingshuai 已提交
530
    char buf[BUF_SIZE + 1];
W
wenjun 已提交
531 532 533 534 535 536
    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 已提交
537 538
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    ASSERT_NE(-1, sfd);
W
wenjun 已提交
539 540 541
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
    srvAddr.sin_port = htons(STACK_PORT);
M
mamingshuai 已提交
542 543
    int ret = ::bind(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    ASSERT_EQ(0, ret);
W
wenjun 已提交
544 545 546 547

    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
    clnAddr.sin_port = htons(PEER_PORT);
M
mamingshuai 已提交
548 549 550 551 552
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, g_udpMsg);
    EXPECT_EQ(0, ret);
    ret = sendto(sfd, buf, strlen(g_udpMsg), 0, (struct sockaddr*)&clnAddr, (socklen_t)sizeof(clnAddr));
    EXPECT_NE(-1, ret);
W
wenjun 已提交
553

M
mamingshuai 已提交
554 555 556
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&clnAddr, &clnAddrLen);
    EXPECT_EQ(strlen(g_udpMsg), (unsigned int)ret);
W
wenjun 已提交
557 558 559 560

    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
    clnAddr.sin_port = htons(PEER_PORT);
M
mamingshuai 已提交
561 562 563
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, g_udpMsg);
    EXPECT_EQ(0, ret);
W
wenjun 已提交
564 565 566 567
    msg.msg_name = &clnAddr;
    msg.msg_namelen = sizeof(clnAddr);
    msg.msg_iov = iov;
    msg.msg_iovlen = len;
M
mamingshuai 已提交
568
    iov[0].iov_base = buf;
W
wenjun 已提交
569
    iov[0].iov_len = strlen(g_udpMsg);
M
mamingshuai 已提交
570
    iov[1].iov_base = buf;
W
wenjun 已提交
571 572
    iov[1].iov_len = strlen(g_udpMsg);
    ret = sendmsg(sfd, &msg, 0);
M
mamingshuai 已提交
573
    EXPECT_EQ(len*strlen(g_udpMsg), (unsigned int)ret);
W
wenjun 已提交
574 575

    ret = close(sfd);
M
mamingshuai 已提交
576
    EXPECT_NE(-1, ret);
W
wenjun 已提交
577 578 579
}

/**
M
mamingshuai 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0300
 * @tc.name      : test inter-process network communication
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInterProcessComm, Function | MediumTest | Level2)
{
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    if (pid == 0) {
        // child:tcp server
        int exitCode = 0;
        int ret = TcpServerForFork();
        if (ret == -1) {
            printf("[tcp server]run fail\n");
            exitCode = 1;
        }
        exit(exitCode);
    } else {
        // parent:tcp client
        sleep(2);
        int second = 1;
        void *ret = CommTcpClientTask(&second);
        if (ret == nullptr) {
            printf("[test select]CommClientTask ret is null\n");
            EXPECT_TRUE(0);
        } else {
            printf("[test select]CommClientTask ret...\n");
            int rst = *((int *)ret);
            printf("[test select]CommClientTask ret [%d]\n", rst);
            EXPECT_EQ(0, rst);
            free(ret);
        }
        int exitCode;
        int procStat = WaitProcessExit(pid, &exitCode);
        ASSERT_EQ(1, procStat);
        ASSERT_EQ(0, exitCode);
    }
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0400
 * @tc.name      : test select timeout
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testSelectTimeout, Function | MediumTest | Level2)
{
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    if (pid == 0) {
        // child: server
        int ret = SelectServerForFork(2);
        if (ret == -2) {
            exit(0);
        } else {
            printf("[select server]fail, ret is[%d]\n", ret);
            exit(1);
        }
    } else {
        int exitCode;
        int procStat = WaitProcessExit(pid, &exitCode);
        ASSERT_EQ(procStat, 1);
        ASSERT_EQ(exitCode, 0);
    }
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0500
 * @tc.name      : test select with one client
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testSelectOneClient, Function | MediumTest | Level2)
{
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    if (pid == 0) {
        // child: server
        int exitCode = 0;
        int ret = SelectServerForFork(5);
        if (ret != 0) {
            printf("[select server]run fail\n");
            exitCode = 1;
        }
        printf("[select server]ret[%d]\n", ret);
        exit(exitCode);
    } else {
        // parent: client
        sleep(2);
        int second = 1;
        void *ret = CommTcpClientTask(&second);
        if (ret == nullptr) {
            printf("[test select]CommClientTask ret is null\n");
            EXPECT_TRUE(0);
        } else {
            int rst = *((int *)ret);
            printf("[test select]CommClientTask ret [%d]\n", rst);
            EXPECT_EQ(0, rst);
            free(ret);
        }
        int exitCode;
        int procStat = WaitProcessExit(pid, &exitCode);
        ASSERT_EQ(1, procStat);
        ASSERT_EQ(0, exitCode);
    }
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0600
 * @tc.name      : test select with multi clients
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testSelectMultiClients, Function | MediumTest | Level2)
{
    pid_t pid = fork();
    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
    if (pid == 0) {
        // child: server
        int exitCode = 0;
        int ret = SelectServerForFork(5);
        if (ret != 0) {
            printf("[select server]run fail\n");
            exitCode = 1;
        }
        printf("[select server]ret[%d]\n", ret);
        exit(exitCode);
    } else {
        // parent: client
        sleep(2);
        int ret;
        pthread_t pCli[4];
        int interval[4] = {2, 1, 3, 1};
        for (int i = 0; i < 4; i++) {
            ret = pthread_create(&pCli[i], nullptr, CommTcpClientTask, &interval[i]);
            EXPECT_EQ(0, ret) << "Errinfo:pthread_create index is [" << i << "]";
        }

        void *rst = nullptr;
        for (int i = 0; i < 4; i++) {
            ret = pthread_join(pCli[i], &rst);
            EXPECT_EQ(0, ret) << "Errinfo:pthread_join index is [" << i << "]";
            if (rst != nullptr) {
                printf("[test select]client thread[%d] return[%d]\n", i, *((int *)rst));
                EXPECT_EQ(0, *((int *)rst));
                free(rst);
            }
        }
        int exitCode;
        int procStat = WaitProcessExit(pid, &exitCode);
        ASSERT_EQ(1, procStat);
        ASSERT_EQ(0, exitCode);
    }
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0700
W
wenjun 已提交
734 735 736
 * @tc.name      : test socket operation
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
737
HWTEST_F(ActsLwipTest, testSocketOpt, Function | MediumTest | Level2)
W
wenjun 已提交
738 739
{
    socklen_t len;
M
mamingshuai 已提交
740 741 742
    struct timeval timeout = {0};
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    EXPECT_NE(-1, fd);
W
wenjun 已提交
743 744 745

    int error = -1;
    len = sizeof(error);
M
mamingshuai 已提交
746 747 748 749
    int ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
    EXPECT_EQ(0, ret);
    EXPECT_EQ(0, error);

W
wenjun 已提交
750 751
    len = sizeof(timeout);
    ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
M
mamingshuai 已提交
752
    EXPECT_EQ(0, ret);
W
wenjun 已提交
753 754 755 756

    timeout.tv_sec = 1000;
    len = sizeof(timeout);
    ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, len);
M
mamingshuai 已提交
757
    EXPECT_EQ(0, ret);
W
wenjun 已提交
758 759 760

    memset_s(&timeout, len, 0, len);
    ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
M
mamingshuai 已提交
761 762 763 764 765 766 767 768 769 770 771 772
    EXPECT_EQ(0, ret);
    EXPECT_EQ(1000, timeout.tv_sec);

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

    flag = 0;
    len = sizeof(flag);
    ret = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, &len);
    EXPECT_EQ(0, ret);
    EXPECT_EQ(1, flag);
W
wenjun 已提交
773 774 775 776

    error = -1;
    len = sizeof(error);
    ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
M
mamingshuai 已提交
777 778
    EXPECT_EQ(0, ret);
    EXPECT_EQ(0, error);
W
wenjun 已提交
779 780

    ret = close(fd);
M
mamingshuai 已提交
781
    EXPECT_EQ(0, ret);
W
wenjun 已提交
782 783 784
}

/**
M
mamingshuai 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0800
 * @tc.name      : test getsockname and getpeername invalid input
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testGetSocketNameInvalidInput, Function | MediumTest | Level3)
{
    struct sockaddr addr = {0};
    socklen_t addrLen = sizeof(addr);
    int ret = getsockname(-1, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getpeername(-1, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getsockname(0, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getpeername(0, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getsockname(1, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getpeername(1, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getsockname(130, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getpeername(130, &addr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getsockname(10, nullptr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getpeername(10, nullptr, &addrLen);
    EXPECT_EQ(-1, ret);
    ret = getsockname(10, &addr, nullptr);
    EXPECT_EQ(-1, ret);
    ret = getpeername(10, &addr, nullptr);
    EXPECT_EQ(-1, ret);
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0900
 * @tc.name      : test inet_pton IPv4 normal
W
wenjun 已提交
822 823
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
824
HWTEST_F(ActsLwipTest, testInetPtonIpv4Normal, Function | MediumTest | Level2)
W
wenjun 已提交
825 826
{
    int ret;
M
mamingshuai 已提交
827 828 829 830 831 832 833
    struct in_addr rst = {0};
    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 已提交
834

M
mamingshuai 已提交
835 836 837
    for (int i = 0; i < 4; i++) {
        ret = inet_pton(AF_INET, cpAddrs[i], &rst);
        EXPECT_EQ(1, ret);
W
wenjun 已提交
838
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
M
mamingshuai 已提交
839
        EXPECT_EQ(expectLittle[i], rst.s_addr);
W
wenjun 已提交
840
#else
M
mamingshuai 已提交
841
        EXPECT_EQ(expectBig[i], rst.s_addr);
W
wenjun 已提交
842
#endif
M
mamingshuai 已提交
843 844 845
        printf("inet_pton %s: un[%u],s[%d],hex[%x]\n", cpAddrs[i], rst.s_addr, rst.s_addr, rst.s_addr);
    }
}
W
wenjun 已提交
846

M
mamingshuai 已提交
847 848 849 850 851 852 853 854 855
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1000
 * @tc.name      : test inet_pton IPv4 abnormal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetPtonIpv4Abnormal, Function | MediumTest | Level2)
{
    int ret;
    struct in_addr rst = {0};
M
mlinlmcc 已提交
856 857
    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 已提交
858 859 860 861
        ret = inet_pton(AF_INET, cpAddrs[i], &rst);
        EXPECT_EQ(0, ret) << "ErrInfo:inet_pton abnormal [" << cpAddrs[i] << "]";
    }
}
W
wenjun 已提交
862

M
mamingshuai 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1100
 * @tc.name      : test inet_pton IPv6 normal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetPtonIpv6Normal, Function | MediumTest | Level2)
{
    int ret;
    struct in6_addr rst = {0};
    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:0011:11:11:11"};
    for (int i = 0; i < 6; i++) {
        ret = inet_pton(AF_INET6, cpAddrs[i], &rst);
        EXPECT_EQ(1, ret) << "ErrInfo:inet_pton [" << cpAddrs[i] << "]";
    }
W
wenjun 已提交
878 879 880
}

/**
M
mamingshuai 已提交
881 882
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1200
 * @tc.name      : test inet_pton IPv6 abnormal
W
wenjun 已提交
883 884
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
885
HWTEST_F(ActsLwipTest, testInetPtonIpv6Abnormal, Function | MediumTest | Level2)
W
wenjun 已提交
886 887
{
    int ret;
M
mamingshuai 已提交
888 889 890 891 892 893 894 895
    struct in6_addr rst = {0};
    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);
        EXPECT_EQ(0, ret) << "ErrInfo:inet_pton abnormal [" << cpAddrs[i] << "]";
    }
}
W
wenjun 已提交
896

M
mamingshuai 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1300
 * @tc.name      : test inet_pton with invalid family
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetPtonInvalidFamily, Function | MediumTest | Level2)
{
    struct in_addr rst = {0};
    int ret = inet_pton(AF_IPX, "127.0.0.1", &rst);
    EXPECT_EQ(-1, ret);
    ret = inet_pton(-1, "127.0.0.1", &rst);
    EXPECT_EQ(-1, ret);
}
W
wenjun 已提交
910

M
mamingshuai 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1400
 * @tc.name      : test inet_ntop IPv4 normal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetNtopIpv4Normal, Function | MediumTest | Level2)
{
    const char* ret = nullptr;
    struct in_addr inputAddr = {0};
    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 已提交
926

M
mamingshuai 已提交
927 928 929 930 931 932 933 934
    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));
935
        ASSERT_NE(ret, nullptr);
936 937 938
        printf("inet_ntop expect [%s]: ret[%s], buf[%s]\n", expectAddrs[i], ret, rstBuff);
        EXPECT_STREQ(expectAddrs[i], ret);
        EXPECT_STREQ(expectAddrs[i], rstBuff);
M
mamingshuai 已提交
939 940
    }
}
W
wenjun 已提交
941

M
mamingshuai 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1500
 * @tc.name      : test inet_ntop IPv4 boundary input
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetNtopIpv4Abnormal, Function | MediumTest | Level2)
{
    const char* ret = nullptr;
    struct in_addr inputAddr = {0};
    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));
        ASSERT_TRUE(ret != NULL);
        EXPECT_STREQ(expectStr[i], ret);
        EXPECT_STREQ(expectStr[i], rstBuff);
    }
W
wenjun 已提交
960 961 962
}

/**
M
mamingshuai 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1600
 * @tc.name      : test inet_ntop IPv6 normal
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetNtopIpv6Normal, Function | MediumTest | Level2)
{
    int iret;
    const char* ret = nullptr;
    struct in6_addr inputAddr = {0};
    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);
        ASSERT_EQ(1, iret);
        ret = inet_ntop(AF_INET6, &inputAddr, rstBuff, sizeof(rstBuff));
        if (ret == nullptr) {
            EXPECT_TRUE(ret != NULL) << "ErrInfo:inet_ntop NULL [" << expectAddrs[i] << "]";
        } else {
            printf("inet_ntop expect [%s]: ret[%s], buf[%s]\n", expectAddrs[i], ret, rstBuff);
            EXPECT_STREQ(expectAddrs[i], ret);
            EXPECT_STREQ(expectAddrs[i], rstBuff);
        }
    }
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1700
 * @tc.name      : test inet_ntop IPv6 boundary input
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetNtopIpv6Abnormal, Function | MediumTest | Level2)
{
    const char* ret = nullptr;
    struct in6_addr inputAddr = {0};
    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);
        }
        ret = inet_ntop(AF_INET6, &inputAddr, rstBuff, sizeof(rstBuff));
        ASSERT_TRUE(ret != NULL);
        EXPECT_STREQ(expectStr[i], ret);
        EXPECT_STREQ(expectStr[i], rstBuff);
    }
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1800
 * @tc.name      : test inet_ntop with invalid family
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(ActsLwipTest, testInetNtopInvalidFamily, Function | MediumTest | Level2)
{
    int iret;
    const char* ret = nullptr;
    struct in6_addr inputAddr = {0};
    char rstBuff[INET6_ADDRSTRLEN];

    iret = inet_pton(AF_INET6, "1::", &inputAddr);
    ASSERT_EQ(1, iret);
    ret = inet_ntop(AF_IPX, &inputAddr, rstBuff, sizeof(rstBuff));
    EXPECT_TRUE(ret == NULL);
    ret = inet_ntop(-1, &inputAddr, rstBuff, sizeof(rstBuff));
    EXPECT_TRUE(ret == NULL);
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1900
W
wenjun 已提交
1035 1036 1037
 * @tc.name      : test invalid parameter
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
1038
HWTEST_F(ActsLwipTest, testInvalidParameter, Function | MediumTest | Level3)
W
wenjun 已提交
1039 1040 1041 1042 1043 1044
{
    int ret;
    int fdFail = -1;
    int fdSuccess = -1;

    fdFail = socket(0, 0, 0);
M
mamingshuai 已提交
1045
    EXPECT_EQ(-1, fdFail);
W
wenjun 已提交
1046
    fdSuccess = socket(AF_INET, SOCK_STREAM, 0);
M
mamingshuai 已提交
1047
    EXPECT_NE(-1, fdSuccess);
W
wenjun 已提交
1048 1049

    ret = ::bind(fdFail, nullptr, sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1050
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1051
    ret = ::bind(fdSuccess, nullptr, sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1052
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1053 1054

    ret = connect(fdFail, nullptr, sizeof(struct sockaddr));
M
mamingshuai 已提交
1055
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1056
    ret = connect(fdSuccess, nullptr, sizeof(struct sockaddr));
M
mamingshuai 已提交
1057
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1058 1059

    ret = accept(fdFail, nullptr, nullptr);
M
mamingshuai 已提交
1060
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1061
    ret = accept(fdSuccess, nullptr, nullptr);
M
mamingshuai 已提交
1062
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1063 1064

    ret = getsockname(fdFail, nullptr, nullptr);
M
mamingshuai 已提交
1065
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1066
    ret = getsockname(fdSuccess, nullptr, nullptr);
M
mamingshuai 已提交
1067
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1068 1069

    ret = getpeername(fdFail, nullptr, nullptr);
M
mamingshuai 已提交
1070
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1071
    ret = getpeername(fdSuccess, nullptr, nullptr);
M
mamingshuai 已提交
1072
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1073 1074

    ret = send(fdFail, nullptr, strlen(g_srvMsg), 0);
M
mamingshuai 已提交
1075
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1076
    ret = send(fdSuccess, nullptr, strlen(g_srvMsg), 0);
M
mamingshuai 已提交
1077
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1078 1079

    ret = sendto(fdFail, nullptr, strlen(g_srvMsg), 0, nullptr, (socklen_t)sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1080
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1081
    ret = sendto(fdSuccess, nullptr, strlen(g_srvMsg), 0, nullptr, (socklen_t)sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1082
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1083

M
mamingshuai 已提交
1084 1085 1086 1087
    ret = recv(fdFail, nullptr, strlen(g_srvMsg), 0);
    EXPECT_EQ(-1, ret);
    ret = recv(fdSuccess, nullptr, strlen(g_srvMsg), 0);
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1088 1089 1090

    struct msghdr msg = {0};
    ret = recvmsg(fdFail, &msg, 0);
M
mamingshuai 已提交
1091
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1092
    ret = recvmsg(fdSuccess, &msg, 0);
M
mamingshuai 已提交
1093
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1094

M
mamingshuai 已提交
1095 1096 1097 1098
    ret = recvfrom(fdFail, nullptr, strlen(g_srvMsg), 0, nullptr, nullptr);
    EXPECT_EQ(-1, ret);
    ret = recvfrom(fdSuccess, nullptr, strlen(g_srvMsg), 0, nullptr, nullptr);
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1099 1100

    ret = setsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t)sizeof(struct timeval));
M
mamingshuai 已提交
1101
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1102
    ret = setsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t)sizeof(struct timeval));
M
mamingshuai 已提交
1103
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1104
    ret = getsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t*)sizeof(struct timeval));
M
mamingshuai 已提交
1105
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1106
    ret = getsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t*)sizeof(struct timeval));
M
mamingshuai 已提交
1107
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1108 1109

    ret = sendmsg(fdFail, nullptr, 0);
M
mamingshuai 已提交
1110
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1111
    ret = sendmsg(fdSuccess, nullptr, 0);
M
mamingshuai 已提交
1112
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1113 1114

    ret = listen(fdFail, 0);
M
mamingshuai 已提交
1115
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1116
    ret = select(fdFail, nullptr, nullptr, nullptr, nullptr);
M
mamingshuai 已提交
1117
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1118 1119

    ret = shutdown(fdFail, SHUT_RD);
M
mamingshuai 已提交
1120
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1121
    ret = shutdown(fdSuccess, -1);
M
mamingshuai 已提交
1122
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1123 1124

    ret = close(fdSuccess);
M
mamingshuai 已提交
1125
    EXPECT_EQ(0, ret);
W
wenjun 已提交
1126
}