LCOV - code coverage report
Current view: top level - glib/glib/tests - environment.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 163 165 98.8 %
Date: 2024-03-26 05:16:46 Functions: 7 7 100.0 %
Branches: 10 16 62.5 %

           Branch data     Line data    Source code
       1                 :            : /* GLIB - Library of useful routines for C programming
       2                 :            :  * Copyright (C) 2010 Ryan Lortie
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This library 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 GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General Public
      17                 :            :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18                 :            :  */
      19                 :            : 
      20                 :            : #include <glib.h>
      21                 :            : 
      22                 :            : static void
      23                 :          1 : test_listenv (void)
      24                 :            : {
      25                 :            :   GHashTable *table;
      26                 :            :   gchar **list;
      27                 :            :   gint i;
      28                 :            : 
      29                 :          1 :   g_test_summary ("Test g_get_environ() returns an array of unique keys, all "
      30                 :            :                   "of which can be individually queried using g_getenv() to "
      31                 :            :                   "return the same value.");
      32                 :            : 
      33                 :          1 :   table = g_hash_table_new_full (g_str_hash, g_str_equal,
      34                 :            :                                  g_free, g_free);
      35                 :            : 
      36                 :          1 :   list = g_get_environ ();
      37         [ +  + ]:        163 :   for (i = 0; list[i]; i++)
      38                 :            :     {
      39                 :            :       gchar **parts;
      40                 :            : 
      41                 :        162 :       parts = g_strsplit (list[i], "=", 2);
      42                 :        162 :       g_assert_null (g_hash_table_lookup (table, parts[0]));
      43         [ +  - ]:        162 :       if (g_strcmp0 (parts[0], ""))
      44                 :        162 :         g_hash_table_insert (table, parts[0], parts[1]);
      45                 :        162 :       g_free (parts);
      46                 :            :     }
      47                 :          1 :   g_strfreev (list);
      48                 :            : 
      49                 :          1 :   g_assert_cmpint (g_hash_table_size (table), >, 0);
      50                 :            : 
      51                 :          1 :   list = g_listenv ();
      52         [ +  + ]:        163 :   for (i = 0; list[i]; i++)
      53                 :            :     {
      54                 :            :       const gchar *expected;
      55                 :            :       const gchar *value;
      56                 :            : 
      57                 :        162 :       expected = g_hash_table_lookup (table, list[i]);
      58                 :        162 :       value = g_getenv (list[i]);
      59                 :        162 :       g_assert_cmpstr (value, ==, expected);
      60                 :        162 :       g_hash_table_remove (table, list[i]);
      61                 :            :     }
      62                 :          1 :   g_assert_cmpint (g_hash_table_size (table), ==, 0);
      63                 :          1 :   g_hash_table_unref (table);
      64                 :          1 :   g_strfreev (list);
      65                 :          1 : }
      66                 :            : 
      67                 :            : static void
      68                 :          1 : test_getenv (void)
      69                 :            : {
      70                 :            :   const gchar *data;
      71                 :          1 :   const gchar *variable = "TEST_G_SETENV";
      72                 :          1 :   const gchar *value1 = "works";
      73                 :          1 :   const gchar *value2 = "again";
      74                 :            : 
      75                 :          1 :   g_test_summary ("Test setting an environment variable using g_setenv(), and "
      76                 :            :                   "that the updated value is queryable using g_getenv().");
      77                 :            : 
      78                 :            :   /* Check that TEST_G_SETENV is not already set */
      79                 :          1 :   g_assert_null (g_getenv (variable));
      80                 :            : 
      81                 :            :   /* Check if g_setenv() failed */
      82                 :          1 :   g_assert_cmpint (g_setenv (variable, value1, TRUE), !=, 0);
      83                 :            : 
      84                 :          1 :   data = g_getenv (variable);
      85                 :          1 :   g_assert_nonnull (data);
      86                 :          1 :   g_assert_cmpstr (data, ==, value1);
      87                 :            : 
      88                 :          1 :   g_assert_cmpint (g_setenv (variable, value2, FALSE), !=, 0);
      89                 :            : 
      90                 :          1 :   data = g_getenv (variable);
      91                 :          1 :   g_assert_nonnull (data);
      92                 :          1 :   g_assert_cmpstr (data, !=, value2);
      93                 :          1 :   g_assert_cmpstr (data, ==, value1);
      94                 :            : 
      95                 :          1 :   g_assert_cmpint (g_setenv (variable, value2, TRUE), !=, 0);
      96                 :            : 
      97                 :          1 :   data = g_getenv (variable);
      98                 :          1 :   g_assert_nonnull (data);
      99                 :          1 :   g_assert_cmpstr (data, !=, value1);
     100                 :          1 :   g_assert_cmpstr (data, ==, value2);
     101                 :            : 
     102                 :          1 :   g_unsetenv (variable);
     103                 :          1 :   g_assert_null (g_getenv (variable));
     104                 :            : 
     105         [ +  - ]:          1 :   if (g_test_undefined ())
     106                 :            :     {
     107                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     108                 :            :                              "*assertion* != NULL*");
     109                 :          1 :       g_assert_false (g_setenv (NULL, "baz", TRUE));
     110                 :          1 :       g_test_assert_expected_messages ();
     111                 :            : 
     112                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     113                 :            :                              "*assertion* != NULL*");
     114                 :          1 :       g_assert_false (g_setenv ("foo", NULL, TRUE));
     115                 :          1 :       g_test_assert_expected_messages ();
     116                 :            : 
     117                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     118                 :            :                              "*assertion* == NULL*");
     119                 :          1 :       g_assert_false (g_setenv ("foo=bar", "baz", TRUE));
     120                 :          1 :       g_test_assert_expected_messages ();
     121                 :            :     }
     122                 :            : 
     123                 :          1 :   g_assert_true (g_setenv ("foo", "bar=baz", TRUE));
     124                 :            : 
     125                 :            :   /* Different OSs return different values; some return NULL because the key
     126                 :            :    * is invalid, but some are happy to return what we set above. */
     127                 :          1 :   data = g_getenv ("foo=bar");
     128         [ +  - ]:          1 :   if (data != NULL)
     129                 :          1 :     g_assert_cmpstr (data, ==, "baz");
     130                 :            :   else
     131                 :            :     {
     132                 :          0 :       data = g_getenv ("foo");
     133                 :          0 :       g_assert_cmpstr (data, ==, "bar=baz");
     134                 :            :     }
     135                 :            : 
     136         [ +  - ]:          1 :   if (g_test_undefined ())
     137                 :            :     {
     138                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     139                 :            :                              "*assertion* != NULL*");
     140                 :          1 :       g_unsetenv (NULL);
     141                 :          1 :       g_test_assert_expected_messages ();
     142                 :            : 
     143                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     144                 :            :                              "*assertion* == NULL*");
     145                 :          1 :       g_unsetenv ("foo=bar");
     146                 :          1 :       g_test_assert_expected_messages ();
     147                 :            :     }
     148                 :            : 
     149                 :          1 :   g_unsetenv ("foo");
     150                 :          1 :   g_assert_null (g_getenv ("foo"));
     151                 :          1 : }
     152                 :            : 
     153                 :            : static void
     154                 :          1 : test_setenv (void)
     155                 :            : {
     156                 :            :   const gchar *var, *value;
     157                 :            : 
     158                 :          1 :   var = "NOSUCHENVVAR";
     159                 :          1 :   value = "value1";
     160                 :            : 
     161                 :          1 :   g_assert_null (g_getenv (var));
     162                 :          1 :   g_setenv (var, value, FALSE);
     163                 :          1 :   g_assert_cmpstr (g_getenv (var), ==, value);
     164                 :          1 :   g_assert_true (g_setenv (var, "value2", FALSE));
     165                 :          1 :   g_assert_cmpstr (g_getenv (var), ==, value);
     166                 :          1 :   g_assert_true (g_setenv (var, "value2", TRUE));
     167                 :          1 :   g_assert_cmpstr (g_getenv (var), ==, "value2");
     168                 :          1 :   g_unsetenv (var);
     169                 :          1 :   g_assert_null (g_getenv (var));
     170                 :          1 : }
     171                 :            : 
     172                 :            : static void
     173                 :          1 : test_environ_array (void)
     174                 :            : {
     175                 :            :   gchar **env;
     176                 :            :   const gchar *value;
     177                 :            : 
     178                 :          1 :   g_test_summary ("Test getting and setting variables on a local envp array "
     179                 :            :                   "(rather than the global envp).");
     180                 :            : 
     181                 :          1 :   env = g_new (gchar *, 1);
     182                 :          1 :   env[0] = NULL;
     183                 :            : 
     184         [ +  - ]:          1 :   if (g_test_undefined ())
     185                 :            :     {
     186                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     187                 :            :                              "*assertion* != NULL*");
     188                 :          1 :       g_environ_getenv (env, NULL);
     189                 :          1 :       g_test_assert_expected_messages ();
     190                 :            :     }
     191                 :            : 
     192                 :          1 :   value = g_environ_getenv (env, "foo");
     193                 :          1 :   g_assert_null (value);
     194                 :            : 
     195         [ +  - ]:          1 :   if (g_test_undefined ())
     196                 :            :     {
     197                 :            :       gchar **undefined_env;
     198                 :            : 
     199                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     200                 :            :                              "*assertion* != NULL*");
     201                 :          1 :       undefined_env = g_environ_setenv (env, NULL, "bar", TRUE);
     202                 :          1 :       g_test_assert_expected_messages ();
     203                 :          1 :       g_strfreev (undefined_env);
     204                 :            : 
     205                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     206                 :            :                              "*assertion* == NULL*");
     207                 :          1 :       undefined_env = g_environ_setenv (env, "foo=fuz", "bar", TRUE);
     208                 :          1 :       g_test_assert_expected_messages ();
     209                 :          1 :       g_strfreev (undefined_env);
     210                 :            : 
     211                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     212                 :            :                              "*assertion* != NULL*");
     213                 :          1 :       undefined_env = g_environ_setenv (env, "foo", NULL, TRUE);
     214                 :          1 :       g_test_assert_expected_messages ();
     215                 :          1 :       g_strfreev (undefined_env);
     216                 :            : 
     217                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     218                 :            :                              "*assertion* != NULL*");
     219                 :          1 :       undefined_env = g_environ_unsetenv (env, NULL);
     220                 :          1 :       g_test_assert_expected_messages ();
     221                 :          1 :       g_strfreev (undefined_env);
     222                 :            :     }
     223                 :            : 
     224                 :          1 :   env = g_environ_setenv (env, "foo", "bar", TRUE);
     225                 :          1 :   value = g_environ_getenv (env, "foo");
     226                 :          1 :   g_assert_cmpstr (value, ==, "bar");
     227                 :            : 
     228                 :          1 :   env = g_environ_setenv (env, "foo2", "bar2", FALSE);
     229                 :          1 :   value = g_environ_getenv (env, "foo");
     230                 :          1 :   g_assert_cmpstr (value, ==, "bar");
     231                 :          1 :   value = g_environ_getenv (env, "foo2");
     232                 :          1 :   g_assert_cmpstr (value, ==, "bar2");
     233                 :            : 
     234                 :          1 :   env = g_environ_setenv (env, "foo", "x", FALSE);
     235                 :          1 :   value = g_environ_getenv (env, "foo");
     236                 :          1 :   g_assert_cmpstr (value, ==, "bar");
     237                 :            : 
     238                 :          1 :   env = g_environ_setenv (env, "foo", "x", TRUE);
     239                 :          1 :   value = g_environ_getenv (env, "foo");
     240                 :          1 :   g_assert_cmpstr (value, ==, "x");
     241                 :            : 
     242                 :          1 :   env = g_environ_unsetenv (env, "foo2");
     243                 :          1 :   value = g_environ_getenv (env, "foo2");
     244                 :          1 :   g_assert_null (value);
     245                 :            : 
     246                 :          1 :   g_strfreev (env);
     247                 :          1 : }
     248                 :            : 
     249                 :            : static void
     250                 :          1 : test_environ_null (void)
     251                 :            : {
     252                 :            :   gchar **env;
     253                 :            :   const gchar *value;
     254                 :            : 
     255                 :          1 :   g_test_summary ("Test getting and setting variables on a NULL envp array.");
     256                 :            : 
     257                 :          1 :   env = NULL;
     258                 :            : 
     259                 :          1 :   value = g_environ_getenv (env, "foo");
     260                 :          1 :   g_assert_null (value);
     261                 :            : 
     262                 :          1 :   env = g_environ_setenv (NULL, "foo", "bar", TRUE);
     263                 :          1 :   g_assert_nonnull (env);
     264                 :          1 :   g_strfreev (env);
     265                 :            : 
     266                 :          1 :   env = g_environ_unsetenv (NULL, "foo");
     267                 :          1 :   g_assert_null (env);
     268                 :          1 : }
     269                 :            : 
     270                 :            : static void
     271                 :          1 : test_environ_case (void)
     272                 :            : {
     273                 :            :   gchar **env;
     274                 :            :   const gchar *value;
     275                 :            : 
     276                 :          1 :   g_test_summary ("Test that matching environment variables is case-insensitive "
     277                 :            :                   "on Windows and not on other platforms, since envvars were "
     278                 :            :                   "case-insensitive on DOS.");
     279                 :            : 
     280                 :          1 :   env = NULL;
     281                 :            : 
     282                 :          1 :   env = g_environ_setenv (env, "foo", "bar", TRUE);
     283                 :          1 :   value = g_environ_getenv (env, "foo");
     284                 :          1 :   g_assert_cmpstr (value, ==, "bar");
     285                 :            : 
     286                 :          1 :   value = g_environ_getenv (env, "Foo");
     287                 :            : #ifdef G_OS_WIN32
     288                 :            :   g_assert_cmpstr (value, ==, "bar");
     289                 :            : #else
     290                 :          1 :   g_assert_null (value);
     291                 :            : #endif
     292                 :            : 
     293                 :          1 :   env = g_environ_setenv (env, "FOO", "x", TRUE);
     294                 :          1 :   value = g_environ_getenv (env, "foo");
     295                 :            : #ifdef G_OS_WIN32
     296                 :            :   g_assert_cmpstr (value, ==, "x");
     297                 :            : #else
     298                 :          1 :   g_assert_cmpstr (value, ==, "bar");
     299                 :            : #endif
     300                 :            : 
     301                 :          1 :   env = g_environ_unsetenv (env, "Foo");
     302                 :          1 :   value = g_environ_getenv (env, "foo");
     303                 :            : #ifdef G_OS_WIN32
     304                 :            :   g_assert_null (value);
     305                 :            : #else
     306                 :          1 :   g_assert_cmpstr (value, ==, "bar");
     307                 :            : #endif
     308                 :            : 
     309                 :          1 :   g_strfreev (env);
     310                 :          1 : }
     311                 :            : 
     312                 :            : int
     313                 :          1 : main (int argc, char **argv)
     314                 :            : {
     315                 :          1 :   g_test_init (&argc, &argv, NULL);
     316                 :            : 
     317                 :          1 :   g_test_add_func ("/environ/listenv", test_listenv);
     318                 :          1 :   g_test_add_func ("/environ/getenv", test_getenv);
     319                 :          1 :   g_test_add_func ("/environ/setenv", test_setenv);
     320                 :          1 :   g_test_add_func ("/environ/array", test_environ_array);
     321                 :          1 :   g_test_add_func ("/environ/null", test_environ_null);
     322                 :          1 :   g_test_add_func ("/environ/case", test_environ_case);
     323                 :            : 
     324                 :          1 :   return g_test_run ();
     325                 :            : }

Generated by: LCOV version 1.14