sparc: refactor sparc64 __mpn_addmul_1 selector to C
[glibc.git] / support / tst-support-namespace.c
blobdbe7cc07c8f8aaa4eb4701362468c5754cffaa7a
1 /* Test entering namespaces.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
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>
27 /* Check that the loopback interface provides multiple addresses which
28 can be used to run independent servers. */
29 static void
30 test_localhost_bind (void)
32 printf ("info: testing loopback interface with multiple addresses\n");
34 /* Create the two server addresses. */
35 static const struct addrinfo hints =
37 .ai_family = AF_INET,
38 .ai_socktype = SOCK_DGRAM,
39 .ai_protocol = IPPROTO_UDP,
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);
46 /* Create the server scokets and bind them to these addresses. */
47 int sockets[3];
48 for (int i = 0; i < 3; ++i)
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);
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)
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));
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)
72 int buf;
73 TEST_VERIFY (recv (sockets[i], &buf, sizeof (buf), 0) == sizeof (buf));
74 TEST_VERIFY (buf == i);
76 for (int i = 0; i < 3; ++i)
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);
86 /* Close all sockets and free the addresses. */
87 for (int i = 0; i < 3; ++i)
89 freeaddrinfo (ai[i]);
90 xclose (sockets[i]);
92 xclose (client);
96 static int
97 do_test (void)
99 bool root = support_become_root ();
100 if (root)
101 printf ("info: acquired root-like privileges\n");
102 bool netns = support_enter_network_namespace ();
103 if (netns)
104 printf ("info: entered network namespace\n");
105 if (support_in_uts_namespace ())
106 printf ("info: also entered UTS namespace\n");
108 if (root && netns)
109 test_localhost_bind ();
111 return 0;
114 #include <support/test-driver.c>