LTP GCOV extension - code coverage report
Current view: directory - trunk/src - checkconf.c
Test: lcov.info
Date: 2007-05-09 Instrumented lines: 77
Code covered: 53.2 % Executed lines: 41

       1                 : /*
       2                 :  * checkconf.c - Miredo conf parser unit test
       3                 :  * $Id: checkconf.c 1938 2007-02-22 20:55:28Z remi $
       4                 :  */
       5                 : 
       6                 : /***********************************************************************
       7                 :  *  Copyright © 2005-2007 Rémi Denis-Courmont.                         *
       8                 :  *  This program is free software; you can redistribute and/or modify  *
       9                 :  *  it under the terms of the GNU General Public License as published  *
      10                 :  *  by the Free Software Foundation; version 2 of the license.         *
      11                 :  *                                                                     *
      12                 :  *  This program 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.               *
      15                 :  *  See the GNU General Public License for more details.               *
      16                 :  *                                                                     *
      17                 :  *  You should have received a copy of the GNU General Public License  *
      18                 :  *  along with this program; if not, you can get it from:              *
      19                 :  *  http://www.gnu.org/copyleft/gpl.html                               *
      20                 :  ***********************************************************************/
      21                 : 
      22                 : #ifdef HAVE_CONFIG_H
      23                 : # include <config.h>
      24                 : #endif
      25                 : 
      26                 : #include <gettext.h>
      27                 : #include <locale.h>
      28                 : #include "binreloc.h"
      29                 : 
      30                 : #include <stdio.h>
      31                 : #include <stdlib.h>
      32                 : #include <stdarg.h>
      33                 : #include <string.h>
      34                 : #include <stdbool.h>
      35                 : 
      36                 : #include <unistd.h>
      37                 : #include <inttypes.h>
      38                 : #include <netinet/in.h>
      39                 : #include "miredo.h"
      40                 : #include "conf.h"
      41                 : 
      42                 : #ifdef HAVE_GETOPT_H
      43                 : # include <getopt.h>
      44                 : #endif
      45                 : 
      46                 : static void logger (void *fail, bool error, const char *fmt, va_list ap)
      47               0 : {
      48               0 :         *((bool *)fail) = true;
      49                 :         (void)error;
      50                 : 
      51               0 :         vfprintf (stderr, fmt, ap);
      52               0 :         fputc ('\n', stderr);
      53               0 : }
      54                 : 
      55                 : /* FIXME: use same more clever code as in main.c */
      56                 : static const char conffile[] = SYSCONFDIR"/miredo/miredo.conf";
      57                 : 
      58                 : static int miredo_checkconf (miredo_conf *conf)
      59               3 : {
      60               3 :         int i, res = 0;
      61               3 :         if (!miredo_conf_parse_syslog_facility (conf, "SyslogFacility", &i))
      62               0 :                 res = -1;
      63                 : 
      64               3 :         bool client = true;
      65                 : 
      66                 :         unsigned line;
      67               3 :         char *val = miredo_conf_get (conf, "RelayType", &line);
      68                 : 
      69               3 :         if (val != NULL)
      70                 :         {
      71               0 :                 if ((strcasecmp (val, "client") == 0)
      72                 :                  || (strcasecmp (val, "autoclient") == 0))
      73               0 :                         client = true;
      74                 :                 else
      75               0 :                 if ((strcasecmp (val, "relay") == 0)
      76                 :                  || (strcasecmp (val, "cone") == 0)
      77                 :                  || (strcasecmp (val, "restricted") == 0))
      78               0 :                         client = false;
      79                 :                 else
      80                 :                 {
      81               0 :                         fprintf (stderr, _("Invalid relay type \"%s\" at line %u"),
      82                 :                                  val, line);
      83               0 :                         fputc ('\n', stderr);
      84               0 :                         res = -1;
      85                 :                 }
      86               0 :                 free (val);
      87                 :         }
      88                 : 
      89                 :         uint32_t u32;
      90                 :         uint16_t u16;
      91                 : 
      92               3 :         if (client)
      93                 :         {
      94                 : #ifdef MIREDO_TEREDO_CLIENT
      95               3 :                 uint32_t ip = 0;
      96                 : 
      97               3 :                 if (!miredo_conf_parse_IPv4 (conf, "ServerAddress", &ip)
      98                 :                  || !miredo_conf_parse_IPv4 (conf, "ServerAddress2", &u32))
      99               0 :                         res = -1;
     100                 : 
     101               3 :                 if (ip == 0)
     102                 :                 {
     103               0 :                         fprintf (stderr, "%s\n", _("Server address not specified"));
     104               0 :                         res = -1;
     105                 :                 }
     106                 : #else
     107                 :                 fprintf (stderr, "%s\n", _("Unsupported Teredo client mode"));
     108                 :                 res = -1;
     109                 : #endif
     110                 :         }
     111                 :         else
     112                 :         {
     113                 :                 uint32_t pref;
     114               0 :                 if (!miredo_conf_parse_teredo_prefix (conf, "Prefix", &pref)
     115                 :                  || !miredo_conf_get_int16 (conf, "InterfaceMTU", &u16, NULL))
     116               0 :                         res = -1;
     117                 :         }
     118                 : 
     119               3 :         if (!miredo_conf_parse_IPv4 (conf, "BindAddress", &u32)
     120                 :          || !miredo_conf_get_int16 (conf, "BindPort", &u16, NULL))
     121               0 :                 res = -1;
     122                 : 
     123               3 :         char *str = miredo_conf_get (conf, "InterfaceName", NULL);
     124               3 :         if (str != NULL)
     125               3 :                 free (str);
     126                 : 
     127               3 :         miredo_conf_clear (conf, 5);
     128               3 :         return res;
     129                 : }
     130                 : 
     131                 : 
     132                 : static int miredo_checkconffile (const char *filename)
     133               3 : {
     134               3 :         bool failed = false;
     135               3 :         miredo_conf *conf = miredo_conf_create (logger, &failed);
     136                 : 
     137               3 :         if (conf == NULL)
     138               0 :                 return -1;
     139                 : 
     140               3 :         if (!miredo_conf_read_file (conf, filename))
     141               0 :                 failed = true;
     142                 :         else
     143               3 :         if (miredo_checkconf (conf))
     144               0 :                 failed = true;
     145                 : 
     146               3 :         miredo_conf_destroy (conf);
     147               3 :         if (failed)
     148                 :         {
     149               0 :                 fprintf (stderr, "%s\n", _("Fatal configuration error"));
     150               0 :                 return -1;
     151                 :         }
     152               3 :         return 0;
     153                 : }
     154                 : 
     155                 : 
     156                 : static int usage (const char *path)
     157               0 : {
     158               0 :         printf ("Usage: %s [CONF_FILE]\n", path);
     159               0 :         return 0;
     160                 : }
     161                 : 
     162                 : 
     163                 : int main(int argc, char *argv[])
     164               3 : {
     165               3 :         (void)br_init (NULL);
     166               3 :         (void)setlocale (LC_ALL, "");
     167               3 :         char *path = br_find_locale_dir (LOCALEDIR);
     168                 :         (void)bindtextdomain (PACKAGE_NAME, path);
     169               3 :         free (path);
     170               3 :         path = NULL;
     171                 : 
     172                 :         static const struct option opts[] =
     173                 :         {
     174                 :                 { "conf",       required_argument, NULL, 'c' },
     175                 :                 { "config",     required_argument, NULL, 'c' },
     176                 :                 { "foreground", no_argument,       NULL, 'f' },
     177                 :                 { "help",       no_argument,       NULL, 'h' },
     178                 :                 { "pidfile",    required_argument, NULL, 'p' },
     179                 :                 { "chroot",     required_argument, NULL, 't' },
     180                 :                 { "chrootdir",  required_argument, NULL, 't' },
     181                 :                 { "user",       required_argument, NULL, 'u' },
     182                 :                 { "username",   required_argument, NULL, 'u' },
     183                 :                 { "version",    no_argument,       NULL, 'V' },
     184                 :                 { NULL,         no_argument,       NULL, '\0'}
     185                 :         };
     186                 : 
     187               3 :         const char *filename = NULL;
     188                 : 
     189                 :         int c;
     190               6 :         while ((c = getopt_long (argc, argv, "c:fhp:t:u:V", opts, NULL)) != -1)
     191               0 :                 switch (c)
     192                 :                 {
     193                 :                         case 'c':
     194               0 :                                 filename = optarg;
     195               0 :                                 break;
     196                 : 
     197                 :                         case 'h':
     198               0 :                                 return usage(argv[0]);
     199                 : 
     200                 :                         case 'V':
     201               0 :                                 return miredo_version();
     202                 :                 }
     203                 : 
     204               3 :         if (optind < argc)
     205               0 :                 filename = argv[optind++];
     206                 :         else
     207               3 :         if (filename == NULL)
     208                 :         {
     209                 :                 /* No parameters provided - attempt in source tree test */
     210               3 :                 const char *srcdir = getenv ("srcdir");
     211                 : 
     212               3 :                 if (srcdir != NULL)
     213               3 :                         filename = "../misc/miredo.conf";
     214                 :                 else
     215               0 :                         filename = conffile;
     216                 :         }
     217                 : 
     218               3 :         int res = miredo_checkconffile (filename);
     219                 : 
     220               3 :         if (path != NULL)
     221               0 :                 free (path);
     222                 : 
     223               3 :         return res;
     224                 : }

Generated by: LTP GCOV extension version 1.5