• 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         (void)close(fd);
58         printf("[ERR][%s:%d] ioctl SIOCSIFADDR failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
59         return -1;
60     }
61 
62     ret = close(fd);
63     if (ret != 0) {
64         return -1;
65     }
66     return 0;
67 }
68 
ChildFunc(void * arg)69 static int ChildFunc(void *arg)
70 {
71     (void)arg;
72     int ret;
73     char oldIp[IpLen] = {NULL};
74     char newIp[IpLen] = {NULL};
75 
76     ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
77     if (ret == 0) {
78         return EXIT_CODE_ERRNO_1;
79     }
80 
81     ret = SetIP("192.168.1.233");
82     if (ret == 0) {
83         return EXIT_CODE_ERRNO_2;
84     }
85 
86     ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
87     if (ret != 0) {
88         return EXIT_CODE_ERRNO_3;
89     }
90 
91     (void)memset_s(newIp, IpLen, 0, IpLen);
92     ret = NetContainerGetLocalIP(IFNAME, newIp, IpLen);
93     if (ret != 0) {
94         return EXIT_CODE_ERRNO_4;
95     }
96 
97     if (strcmp(PEER_IP, newIp) != 0) {
98         return EXIT_CODE_ERRNO_5;
99     }
100     printf("######## [%s:%d] %s: %s ########\n", __FUNCTION__, __LINE__, IFNAME, newIp);
101     return 0;
102 }
103 
ItNetContainer001(void)104 void ItNetContainer001(void)
105 {
106     int ret;
107     char oldIp[IpLen] = {NULL};
108     char newIp[IpLen] = {NULL};
109 
110     char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
111     EXPECT_STRNE(stack, NULL);
112     char *stackTop = stack + STACK_SIZE;
113 
114     ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
115     ASSERT_EQ(ret, 0);
116 
117     auto pid = clone(ChildFunc, stackTop, SIGCHLD | CLONE_NEWNET, NULL);
118     ASSERT_NE(pid, -1);
119 
120     int status;
121     ret = waitpid(pid, &status, 0);
122     ASSERT_EQ(ret, pid);
123 
124     int exitCode = WEXITSTATUS(status);
125     ASSERT_EQ(exitCode, 0);
126 
127     ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
128     ASSERT_EQ(ret, 0);
129 
130     ret = strcmp(oldIp, newIp);
131     ASSERT_EQ(ret, 0);
132 }
133