LCOV - code coverage report
Current view: top level - dirmngr - t-ldap-parse-uri.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 58 90 64.4 %
Date: 2015-11-05 17:10:59 Functions: 8 8 100.0 %

          Line data    Source code
       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 <http://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          20 : check_ldap_uri_p (int test_count, struct test_ldap_uri_p *test)
      34             : {
      35          20 :   int result = ldap_uri_p (test->uri);
      36          20 :   if (result != test->result)
      37             :     {
      38           0 :       printf ("'%s' is %san LDAP schema, but ldap_uri_p says opposite.\n",
      39           0 :               test->uri, test->result ? "" : "not ");
      40           0 :       fail(1000 * test_count);
      41             :     }
      42          20 : }
      43             : 
      44             : static void
      45           1 : test_ldap_uri_p (void)
      46             : {
      47           1 :   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          22 :   for (test_count = 1;
      75          21 :        test_count <= sizeof (tests) / sizeof (tests[0]);
      76          20 :        test_count ++)
      77          20 :     check_ldap_uri_p (test_count, &tests[test_count - 1]);
      78           1 : }
      79             : 
      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          25 : cmp (const char *a, const char *b)
      94             : {
      95          25 :   if (! a)
      96          10 :     a = "";
      97          25 :   if (! b)
      98          10 :     b = "";
      99             : 
     100          25 :   return strcmp (a, b) == 0;
     101             : }
     102             : 
     103             : void
     104           6 : 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           6 :   err = ldap_parse_uri (&puri, test->uri);
     110           6 :   if (err)
     111             :     {
     112           0 :       printf ("Parsing '%s' failed (%d).\n", test->uri, err);
     113           0 :       fail (test_count * 1000 + 0);
     114             :     }
     115             : 
     116           6 :   if (! cmp(test->scheme, puri->scheme))
     117             :     {
     118           0 :       printf ("scheme mismatch: got '%s', expected '%s'.\n",
     119           0 :               puri->scheme, test->scheme);
     120           0 :       fail (test_count * 1000 + 1);
     121             :     }
     122             : 
     123           6 :   if (! cmp(test->host, puri->host))
     124             :     {
     125           0 :       printf ("host mismatch: got '%s', expected '%s'.\n",
     126           0 :               puri->host, test->host);
     127           0 :       fail (test_count * 1000 + 2);
     128             :     }
     129             : 
     130           6 :   if (test->port != puri->port)
     131             :     {
     132           0 :       printf ("port mismatch: got '%d', expected '%d'.\n",
     133           0 :               puri->port, test->port);
     134           0 :       fail (test_count * 1000 + 3);
     135             :     }
     136             : 
     137           6 :   if (test->use_tls != puri->use_tls)
     138             :     {
     139           0 :       printf ("use_tls mismatch: got '%d', expected '%d'.\n",
     140           0 :               puri->use_tls, test->use_tls);
     141           0 :       fail (test_count * 1000 + 4);
     142             :     }
     143             : 
     144           6 :   if (! cmp(test->path, puri->path))
     145             :     {
     146           0 :       printf ("path mismatch: got '%s', expected '%s'.\n",
     147           0 :               puri->path, test->path);
     148           0 :       fail (test_count * 1000 + 5);
     149             :     }
     150             : 
     151           6 :   if (! cmp(test->auth, puri->auth))
     152             :     {
     153           0 :       printf ("auth mismatch: got '%s', expected '%s'.\n",
     154           0 :               puri->auth, test->auth);
     155           0 :       fail (test_count * 1000 + 6);
     156             :     }
     157             : 
     158           6 :   if (! test->password && ! puri->query)
     159             :     /* Ok.  */
     160             :     ;
     161           1 :   else if (test->password && ! puri->query)
     162             :     {
     163           0 :       printf ("password mismatch: got NULL, expected '%s'.\n",
     164             :               test->auth);
     165           0 :       fail (test_count * 1000 + 7);
     166             :     }
     167           1 :   else if (! test->password && puri->query)
     168             :     {
     169           0 :       printf ("password mismatch: got something, expected NULL.\n");
     170           0 :       fail (test_count * 1000 + 8);
     171             :     }
     172           2 :   else if (! (test->password && puri->query
     173           1 :               && puri->query->name && puri->query->value
     174           1 :               && strcmp (puri->query->name, "password") == 0
     175           1 :               && cmp (puri->query->value, test->password)))
     176             :     {
     177           0 :       printf ("password mismatch: got '%s:%s', expected 'password:%s'.\n",
     178           0 :               puri->query->name, puri->query->value,
     179             :               test->password);
     180           0 :       fail (test_count * 1000 + 9);
     181             :     }
     182             : 
     183           6 :   http_release_parsed_uri (puri);
     184           6 : }
     185             : 
     186             : static void
     187           1 : test_ldap_parse_uri (void)
     188             : {
     189           1 :   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           8 :   for (test_count = 1;
     203           7 :        test_count <= sizeof (tests) / sizeof (tests[0]);
     204           6 :        test_count ++)
     205           6 :     check_ldap_parse_uri (test_count, &tests[test_count - 1]);
     206           1 : }
     207             : 
     208             : struct test_ldap_escape_filter
     209             : {
     210             :   const char *filter;
     211             :   const char *result;
     212             : };
     213             : 
     214             : static void
     215           4 : check_ldap_escape_filter (int test_count, struct test_ldap_escape_filter *test)
     216             : {
     217           4 :   char *result = ldap_escape_filter (test->filter);
     218             : 
     219           4 :   if (strcmp (result, test->result) != 0)
     220             :     {
     221           0 :       printf ("Filter: '%s'.  Escaped: '%s'.  Expected: '%s'.\n",
     222             :               test->filter, result, test->result);
     223           0 :       fail (test_count * 1000);
     224             :     }
     225           4 : }
     226             : 
     227             : static void
     228           1 : test_ldap_escape_filter (void)
     229             : {
     230           1 :   struct test_ldap_escape_filter tests[] = {
     231             :     { "foobar", "foobar" },
     232             :     { "", "" },
     233             :     { "(foo)", "%28foo%29" },
     234             :     { "* ( ) \\ /", "%2a %28 %29 %5c %2f" }
     235             :   };
     236             : 
     237             :   int test_count;
     238           6 :   for (test_count = 1;
     239           5 :        test_count <= sizeof (tests) / sizeof (tests[0]);
     240           4 :        test_count ++)
     241           4 :     check_ldap_escape_filter (test_count, &tests[test_count - 1]);
     242           1 : }
     243             : 
     244             : int
     245           1 : main (int argc, char **argv)
     246             : {
     247             :   (void)argc;
     248             :   (void)argv;
     249             : 
     250           1 :   test_ldap_uri_p ();
     251           1 :   test_ldap_parse_uri ();
     252           1 :   test_ldap_escape_filter ();
     253             : 
     254           1 :   return 0;
     255             : }

Generated by: LCOV version 1.11