chiark / gitweb /
dirmngr: Fix for --disable-libdns usage.
[gnupg2.git] / dirmngr / t-ldap-parse-uri.c
1 /* t-ldap-parse-uri.c - Regression tests for ldap-parse-uri.c.
2  * Copyright (C) 2015  g10 Code GmbH
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21
22 #include "ldap-parse-uri.h"
23
24 #include "t-support.h"
25
26 struct test_ldap_uri_p
27 {
28   const char *uri;
29   int result;
30 };
31
32 void
33 check_ldap_uri_p (int test_count, struct test_ldap_uri_p *test)
34 {
35   int result = ldap_uri_p (test->uri);
36   if (result != test->result)
37     {
38       printf ("'%s' is %san LDAP schema, but ldap_uri_p says opposite.\n",
39               test->uri, test->result ? "" : "not ");
40       fail(1000 * test_count);
41     }
42 }
43
44 static void
45 test_ldap_uri_p (void)
46 {
47   struct test_ldap_uri_p tests[] = {
48     { "ldap://foo", 1 },
49     { "ldap://", 1 },
50     { "ldap:", 1 },
51     { "ldap", 0 },
52     { "ldapfoobar", 0 },
53
54     { "ldaps://foo", 1 },
55     { "ldaps://", 1 },
56     { "ldaps:", 1 },
57     { "ldaps", 0 },
58     { "ldapsfoobar", 0 },
59
60     { "ldapi://foo", 1 },
61     { "ldapi://", 1 },
62     { "ldapi:", 1 },
63     { "ldapi", 0 },
64     { "ldapifoobar", 0 },
65
66     { "LDAP://FOO", 1 },
67     { "LDAP://", 1 },
68     { "LDAP:", 1 },
69     { "LDAP", 0 },
70     { "LDAPFOOBAR", 0 }
71   };
72
73   int test_count;
74   for (test_count = 1;
75        test_count <= sizeof (tests) / sizeof (tests[0]);
76        test_count ++)
77     check_ldap_uri_p (test_count, &tests[test_count - 1]);
78 }
79 \f
80 struct test_ldap_parse_uri
81 {
82   const char *uri;
83   const char *scheme;
84   const char *host;
85   const int port;
86   const int use_tls;
87   const char *path;  /* basedn. */
88   const char *auth;  /* binddn.  */
89   const char *password;  /* query[1].  */
90 };
91
92 static int
93 cmp (const char *a, const char *b)
94 {
95   if (! a)
96     a = "";
97   if (! b)
98     b = "";
99
100   return strcmp (a, b) == 0;
101 }
102
103 void
104 check_ldap_parse_uri (int test_count, struct test_ldap_parse_uri *test)
105 {
106   gpg_error_t err;
107   parsed_uri_t puri;
108
109   err = ldap_parse_uri (&puri, test->uri);
110   if (err)
111     {
112       printf ("Parsing '%s' failed (%d).\n", test->uri, err);
113       fail (test_count * 1000 + 0);
114     }
115
116   if (! cmp(test->scheme, puri->scheme))
117     {
118       printf ("scheme mismatch: got '%s', expected '%s'.\n",
119               puri->scheme, test->scheme);
120       fail (test_count * 1000 + 1);
121     }
122
123   if (! cmp(test->host, puri->host))
124     {
125       printf ("host mismatch: got '%s', expected '%s'.\n",
126               puri->host, test->host);
127       fail (test_count * 1000 + 2);
128     }
129
130   if (test->port != puri->port)
131     {
132       printf ("port mismatch: got '%d', expected '%d'.\n",
133               puri->port, test->port);
134       fail (test_count * 1000 + 3);
135     }
136
137   if (test->use_tls != puri->use_tls)
138     {
139       printf ("use_tls mismatch: got '%d', expected '%d'.\n",
140               puri->use_tls, test->use_tls);
141       fail (test_count * 1000 + 4);
142     }
143
144   if (! cmp(test->path, puri->path))
145     {
146       printf ("path mismatch: got '%s', expected '%s'.\n",
147               puri->path, test->path);
148       fail (test_count * 1000 + 5);
149     }
150
151   if (! cmp(test->auth, puri->auth))
152     {
153       printf ("auth mismatch: got '%s', expected '%s'.\n",
154               puri->auth, test->auth);
155       fail (test_count * 1000 + 6);
156     }
157
158   if (! test->password && ! puri->query)
159     /* Ok.  */
160     ;
161   else if (test->password && ! puri->query)
162     {
163       printf ("password mismatch: got NULL, expected '%s'.\n",
164               test->auth);
165       fail (test_count * 1000 + 7);
166     }
167   else if (! test->password && puri->query)
168     {
169       printf ("password mismatch: got something, expected NULL.\n");
170       fail (test_count * 1000 + 8);
171     }
172   else if (! (test->password && puri->query
173               && puri->query->name && puri->query->value
174               && strcmp (puri->query->name, "password") == 0
175               && cmp (puri->query->value, test->password)))
176     {
177       printf ("password mismatch: got '%s:%s', expected 'password:%s'.\n",
178               puri->query->name, puri->query->value,
179               test->password);
180       fail (test_count * 1000 + 9);
181     }
182
183   http_release_parsed_uri (puri);
184 }
185
186 static void
187 test_ldap_parse_uri (void)
188 {
189   struct test_ldap_parse_uri tests[] = {
190     { "ldap://", "ldap", NULL, 389, 0, NULL, NULL, NULL },
191     { "ldap://host", "ldap", "host", 389, 0, NULL, NULL, NULL },
192     { "ldap://host:100", "ldap", "host", 100, 0, NULL, NULL, NULL },
193     { "ldaps://host", "ldaps", "host", 636, 1, NULL, NULL, NULL },
194     { "ldap://host/ou%3DPGP%20Keys%2Cdc%3DEXAMPLE%2Cdc%3DORG",
195       "ldap", "host", 389, 0, "ou=PGP Keys,dc=EXAMPLE,dc=ORG" },
196     { "ldap://host/????bindname=uid%3Duser%2Cou%3DPGP%20Users%2Cdc%3DEXAMPLE%2Cdc%3DORG,password=foobar",
197       "ldap", "host", 389, 0, "",
198       "uid=user,ou=PGP Users,dc=EXAMPLE,dc=ORG", "foobar" }
199   };
200
201   int test_count;
202   for (test_count = 1;
203        test_count <= sizeof (tests) / sizeof (tests[0]);
204        test_count ++)
205     check_ldap_parse_uri (test_count, &tests[test_count - 1]);
206 }
207 \f
208 struct test_ldap_escape_filter
209 {
210   const char *filter;
211   const char *result;
212 };
213
214 static void
215 check_ldap_escape_filter (int test_count, struct test_ldap_escape_filter *test)
216 {
217   char *result = ldap_escape_filter (test->filter);
218
219   if (strcmp (result, test->result) != 0)
220     {
221       printf ("Filter: '%s'.  Escaped: '%s'.  Expected: '%s'.\n",
222               test->filter, result, test->result);
223       fail (test_count * 1000);
224     }
225
226   xfree (result);
227 }
228
229 static void
230 test_ldap_escape_filter (void)
231 {
232   struct test_ldap_escape_filter tests[] = {
233     { "foobar", "foobar" },
234     { "", "" },
235     { "(foo)", "%28foo%29" },
236     { "* ( ) \\ /", "%2a %28 %29 %5c %2f" }
237   };
238
239   int test_count;
240   for (test_count = 1;
241        test_count <= sizeof (tests) / sizeof (tests[0]);
242        test_count ++)
243     check_ldap_escape_filter (test_count, &tests[test_count - 1]);
244 }
245 \f
246 int
247 main (int argc, char **argv)
248 {
249   (void)argc;
250   (void)argv;
251
252   test_ldap_uri_p ();
253   test_ldap_parse_uri ();
254   test_ldap_escape_filter ();
255
256   return 0;
257 }