"Fossies" - the Fresh Open Source Software Archive

Member "nss-pam-ldapd-0.9.12/tests/test_cfg.c" (15 Nov 2021, 9560 Bytes) of package /linux/privat/nss-pam-ldapd-0.9.12.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2    test_cfg.c - simple test for the cfg module
    3    This file is part of the nss-pam-ldapd library.
    4 
    5    Copyright (C) 2007-2021 Arthur de Jong
    6 
    7    This library is free software; you can redistribute it and/or
    8    modify it under the terms of the GNU Lesser General Public
    9    License as published by the Free Software Foundation; either
   10    version 2.1 of the License, or (at your option) any later version.
   11 
   12    This library is distributed in the hope that it will be useful,
   13    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15    Lesser General Public License for more details.
   16 
   17    You should have received a copy of the GNU Lesser General Public
   18    License along with this library; if not, write to the Free Software
   19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   20    02110-1301 USA
   21 */
   22 
   23 #include "config.h"
   24 
   25 #include <stdio.h>
   26 #include <string.h>
   27 #include <assert.h>
   28 
   29 #include "common.h"
   30 
   31 /* we include cfg.c because we want to test the static methods */
   32 #include "nslcd/cfg.c"
   33 
   34 static void test_xstrdup(void)
   35 {
   36   static const char *foo = "testString123";
   37   char *bar;
   38   bar = xstrdup(foo);
   39   /* we should have a new value */
   40   assert(bar != NULL);
   41   /* the contents should be the same */
   42   assertstreq(foo, bar);
   43   /* but the pointer should be different */
   44   assert(foo != bar);
   45   /* free memory */
   46   free(bar);
   47 }
   48 
   49 static void test_add_uris(void)
   50 {
   51   static struct ldap_config cfg;
   52   int i;
   53   /* set up config */
   54   cfg_defaults(&cfg);
   55   assert(cfg.uris[0].uri == NULL);
   56   /* add a uri */
   57   add_uri(__FILE__, __LINE__, &cfg, "ldap://localhost");
   58   assert(cfg.uris[0].uri != NULL);
   59   assert(cfg.uris[1].uri == NULL);
   60   /* add some more uris */
   61   for (i = 1; i < NSS_LDAP_CONFIG_MAX_URIS; i++)
   62   {
   63     add_uri(__FILE__, __LINE__, &cfg, "ldap://localhost");
   64     assert(cfg.uris[i].uri != NULL);
   65     assert(cfg.uris[i + 1].uri == NULL);
   66   }
   67   /* inserting one more entry should call exit():
   68      add_uri(__FILE__, __LINE__, &cfg, "ldap://localhost");
   69      assert(cfg.uris[i] != NULL);
   70      assert(cfg.uris[i + 1] == NULL); */
   71   /* there is no cfg_free() so we have a memory leak here */
   72 }
   73 
   74 static void test_parse_boolean(void)
   75 {
   76   assert(parse_boolean(__FILE__, __LINE__, "True") == 1);
   77   assert(parse_boolean(__FILE__, __LINE__, "faLSe") == 0);
   78   assert(parse_boolean(__FILE__, __LINE__, "1") == 1);
   79   assert(parse_boolean(__FILE__, __LINE__, "oFF") == 0);
   80   assert(parse_boolean(__FILE__, __LINE__, "Yes") == 1);
   81   assert(parse_boolean(__FILE__, __LINE__, "No") == 0);
   82   /* most other values should call exit():
   83      assert(parse_boolean(__FILE__, __LINE__, "Foo") == 0); */
   84 }
   85 
   86 static void test_parse_scope(void)
   87 {
   88   struct ldap_config cfg;
   89   handle_scope(__FILE__, __LINE__, "scope", "sUb", &cfg);
   90   assert(cfg.scope == LDAP_SCOPE_SUBTREE);
   91   handle_scope(__FILE__, __LINE__, "scope", "subtree", &cfg);
   92   assert(cfg.scope == LDAP_SCOPE_SUBTREE);
   93   handle_scope(__FILE__, __LINE__, "scope", "ONE", &cfg);
   94   assert(cfg.scope == LDAP_SCOPE_ONELEVEL);
   95   handle_scope(__FILE__, __LINE__, "scope", "oneLevel", &cfg);
   96   assert(cfg.scope == LDAP_SCOPE_ONELEVEL);
   97   handle_scope(__FILE__, __LINE__, "scope", "base", &cfg);
   98   assert(cfg.scope == LDAP_SCOPE_BASE);
   99   handle_scope(__FILE__, __LINE__, "scope", "bASe", &cfg);
  100   assert(cfg.scope == LDAP_SCOPE_BASE);
  101 #ifdef LDAP_SCOPE_CHILDREN
  102   handle_scope(__FILE__, __LINE__, "scope", "children", &cfg);
  103   assert(cfg.scope == LDAP_SCOPE_CHILDREN);
  104 #endif /* LDAP_SCOPE_CHILDREN */
  105   /* most other values should call exit():
  106      handle_scope(__FILE__, __LINE__, "scope", "BSAE", &cfg); */
  107 }
  108 
  109 static void test_parse_map(void)
  110 {
  111   char *line;
  112   /* some general assertions */
  113   assert((LM_ALIASES != LM_ETHERS) && (LM_ALIASES != LM_GROUP) &&
  114          (LM_ALIASES != LM_HOSTS) && (LM_ALIASES != LM_NETGROUP) &&
  115          (LM_ALIASES != LM_NETWORKS) && (LM_ALIASES != LM_PASSWD) &&
  116          (LM_ALIASES != LM_PROTOCOLS) && (LM_ALIASES != LM_RPC) &&
  117          (LM_ALIASES != LM_SERVICES) && (LM_ALIASES != LM_SHADOW));
  118   assert((LM_ETHERS != LM_GROUP) && (LM_ETHERS != LM_HOSTS) &&
  119          (LM_ETHERS != LM_NETGROUP) && (LM_ETHERS != LM_NETWORKS) &&
  120          (LM_ETHERS != LM_PASSWD) && (LM_ETHERS != LM_PROTOCOLS) &&
  121          (LM_ETHERS != LM_RPC) && (LM_ETHERS != LM_SERVICES) &&
  122          (LM_ETHERS != LM_SHADOW));
  123   assert((LM_GROUP != LM_HOSTS) && (LM_GROUP != LM_NETGROUP) &&
  124          (LM_GROUP != LM_NETWORKS) && (LM_GROUP != LM_PASSWD) &&
  125          (LM_GROUP != LM_PROTOCOLS) && (LM_GROUP != LM_RPC) &&
  126          (LM_GROUP != LM_SERVICES) && (LM_GROUP != LM_SHADOW));
  127   assert((LM_HOSTS != LM_NETGROUP) && (LM_HOSTS != LM_NETWORKS) &&
  128          (LM_HOSTS != LM_PASSWD) && (LM_HOSTS != LM_PROTOCOLS) &&
  129          (LM_HOSTS != LM_RPC) && (LM_HOSTS != LM_SERVICES) &&
  130          (LM_HOSTS != LM_SHADOW));
  131   assert((LM_NETGROUP != LM_NETWORKS) && (LM_NETGROUP != LM_PASSWD) &&
  132          (LM_NETGROUP != LM_PROTOCOLS) && (LM_NETGROUP != LM_RPC) &&
  133          (LM_NETGROUP != LM_SERVICES) && (LM_NETGROUP != LM_SHADOW));
  134   assert((LM_NETWORKS != LM_PASSWD) && (LM_NETWORKS != LM_PROTOCOLS) &&
  135          (LM_NETWORKS != LM_RPC) && (LM_NETWORKS != LM_SERVICES) &&
  136          (LM_NETWORKS != LM_SHADOW));
  137   assert((LM_PASSWD != LM_PROTOCOLS) && (LM_PASSWD != LM_RPC) &&
  138          (LM_PASSWD != LM_SERVICES) && (LM_PASSWD != LM_SHADOW));
  139   assert((LM_PROTOCOLS != LM_RPC) && (LM_PROTOCOLS != LM_SERVICES) &&
  140          (LM_PROTOCOLS != LM_SHADOW));
  141   assert((LM_RPC != LM_SERVICES) && (LM_RPC != LM_SHADOW));
  142   assert((LM_SERVICES != LM_SHADOW));
  143   /* test supported names */
  144   line = "alIas"; assert(get_map(&line) == LM_ALIASES);
  145   line = "AliasES"; assert(get_map(&line) == LM_ALIASES);
  146   line = "ether"; assert(get_map(&line) == LM_ETHERS);
  147   line = "ethers"; assert(get_map(&line) == LM_ETHERS);
  148   line = "group"; assert(get_map(&line) == LM_GROUP);
  149   line = "host"; assert(get_map(&line) == LM_HOSTS);
  150   line = "hosts"; assert(get_map(&line) == LM_HOSTS);
  151   line = "netgroup"; assert(get_map(&line) == LM_NETGROUP);
  152   line = "network"; assert(get_map(&line) == LM_NETWORKS);
  153   line = "networks"; assert(get_map(&line) == LM_NETWORKS);
  154   line = "passwd"; assert(get_map(&line) == LM_PASSWD);
  155   line = "protocol"; assert(get_map(&line) == LM_PROTOCOLS);
  156   line = "protocols"; assert(get_map(&line) == LM_PROTOCOLS);
  157   line = "rpc"; assert(get_map(&line) == LM_RPC);
  158   line = "service"; assert(get_map(&line) == LM_SERVICES);
  159   line = "services"; assert(get_map(&line) == LM_SERVICES);
  160   line = "shadow"; assert(get_map(&line) == LM_SHADOW);
  161   line = "unknown"; assert(get_map(&line) == LM_NONE);
  162   line = "x"; assert(get_map(&line) == LM_NONE);
  163 }
  164 
  165 static void test_parse_map_statement(void)
  166 {
  167   /* TODO: implement */
  168 }
  169 
  170 static void test_tokenize(void)
  171 {
  172   /* this leaks memory all over the place */
  173   char *line = strdup("yes  this is 1 simple line");
  174   char *str;
  175   int i;
  176   i = get_boolean(__FILE__, __LINE__, __PRETTY_FUNCTION__, &line);
  177   assert(i == 1);
  178   str = get_strdup(__FILE__, __LINE__, __PRETTY_FUNCTION__, &line);
  179   assertstreq(str, "this");
  180   str = get_strdup(__FILE__, __LINE__, __PRETTY_FUNCTION__, &line);
  181   assertstreq(str, "is");
  182   i = get_int(__FILE__, __LINE__, __PRETTY_FUNCTION__, &line);
  183   assert(i == 1);
  184   str = get_linedup(__FILE__, __LINE__, __PRETTY_FUNCTION__, &line);
  185   assertstreq(str, "simple line");
  186   get_eol(__FILE__, __LINE__, __PRETTY_FUNCTION__, &line);
  187 }
  188 
  189 extern const char *passwd_bases[];
  190 extern const char *group_bases[];
  191 extern const char *group_filter;
  192 extern int passwd_scope;
  193 
  194 static void test_read(void)
  195 {
  196   FILE *fp;
  197   struct ldap_config cfg;
  198   /* write some stuff to a temporary file */
  199   fp = fopen("temp.cfg", "w");
  200   assert(fp != NULL);
  201   fprintf(fp, "# a line of comments\n"
  202           "uri ldap://127.0.0.1/\n"
  203           "uri ldap:/// ldaps://127.0.0.1/\n"
  204           "base dc=test, dc=tld\n"
  205           "base passwd ou=Some People,dc=test,dc=tld\n"
  206           "base group \"\"\n"
  207           "map\tpasswd uid\t\tsAMAccountName\n"
  208           "map passwd homeDirectory \"${homeDirectory:-/home/$uid}\"  \n"
  209           "map    passwd gecos            \"${givenName}. ${sn}\"\n"
  210           "filter group (&(objeclClass=posixGroup)(gid=1*))\n"
  211           "\n"
  212           "scope passwd one\n"
  213           "cache dn2uid 10m 1s\n");
  214   fclose(fp);
  215   /* parse the file */
  216   cfg_defaults(&cfg);
  217   cfg_read("temp.cfg", &cfg);
  218   /* check results */
  219   assert(cfg.uris[0].uri != NULL);
  220   assert(cfg.uris[1].uri != NULL);
  221   assert(cfg.uris[2].uri != NULL);
  222   assertstreq(cfg.uris[0].uri, "ldap://127.0.0.1/");
  223   assertstreq(cfg.uris[1].uri, "ldap:///");
  224   assertstreq(cfg.uris[2].uri, "ldaps://127.0.0.1/");
  225   assert(cfg.uris[3].uri == NULL);
  226   assertstreq(cfg.bases[0], "dc=test, dc=tld");
  227   assertstreq(passwd_bases[0], "ou=Some People,dc=test,dc=tld");
  228   assertstreq(group_bases[0], "");
  229   assertstreq(attmap_passwd_uid, "sAMAccountName");
  230   assertstreq(attmap_passwd_homeDirectory, "\"${homeDirectory:-/home/$uid}\"");
  231   assertstreq(attmap_passwd_gecos, "\"${givenName}. ${sn}\"");
  232   assertstreq(group_filter, "(&(objeclClass=posixGroup)(gid=1*))");
  233   assert(passwd_scope == LDAP_SCOPE_ONELEVEL);
  234   assert(cfg.cache_dn2uid_positive == 10 * 60);
  235   assert(cfg.cache_dn2uid_negative == 1);
  236   /* remove temporary file */
  237   remove("temp.cfg");
  238 }
  239 
  240 /* the main program... */
  241 int main(int UNUSED(argc), char UNUSED(*argv[]))
  242 {
  243   test_xstrdup();
  244   test_add_uris();
  245   test_parse_boolean();
  246   test_parse_scope();
  247   test_parse_map();
  248   test_parse_map_statement();
  249   test_tokenize();
  250   test_read();
  251   return EXIT_SUCCESS;
  252 }