Andrii Anisov | 6e29ac4 | 2020-08-06 12:42:52 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2002, Uwe Bonnes |
| 4 | * Copyright (c) 2001-2004, Roger Dingledine. |
| 5 | * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. |
| 6 | * Copyright (c) 2007-2016, The Tor Project, Inc. |
| 7 | * Copyright (c) 2020, EPAM Systems Inc. |
| 8 | * |
| 9 | * Unit tests for sscanf() function |
| 10 | */ |
| 11 | |
Andrii Anisov | 6e29ac4 | 2020-08-06 12:42:52 +0300 | [diff] [blame] | 12 | #include <command.h> |
| 13 | #include <log.h> |
| 14 | #include <test/lib.h> |
| 15 | #include <test/test.h> |
| 16 | #include <test/ut.h> |
| 17 | |
| 18 | #define EOF -1 |
| 19 | |
| 20 | /** |
| 21 | * lib_sscanf() - unit test for sscanf() |
| 22 | * @uts: unit test state |
| 23 | * |
| 24 | * Test sscanf() with varied parameters in different combinations passed |
| 25 | * as arguments. |
| 26 | * |
| 27 | * Return: 0 - success |
| 28 | * 1 - failure |
| 29 | */ |
| 30 | static int lib_sscanf(struct unit_test_state *uts) |
| 31 | { |
| 32 | char buffer[100], buffer1[100]; |
| 33 | int result, ret; |
| 34 | static const char pname[] = " Hello World!\n"; |
| 35 | int hour = 21, min = 59, sec = 20; |
| 36 | int number, number_so_far; |
| 37 | unsigned int u1, u2, u3; |
| 38 | char s1[20], s2[10], s3[10], ch; |
| 39 | int r, int1, int2; |
| 40 | long lng1; |
| 41 | |
| 42 | /* check EOF */ |
| 43 | strcpy(buffer, ""); |
| 44 | ret = sscanf(buffer, "%d", &result); |
| 45 | ut_asserteq(ret, EOF); |
| 46 | |
| 47 | /* check %x */ |
| 48 | strcpy(buffer, "0x519"); |
| 49 | ut_asserteq(sscanf(buffer, "%x", &result), 1); |
| 50 | ut_asserteq(result, 0x519); |
| 51 | |
| 52 | strcpy(buffer, "0x51a"); |
| 53 | ut_asserteq(sscanf(buffer, "%x", &result), 1); |
| 54 | ut_asserteq(result, 0x51a); |
| 55 | |
| 56 | strcpy(buffer, "0x51g"); |
| 57 | ut_asserteq(sscanf(buffer, "%x", &result), 1); |
| 58 | ut_asserteq(result, 0x51); |
| 59 | |
| 60 | /* check strings */ |
| 61 | ret = sprintf(buffer, " %s", pname); |
| 62 | ret = sscanf(buffer, "%*c%[^\n]", buffer1); |
| 63 | ut_asserteq(ret, 1); |
| 64 | ut_asserteq(strncmp(pname, buffer1, strlen(buffer1)), 0); |
| 65 | |
| 66 | /* check digits */ |
| 67 | ret = sprintf(buffer, "%d:%d:%d", hour, min, sec); |
| 68 | ret = sscanf(buffer, "%d%n", &number, &number_so_far); |
| 69 | ut_asserteq(ret, 1); |
| 70 | ut_asserteq(number, hour); |
| 71 | ut_asserteq(number_so_far, 2); |
| 72 | |
| 73 | ret = sscanf(buffer + 2, "%*c%n", &number_so_far); |
| 74 | ut_asserteq(ret, 0); |
| 75 | ut_asserteq(number_so_far, 1); |
| 76 | |
| 77 | /* Check %i */ |
| 78 | strcpy(buffer, "123"); |
| 79 | ret = sscanf(buffer, "%i", &result); |
| 80 | ut_asserteq(ret, 1); |
| 81 | ut_asserteq(result, 123); |
| 82 | ret = sscanf(buffer, "%d", &result); |
| 83 | ut_asserteq(ret, 1); |
| 84 | ut_asserteq(result, 123); |
| 85 | |
| 86 | ut_asserteq(0, sscanf("hello world", "hello world")); |
| 87 | ut_asserteq(0, sscanf("hello world", "good bye")); |
| 88 | /* Excess data */ |
| 89 | ut_asserteq(0, sscanf("hello 3", "%u", &u1)); /* have to match the start */ |
| 90 | ut_asserteq(1, sscanf("3 hello", "%u", &u1)); /* but trailing is alright */ |
| 91 | |
| 92 | /* Numbers (ie. %u) */ |
| 93 | ut_asserteq(0, sscanf("hello world 3", "hello worlb %u", &u1)); /* d vs b */ |
| 94 | ut_asserteq(1, sscanf("12345", "%u", &u1)); |
| 95 | ut_asserteq(12345u, u1); |
| 96 | ut_asserteq(1, sscanf("0", "%u", &u1)); |
| 97 | ut_asserteq(0u, u1); |
| 98 | ut_asserteq(1, sscanf("0000", "%u", &u2)); |
| 99 | ut_asserteq(0u, u2); |
| 100 | ut_asserteq(0, sscanf("A", "%u", &u1)); /* bogus number */ |
| 101 | |
| 102 | /* Numbers with size (eg. %2u) */ |
| 103 | ut_asserteq(2, sscanf("123456", "%2u%u", &u1, &u2)); |
| 104 | ut_asserteq(12u, u1); |
| 105 | ut_asserteq(3456u, u2); |
| 106 | ut_asserteq(1, sscanf("123456", "%8u", &u1)); |
| 107 | ut_asserteq(123456u, u1); |
| 108 | ut_asserteq(1, sscanf("123457 ", "%8u", &u1)); |
| 109 | ut_asserteq(123457u, u1); |
| 110 | ut_asserteq(3, sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3)); |
| 111 | ut_asserteq(12u, u1); |
| 112 | ut_asserteq(3u, u2); |
| 113 | ut_asserteq(456u, u3); |
| 114 | ut_asserteq(3, sscanf("67:8:099", "%2u:%2u:%3u", &u1, &u2, &u3)); /* 0s */ |
| 115 | ut_asserteq(67u, u1); |
| 116 | ut_asserteq(8u, u2); |
| 117 | ut_asserteq(99u, u3); |
| 118 | /* Arbitrary amounts of 0-padding are okay */ |
| 119 | ut_asserteq(3, sscanf("12:03:000000000000000099", "%2u:%2u:%u", &u1, &u2, &u3)); |
| 120 | ut_asserteq(12u, u1); |
| 121 | ut_asserteq(3u, u2); |
| 122 | ut_asserteq(99u, u3); |
| 123 | |
| 124 | /* Hex (ie. %x) */ |
| 125 | ut_asserteq(3, sscanf("1234 02aBcdEf ff", "%x %x %x", &u1, &u2, &u3)); |
| 126 | ut_asserteq(0x1234, u1); |
| 127 | ut_asserteq(0x2ABCDEF, u2); |
| 128 | ut_asserteq(0xFF, u3); |
| 129 | /* Width works on %x */ |
| 130 | ut_asserteq(3, sscanf("f00dcafe444", "%4x%4x%u", &u1, &u2, &u3)); |
| 131 | ut_asserteq(0xf00d, u1); |
| 132 | ut_asserteq(0xcafe, u2); |
| 133 | ut_asserteq(444, u3); |
| 134 | |
| 135 | /* Literal '%' (ie. '%%') */ |
| 136 | ut_asserteq(1, sscanf("99% fresh", "%3u%% fresh", &u1)); |
| 137 | ut_asserteq(99, u1); |
| 138 | ut_asserteq(0, sscanf("99 fresh", "%% %3u %s", &u1, s1)); |
| 139 | ut_asserteq(1, sscanf("99 fresh", "%3u%% %s", &u1, s1)); |
| 140 | ut_asserteq(2, sscanf("99 fresh", "%3u %5s %%", &u1, s1)); |
| 141 | ut_asserteq(99, u1); |
| 142 | ut_asserteq_str(s1, "fresh"); |
| 143 | ut_asserteq(1, sscanf("% boo", "%% %3s", s1)); |
| 144 | ut_asserteq_str("boo", s1); |
| 145 | |
| 146 | /* Strings (ie. %s) */ |
| 147 | ut_asserteq(2, sscanf("hello", "%3s%7s", s1, s2)); |
| 148 | ut_asserteq_str(s1, "hel"); |
| 149 | ut_asserteq_str(s2, "lo"); |
| 150 | ut_asserteq(2, sscanf("WD40", "%2s%u", s3, &u1)); /* %s%u */ |
| 151 | ut_asserteq_str(s3, "WD"); |
| 152 | ut_asserteq(40, u1); |
| 153 | ut_asserteq(2, sscanf("WD40", "%3s%u", s3, &u1)); /* %s%u */ |
| 154 | ut_asserteq_str(s3, "WD4"); |
| 155 | ut_asserteq(0, u1); |
| 156 | ut_asserteq(2, sscanf("76trombones", "%6u%9s", &u1, s1)); /* %u%s */ |
| 157 | ut_asserteq(76, u1); |
| 158 | ut_asserteq_str(s1, "trombones"); |
| 159 | |
| 160 | ut_asserteq(3, sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch)); |
| 161 | ut_asserteq(4, sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch)); |
| 162 | ut_asserteq(' ', ch); |
| 163 | |
| 164 | r = sscanf("12345 -67890 -1", "%d %ld %d", &int1, &lng1, &int2); |
| 165 | ut_asserteq(r, 3); |
| 166 | ut_asserteq(int1, 12345); |
| 167 | ut_asserteq(lng1, -67890); |
| 168 | ut_asserteq(int2, -1); |
| 169 | |
| 170 | return 0; |
| 171 | } |
Andrii Anisov | 6e29ac4 | 2020-08-06 12:42:52 +0300 | [diff] [blame] | 172 | LIB_TEST(lib_sscanf, 0); |