Release libxcb 1.0 RC3 (0.9.93).
[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("::1:0", "::1", 0, 0);
123         parse_display_pass("::1:0.1", "::1", 0, 1);
124         parse_display_pass("2002:83fc:d052::1:0", "2002:83fc:d052::1", 0, 0);
125         parse_display_pass("2002:83fc:d052::1:0.1", "2002:83fc:d052::1", 0, 1);
126 }
127 END_TEST
128
129 START_TEST(parse_display_decnet)
130 {
131         parse_display_pass("myws::0", "myws:", 0, 0);
132         parse_display_pass("big::1", "big:", 1, 0);
133         parse_display_pass("hydra::0.1", "hydra:", 0, 1);
134 }
135 END_TEST
136
137 START_TEST(parse_display_negative)
138 {
139         parse_display_fail(0);
140         parse_display_fail("");
141         parse_display_fail(":");
142         parse_display_fail("::");
143         parse_display_fail(":.");
144         parse_display_fail(":a");
145         parse_display_fail(":a.");
146         parse_display_fail(":0.");
147         parse_display_fail(":0.a");
148         parse_display_fail(":0.0.");
149
150         parse_display_fail("localhost");
151         parse_display_fail("localhost:");
152 }
153 END_TEST
154
155 /* }}} */
156
157 Suite *public_suite(void)
158 {
159         Suite *s = suite_create("Public API");
160         putenv("DISPLAY");
161         suite_add_test(s, parse_display_unix, "xcb_parse_display unix");
162         suite_add_test(s, parse_display_ip, "xcb_parse_display ip");
163         suite_add_test(s, parse_display_ipv4, "xcb_parse_display ipv4");
164         suite_add_test(s, parse_display_ipv6, "xcb_parse_display ipv6");
165         suite_add_test(s, parse_display_decnet, "xcb_parse_display decnet");
166         suite_add_test(s, parse_display_negative, "xcb_parse_display negative");
167         return s;
168 }