From 4335badd0e85341a2515c00e5b73d6a921ecdd1b Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Sat, 7 Oct 2017 04:39:14 +0200 Subject: utils: add osmo_is_hexstr(), add unit test Will be used by OsmoHLR to validate VTY and CTRL input. Change-Id: Idf75946eb0a84e145adad13fc7c78bb7a267aa0a --- tests/utils/utils_test.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ tests/utils/utils_test.ok | 31 +++++++++++++++++++++++ 2 files changed, 94 insertions(+) (limited to 'tests/utils') diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c index cad162d9..4a4b121f 100644 --- a/tests/utils/utils_test.c +++ b/tests/utils/utils_test.c @@ -219,6 +219,68 @@ static void test_idtag_parsing(void) OSMO_ASSERT(!TLVP_PRESENT(&tvp, 0x25)); } +static struct { + const char *str; + int min_digits; + int max_digits; + bool require_even; + bool expect_ok; +} test_hexstrs[] = { + { NULL, 0, 10, false, true }, + { NULL, 1, 10, false, false }, + { "", 0, 10, false, true }, + { "", 1, 10, false, false }, + { " ", 0, 10, false, false }, + { "1", 0, 10, false, true }, + { "1", 1, 10, false, true }, + { "1", 1, 10, true, false }, + { "1", 2, 10, false, false }, + { "123", 1, 10, false, true }, + { "123", 1, 10, true, false }, + { "123", 4, 10, false, false }, + { "1234", 4, 10, true, true }, + { "12345", 4, 10, true, false }, + { "123456", 4, 10, true, true }, + { "1234567", 4, 10, true, false }, + { "12345678", 4, 10, true, true }, + { "123456789", 4, 10, true, false }, + { "123456789a", 4, 10, true, true }, + { "123456789ab", 4, 10, true, false }, + { "123456789abc", 4, 10, true, false }, + { "123456789ab", 4, 10, false, false }, + { "123456789abc", 4, 10, false, false }, + { "0123456789abcdefABCDEF", 0, 100, false, true }, + { "0123456789 abcdef ABCDEF", 0, 100, false, false }, + { "foobar", 0, 100, false, false }, + { "BeadedBeeAced1EbbedDefacedFacade", 32, 32, true, true }, + { "C01ffedC1cadaeAc1d1f1edAcac1aB0a", 32, 32, false, true }, + { "DeafBeddedBabeAcceededFadedDecaff", 32, 32, false, false }, +}; + +bool test_is_hexstr() +{ + int i; + bool pass = true; + bool ok = true; + printf("\n----- %s\n", __func__); + + for (i = 0; i < ARRAY_SIZE(test_hexstrs); i++) { + ok = osmo_is_hexstr(test_hexstrs[i].str, + test_hexstrs[i].min_digits, + test_hexstrs[i].max_digits, + test_hexstrs[i].require_even); + pass = pass && (ok == test_hexstrs[i].expect_ok); + printf("%2d: %s str='%s' min=%d max=%d even=%d expect=%s\n", + i, test_hexstrs[i].expect_ok == ok ? "pass" : "FAIL", + test_hexstrs[i].str, + test_hexstrs[i].min_digits, + test_hexstrs[i].max_digits, + test_hexstrs[i].require_even, + test_hexstrs[i].expect_ok ? "valid" : "invalid"); + } + return pass; +} + int main(int argc, char **argv) { static const struct log_info log_info = {}; @@ -227,5 +289,6 @@ int main(int argc, char **argv) hexdump_test(); hexparse_test(); test_idtag_parsing(); + test_is_hexstr(); return 0; } diff --git a/tests/utils/utils_test.ok b/tests/utils/utils_test.ok index e9be0187..45156f7f 100644 --- a/tests/utils/utils_test.ok +++ b/tests/utils/utils_test.ok @@ -26,3 +26,34 @@ Hexparse with uneven amount of digits rc = -1 Hexparse with invalid char rc = -1 + +----- test_is_hexstr + 0: pass str='(null)' min=0 max=10 even=0 expect=valid + 1: pass str='(null)' min=1 max=10 even=0 expect=invalid + 2: pass str='' min=0 max=10 even=0 expect=valid + 3: pass str='' min=1 max=10 even=0 expect=invalid + 4: pass str=' ' min=0 max=10 even=0 expect=invalid + 5: pass str='1' min=0 max=10 even=0 expect=valid + 6: pass str='1' min=1 max=10 even=0 expect=valid + 7: pass str='1' min=1 max=10 even=1 expect=invalid + 8: pass str='1' min=2 max=10 even=0 expect=invalid + 9: pass str='123' min=1 max=10 even=0 expect=valid +10: pass str='123' min=1 max=10 even=1 expect=invalid +11: pass str='123' min=4 max=10 even=0 expect=invalid +12: pass str='1234' min=4 max=10 even=1 expect=valid +13: pass str='12345' min=4 max=10 even=1 expect=invalid +14: pass str='123456' min=4 max=10 even=1 expect=valid +15: pass str='1234567' min=4 max=10 even=1 expect=invalid +16: pass str='12345678' min=4 max=10 even=1 expect=valid +17: pass str='123456789' min=4 max=10 even=1 expect=invalid +18: pass str='123456789a' min=4 max=10 even=1 expect=valid +19: pass str='123456789ab' min=4 max=10 even=1 expect=invalid +20: pass str='123456789abc' min=4 max=10 even=1 expect=invalid +21: pass str='123456789ab' min=4 max=10 even=0 expect=invalid +22: pass str='123456789abc' min=4 max=10 even=0 expect=invalid +23: pass str='0123456789abcdefABCDEF' min=0 max=100 even=0 expect=valid +24: pass str='0123456789 abcdef ABCDEF' min=0 max=100 even=0 expect=invalid +25: pass str='foobar' min=0 max=100 even=0 expect=invalid +26: pass str='BeadedBeeAced1EbbedDefacedFacade' min=32 max=32 even=1 expect=valid +27: pass str='C01ffedC1cadaeAc1d1f1edAcac1aB0a' min=32 max=32 even=0 expect=valid +28: pass str='DeafBeddedBabeAcceededFadedDecaff' min=32 max=32 even=0 expect=invalid -- cgit v1.2.3