Add tests for strfrom functions
[glibc.git] / inet / tst-inet6_scopeid_pton.c
blobf9f541e30ad24c3c4232f7567e815559cbb85a26
1 /* Tests for __inet6_scopeid_pton.
2 Copyright (C) 2016 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 <arpa/inet.h>
20 #include <inttypes.h>
21 #include <net-internal.h>
22 #include <net/if.h>
23 #include <stdio.h>
24 #include <stdlib.h>
26 /* An interface which is known to the system. */
27 static const char *interface_name;
28 static uint32_t interface_index;
30 /* Initiale the variables above. */
31 static void
32 setup_interface (void)
34 struct if_nameindex *list = if_nameindex ();
35 if (list != NULL && list[0].if_index != 0 && list[0].if_name[0] != '\0')
37 interface_name = list[0].if_name;
38 interface_index = list[0].if_index;
42 /* Convert ADDRESS to struct in6_addr. */
43 static struct in6_addr
44 from_string (const char *address)
46 struct in6_addr addr;
47 if (inet_pton (AF_INET6, address, &addr) != 1)
49 printf ("error: inet_pton (\"%s\") failed\n", address);
50 exit (1);
52 return addr;
55 /* Check a single address were we expected a failure. */
56 static void
57 expect_failure (const char *address, const char *scope)
59 struct in6_addr addr = from_string (address);
60 uint32_t result = 1234;
61 if (__inet6_scopeid_pton (&addr, scope, &result) == 0)
63 printf ("error: unexpected success for %s%%%s\n",
64 address, scope);
65 exit (1);
67 if (result != 1234)
69 printf ("error: unexpected result update for %s%%%s\n",
70 address, scope);
71 exit (1);
75 /* Check a single address were we expected a success. */
76 static void
77 expect_success (const char *address, const char *scope, uint32_t expected)
79 struct in6_addr addr = from_string (address);
80 uint32_t actual = expected + 1;
81 if (__inet6_scopeid_pton (&addr, scope, &actual) != 0)
83 printf ("error: unexpected failure for %s%%%s\n",
84 address, scope);
85 exit (1);
87 if (actual != expected)
89 printf ("error: unexpected result for for %s%%%s\n",
90 address, scope);
91 printf (" expected: %" PRIu32 "\n", expected);
92 printf (" actual: %" PRIu32 "\n", actual);
93 exit (1);
97 static int
98 do_test (void)
100 setup_interface ();
102 static const char *test_addresses[]
103 = { "::", "::1", "2001:db8::1", NULL };
104 for (int i = 0; test_addresses[i] != NULL; ++i)
106 expect_success (test_addresses[i], "0", 0);
107 expect_success (test_addresses[i], "5555", 5555);
109 expect_failure (test_addresses[i], "");
110 expect_failure (test_addresses[i], "-1");
111 expect_failure (test_addresses[i], "-99");
112 expect_failure (test_addresses[i], "037777777777");
113 expect_failure (test_addresses[i], "0x");
114 expect_failure (test_addresses[i], "0x1");
117 if (interface_name != NULL)
119 expect_success ("fe80::1", interface_name, interface_index);
120 expect_success ("ff02::1", interface_name, interface_index);
121 expect_failure ("::", interface_name);
122 expect_failure ("::1", interface_name);
123 expect_failure ("ff01::1", interface_name);
124 expect_failure ("2001:db8::1", interface_name);
127 return 0;
130 #define TEST_FUNCTION do_test ()
131 #include "../test-skeleton.c"