Blame stdlib/tst-environ.c

Packit 6c4009
/* Copyright (C) 1999-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <libc-diag.h>
Packit 6c4009
Packit 6c4009
#define VAR "FOOBAR"
Packit 6c4009
Packit 6c4009
char putenv_val[100] = VAR "=some longer value";
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  const char *valp;
Packit 6c4009
Packit 6c4009
  /* First test: remove entry FOOBAR, whether it exists or not.  */
Packit 6c4009
  unsetenv (VAR);
Packit 6c4009
Packit 6c4009
  /* Now getting the value should fail.  */
Packit 6c4009
  if (getenv (VAR) != NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("There should be no `%s' value\n", VAR);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now add a value, with the replace flag cleared.  */
Packit 6c4009
  if (setenv (VAR, "one", 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("setenv #1 failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Getting this value should now be possible.  */
Packit 6c4009
  valp = getenv (VAR);
Packit 6c4009
  if (valp == NULL || strcmp (valp, "one") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #2 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Try to replace without the replace flag set.  This should fail.  */
Packit 6c4009
  if (setenv (VAR, "two", 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("setenv #2 failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The value shouldn't have changed.  */
Packit 6c4009
  valp = getenv (VAR);
Packit 6c4009
  if (valp == NULL || strcmp (valp, "one") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #3 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now replace the value using putenv.  */
Packit 6c4009
  if (putenv (putenv_val) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("putenv #1 failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The value should have changed now.  */
Packit 6c4009
  valp = getenv (VAR);
Packit 6c4009
  if (valp == NULL || strcmp (valp, "some longer value") != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("getenv #4 failed (is \"%s\")\n", valp);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now one tricky check: changing the variable passed in putenv should
Packit 6c4009
     change the environment.  */
Packit 6c4009
  strcpy (&putenv_val[sizeof VAR], "a short one");
Packit 6c4009
Packit 6c4009
  /* The value should have changed again.  */
Packit 6c4009
  valp = getenv (VAR);
Packit 6c4009
  if (valp == NULL || strcmp (valp, "a short one") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #5 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* It should even be possible to rename the variable.  */
Packit 6c4009
  strcpy (putenv_val, "XYZZY=some other value");
Packit 6c4009
Packit 6c4009
  /* Now a lookup using the old name should fail.  */
Packit 6c4009
  if (getenv (VAR) != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #6 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* But using the new name it should work.  */
Packit 6c4009
  valp = getenv ("XYZZY");
Packit 6c4009
  if (valp == NULL || strcmp (valp, "some other value") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #7 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Create a new variable with the old name.  */
Packit 6c4009
  if (setenv (VAR, "a new value", 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("setenv #3 failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* At this point a getenv call must return the new value.  */
Packit 6c4009
  valp = getenv (VAR);
Packit 6c4009
  if (valp == NULL || strcmp (valp, "a new value") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #8 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Black magic: rename the variable we added using putenv back.  */
Packit 6c4009
  strcpy (putenv_val, VAR "=old name new value");
Packit 6c4009
Packit 6c4009
  /* This is interesting.  We have two variables with the same name.
Packit 6c4009
     Getting a value should return one of them.  */
Packit 6c4009
  valp = getenv (VAR);
Packit 6c4009
  if (valp == NULL
Packit 6c4009
      || (strcmp (valp, "a new value") != 0
Packit 6c4009
	  && strcmp (valp, "old name new value") != 0))
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #9 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* More fun ahead: we are now removing the variable.  This should remove
Packit 6c4009
     both values.  The cast is ok: this call should never put the string
Packit 6c4009
     in the environment and it should never modify it.  */
Packit 6c4009
  putenv ((char *) VAR);
Packit 6c4009
Packit 6c4009
  /* Getting the value should now fail.  */
Packit 6c4009
  if (getenv (VAR) != NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR));
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now a test with an environment variable that's one character long.
Packit 6c4009
     This is to test a special case in the getenv implementation.  */
Packit 6c4009
  strcpy (putenv_val, "X=one character test");
Packit 6c4009
  if (putenv (putenv_val) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("putenv #2 failed: %m\n");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  valp = getenv ("X");
Packit 6c4009
  if (valp == NULL || strcmp (valp, "one character test") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("getenv #11 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
Packit 6c4009
     or if name contains '=' character.  */
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (setenv (NULL, "val", 1) >= 0 || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      puts ("setenv #4 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (setenv ("", "val", 0) >= 0 || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      puts ("setenv #5 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (setenv ("var=val", "val", 1) >= 0 || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      puts ("setenv #6 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* This deliberately tests supplying a null pointer to a function whose
Packit 6c4009
     argument is marked __attribute__ ((nonnull)). */
Packit 6c4009
  DIAG_PUSH_NEEDS_COMMENT;
Packit 6c4009
  DIAG_IGNORE_NEEDS_COMMENT(5, "-Wnonnull");
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (unsetenv (NULL) >= 0 || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      puts ("unsetenv #1 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  DIAG_POP_NEEDS_COMMENT;
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (unsetenv ("") >= 0 || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      puts ("unsetenv #2 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (unsetenv ("x=y") >= 0 || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      puts ("unsetenv #3 failed");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"