|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 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) |
| 19 | +{ |
| 20 | + assert_null(pcmk__time_parse_duration(NULL)); |
| 21 | + assert_null(pcmk__time_parse_duration("")); |
| 22 | +} |
| 23 | + |
| 24 | +static void |
| 25 | +invalid_arg(void **state) |
| 26 | +{ |
| 27 | + // Valid except doesn't start with P |
| 28 | + assert_null(pcmk__time_parse_duration("X3Y6M4DT12H30M5S")); |
| 29 | + |
| 30 | + // Illegal character after P |
| 31 | + assert_null(pcmk__time_parse_duration("P")); |
| 32 | + assert_null(pcmk__time_parse_duration("P 3Y6M4DT12H30M5S")); |
| 33 | + assert_null(pcmk__time_parse_duration("PX3Y6M4DT12H30M5S")); |
| 34 | + |
| 35 | + // Missing or invalid units |
| 36 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT12H30M5")); |
| 37 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT12H30M5X")); |
| 38 | + assert_null(pcmk__time_parse_duration("P3X6M4DT12H30M5S")); |
| 39 | + assert_null(pcmk__time_parse_duration("PT")); |
| 40 | + assert_null(pcmk__time_parse_duration("P/")); |
| 41 | + |
| 42 | +#if 0 |
| 43 | + // @TODO The current implementation treats these as valid |
| 44 | + |
| 45 | + // Units out of order |
| 46 | + assert_null(pcmk__time_parse_duration("P6M3Y4DT12H30M5S")); |
| 47 | + assert_null(pcmk__time_parse_duration("P6M3DT12HY430M5S")); |
| 48 | + |
| 49 | + // Same unit specified multiple times |
| 50 | + assert_null(pcmk__time_parse_duration("P6Y4M3D1MT12H30M5S")); |
| 51 | + |
| 52 | + // Weeks mixed with other units |
| 53 | + assert_null(pcmk__time_parse_duration("P6Y4M3W3D1MT12H30M5S")); |
| 54 | + assert_null(pcmk__time_parse_duration("P3WT12H30M5S")); |
| 55 | +#endif |
| 56 | +} |
| 57 | + |
| 58 | +static void |
| 59 | +overflow(void **state) |
| 60 | +{ |
| 61 | + // Too large |
| 62 | + assert_null(pcmk__time_parse_duration("P2147483648Y6M4DT12H30M5S")); |
| 63 | + assert_null(pcmk__time_parse_duration("P3Y2147483648M4DT12H30M5S")); |
| 64 | + assert_null(pcmk__time_parse_duration("P3Y6M2147483648DT12H30M5S")); |
| 65 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT2147483648H30M5S")); |
| 66 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT12H2147483648M5S")); |
| 67 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT12H30MP2147483648S")); |
| 68 | + |
| 69 | + // Too small |
| 70 | + assert_null(pcmk__time_parse_duration("P-2147483648Y6M4DT12H30M5S")); |
| 71 | + assert_null(pcmk__time_parse_duration("P3Y-2147483648M4DT12H30M5S")); |
| 72 | + assert_null(pcmk__time_parse_duration("P3Y6M-2147483648DT12H30M5S")); |
| 73 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT-2147483648H30M5S")); |
| 74 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT12H-2147483648M5S")); |
| 75 | + assert_null(pcmk__time_parse_duration("P3Y6M4DT12H30MP-2147483648S")); |
| 76 | +} |
| 77 | + |
| 78 | +static void |
| 79 | +valid_arg(void **state) |
| 80 | +{ |
| 81 | + // @TODO Check result value |
| 82 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4DT12H30M5S")); |
| 83 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4DT12H30M-5S")); |
| 84 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4DT12H-30M5S")); |
| 85 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4DT-12H30M5S")); |
| 86 | + assert_non_null(pcmk__time_parse_duration("P3Y6M-4DT12H30M5S")); |
| 87 | + assert_non_null(pcmk__time_parse_duration("P3Y-6M4DT12H30M5S")); |
| 88 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4DT12H30M")); |
| 89 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4D")); |
| 90 | + assert_non_null(pcmk__time_parse_duration("P1M")); // 1 month |
| 91 | + assert_non_null(pcmk__time_parse_duration("PT1M")); // 1 minute |
| 92 | + assert_non_null(pcmk__time_parse_duration("P7W")); |
| 93 | + |
| 94 | +#if 0 |
| 95 | + // @TODO Current implementation can't handle these cases |
| 96 | + |
| 97 | + // Fractional value for last unit |
| 98 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4DT12H30.5M")); |
| 99 | + assert_non_null(pcmk__time_parse_duration("P3Y6M4DT12H30,5M")); |
| 100 | + |
| 101 | + // P<YYYY>-<MM>-<DD>T<hh>:<mm>:<ss> format |
| 102 | + assert_non_null(pcmk__time_parse_duration("P0003-02-01T11:10:09"); |
| 103 | +#endif |
| 104 | +} |
| 105 | + |
| 106 | +PCMK__UNIT_TEST(NULL, NULL, |
| 107 | + cmocka_unit_test(empty_arg), |
| 108 | + cmocka_unit_test(invalid_arg), |
| 109 | + cmocka_unit_test(overflow), |
| 110 | + cmocka_unit_test(valid_arg)); |
0 commit comments