root/lib/common/tests/io/pcmk__get_tmpdir_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. getenv_returns_invalid
  2. getenv_returns_valid

   1 /*
   2  * Copyright 2021-2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <crm/common/unittest_internal.h>
  13 
  14 #include "mock_private.h"
  15 
  16 static void
  17 getenv_returns_invalid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  18 {
  19     const char *result;
  20 
  21     pcmk__mock_getenv = true;
  22 
  23     expect_string(__wrap_getenv, name, "TMPDIR");
  24     will_return(__wrap_getenv, NULL);                   // getenv("TMPDIR") return value
  25     result = pcmk__get_tmpdir();
  26     assert_string_equal(result, "/tmp");
  27 
  28     expect_string(__wrap_getenv, name, "TMPDIR");
  29     will_return(__wrap_getenv, "");                     // getenv("TMPDIR") return value
  30     result = pcmk__get_tmpdir();
  31     assert_string_equal(result, "/tmp");
  32 
  33     expect_string(__wrap_getenv, name, "TMPDIR");
  34     will_return(__wrap_getenv, "subpath");              // getenv("TMPDIR") return value
  35     result = pcmk__get_tmpdir();
  36     assert_string_equal(result, "/tmp");
  37 
  38     pcmk__mock_getenv = false;
  39 }
  40 
  41 static void
  42 getenv_returns_valid(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44     const char *result;
  45 
  46     pcmk__mock_getenv = true;
  47 
  48     expect_string(__wrap_getenv, name, "TMPDIR");
  49     will_return(__wrap_getenv, "/var/tmp");             // getenv("TMPDIR") return value
  50     result = pcmk__get_tmpdir();
  51     assert_string_equal(result, "/var/tmp");
  52 
  53     expect_string(__wrap_getenv, name, "TMPDIR");
  54     will_return(__wrap_getenv, "/");                    // getenv("TMPDIR") return value
  55     result = pcmk__get_tmpdir();
  56     assert_string_equal(result, "/");
  57 
  58     expect_string(__wrap_getenv, name, "TMPDIR");
  59     will_return(__wrap_getenv, "/tmp/abcd.1234");       // getenv("TMPDIR") return value
  60     result = pcmk__get_tmpdir();
  61     assert_string_equal(result, "/tmp/abcd.1234");
  62 
  63     pcmk__mock_getenv = false;
  64 }
  65 
  66 PCMK__UNIT_TEST(NULL, NULL,
  67                 cmocka_unit_test(getenv_returns_invalid),
  68                 cmocka_unit_test(getenv_returns_valid))

/* [previous][next][first][last][top][bottom][index][help] */