support: Use macros for *stat wrappers
[glibc.git] / nss / tst-nss-gai-actions.c
blobe42bbd6caeaa37a6fc377fa4f8e6b30348c71b9b
1 /* Test continue and merge NSS actions for getaddrinfo.
2 Copyright The GNU Toolchain Authors.
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 <https://www.gnu.org/licenses/>. */
19 #include <dlfcn.h>
20 #include <gnu/lib-names.h>
21 #include <nss.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <support/check.h>
27 #include <support/format_nss.h>
28 #include <support/support.h>
29 #include <support/xstdio.h>
30 #include <support/xunistd.h>
32 enum
34 ACTION_MERGE = 0,
35 ACTION_CONTINUE,
38 static const char *
39 family_str (int family)
41 switch (family)
43 case AF_UNSPEC:
44 return "AF_UNSPEC";
45 case AF_INET:
46 return "AF_INET";
47 default:
48 __builtin_unreachable ();
52 static const char *
53 action_str (int action)
55 switch (action)
57 case ACTION_MERGE:
58 return "merge";
59 case ACTION_CONTINUE:
60 return "continue";
61 default:
62 __builtin_unreachable ();
66 static void
67 do_one_test (int action, int family, bool canon)
69 struct addrinfo hints =
71 .ai_family = family,
74 struct addrinfo *ai;
76 if (canon)
77 hints.ai_flags = AI_CANONNAME;
79 printf ("***** Testing \"files [SUCCESS=%s] files\" for family %s, %s\n",
80 action_str (action), family_str (family),
81 canon ? "AI_CANONNAME" : "");
83 int ret = getaddrinfo ("example.org", "80", &hints, &ai);
85 switch (action)
87 case ACTION_MERGE:
88 if (ret == 0)
90 char *formatted = support_format_addrinfo (ai, ret);
92 printf ("merge unexpectedly succeeded:\n %s\n", formatted);
93 support_record_failure ();
94 free (formatted);
95 break;
97 else
98 return;
99 case ACTION_CONTINUE:
101 char *formatted = support_format_addrinfo (ai, ret);
103 /* Verify that the result appears exactly once. */
104 const char *expected = "address: STREAM/TCP 192.0.0.1 80\n"
105 "address: DGRAM/UDP 192.0.0.1 80\n"
106 "address: RAW/IP 192.0.0.1 80\n";
108 const char *contains = strstr (formatted, expected);
109 const char *contains2 = NULL;
111 if (contains != NULL)
112 contains2 = strstr (contains + strlen (expected), expected);
114 if (contains == NULL || contains2 != NULL)
116 printf ("continue failed:\n%s\n", formatted);
117 support_record_failure ();
120 free (formatted);
121 break;
123 default:
124 __builtin_unreachable ();
128 static void
129 do_one_test_set (int action)
131 char buf[32];
133 snprintf (buf, sizeof (buf), "files [SUCCESS=%s] files",
134 action_str (action));
135 __nss_configure_lookup ("hosts", buf);
137 do_one_test (action, AF_UNSPEC, false);
138 do_one_test (action, AF_INET, false);
139 do_one_test (action, AF_INET, true);
142 static int
143 do_test (void)
145 do_one_test_set (ACTION_CONTINUE);
146 do_one_test_set (ACTION_MERGE);
147 return 0;
150 #include <support/test-driver.c>