• 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 "It_container_test.h"
31 
UtsContainerTest(void * arg)32 static int UtsContainerTest(void *arg)
33 {
34     (void)arg;
35     std::string containerType = "uts";
36 
37     int parentPid = getpid();
38     auto parentlink = ReadlinkContainer(parentPid, containerType);
39 
40     int childsPid = CloneWrapper(ChildFunction, CLONE_NEWUTS, NULL);
41     if (childsPid == -1) {
42         return EXIT_CODE_ERRNO_1;
43     }
44     auto childlink = ReadlinkContainer(childsPid, containerType);
45 
46     std::string filePath = GenContainerLinkPath(childsPid, containerType);
47     int fd = open(filePath.c_str(), O_RDONLY);
48     if (fd == -1) {
49         return EXIT_CODE_ERRNO_2;
50     }
51 
52     int ret = setns(fd, CLONE_NEWUTS);
53     (void)close(fd);
54     if (ret == -1) {
55         return EXIT_CODE_ERRNO_3;
56     }
57 
58     auto parentlink1 = ReadlinkContainer(parentPid, containerType);
59 
60     ret = parentlink.compare(parentlink1);
61     if (ret == 0) {
62         return EXIT_CODE_ERRNO_4;
63     }
64     ret = parentlink1.compare(childlink);
65     if (ret != 0) {
66         return EXIT_CODE_ERRNO_5;
67     }
68 
69     int status;
70     ret = waitpid(childsPid, &status, 0);
71     if (ret != childsPid) {
72         return EXIT_CODE_ERRNO_6;
73     }
74 
75     int exitCode = WEXITSTATUS(status);
76     if (exitCode != 0) {
77         return EXIT_CODE_ERRNO_7;
78     }
79     return 0;
80 }
81 
ItUtsContainer006(void)82 void ItUtsContainer006(void)
83 {
84     int ret;
85 
86     int arg = CHILD_FUNC_ARG;
87     auto pid = CloneWrapper(UtsContainerTest, CLONE_NEWUTS, &arg);
88     ASSERT_NE(pid, -1);
89 
90     int status;
91     ret = waitpid(pid, &status, 0);
92     ASSERT_EQ(ret, pid);
93 
94     ret = WIFEXITED(status);
95     ASSERT_NE(ret, 0);
96 
97     int exitCode = WEXITSTATUS(status);
98     ASSERT_EQ(exitCode, 0);
99 }
100