root/lib/common/tests/iso8601/crm_time_parse_duration_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. empty_arg
  2. invalid_arg
  3. valid_arg

   1 /*
   2  * Copyright 2024 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 <crm/common/iso8601.h>
  15 #include "crmcommon_private.h"
  16 
  17 static void
  18 empty_arg(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  19 {
  20     assert_null(crm_time_parse_duration(NULL));
  21     assert_null(crm_time_parse_duration(""));
  22 }
  23 
  24 static void
  25 invalid_arg(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  26 {
  27     // Valid except doesn't start with P
  28     assert_null(crm_time_parse_duration("X3Y6M4DT12H30M5S"));
  29 
  30     // Illegal character after P
  31     assert_null(crm_time_parse_duration("P"));
  32     assert_null(crm_time_parse_duration("P 3Y6M4DT12H30M5S"));
  33     assert_null(crm_time_parse_duration("PX3Y6M4DT12H30M5S"));
  34 
  35     // Integer overflow
  36     assert_null(crm_time_parse_duration("P2147483648Y6M4DT12H30M5S"));
  37     assert_null(crm_time_parse_duration("P3Y2147483648M4DT12H30M5S"));
  38     assert_null(crm_time_parse_duration("P3Y6M2147483648DT12H30M5S"));
  39     assert_null(crm_time_parse_duration("P3Y6M4DT2147483648H30M5S"));
  40     assert_null(crm_time_parse_duration("P3Y6M4DT12H2147483648M5S"));
  41     assert_null(crm_time_parse_duration("P3Y6M4DT12H30MP2147483648S"));
  42 
  43     // Missing or invalid units
  44     assert_null(crm_time_parse_duration("P3Y6M4DT12H30M5"));
  45     assert_null(crm_time_parse_duration("P3Y6M4DT12H30M5X"));
  46     assert_null(crm_time_parse_duration("P3X6M4DT12H30M5S"));
  47     assert_null(crm_time_parse_duration("PT"));
  48     assert_null(crm_time_parse_duration("P/"));
  49 
  50 #if 0
  51     // @TODO The current implementation treats these as valid
  52 
  53     // Units out of order
  54     assert_null(crm_time_parse_duration("P6M3Y4DT12H30M5S"));
  55     assert_null(crm_time_parse_duration("P6M3DT12HY430M5S"));
  56 
  57     // Same unit specified multiple times
  58     assert_null(crm_time_parse_duration("P6Y4M3D1MT12H30M5S"));
  59 
  60     // Weeks mixed with other units
  61     assert_null(crm_time_parse_duration("P6Y4M3W3D1MT12H30M5S"));
  62     assert_null(crm_time_parse_duration("P3WT12H30M5S"));
  63 #endif
  64 }
  65 
  66 static void
  67 valid_arg(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69     // @TODO Check result value
  70     assert_non_null(crm_time_parse_duration("P3Y6M4DT12H30M5S"));
  71     assert_non_null(crm_time_parse_duration("P3Y6M4DT12H30M"));
  72     assert_non_null(crm_time_parse_duration("P3Y6M4D"));
  73     assert_non_null(crm_time_parse_duration("P1M"));  // 1 month
  74     assert_non_null(crm_time_parse_duration("PT1M")); // 1 minute
  75     assert_non_null(crm_time_parse_duration("P7W"));
  76 
  77 #if 0
  78     // @TODO Current implementation can't handle these cases
  79 
  80     // Fractional value for last unit
  81     assert_non_null(crm_time_parse_duration("P3Y6M4DT12H30.5M"));
  82     assert_non_null(crm_time_parse_duration("P3Y6M4DT12H30,5M"));
  83 
  84     // P<YYYY>-<MM>-<DD>T<hh>:<mm>:<ss> format
  85     assert_non_null(crm_time_parse_duration("P0003-02-01T11:10:09");
  86 #endif
  87 }
  88 
  89 PCMK__UNIT_TEST(NULL, NULL,
  90                 cmocka_unit_test(empty_arg),
  91                 cmocka_unit_test(invalid_arg),
  92                 cmocka_unit_test(valid_arg));

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