ActsLwipTest.cpp 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
 * 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"
S
shuxiong 已提交
17 18 19
#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 <signal.h>
25
#include "securec.h"
W
wenjun 已提交
26 27 28 29

using namespace std;
using namespace testing::ext;

M
mamingshuai 已提交
30
static const int TEST_FD_COUNT = 10;
W
wenjun 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44
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
45 46 47 48 49
    static void SetUpTestCase(void)
    {
        // Ignore SIGPIPE Signals
        signal(SIGPIPE, SIG_IGN);
    }
W
wenjun 已提交
50 51 52 53 54 55 56 57
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void) {}
    // Testcase setup
    virtual void SetUp() {}
    // Testcase teardown
    virtual void TearDown() {}
};

M
mamingshuai 已提交
58
static int CommInitTcpServer(int port)
W
wenjun 已提交
59
{
M
mamingshuai 已提交
60 61 62 63 64 65 66 67 68 69
    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 已提交
70
    struct sockaddr_in srvAddr = {0};
M
mamingshuai 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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 已提交
90

M
mamingshuai 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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 已提交
106 107
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
M
mamingshuai 已提交
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 150 151 152 153 154
    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 已提交
155

M
mamingshuai 已提交
156 157 158 159
static void* SampleTcpServerTask(void* p)
{
    int srvFd = CommInitTcpServer(STACK_PORT);
    EXPECT_NE(-1, srvFd);
W
wenjun 已提交
160 161

    WAIT();
M
mamingshuai 已提交
162 163 164
    struct sockaddr_in clnAddr = {0};
    socklen_t clnAddrLen = sizeof(clnAddr);
    int clnFd = accept(srvFd, (struct sockaddr*)&clnAddr, &clnAddrLen);
W
wenjun 已提交
165
    printf("[tcp server]accept <%s:%d>\n", inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
M
mamingshuai 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    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 已提交
182

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

    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
193 194 195
    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 已提交
196 197 198

    WAIT();
    int len = 2;
M
mamingshuai 已提交
199 200
    struct msghdr msg = {0};
    struct iovec iov[2] = {0};
W
wenjun 已提交
201 202 203 204 205
    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 已提交
206
    EXPECT_EQ(0, ret);
W
wenjun 已提交
207 208 209 210 211 212 213 214
    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 已提交
215
    ret = sendmsg(clnFd, &msg, 0);
W
wenjun 已提交
216
    printf("[tcp server]sendmsg, ret=%d\n", ret);
M
mamingshuai 已提交
217
    EXPECT_EQ(len * srvMsgLen, (unsigned int)ret);
W
wenjun 已提交
218 219 220 221 222 223 224 225 226 227

    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 已提交
228 229 230 231 232 233 234 235 236 237 238
    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 已提交
239 240 241
    return nullptr;
}

M
mamingshuai 已提交
242
static void* SampleTcpClientTask(void* p)
W
wenjun 已提交
243
{
M
mamingshuai 已提交
244 245 246
    int clnFd = socket(AF_INET, SOCK_STREAM, 0);
    printf("[tcp client]create socket\n");
    EXPECT_NE(-1, clnFd);
W
wenjun 已提交
247 248

    WAIT();
M
mamingshuai 已提交
249
    struct sockaddr_in srvAddr = {0};
W
wenjun 已提交
250 251 252
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
    srvAddr.sin_port = htons(PEER_PORT);
M
mamingshuai 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    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 已提交
273 274
    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
    ret = strcpy_s(buf, BUF_SIZE, g_cliMsg);
M
mamingshuai 已提交
275 276
    EXPECT_EQ(0, ret);
    ret = send(clnFd, buf, cliMsgLen, 0);
W
wenjun 已提交
277
    printf("[tcp client]send, ret=%d\n", ret);
M
mamingshuai 已提交
278
    EXPECT_EQ(cliMsgLen, (unsigned int)ret);
W
wenjun 已提交
279 280

    memset_s(buf, BUF_SIZE, 0, BUF_SIZE);
M
mamingshuai 已提交
281 282 283
    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 已提交
284 285 286

    WAIT();
    int len = 2;
M
mamingshuai 已提交
287 288 289
    struct msghdr msg = {0};
    struct iovec iov[2] = {0};
    struct sockaddr_in clnAddr = {0};
W
wenjun 已提交
290 291 292 293 294
    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 已提交
295
    EXPECT_EQ(0, ret);
W
wenjun 已提交
296 297 298 299 300 301 302 303
    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 已提交
304
    ret = sendmsg(clnFd, &msg, 0);
W
wenjun 已提交
305
    printf("[tcp client]sendmsg, ret=%d\n", ret);
M
mamingshuai 已提交
306
    EXPECT_EQ(len * cliMsgLen, (unsigned int)ret);
W
wenjun 已提交
307 308 309 310 311 312 313 314 315 316

    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 已提交
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 370 371 372 373 374
    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 已提交
375

M
mamingshuai 已提交
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
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 已提交
407

M
mamingshuai 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420
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;
S
shuxiong 已提交
421 422
    long timeout = static_cast<long>(timeoutSec);
    struct timeval timev = {.tv_sec = timeout, .tv_usec = 0};
M
mamingshuai 已提交
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 493 494 495 496 497
    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 已提交
498 499 500 501
}

/**
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_0100
M
mamingshuai 已提交
502
 * @tc.name      : sample test tcp
W
wenjun 已提交
503 504
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
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 已提交
533 534
{
    int len = 2;
M
mamingshuai 已提交
535
    char buf[BUF_SIZE + 1];
W
wenjun 已提交
536 537 538 539 540 541
    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 已提交
542 543
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);
    ASSERT_NE(-1, sfd);
W
wenjun 已提交
544 545 546
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
    srvAddr.sin_port = htons(STACK_PORT);
M
mamingshuai 已提交
547 548
    int ret = ::bind(sfd, (struct sockaddr*)&srvAddr, sizeof(srvAddr));
    ASSERT_EQ(0, ret);
W
wenjun 已提交
549 550 551 552

    clnAddr.sin_family = AF_INET;
    clnAddr.sin_addr.s_addr = inet_addr(g_localHost);
    clnAddr.sin_port = htons(PEER_PORT);
M
mamingshuai 已提交
553 554 555 556 557
    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 已提交
558

M
mamingshuai 已提交
559 560 561
    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 已提交
562 563 564 565

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

    ret = close(sfd);
M
mamingshuai 已提交
581
    EXPECT_NE(-1, ret);
W
wenjun 已提交
582 583 584
}

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

    int error = -1;
    len = sizeof(error);
M
mamingshuai 已提交
751 752 753 754
    int ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
    EXPECT_EQ(0, ret);
    EXPECT_EQ(0, error);

W
wenjun 已提交
755 756
    len = sizeof(timeout);
    ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
M
mamingshuai 已提交
757
    EXPECT_EQ(0, ret);
W
wenjun 已提交
758 759 760 761

    timeout.tv_sec = 1000;
    len = sizeof(timeout);
    ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, len);
M
mamingshuai 已提交
762
    EXPECT_EQ(0, ret);
W
wenjun 已提交
763 764 765

    memset_s(&timeout, len, 0, len);
    ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len);
M
mamingshuai 已提交
766 767 768 769 770 771 772 773 774 775 776 777
    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 已提交
778 779 780 781

    error = -1;
    len = sizeof(error);
    ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
M
mamingshuai 已提交
782 783
    EXPECT_EQ(0, ret);
    EXPECT_EQ(0, error);
W
wenjun 已提交
784 785

    ret = close(fd);
M
mamingshuai 已提交
786
    EXPECT_EQ(0, ret);
W
wenjun 已提交
787 788 789
}

/**
M
mamingshuai 已提交
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 822 823 824 825 826
 * @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 已提交
827 828
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
829
HWTEST_F(ActsLwipTest, testInetPtonIpv4Normal, Function | MediumTest | Level2)
W
wenjun 已提交
830 831
{
    int ret;
M
mamingshuai 已提交
832 833 834 835 836 837 838
    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 已提交
839

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

M
mamingshuai 已提交
852 853 854 855 856 857 858 859 860
/**
 * @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 已提交
861 862
    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 已提交
863 864 865 866
        ret = inet_pton(AF_INET, cpAddrs[i], &rst);
        EXPECT_EQ(0, ret) << "ErrInfo:inet_pton abnormal [" << cpAddrs[i] << "]";
    }
}
W
wenjun 已提交
867

M
mamingshuai 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
/**
 * @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 已提交
883 884 885
}

/**
M
mamingshuai 已提交
886 887
 * @tc.number    : SUB_COMMUNICATION_LWIP_SDK_1200
 * @tc.name      : test inet_pton IPv6 abnormal
W
wenjun 已提交
888 889
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
890
HWTEST_F(ActsLwipTest, testInetPtonIpv6Abnormal, Function | MediumTest | Level2)
W
wenjun 已提交
891 892
{
    int ret;
M
mamingshuai 已提交
893 894 895 896 897 898 899 900
    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 已提交
901

M
mamingshuai 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914
/**
 * @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 已提交
915

M
mamingshuai 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
/**
 * @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 已提交
931

M
mamingshuai 已提交
932 933 934 935 936 937 938 939
    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));
940
        ASSERT_NE(ret, nullptr);
941 942 943
        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 已提交
944 945
    }
}
W
wenjun 已提交
946

M
mamingshuai 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
/**
 * @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 已提交
965 966 967
}

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

    fdFail = socket(0, 0, 0);
M
mamingshuai 已提交
1050
    EXPECT_EQ(-1, fdFail);
W
wenjun 已提交
1051
    fdSuccess = socket(AF_INET, SOCK_STREAM, 0);
M
mamingshuai 已提交
1052
    EXPECT_NE(-1, fdSuccess);
W
wenjun 已提交
1053 1054

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

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

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

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

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

    ret = send(fdFail, nullptr, strlen(g_srvMsg), 0);
M
mamingshuai 已提交
1080
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1081
    ret = send(fdSuccess, nullptr, strlen(g_srvMsg), 0);
M
mamingshuai 已提交
1082
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1083 1084

    ret = sendto(fdFail, nullptr, strlen(g_srvMsg), 0, nullptr, (socklen_t)sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1085
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1086
    ret = sendto(fdSuccess, nullptr, strlen(g_srvMsg), 0, nullptr, (socklen_t)sizeof(struct sockaddr_in));
M
mamingshuai 已提交
1087
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1088

M
mamingshuai 已提交
1089 1090 1091 1092
    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 已提交
1093 1094 1095

    struct msghdr msg = {0};
    ret = recvmsg(fdFail, &msg, 0);
M
mamingshuai 已提交
1096
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1097
    ret = recvmsg(fdSuccess, &msg, 0);
M
mamingshuai 已提交
1098
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1099

M
mamingshuai 已提交
1100 1101 1102 1103
    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 已提交
1104 1105

    ret = setsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t)sizeof(struct timeval));
M
mamingshuai 已提交
1106
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1107
    ret = setsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t)sizeof(struct timeval));
M
mamingshuai 已提交
1108
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1109
    ret = getsockopt(fdFail, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t*)sizeof(struct timeval));
M
mamingshuai 已提交
1110
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1111
    ret = getsockopt(fdSuccess, SOL_SOCKET, SO_RCVTIMEO, nullptr, (socklen_t*)sizeof(struct timeval));
M
mamingshuai 已提交
1112
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1113 1114

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

    ret = listen(fdFail, 0);
M
mamingshuai 已提交
1120
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1121
    ret = select(fdFail, nullptr, nullptr, nullptr, nullptr);
M
mamingshuai 已提交
1122
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1123 1124

    ret = shutdown(fdFail, SHUT_RD);
M
mamingshuai 已提交
1125
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1126
    ret = shutdown(fdSuccess, -1);
M
mamingshuai 已提交
1127
    EXPECT_EQ(-1, ret);
W
wenjun 已提交
1128 1129

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