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"
24 #include "t-support.h"
26 struct test_ldap_uri_p
33 check_ldap_uri_p (int test_count, struct test_ldap_uri_p *test)
35 int result = ldap_uri_p (test->uri);
36 if (result != test->result)
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);
45 test_ldap_uri_p (void)
47 struct test_ldap_uri_p tests[] = {
75 test_count <= sizeof (tests) / sizeof (tests[0]);
77 check_ldap_uri_p (test_count, &tests[test_count - 1]);
80 struct test_ldap_parse_uri
87 const char *path; /* basedn. */
88 const char *auth; /* binddn. */
89 const char *password; /* query[1]. */
93 cmp (const char *a, const char *b)
100 return strcmp (a, b) == 0;
104 check_ldap_parse_uri (int test_count, struct test_ldap_parse_uri *test)
109 err = ldap_parse_uri (&puri, test->uri);
112 printf ("Parsing '%s' failed (%d).\n", test->uri, err);
113 fail (test_count * 1000 + 0);
116 if (! cmp(test->scheme, puri->scheme))
118 printf ("scheme mismatch: got '%s', expected '%s'.\n",
119 puri->scheme, test->scheme);
120 fail (test_count * 1000 + 1);
123 if (! cmp(test->host, puri->host))
125 printf ("host mismatch: got '%s', expected '%s'.\n",
126 puri->host, test->host);
127 fail (test_count * 1000 + 2);
130 if (test->port != puri->port)
132 printf ("port mismatch: got '%d', expected '%d'.\n",
133 puri->port, test->port);
134 fail (test_count * 1000 + 3);
137 if (test->use_tls != puri->use_tls)
139 printf ("use_tls mismatch: got '%d', expected '%d'.\n",
140 puri->use_tls, test->use_tls);
141 fail (test_count * 1000 + 4);
144 if (! cmp(test->path, puri->path))
146 printf ("path mismatch: got '%s', expected '%s'.\n",
147 puri->path, test->path);
148 fail (test_count * 1000 + 5);
151 if (! cmp(test->auth, puri->auth))
153 printf ("auth mismatch: got '%s', expected '%s'.\n",
154 puri->auth, test->auth);
155 fail (test_count * 1000 + 6);
158 if (! test->password && ! puri->query)
161 else if (test->password && ! puri->query)
163 printf ("password mismatch: got NULL, expected '%s'.\n",
165 fail (test_count * 1000 + 7);
167 else if (! test->password && puri->query)
169 printf ("password mismatch: got something, expected NULL.\n");
170 fail (test_count * 1000 + 8);
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)))
177 printf ("password mismatch: got '%s:%s', expected 'password:%s'.\n",
178 puri->query->name, puri->query->value,
180 fail (test_count * 1000 + 9);
183 http_release_parsed_uri (puri);
187 test_ldap_parse_uri (void)
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" }
203 test_count <= sizeof (tests) / sizeof (tests[0]);
205 check_ldap_parse_uri (test_count, &tests[test_count - 1]);
208 struct test_ldap_escape_filter
215 check_ldap_escape_filter (int test_count, struct test_ldap_escape_filter *test)
217 char *result = ldap_escape_filter (test->filter);
219 if (strcmp (result, test->result) != 0)
221 printf ("Filter: '%s'. Escaped: '%s'. Expected: '%s'.\n",
222 test->filter, result, test->result);
223 fail (test_count * 1000);
230 test_ldap_escape_filter (void)
232 struct test_ldap_escape_filter tests[] = {
233 { "foobar", "foobar" },
235 { "(foo)", "%28foo%29" },
236 { "* ( ) \\ /", "%2a %28 %29 %5c %2f" }
241 test_count <= sizeof (tests) / sizeof (tests[0]);
243 check_ldap_escape_filter (test_count, &tests[test_count - 1]);
247 main (int argc, char **argv)
253 test_ldap_parse_uri ();
254 test_ldap_escape_filter ();