• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/socket.h>
33 #include <poll.h>
34 #include "It_container_test.h"
35 
36 static const int PORT = 8004;
37 static const char *LOCALHOST = "127.0.0.1";
38 
UdpTcpBind(int * sock1,int * sock2)39 static int UdpTcpBind(int *sock1, int *sock2)
40 {
41     int ret;
42     int udp_sock;
43     int tcp_sock;
44 
45     udp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
46     if (udp_sock < 0) {
47         return EXIT_CODE_ERRNO_1;
48     }
49 
50     struct sockaddr_in sa;
51     sa.sin_family = AF_INET;
52     sa.sin_addr.s_addr = inet_addr(LOCALHOST);
53     sa.sin_port = htons(PORT);
54 
55     ret = bind(udp_sock, const_cast<struct sockaddr *>(reinterpret_cast<struct sockaddr *>(&sa)), sizeof(sa));
56     if (ret != 0) {
57         return EXIT_CODE_ERRNO_2;
58     }
59 
60     tcp_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
61     if (tcp_sock < 0) {
62         return EXIT_CODE_ERRNO_3;
63     }
64 
65     ret = bind(tcp_sock, const_cast<struct sockaddr *>(reinterpret_cast<struct sockaddr *>(&sa)), sizeof(sa));
66     if (ret != 0) {
67         return EXIT_CODE_ERRNO_4;
68     }
69 
70     (*sock1) = udp_sock;
71     (*sock2) = tcp_sock;
72 
73     return 0;
74 }
75 
ClientFunc(void * param)76 static int ClientFunc(void *param)
77 {
78     (void)param;
79     int ret;
80     int udp_sock;
81     int tcp_sock;
82 
83     ret = UdpTcpBind(&udp_sock, &tcp_sock);
84     if (ret != 0) {
85         return EXIT_CODE_ERRNO_1;
86     }
87 
88     (void)close(udp_sock);
89     (void)close(tcp_sock);
90 
91     return ret;
92 }
93 
ItNetContainer008(void)94 void ItNetContainer008(void)
95 {
96     int ret;
97     int status;
98     int udp_sock;
99     int tcp_sock;
100 
101     ret = UdpTcpBind(&udp_sock, &tcp_sock);
102     ASSERT_EQ(ret, 0);
103 
104     char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
105     EXPECT_STRNE(stack, nullptr);
106     char *stackTop = stack + STACK_SIZE;
107     int arg = CHILD_FUNC_ARG;
108     int pid = clone(ClientFunc, stackTop, SIGCHLD | CLONE_NEWNET, &arg);
109     ASSERT_NE(pid, -1);
110 
111     ret = waitpid(pid, &status, 0);
112     ASSERT_EQ(ret, pid);
113 
114     int exitCode = WEXITSTATUS(status);
115     ASSERT_EQ(exitCode, 0);
116 
117     (void)close(udp_sock);
118     (void)close(tcp_sock);
119 }
120