• 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 <arpa/inet.h>
31 #include <net/if.h>
32 #include "It_container_test.h"
33 
34 using namespace std;
35 static const int IpLen = 16;
36 static const char *NETMASK = "255.255.255.0";
37 static const char *GW = "192.168.100.1";
38 static const char *IFNAME = "veth0";
39 static const char *PEER_IP = "192.168.100.5";
40 
SetIP(char * ip)41 static int SetIP(char *ip)
42 {
43     struct ifreq ifr;
44     int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
45     if (fd < 0) {
46         return -1;
47     }
48     int ret = strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), "eth0", IFNAMSIZ);
49     if (ret != EOK) {
50         (void)close(fd);
51         return -1;
52     }
53     ifr.ifr_addr.sa_family = AF_INET;
54     inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + 2); /* 2: offset */
55     ret = ioctl(fd, SIOCSIFADDR, &ifr);
56     if (ret != 0) {
57         printf("[ERR][%s:%d] ioctl SIOCSIFADDR failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
58         (void)close(fd);
59         return -1;
60     }
61     ret = close(fd);
62     if (ret != 0) {
63         return -1;
64     }
65     return 0;
66 }
67 
ChildFunc(void * arg)68 static int ChildFunc(void *arg)
69 {
70     (void)arg;
71     int ret;
72     char oldIp[IpLen] = {NULL};
73     char newIp[IpLen] = {NULL};
74 
75     ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
76     if (ret != 0) {
77         return EXIT_CODE_ERRNO_1;
78     }
79 
80     ret = unshare(CLONE_NEWNET);
81     if (ret < 0) {
82         return EXIT_CODE_ERRNO_2;
83     }
84 
85     ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
86     if (ret == 0) {
87         return EXIT_CODE_ERRNO_3;
88     }
89 
90     ret = SetIP("192.168.1.234");
91     if (ret == 0) {
92         return EXIT_CODE_ERRNO_4;
93     }
94 
95     ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
96     if (ret != 0) {
97         return EXIT_CODE_ERRNO_5;
98     }
99 
100     (void)memset_s(newIp, IpLen, 0, IpLen);
101     ret = NetContainerGetLocalIP(IFNAME, newIp, IpLen);
102     if (ret != 0) {
103         return EXIT_CODE_ERRNO_6;
104     }
105 
106     if (strcmp(PEER_IP, newIp) != 0) {
107         return EXIT_CODE_ERRNO_7;
108     }
109     printf("######## [%s:%d] %s: %s ########\n", __FUNCTION__, __LINE__, IFNAME, newIp);
110     return 0;
111 }
112 
ItNetContainer002(void)113 void ItNetContainer002(void)
114 {
115     int ret;
116     char oldIp[IpLen] = {NULL};
117     char newIp[IpLen] = {NULL};
118 
119     char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
120     EXPECT_STRNE(stack, NULL);
121     char *stackTop = stack + STACK_SIZE;
122 
123     ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
124     ASSERT_EQ(ret, 0);
125 
126     auto pid = clone(ChildFunc, stackTop, SIGCHLD, NULL);
127     ASSERT_NE(pid, -1);
128 
129     int status;
130     ret = waitpid(pid, &status, 0);
131     ASSERT_EQ(ret, pid);
132 
133     int exitCode = WEXITSTATUS(status);
134     ASSERT_EQ(exitCode, 0);
135 
136     ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
137     ASSERT_EQ(ret, 0);
138 
139     ret = strcmp(oldIp, newIp);
140     ASSERT_EQ(ret, 0);
141 }
142