1 /* t-ldap-parse-uri.c - Regression tests for ldap-parse-uri.c.
2 * Copyright (C) 2015 g10 Code GmbH
4 * This file is part of GnuPG.
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.
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.
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/>.
22 #include "ldap-parse-uri.h"
23 #include "t-support.h"
25 #define PGM "t-ldap-parse-uri"
30 struct test_ldap_uri_p
37 check_ldap_uri_p (int test_count, struct test_ldap_uri_p *test)
42 fprintf (stderr, PGM ": checking '%s'\n", test->uri);
43 result = ldap_uri_p (test->uri);
44 if (result != test->result)
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);
53 test_ldap_uri_p (void)
55 struct test_ldap_uri_p tests[] = {
83 test_count <= sizeof (tests) / sizeof (tests[0]);
85 check_ldap_uri_p (test_count, &tests[test_count - 1]);
88 struct test_ldap_parse_uri
95 const char *path; /* basedn. */
96 const char *auth; /* binddn. */
97 const char *password; /* query[1]. */
101 cmp (const char *a, const char *b)
108 return strcmp (a, b) == 0;
112 check_ldap_parse_uri (int test_count, struct test_ldap_parse_uri *test)
118 fprintf (stderr, PGM ": parsing '%s'\n", test->uri);
119 err = ldap_parse_uri (&puri, test->uri);
122 printf ("Parsing '%s' failed (%d).\n", test->uri, err);
123 fail (test_count * 1000 + 0);
126 if (! cmp(test->scheme, puri->scheme))
128 printf ("scheme mismatch: got '%s', expected '%s'.\n",
129 puri->scheme, test->scheme);
130 fail (test_count * 1000 + 1);
133 if (! cmp(test->host, puri->host))
135 printf ("host mismatch: got '%s', expected '%s'.\n",
136 puri->host, test->host);
137 fail (test_count * 1000 + 2);
140 if (test->port != puri->port)
142 printf ("port mismatch: got '%d', expected '%d'.\n",
143 puri->port, test->port);
144 fail (test_count * 1000 + 3);
147 if (test->use_tls != puri->use_tls)
149 printf ("use_tls mismatch: got '%d', expected '%d'.\n",
150 puri->use_tls, test->use_tls);
151 fail (test_count * 1000 + 4);
154 if (! cmp(test->path, puri->path))
156 printf ("path mismatch: got '%s', expected '%s'.\n",
157 puri->path, test->path);
158 fail (test_count * 1000 + 5);
161 if (! cmp(test->auth, puri->auth))
163 printf ("auth mismatch: got '%s', expected '%s'.\n",
164 puri->auth, test->auth);
165 fail (test_count * 1000 + 6);
168 if (! test->password && ! puri->query)
171 else if (test->password && ! puri->query)
173 printf ("password mismatch: got NULL, expected '%s'.\n",
175 fail (test_count * 1000 + 7);
177 else if (! test->password && puri->query)
179 printf ("password mismatch: got something, expected NULL.\n");
180 fail (test_count * 1000 + 8);
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)))
187 printf ("password mismatch: got '%s:%s', expected 'password:%s'.\n",
188 puri->query->name, puri->query->value,
190 fail (test_count * 1000 + 9);
193 http_release_parsed_uri (puri);
197 test_ldap_parse_uri (void)
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" }
213 test_count <= sizeof (tests) / sizeof (tests[0]);
215 check_ldap_parse_uri (test_count, &tests[test_count - 1]);
218 struct test_ldap_escape_filter
225 check_ldap_escape_filter (int test_count, struct test_ldap_escape_filter *test)
227 char *result = ldap_escape_filter (test->filter);
229 if (strcmp (result, test->result) != 0)
231 printf ("Filter: '%s'. Escaped: '%s'. Expected: '%s'.\n",
232 test->filter, result, test->result);
233 fail (test_count * 1000);
240 test_ldap_escape_filter (void)
242 struct test_ldap_escape_filter tests[] = {
243 { "foobar", "foobar" },
245 { "(foo)", "%28foo%29" },
246 { "* ( ) \\ /", "%2a %28 %29 %5c %2f" }
251 test_count <= sizeof (tests) / sizeof (tests[0]);
253 check_ldap_escape_filter (test_count, &tests[test_count - 1]);
259 main (int argc, char **argv)
265 while (argc && last_argc != argc )
268 if (!strcmp (*argv, "--"))
273 else if (!strcmp (*argv, "--help"))
275 fputs ("usage: " PGM "\n"
277 " --verbose print timings etc.\n",
281 else if (!strcmp (*argv, "--verbose"))
286 else if (!strncmp (*argv, "--", 2))
288 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
294 fprintf (stderr, PGM ": no argumenst are expected\n");
299 test_ldap_parse_uri ();
300 test_ldap_escape_filter ();