Bump to 2.4.3
[platform/upstream/gpg2.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 #include "t-support.h"
24
25 #define PGM "t-ldap-parse-uri"
26
27 static int verbose;
28
29
30 struct test_ldap_uri_p
31 {
32   const char *uri;
33   int result;
34 };
35
36 void
37 check_ldap_uri_p (int test_count, struct test_ldap_uri_p *test)
38 {
39   int result;
40
41   if (verbose)
42     fprintf (stderr, PGM ": checking '%s'\n", test->uri);
43   result = ldap_uri_p (test->uri);
44   if (result != test->result)
45     {
46       printf ("'%s' is %san LDAP schema, but ldap_uri_p says opposite.\n",
47               test->uri, test->result ? "" : "not ");
48       fail(1000 * test_count);
49     }
50 }
51
52 static void
53 test_ldap_uri_p (void)
54 {
55   struct test_ldap_uri_p tests[] = {
56     { "ldap://foo", 1 },
57     { "ldap://", 1 },
58     { "ldap:", 1 },
59     { "ldap", 0 },
60     { "ldapfoobar", 0 },
61
62     { "ldaps://foo", 1 },
63     { "ldaps://", 1 },
64     { "ldaps:", 1 },
65     { "ldaps", 0 },
66     { "ldapsfoobar", 0 },
67
68     { "ldapi://foo", 1 },
69     { "ldapi://", 1 },
70     { "ldapi:", 1 },
71     { "ldapi", 0 },
72     { "ldapifoobar", 0 },
73
74     { "LDAP://FOO", 1 },
75     { "LDAP://", 1 },
76     { "LDAP:", 1 },
77     { "LDAP", 0 },
78     { "LDAPFOOBAR", 0 }
79   };
80
81   int test_count;
82   for (test_count = 1;
83        test_count <= sizeof (tests) / sizeof (tests[0]);
84        test_count ++)
85     check_ldap_uri_p (test_count, &tests[test_count - 1]);
86 }
87 \f
88 struct test_ldap_parse_uri
89 {
90   const char *uri;
91   const char *scheme;
92   const char *host;
93   const int port;
94   const int use_tls;
95   const char *path;  /* basedn. */
96   const char *auth;  /* binddn.  */
97   const char *password;  /* query[1].  */
98 };
99
100 static int
101 cmp (const char *a, const char *b)
102 {
103   if (! a)
104     a = "";
105   if (! b)
106     b = "";
107
108   return strcmp (a, b) == 0;
109 }
110
111 void
112 check_ldap_parse_uri (int test_count, struct test_ldap_parse_uri *test)
113 {
114   gpg_error_t err;
115   parsed_uri_t puri;
116
117   if (verbose)
118     fprintf (stderr, PGM ": parsing '%s'\n", test->uri);
119   err = ldap_parse_uri (&puri, test->uri);
120   if (err)
121     {
122       printf ("Parsing '%s' failed (%d).\n", test->uri, err);
123       fail (test_count * 1000 + 0);
124     }
125
126   if (! cmp(test->scheme, puri->scheme))
127     {
128       printf ("scheme mismatch: got '%s', expected '%s'.\n",
129               puri->scheme, test->scheme);
130       fail (test_count * 1000 + 1);
131     }
132
133   if (! cmp(test->host, puri->host))
134     {
135       printf ("host mismatch: got '%s', expected '%s'.\n",
136               puri->host, test->host);
137       fail (test_count * 1000 + 2);
138     }
139
140   if (test->port != puri->port)
141     {
142       printf ("port mismatch: got '%d', expected '%d'.\n",
143               puri->port, test->port);
144       fail (test_count * 1000 + 3);
145     }
146
147   if (test->use_tls != puri->use_tls)
148     {
149       printf ("use_tls mismatch: got '%d', expected '%d'.\n",
150               puri->use_tls, test->use_tls);
151       fail (test_count * 1000 + 4);
152     }
153
154   if (! cmp(test->path, puri->path))
155     {
156       printf ("path mismatch: got '%s', expected '%s'.\n",
157               puri->path, test->path);
158       fail (test_count * 1000 + 5);
159     }
160
161   if (! cmp(test->auth, puri->auth))
162     {
163       printf ("auth mismatch: got '%s', expected '%s'.\n",
164               puri->auth, test->auth);
165       fail (test_count * 1000 + 6);
166     }
167
168   if (! test->password && ! puri->query)
169     /* Ok.  */
170     ;
171   else if (test->password && ! puri->query)
172     {
173       printf ("password mismatch: got NULL, expected '%s'.\n",
174               test->auth);
175       fail (test_count * 1000 + 7);
176     }
177   else if (! test->password && puri->query)
178     {
179       printf ("password mismatch: got something, expected NULL.\n");
180       fail (test_count * 1000 + 8);
181     }
182   else if (! (test->password && puri->query
183               && puri->query->name && puri->query->value
184               && strcmp (puri->query->name, "password") == 0
185               && cmp (puri->query->value, test->password)))
186     {
187       printf ("password mismatch: got '%s:%s', expected 'password:%s'.\n",
188               puri->query->name, puri->query->value,
189               test->password);
190       fail (test_count * 1000 + 9);
191     }
192
193   http_release_parsed_uri (puri);
194 }
195
196 static void
197 test_ldap_parse_uri (void)
198 {
199   struct test_ldap_parse_uri tests[] = {
200     { "ldap://", "ldap", NULL, 389, 0, NULL, NULL, NULL },
201     { "ldap://host", "ldap", "host", 389, 0, NULL, NULL, NULL },
202     { "ldap://host:100", "ldap", "host", 100, 0, NULL, NULL, NULL },
203     { "ldaps://host", "ldaps", "host", 636, 1, NULL, NULL, NULL },
204     { "ldap://host/ou%3DPGP%20Keys%2Cdc%3DEXAMPLE%2Cdc%3DORG",
205       "ldap", "host", 389, 0, "ou=PGP Keys,dc=EXAMPLE,dc=ORG" },
206     { "ldap://host/????bindname=uid%3Duser%2Cou%3DPGP%20Users%2Cdc%3DEXAMPLE%2Cdc%3DORG,password=foobar",
207       "ldap", "host", 389, 0, "",
208       "uid=user,ou=PGP Users,dc=EXAMPLE,dc=ORG", "foobar" }
209   };
210
211   int test_count;
212   for (test_count = 1;
213        test_count <= sizeof (tests) / sizeof (tests[0]);
214        test_count ++)
215     check_ldap_parse_uri (test_count, &tests[test_count - 1]);
216 }
217 \f
218 struct test_ldap_escape_filter
219 {
220   const char *filter;
221   const char *result;
222 };
223
224 static void
225 check_ldap_escape_filter (int test_count, struct test_ldap_escape_filter *test)
226 {
227   char *result = ldap_escape_filter (test->filter);
228
229   if (strcmp (result, test->result) != 0)
230     {
231       printf ("Filter: '%s'.  Escaped: '%s'.  Expected: '%s'.\n",
232               test->filter, result, test->result);
233       fail (test_count * 1000);
234     }
235
236   xfree (result);
237 }
238
239 static void
240 test_ldap_escape_filter (void)
241 {
242   struct test_ldap_escape_filter tests[] = {
243     { "foobar", "foobar" },
244     { "", "" },
245     { "(foo)", "%28foo%29" },
246     { "* ( ) \\ /", "%2a %28 %29 %5c %2f" }
247   };
248
249   int test_count;
250   for (test_count = 1;
251        test_count <= sizeof (tests) / sizeof (tests[0]);
252        test_count ++)
253     check_ldap_escape_filter (test_count, &tests[test_count - 1]);
254 }
255
256
257 \f
258 int
259 main (int argc, char **argv)
260 {
261   int last_argc = -1;
262
263   if (argc)
264     { argc--; argv++; }
265   while (argc && last_argc != argc )
266     {
267       last_argc = argc;
268       if (!strcmp (*argv, "--"))
269         {
270           argc--; argv++;
271           break;
272         }
273       else if (!strcmp (*argv, "--help"))
274         {
275           fputs ("usage: " PGM "\n"
276                  "Options:\n"
277                  "  --verbose         print timings etc.\n",
278                  stdout);
279           exit (0);
280         }
281       else if (!strcmp (*argv, "--verbose"))
282         {
283           verbose++;
284           argc--; argv++;
285         }
286       else if (!strncmp (*argv, "--", 2))
287         {
288           fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
289           exit (1);
290         }
291     }
292   if (argc)
293     {
294       fprintf (stderr, PGM ": no argumenst are expected\n");
295       exit (1);
296     }
297
298   test_ldap_uri_p ();
299   test_ldap_parse_uri ();
300   test_ldap_escape_filter ();
301
302   return 0;
303 }