(root)/
glib-2.79.0/
glib/
tests/
environment.c
       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  test_listenv (void)
      24  {
      25    GHashTable *table;
      26    gchar **list;
      27    gint i;
      28  
      29    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    table = g_hash_table_new_full (g_str_hash, g_str_equal,
      34                                   g_free, g_free);
      35  
      36    list = g_get_environ ();
      37    for (i = 0; list[i]; i++)
      38      {
      39        gchar **parts;
      40  
      41        parts = g_strsplit (list[i], "=", 2);
      42        g_assert_null (g_hash_table_lookup (table, parts[0]));
      43        if (g_strcmp0 (parts[0], ""))
      44          g_hash_table_insert (table, parts[0], parts[1]);
      45        g_free (parts);
      46      }
      47    g_strfreev (list);
      48  
      49    g_assert_cmpint (g_hash_table_size (table), >, 0);
      50  
      51    list = g_listenv ();
      52    for (i = 0; list[i]; i++)
      53      {
      54        const gchar *expected;
      55        const gchar *value;
      56  
      57        expected = g_hash_table_lookup (table, list[i]);
      58        value = g_getenv (list[i]);
      59        g_assert_cmpstr (value, ==, expected);
      60        g_hash_table_remove (table, list[i]);
      61      }
      62    g_assert_cmpint (g_hash_table_size (table), ==, 0);
      63    g_hash_table_unref (table);
      64    g_strfreev (list);
      65  }
      66  
      67  static void
      68  test_getenv (void)
      69  {
      70    const gchar *data;
      71    const gchar *variable = "TEST_G_SETENV";
      72    const gchar *value1 = "works";
      73    const gchar *value2 = "again";
      74  
      75    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    g_assert_null (g_getenv (variable));
      80  
      81    /* Check if g_setenv() failed */
      82    g_assert_cmpint (g_setenv (variable, value1, TRUE), !=, 0);
      83  
      84    data = g_getenv (variable);
      85    g_assert_nonnull (data);
      86    g_assert_cmpstr (data, ==, value1);
      87  
      88    g_assert_cmpint (g_setenv (variable, value2, FALSE), !=, 0);
      89  
      90    data = g_getenv (variable);
      91    g_assert_nonnull (data);
      92    g_assert_cmpstr (data, !=, value2);
      93    g_assert_cmpstr (data, ==, value1);
      94  
      95    g_assert_cmpint (g_setenv (variable, value2, TRUE), !=, 0);
      96  
      97    data = g_getenv (variable);
      98    g_assert_nonnull (data);
      99    g_assert_cmpstr (data, !=, value1);
     100    g_assert_cmpstr (data, ==, value2);
     101  
     102    g_unsetenv (variable);
     103    g_assert_null (g_getenv (variable));
     104  
     105    if (g_test_undefined ())
     106      {
     107        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     108                               "*assertion* != NULL*");
     109        g_assert_false (g_setenv (NULL, "baz", TRUE));
     110        g_test_assert_expected_messages ();
     111  
     112        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     113                               "*assertion* != NULL*");
     114        g_assert_false (g_setenv ("foo", NULL, TRUE));
     115        g_test_assert_expected_messages ();
     116  
     117        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     118                               "*assertion* == NULL*");
     119        g_assert_false (g_setenv ("foo=bar", "baz", TRUE));
     120        g_test_assert_expected_messages ();
     121      }
     122  
     123    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    data = g_getenv ("foo=bar");
     128    if (data != NULL)
     129      g_assert_cmpstr (data, ==, "baz");
     130    else
     131      {
     132        data = g_getenv ("foo");
     133        g_assert_cmpstr (data, ==, "bar=baz");
     134      }
     135  
     136    if (g_test_undefined ())
     137      {
     138        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     139                               "*assertion* != NULL*");
     140        g_unsetenv (NULL);
     141        g_test_assert_expected_messages ();
     142  
     143        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     144                               "*assertion* == NULL*");
     145        g_unsetenv ("foo=bar");
     146        g_test_assert_expected_messages ();
     147      }
     148  
     149    g_unsetenv ("foo");
     150    g_assert_null (g_getenv ("foo"));
     151  }
     152  
     153  static void
     154  test_setenv (void)
     155  {
     156    const gchar *var, *value;
     157  
     158    var = "NOSUCHENVVAR";
     159    value = "value1";
     160  
     161    g_assert_null (g_getenv (var));
     162    g_setenv (var, value, FALSE);
     163    g_assert_cmpstr (g_getenv (var), ==, value);
     164    g_assert_true (g_setenv (var, "value2", FALSE));
     165    g_assert_cmpstr (g_getenv (var), ==, value);
     166    g_assert_true (g_setenv (var, "value2", TRUE));
     167    g_assert_cmpstr (g_getenv (var), ==, "value2");
     168    g_unsetenv (var);
     169    g_assert_null (g_getenv (var));
     170  }
     171  
     172  static void
     173  test_environ_array (void)
     174  {
     175    gchar **env;
     176    const gchar *value;
     177  
     178    g_test_summary ("Test getting and setting variables on a local envp array "
     179                    "(rather than the global envp).");
     180  
     181    env = g_new (gchar *, 1);
     182    env[0] = NULL;
     183  
     184    if (g_test_undefined ())
     185      {
     186        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     187                               "*assertion* != NULL*");
     188        g_environ_getenv (env, NULL);
     189        g_test_assert_expected_messages ();
     190      }
     191  
     192    value = g_environ_getenv (env, "foo");
     193    g_assert_null (value);
     194  
     195    if (g_test_undefined ())
     196      {
     197        gchar **undefined_env;
     198  
     199        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     200                               "*assertion* != NULL*");
     201        undefined_env = g_environ_setenv (env, NULL, "bar", TRUE);
     202        g_test_assert_expected_messages ();
     203        g_strfreev (undefined_env);
     204  
     205        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     206                               "*assertion* == NULL*");
     207        undefined_env = g_environ_setenv (env, "foo=fuz", "bar", TRUE);
     208        g_test_assert_expected_messages ();
     209        g_strfreev (undefined_env);
     210  
     211        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     212                               "*assertion* != NULL*");
     213        undefined_env = g_environ_setenv (env, "foo", NULL, TRUE);
     214        g_test_assert_expected_messages ();
     215        g_strfreev (undefined_env);
     216  
     217        g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     218                               "*assertion* != NULL*");
     219        undefined_env = g_environ_unsetenv (env, NULL);
     220        g_test_assert_expected_messages ();
     221        g_strfreev (undefined_env);
     222      }
     223  
     224    env = g_environ_setenv (env, "foo", "bar", TRUE);
     225    value = g_environ_getenv (env, "foo");
     226    g_assert_cmpstr (value, ==, "bar");
     227  
     228    env = g_environ_setenv (env, "foo2", "bar2", FALSE);
     229    value = g_environ_getenv (env, "foo");
     230    g_assert_cmpstr (value, ==, "bar");
     231    value = g_environ_getenv (env, "foo2");
     232    g_assert_cmpstr (value, ==, "bar2");
     233  
     234    env = g_environ_setenv (env, "foo", "x", FALSE);
     235    value = g_environ_getenv (env, "foo");
     236    g_assert_cmpstr (value, ==, "bar");
     237  
     238    env = g_environ_setenv (env, "foo", "x", TRUE);
     239    value = g_environ_getenv (env, "foo");
     240    g_assert_cmpstr (value, ==, "x");
     241  
     242    env = g_environ_unsetenv (env, "foo2");
     243    value = g_environ_getenv (env, "foo2");
     244    g_assert_null (value);
     245  
     246    g_strfreev (env);
     247  }
     248  
     249  static void
     250  test_environ_null (void)
     251  {
     252    gchar **env;
     253    const gchar *value;
     254  
     255    g_test_summary ("Test getting and setting variables on a NULL envp array.");
     256  
     257    env = NULL;
     258  
     259    value = g_environ_getenv (env, "foo");
     260    g_assert_null (value);
     261  
     262    env = g_environ_setenv (NULL, "foo", "bar", TRUE);
     263    g_assert_nonnull (env);
     264    g_strfreev (env);
     265  
     266    env = g_environ_unsetenv (NULL, "foo");
     267    g_assert_null (env);
     268  }
     269  
     270  static void
     271  test_environ_case (void)
     272  {
     273    gchar **env;
     274    const gchar *value;
     275  
     276    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    env = NULL;
     281  
     282    env = g_environ_setenv (env, "foo", "bar", TRUE);
     283    value = g_environ_getenv (env, "foo");
     284    g_assert_cmpstr (value, ==, "bar");
     285  
     286    value = g_environ_getenv (env, "Foo");
     287  #ifdef G_OS_WIN32
     288    g_assert_cmpstr (value, ==, "bar");
     289  #else
     290    g_assert_null (value);
     291  #endif
     292  
     293    env = g_environ_setenv (env, "FOO", "x", TRUE);
     294    value = g_environ_getenv (env, "foo");
     295  #ifdef G_OS_WIN32
     296    g_assert_cmpstr (value, ==, "x");
     297  #else
     298    g_assert_cmpstr (value, ==, "bar");
     299  #endif
     300  
     301    env = g_environ_unsetenv (env, "Foo");
     302    value = g_environ_getenv (env, "foo");
     303  #ifdef G_OS_WIN32
     304    g_assert_null (value);
     305  #else
     306    g_assert_cmpstr (value, ==, "bar");
     307  #endif
     308  
     309    g_strfreev (env);
     310  }
     311  
     312  int
     313  main (int argc, char **argv)
     314  {
     315    g_test_init (&argc, &argv, NULL);
     316  
     317    g_test_add_func ("/environ/listenv", test_listenv);
     318    g_test_add_func ("/environ/getenv", test_getenv);
     319    g_test_add_func ("/environ/setenv", test_setenv);
     320    g_test_add_func ("/environ/array", test_environ_array);
     321    g_test_add_func ("/environ/null", test_environ_null);
     322    g_test_add_func ("/environ/case", test_environ_case);
     323  
     324    return g_test_run ();
     325  }