LCOV - code coverage report
Current view: top level - common - t-session-env.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 120 161 74.5 %
Date: 2016-11-29 15:00:56 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* t-session-env.c - Module test for session-env.c
       2             :  *      Copyright (C) 2009 Free Software Foundation, Inc.
       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 <https://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : #include <stdio.h>
      22             : #include <stdlib.h>
      23             : #include <errno.h>
      24             : #include <assert.h>
      25             : 
      26             : #include "util.h"
      27             : #include "session-env.h"
      28             : 
      29             : #define pass()  do { ; } while(0)
      30             : #define fail(e) do { fprintf (stderr, "%s:%d: function failed: %s\n",  \
      31             :                                __FILE__,__LINE__, gpg_strerror (e));  \
      32             :                      exit (1);                                        \
      33             :                    } while(0)
      34             : 
      35             : static int verbose;
      36             : 
      37             : static void
      38           8 : listall (session_env_t se)
      39             : {
      40           8 :   int iterator = 0;
      41             :   const char *name, *value;
      42             :   int def;
      43             : 
      44           8 :   if (verbose)
      45           0 :     printf ("environment of %p\n", se);
      46         604 :   while ( (name = session_env_listenv (se, &iterator, &value, &def)) )
      47         588 :     if (verbose)
      48           0 :       printf ("  %s%s=%s\n",  def? "[def] ":"      ", name, value);
      49             : 
      50           8 : }
      51             : 
      52             : 
      53             : static void
      54           1 : show_stdnames (void)
      55             : {
      56             :   const char *name, *assname;
      57           1 :   int iterator = 0;
      58             :   int count;
      59             : 
      60           1 :   printf ("    > Known envvars:");
      61           1 :   count = 20;
      62          12 :   while ((name = session_env_list_stdenvnames (&iterator, &assname)))
      63             :     {
      64          10 :       if (count > 60)
      65             :         {
      66           2 :           printf ("\n    >");
      67           2 :           count = 7;
      68             :         }
      69          10 :       printf ( " %s", name);
      70          10 :       count += strlen (name) + 1;
      71          10 :       if (assname)
      72             :         {
      73           5 :           printf ( "(%s)", assname);
      74           5 :           count += strlen (assname) + 2;
      75             :         }
      76             :     }
      77           1 :   putchar('\n');
      78           1 : }
      79             : 
      80             : 
      81             : static void
      82           1 : test_all (void)
      83             : {
      84             :   gpg_error_t err;
      85             :   session_env_t se_0, se;
      86             :   const char *s, *s2;
      87             :   int idx;
      88             : 
      89           1 :   se_0 = session_env_new ();
      90           1 :   if (!se_0)
      91           0 :     fail (gpg_error_from_syserror ());
      92           1 :   se = session_env_new ();
      93           1 :   if (!se)
      94           0 :     fail (gpg_error_from_syserror ());
      95             : 
      96           1 :   err = session_env_putenv (se, NULL);
      97           1 :   if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
      98           0 :     fail (err);
      99           1 :   err = session_env_putenv (se, "");
     100           1 :   if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
     101           0 :     fail (err);
     102           1 :   err = session_env_putenv (se, "=");
     103           1 :   if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
     104           0 :     fail (err);
     105             : 
     106             :   /* Delete some nonexistant variables.  */
     107           1 :   err = session_env_putenv (se, "A");
     108           1 :   if (err)
     109           0 :     fail (err);
     110           1 :   err = session_env_putenv (se, "a");
     111           1 :   if (err)
     112           0 :     fail (err);
     113           1 :   err = session_env_putenv (se, "_aaaa aaaaaasssssssssssss\nddd");
     114           1 :   if (err)
     115           0 :     fail (err);
     116             : 
     117             :   /* Create a few variables.  */
     118           1 :   err = session_env_putenv (se, "EMPTY=");
     119           1 :   if (err)
     120           0 :     fail (err);
     121           1 :   err = session_env_putenv (se, "foo=value_of_foo");
     122           1 :   if (err)
     123           0 :     fail (err);
     124           1 :   err = session_env_putenv (se, "bar=the value_of_bar");
     125           1 :   if (err)
     126           0 :     fail (err);
     127           1 :   err = session_env_putenv (se, "baz=this-is-baz");
     128           1 :   if (err)
     129           0 :     fail (err);
     130           1 :   err = session_env_putenv (se, "BAZ=this-is-big-baz");
     131           1 :   if (err)
     132           0 :     fail (err);
     133             : 
     134           1 :   listall (se);
     135             : 
     136             :   /* Update one.  */
     137           1 :   err = session_env_putenv (se, "baz=this-is-another-baz");
     138           1 :   if (err)
     139           0 :     fail (err);
     140             : 
     141           1 :   listall (se);
     142             : 
     143             :   /* Delete one.  */
     144           1 :   err = session_env_putenv (se, "bar");
     145           1 :   if (err)
     146           0 :     fail (err);
     147             : 
     148           1 :   listall (se);
     149             : 
     150             :   /* Insert a new one.  */
     151           1 :   err = session_env_putenv (se, "FOO=value_of_foo");
     152           1 :   if (err)
     153           0 :     fail (err);
     154             : 
     155           1 :   listall (se);
     156             : 
     157             :   /* Retrieve a default one.  */
     158           1 :   s = session_env_getenv_or_default (se, "HOME", NULL);
     159           1 :   if (!s)
     160             :     {
     161           0 :       fprintf (stderr, "failed to get default of HOME\n");
     162           0 :       exit (1);
     163             :     }
     164             : 
     165           1 :   s = session_env_getenv (se, "HOME");
     166           1 :   if (s)
     167           0 :     fail(0);  /* This is a default value, thus we should not see it.  */
     168             : 
     169           1 :   s = session_env_getenv_or_default (se, "HOME", NULL);
     170           1 :   if (!s)
     171           0 :     fail(0);  /* But here we should see it.  */
     172             : 
     173             :   /* Add a few more.  */
     174           1 :   err = session_env_putenv (se, "X1=A value");
     175           1 :   if (err)
     176           0 :     fail (err);
     177           1 :   err = session_env_putenv (se, "X2=Another value");
     178           1 :   if (err)
     179           0 :     fail (err);
     180           1 :   err = session_env_putenv (se, "X3=A value");
     181           1 :   if (err)
     182           0 :     fail (err);
     183             : 
     184           1 :   listall (se);
     185             : 
     186             :   /* Check that we can overwrite a default value.  */
     187           1 :   err = session_env_putenv (se, "HOME=/this/is/my/new/home");
     188           1 :   if (err)
     189           0 :     fail (err);
     190             :   /* And that we get this string back.  */
     191           1 :   s = session_env_getenv (se, "HOME");
     192           1 :   if (!s)
     193           0 :     fail (0);
     194           1 :   if (strcmp (s, "/this/is/my/new/home"))
     195           0 :     fail (0);
     196             :   /* A new get default should return the very same string.  */
     197           1 :   s2 = session_env_getenv_or_default (se, "HOME", NULL);
     198           1 :   if (!s2)
     199           0 :     fail (0);
     200           1 :   if (s2 != s)
     201           0 :     fail (0);
     202             : 
     203           1 :   listall (se);
     204             : 
     205             :   /* Check that the other object is clean.  */
     206             :   {
     207           1 :     int iterator = 0;
     208             : 
     209           1 :     if (session_env_listenv (se_0, &iterator, NULL, NULL))
     210           0 :       fail (0);
     211             :   }
     212             : 
     213             : 
     214           1 :   session_env_release (se);
     215             : 
     216             :   /* Use a new session for quick mass test.  */
     217           1 :   se = session_env_new ();
     218           1 :   if (!se)
     219           0 :     fail (gpg_error_from_syserror ());
     220             : 
     221             :   /* Create.  */
     222         501 :   for (idx=0; idx < 500; idx++)
     223             :     {
     224             :       char buf[100];
     225             : 
     226         500 :       snprintf (buf, sizeof buf, "FOO_%d=Value for %x", idx, idx);
     227         500 :       err = session_env_putenv (se, buf);
     228         500 :       if (err)
     229           0 :         fail (err);
     230             :     }
     231           1 :   err = session_env_setenv (se, "TEST1", "value1");
     232           1 :   if (err)
     233           0 :     fail (err);
     234           1 :   err = session_env_setenv (se, "TEST1", "value1-updated");
     235           1 :   if (err)
     236           0 :     fail (err);
     237             : 
     238           1 :   listall (se);
     239             : 
     240             :   /* Delete all.  */
     241         501 :   for (idx=0; idx < 500; idx++)
     242             :     {
     243             :       char buf[100];
     244             : 
     245         500 :       snprintf (buf, sizeof buf, "FOO_%d", idx);
     246         500 :       err = session_env_putenv (se, buf);
     247         500 :       if (err)
     248           0 :         fail (err);
     249             :     }
     250           1 :   err = session_env_setenv (se, "TEST1", NULL);
     251           1 :   if (err)
     252           0 :     fail (err);
     253             : 
     254             :   /* Check that all are deleted.  */
     255             :   {
     256           1 :     int iterator = 0;
     257             : 
     258           1 :     if (session_env_listenv (se, &iterator, NULL, NULL))
     259           0 :       fail (0);
     260             :   }
     261             : 
     262             :   /* Add a few strings again.  */
     263         501 :   for (idx=0; idx < 500; idx++)
     264             :     {
     265             :       char buf[100];
     266             : 
     267         500 :       if (!(idx % 10))
     268             :         {
     269          50 :           if ( !(idx % 3))
     270          17 :             snprintf (buf, sizeof buf, "FOO_%d=", idx);
     271             :           else
     272          33 :             snprintf (buf, sizeof buf, "FOO_%d=new value for %x", idx, idx);
     273          50 :           err = session_env_putenv (se, buf);
     274          50 :           if (err)
     275           0 :             fail (err);
     276             :         }
     277             :     }
     278             : 
     279           1 :   listall (se);
     280             : 
     281           1 :   session_env_release (se);
     282             : 
     283           1 :   session_env_release (se_0);
     284           1 : }
     285             : 
     286             : 
     287             : 
     288             : int
     289           1 : main (int argc, char **argv)
     290             : {
     291           1 :   if (argc)
     292           1 :     { argc--; argv++; }
     293           1 :   if (argc && !strcmp (argv[0], "--verbose"))
     294             :     {
     295           0 :       verbose = 1;
     296           0 :       argc--; argv++;
     297             :     }
     298             : 
     299             : 
     300           1 :   show_stdnames ();
     301           1 :   test_all ();
     302             : 
     303           1 :   return 0;
     304             : }

Generated by: LCOV version 1.11