1/* Test entering namespaces.
2 Copyright (C) 2016-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <errno.h>
20#include <netdb.h>
21#include <stdio.h>
22#include <support/check.h>
23#include <support/namespace.h>
24#include <support/xsocket.h>
25#include <support/xunistd.h>
26
27/* Check that the loopback interface provides multiple addresses which
28 can be used to run independent servers. */
29static void
30test_localhost_bind (void)
31{
32 printf (format: "info: testing loopback interface with multiple addresses\n");
33
34 /* Create the two server addresses. */
35 static const struct addrinfo hints =
36 {
37 .ai_family = AF_INET,
38 .ai_socktype = SOCK_DGRAM,
39 .ai_protocol = IPPROTO_UDP,
40 };
41 struct addrinfo *ai[3];
42 TEST_VERIFY_EXIT (getaddrinfo ("127.0.0.1", "53", &hints, ai + 0) == 0);
43 TEST_VERIFY_EXIT (getaddrinfo ("127.0.0.2", "53", &hints, ai + 1) == 0);
44 TEST_VERIFY_EXIT (getaddrinfo ("127.0.0.3", "53", &hints, ai + 2) == 0);
45
46 /* Create the server scokets and bind them to these addresses. */
47 int sockets[3];
48 for (int i = 0; i < 3; ++i)
49 {
50 sockets[i] = xsocket
51 (ai[i]->ai_family, ai[i]->ai_socktype, ai[i]->ai_protocol);
52 xbind (sockets[i], ai[i]->ai_addr, ai[i]->ai_addrlen);
53 }
54
55 /* Send two packets to each server. */
56 int client = xsocket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
57 for (int i = 0; i < 3; ++i)
58 {
59 TEST_VERIFY (sendto (client, &i, sizeof (i), 0,
60 ai[i]->ai_addr, ai[i]->ai_addrlen) == sizeof (i));
61 int j = i + 256;
62 TEST_VERIFY (sendto (client, &j, sizeof (j), 0,
63 ai[i]->ai_addr, ai[i]->ai_addrlen) == sizeof (j));
64 }
65
66 /* Check that the packets can be received with the expected
67 contents. Note that the receive calls interleave differently,
68 which hopefully proves that the sockets are, indeed,
69 independent. */
70 for (int i = 0; i < 3; ++i)
71 {
72 int buf;
73 TEST_VERIFY (recv (sockets[i], &buf, sizeof (buf), 0) == sizeof (buf));
74 TEST_VERIFY (buf == i);
75 }
76 for (int i = 0; i < 3; ++i)
77 {
78 int buf;
79 TEST_VERIFY (recv (sockets[i], &buf, sizeof (buf), 0) == sizeof (buf));
80 TEST_VERIFY (buf == i + 256);
81 /* Check that there is no more data to receive. */
82 TEST_VERIFY (recv (sockets[i], &buf, sizeof (buf), MSG_DONTWAIT) == -1);
83 TEST_VERIFY (errno == EWOULDBLOCK || errno == EAGAIN);
84 }
85
86 /* Close all sockets and free the addresses. */
87 for (int i = 0; i < 3; ++i)
88 {
89 freeaddrinfo (ai: ai[i]);
90 xclose (sockets[i]);
91 }
92 xclose (client);
93}
94
95
96static int
97do_test (void)
98{
99 bool root = support_become_root ();
100 if (root)
101 printf (format: "info: acquired root-like privileges\n");
102 bool netns = support_enter_network_namespace ();
103 if (netns)
104 printf (format: "info: entered network namespace\n");
105 if (support_in_uts_namespace ())
106 printf (format: "info: also entered UTS namespace\n");
107
108 if (root && netns)
109 test_localhost_bind ();
110
111 return 0;
112}
113
114#include <support/test-driver.c>
115

source code of glibc/support/tst-support-namespace.c