Check for getaddrinfo rather than gethostbyname in configure.ac
[free-sw/xcb/libxcb] / tests / check_public.c
1 #include <check.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "check_suites.h"
5 #include "xcb.h"
6
7 /* xcb_parse_display tests {{{ */
8
9 typedef enum test_type_t {
10         TEST_ARGUMENT, TEST_ENVIRONMENT, TEST_END
11 } test_type_t;
12 static const char *const test_string[] = { "", "via $DISPLAY " };
13
14 static void parse_display_pass(const char *name, const char *host, const int display, const int screen)
15 {
16         int success;
17         char *got_host;
18         int got_display, got_screen;
19         const char *argument = 0;
20         test_type_t test_type;
21
22         for(test_type = TEST_ARGUMENT; test_type != TEST_END; test_type++)
23         {
24                 if(test_type == TEST_ARGUMENT)
25                 {
26                         argument = name;
27                         putenv("DISPLAY");
28                 }
29                 else if(test_type == TEST_ENVIRONMENT)
30                 {
31                         argument = 0;
32                         setenv("DISPLAY", name, 1);
33                 }
34
35                 got_host = (char *) -1;
36                 got_display = got_screen = -42;
37                 mark_point();
38                 success = xcb_parse_display(argument, &got_host, &got_display, &got_screen);
39                 fail_unless(success, "unexpected parse failure %sfor '%s'", test_string[test_type], name);
40                 fail_unless(strcmp(host, got_host) == 0, "parse %sproduced unexpected hostname '%s' for '%s': expected '%s'", test_string[test_type], got_host, name, host);
41                 fail_unless(display == got_display, "parse %sproduced unexpected display '%d' for '%s': expected '%d'", test_string[test_type], got_display, name, display);
42                 fail_unless(screen == got_screen, "parse %sproduced unexpected screen '%d' for '%s': expected '%d'", test_string[test_type], got_screen, name, screen);
43
44                 got_host = (char *) -1;
45                 got_display = got_screen = -42;
46                 mark_point();
47                 success = xcb_parse_display(argument, &got_host, &got_display, 0);
48                 fail_unless(success, "unexpected screenless parse failure %sfor '%s'", test_string[test_type], name);
49                 fail_unless(strcmp(host, got_host) == 0, "screenless parse %sproduced unexpected hostname '%s' for '%s': expected '%s'", test_string[test_type], got_host, name, host);
50                 fail_unless(display == got_display, "screenless parse %sproduced unexpected display '%d' for '%s': expected '%d'", test_string[test_type], got_display, name, display);
51         }
52         putenv("DISPLAY");
53 }
54
55 static void parse_display_fail(const char *name)
56 {
57         int success;
58         char *got_host;
59         int got_display, got_screen;
60         const char *argument = 0;
61         test_type_t test_type;
62
63         for(test_type = TEST_ARGUMENT; test_type != TEST_END; test_type++)
64         {
65                 if(test_type == TEST_ARGUMENT)
66                 {
67                         argument = name;
68                         putenv("DISPLAY");
69                 }
70                 else if(test_type == TEST_ENVIRONMENT)
71                 {
72                         argument = 0;
73                         setenv("DISPLAY", name, 1);
74                 }
75
76                 got_host = (char *) -1;
77                 got_display = got_screen = -42;
78                 mark_point();
79                 success = xcb_parse_display(argument, &got_host, &got_display, &got_screen);
80                 fail_unless(!success, "unexpected parse success %sfor '%s'", test_string[test_type], name);
81                 fail_unless(got_host == (char *) -1, "host changed on parse failure %sfor '%s': got %p", test_string[test_type], name, got_host);
82                 fail_unless(got_display == -42, "display changed on parse failure %sfor '%s': got %d", test_string[test_type], name, got_display);
83                 fail_unless(got_screen == -42, "screen changed on parse failure %sfor '%s': got %d", test_string[test_type], name, got_screen);
84
85                 got_host = (char *) -1;
86                 got_display = got_screen = -42;
87                 mark_point();
88                 success = xcb_parse_display(argument, &got_host, &got_display, 0);
89                 fail_unless(!success, "unexpected screenless parse success %sfor '%s'", test_string[test_type], name);
90                 fail_unless(got_host == (char *) -1, "host changed on parse failure %sfor '%s': got %p", test_string[test_type], name, got_host);
91                 fail_unless(got_display == -42, "display changed on parse failure %sfor '%s': got %d", test_string[test_type], name, got_display);
92         }
93         putenv("DISPLAY");
94 }
95
96 START_TEST(parse_display_unix)
97 {
98         parse_display_pass(":0", "", 0, 0);
99         parse_display_pass(":1", "", 1, 0);
100         parse_display_pass(":0.1", "", 0, 1);
101 }
102 END_TEST
103
104 START_TEST(parse_display_ip)
105 {
106         parse_display_pass("x.org:0", "x.org", 0, 0);
107         parse_display_pass("expo:0", "expo", 0, 0);
108         parse_display_pass("bigmachine:1", "bigmachine", 1, 0);
109         parse_display_pass("hydra:0.1", "hydra", 0, 1);
110 }
111 END_TEST
112
113 START_TEST(parse_display_ipv4)
114 {
115         parse_display_pass("198.112.45.11:0", "198.112.45.11", 0, 0);
116         parse_display_pass("198.112.45.11:0.1", "198.112.45.11", 0, 1);
117 }
118 END_TEST
119
120 START_TEST(parse_display_ipv6)
121 {
122         parse_display_pass(":::0", "::", 0, 0);
123         parse_display_pass("1:::0", "1::", 0, 0);
124         parse_display_pass("::1:0", "::1", 0, 0);
125         parse_display_pass("::1:0.1", "::1", 0, 1);
126         parse_display_pass("::127.0.0.1:0", "::127.0.0.1", 0, 0);
127         parse_display_pass("::ffff:127.0.0.1:0", "::ffff:127.0.0.1", 0, 0);
128         parse_display_pass("2002:83fc:d052::1:0", "2002:83fc:d052::1", 0, 0);
129         parse_display_pass("2002:83fc:d052::1:0.1", "2002:83fc:d052::1", 0, 1);
130         parse_display_pass("[::]:0", "[::]", 0, 0);
131         parse_display_pass("[1::]:0", "[1::]", 0, 0);
132         parse_display_pass("[::1]:0", "[::1]", 0, 0);
133         parse_display_pass("[::1]:0.1", "[::1]", 0, 1);
134         parse_display_pass("[::127.0.0.1]:0", "[::127.0.0.1]", 0, 0);
135         parse_display_pass("[::ffff:127.0.0.1]:0", "[::ffff:127.0.0.1]", 0, 0);
136         parse_display_pass("[2002:83fc:d052::1]:0", "[2002:83fc:d052::1]", 0, 0);
137         parse_display_pass("[2002:83fc:d052::1]:0.1", "[2002:83fc:d052::1]", 0, 1);
138 }
139 END_TEST
140
141 START_TEST(parse_display_decnet)
142 {
143         parse_display_pass("myws::0", "myws:", 0, 0);
144         parse_display_pass("big::1", "big:", 1, 0);
145         parse_display_pass("hydra::0.1", "hydra:", 0, 1);
146 }
147 END_TEST
148
149 START_TEST(parse_display_negative)
150 {
151         parse_display_fail(0);
152         parse_display_fail("");
153         parse_display_fail(":");
154         parse_display_fail("::");
155         parse_display_fail(":::");
156         parse_display_fail(":.");
157         parse_display_fail(":a");
158         parse_display_fail(":a.");
159         parse_display_fail(":0.");
160         parse_display_fail(":.a");
161         parse_display_fail(":.0");
162         parse_display_fail(":0.a");
163         parse_display_fail(":0.0.");
164
165         parse_display_fail("127.0.0.1");
166         parse_display_fail("127.0.0.1:");
167         parse_display_fail("127.0.0.1::");
168         parse_display_fail("::127.0.0.1");
169         parse_display_fail("::127.0.0.1:");
170         parse_display_fail("::127.0.0.1::");
171         parse_display_fail("::ffff:127.0.0.1");
172         parse_display_fail("::ffff:127.0.0.1:");
173         parse_display_fail("::ffff:127.0.0.1::");
174         parse_display_fail("localhost");
175         parse_display_fail("localhost:");
176         parse_display_fail("localhost::");
177 }
178 END_TEST
179
180 /* }}} */
181
182 Suite *public_suite(void)
183 {
184         Suite *s = suite_create("Public API");
185         putenv("DISPLAY");
186         suite_add_test(s, parse_display_unix, "xcb_parse_display unix");
187         suite_add_test(s, parse_display_ip, "xcb_parse_display ip");
188         suite_add_test(s, parse_display_ipv4, "xcb_parse_display ipv4");
189         suite_add_test(s, parse_display_ipv6, "xcb_parse_display ipv6");
190         suite_add_test(s, parse_display_decnet, "xcb_parse_display decnet");
191         suite_add_test(s, parse_display_negative, "xcb_parse_display negative");
192         return s;
193 }