root/lib/common/tests/strings/pcmk__str_update_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. update_null
  2. update_same
  3. update_different
  4. strdup_fails

   1 /*
   2  * Copyright 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 update_null(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  18     char *str = NULL;
  19 
  20     // These just make sure they don't crash
  21     pcmk__str_update(NULL, NULL);
  22     pcmk__str_update(NULL, "value");
  23 
  24     // Update an already NULL string to NULL
  25     pcmk__str_update(&str, NULL);
  26     assert_null(str);
  27 
  28     // Update an already allocated string to NULL
  29     str = strdup("hello");
  30     pcmk__str_update(&str, NULL);
  31     assert_null(str);
  32 }
  33 
  34 static void
  35 update_same(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  36     char *str = NULL;
  37     char *saved = NULL;
  38 
  39     str = strdup("hello");
  40     saved = str;
  41     pcmk__str_update(&str, "hello");
  42     assert_ptr_equal(saved, str); // No free and reallocation
  43     free(str);
  44 }
  45 
  46 static void
  47 update_different(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  48     char *str = NULL;
  49 
  50     str = strdup("hello");
  51     pcmk__str_update(&str, "world");
  52     assert_string_equal(str, "world");
  53     free(str);
  54 }
  55 
  56 static void
  57 strdup_fails(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  58     char *str = NULL;
  59 
  60     str = strdup("hello");
  61 
  62     pcmk__assert_asserts(
  63         {
  64             pcmk__mock_strdup = true;   // strdup() will return NULL
  65             expect_string(__wrap_strdup, s, "world");
  66             pcmk__str_update(&str, "world");
  67             pcmk__mock_strdup = false;  // Use the real strdup()
  68         }
  69     );
  70 
  71     free(str);
  72 }
  73 
  74 PCMK__UNIT_TEST(NULL, NULL,
  75                 cmocka_unit_test(update_null),
  76                 cmocka_unit_test(update_same),
  77                 cmocka_unit_test(update_different),
  78                 cmocka_unit_test(strdup_fails))

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