1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include "It_container_test.h"
33
34 static const char *LOCALHOST = "127.0.0.1";
35 static const int LOCALPORT = 8003;
36 static const char *MSG = "Tis is UDP Test!";
37 static const int DATA_LEN = 128;
38
ChildFunc(void * arg)39 static int ChildFunc(void *arg)
40 {
41 int ret;
42 int sock;
43 int recv_data_len;
44 char recv_data[DATA_LEN];
45 struct sockaddr_in addr;
46
47 sock = socket(AF_INET, SOCK_DGRAM, 0);
48 if (sock < 0) {
49 return EXIT_CODE_ERRNO_1;
50 }
51
52 addr.sin_family = AF_INET;
53 addr.sin_addr.s_addr = inet_addr(LOCALHOST);
54 addr.sin_port = htons(LOCALPORT);
55 (void)memset_s(&(addr.sin_zero), sizeof(addr.sin_zero), 0, sizeof(addr.sin_zero));
56
57 ret = bind(sock, const_cast<struct sockaddr *>(reinterpret_cast<struct sockaddr *>(&addr)),
58 sizeof(struct sockaddr));
59 if (ret < 0) {
60 return EXIT_CODE_ERRNO_2;
61 }
62
63 ret = sendto(sock, MSG, DATA_LEN, 0, const_cast<struct sockaddr *>(reinterpret_cast<struct sockaddr *>(&addr)),
64 (socklen_t)sizeof(addr));
65 if (ret < 0) {
66 return EXIT_CODE_ERRNO_3;
67 }
68
69 ret = recvfrom(sock, recv_data, DATA_LEN, 0, nullptr, nullptr);
70 if (ret < 0) {
71 return EXIT_CODE_ERRNO_4;
72 }
73
74 ret = strcmp(recv_data, MSG);
75 if (ret != 0) {
76 return EXIT_CODE_ERRNO_5;
77 }
78
79 (void)close(sock);
80
81 return 0;
82 }
83
ItNetContainer007(void)84 void ItNetContainer007(void)
85 {
86 int ret = 0;
87 int status;
88
89 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
90 EXPECT_STRNE(stack, nullptr);
91 char *stackTop = stack + STACK_SIZE;
92
93 int arg = CHILD_FUNC_ARG;
94 auto pid = clone(ChildFunc, stackTop, SIGCHLD | CLONE_NEWNET, &arg);
95 ASSERT_NE(pid, -1);
96
97 ret = waitpid(pid, &status, 0);
98 ASSERT_EQ(ret, pid);
99
100 int exitCode = WEXITSTATUS(status);
101 ASSERT_EQ(exitCode, 0);
102 }
103