diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 18:13:27 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 18:13:27 +0100 |
| commit | 78c3cebc389a2b0d426d74900a65b8656edb426d (patch) | |
| tree | 122003fecc6b53c2dde6393896a852e3689900ff /tests | |
| parent | 6837c5ac80e3624c6dc3a82d03e50d68b2f5a989 (diff) | |
Third run of making variable and function names more readable, integer scans and ip parsers
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/t_ip.c | 54 | ||||
| -rw-r--r-- | tests/t_scan.c | 124 | ||||
| -rw-r--r-- | tests/t_urlencoded.c | 29 |
3 files changed, 114 insertions, 93 deletions
diff --git a/tests/t_ip.c b/tests/t_ip.c index 2fe3a72..8d71679 100644 --- a/tests/t_ip.c +++ b/tests/t_ip.c | |||
| @@ -95,27 +95,27 @@ static void case_scan_forms(void) { | |||
| 95 | char ip[16]; | 95 | char ip[16]; |
| 96 | 96 | ||
| 97 | test_case("2.2 form 1, every group spelled out"); | 97 | test_case("2.2 form 1, every group spelled out"); |
| 98 | CHECK_INT(ip_scan("1111:2222:3333:4444:5555:6666:7777:8888", ip), 39); | 98 | CHECK_INT(ip_parse("1111:2222:3333:4444:5555:6666:7777:8888", ip), 39); |
| 99 | CHECK_MEM(ip, "\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x66\x66\x77\x77\x88\x88", 16); | 99 | CHECK_MEM(ip, "\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x66\x66\x77\x77\x88\x88", 16); |
| 100 | 100 | ||
| 101 | test_case("2.2 form 2, zero groups abbreviated"); | 101 | test_case("2.2 form 2, zero groups abbreviated"); |
| 102 | CHECK_INT(ip_scan("2001:db8::1", ip), 11); | 102 | CHECK_INT(ip_parse("2001:db8::1", ip), 11); |
| 103 | CHECK_MEM(ip, "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16); | 103 | CHECK_MEM(ip, "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16); |
| 104 | CHECK_INT(ip_scan("::1", ip), 3); | 104 | CHECK_INT(ip_parse("::1", ip), 3); |
| 105 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16); | 105 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16); |
| 106 | CHECK_INT(ip_scan("::", ip), 2); | 106 | CHECK_INT(ip_parse("::", ip), 2); |
| 107 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16); | 107 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16); |
| 108 | CHECK_INT(ip_scan("1::", ip), 3); | 108 | CHECK_INT(ip_parse("1::", ip), 3); |
| 109 | CHECK_MEM(ip, "\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16); | 109 | CHECK_MEM(ip, "\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16); |
| 110 | 110 | ||
| 111 | test_case("2.2 form 3, a dotted quad in the last two groups"); | 111 | test_case("2.2 form 3, a dotted quad in the last two groups"); |
| 112 | CHECK_INT(ip_scan("::ffff:1.2.3.4", ip), 14); | 112 | CHECK_INT(ip_parse("::ffff:1.2.3.4", ip), 14); |
| 113 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x01\x02\x03\x04", 16); | 113 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x01\x02\x03\x04", 16); |
| 114 | CHECK_INT(ip_scan("1:2:3:4:5:6:1.2.3.4", ip), 19); | 114 | CHECK_INT(ip_parse("1:2:3:4:5:6:1.2.3.4", ip), 19); |
| 115 | CHECK_MEM(ip, "\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x01\x02\x03\x04", 16); | 115 | CHECK_MEM(ip, "\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x01\x02\x03\x04", 16); |
| 116 | 116 | ||
| 117 | test_case("upper case input is accepted"); | 117 | test_case("upper case input is accepted"); |
| 118 | CHECK_INT(ip_scan("2001:DB8::1", ip), 11); | 118 | CHECK_INT(ip_parse("2001:DB8::1", ip), 11); |
| 119 | CHECK_MEM(ip, "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16); | 119 | CHECK_MEM(ip, "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16); |
| 120 | } | 120 | } |
| 121 | 121 | ||
| @@ -125,7 +125,7 @@ static void case_scan_accepts_bare_ipv4(void) { | |||
| 125 | /* Every address opentracker holds is sixteen bytes, so an operator writing | 125 | /* Every address opentracker holds is sixteen bytes, so an operator writing |
| 126 | a plain IPv4 address in the config has to land as the v4 mapped form */ | 126 | a plain IPv4 address in the config has to land as the v4 mapped form */ |
| 127 | test_case("a bare IPv4 address is stored v4 mapped"); | 127 | test_case("a bare IPv4 address is stored v4 mapped"); |
| 128 | CHECK_INT(ip_scan("10.0.0.1", ip), 8); | 128 | CHECK_INT(ip_parse("10.0.0.1", ip), 8); |
| 129 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x0a\x00\x00\x01", 16); | 129 | CHECK_MEM(ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x0a\x00\x00\x01", 16); |
| 130 | CHECK(ip_is_v4mapped(ip)); | 130 | CHECK(ip_is_v4mapped(ip)); |
| 131 | } | 131 | } |
| @@ -136,39 +136,39 @@ static void case_scan_reports_length(void) { | |||
| 136 | /* The config parser continues from where the address ended, which is how | 136 | /* The config parser continues from where the address ended, which is how |
| 137 | "address:port" and "address/bits" are read */ | 137 | "address:port" and "address/bits" are read */ |
| 138 | test_case("scanning stops at the character after the address"); | 138 | test_case("scanning stops at the character after the address"); |
| 139 | CHECK_INT(ip_scan("10.0.0.1:6969", ip), 8); | 139 | CHECK_INT(ip_parse("10.0.0.1:6969", ip), 8); |
| 140 | CHECK_INT(ip_scan("2001:db8::1/64", ip), 11); | 140 | CHECK_INT(ip_parse("2001:db8::1/64", ip), 11); |
| 141 | CHECK_INT(ip_scan("::1]", ip), 3); | 141 | CHECK_INT(ip_parse("::1]", ip), 3); |
| 142 | CHECK_INT(ip_scan("1:2:3:4:5:6:7:8 trailing", ip), 15); | 142 | CHECK_INT(ip_parse("1:2:3:4:5:6:7:8 trailing", ip), 15); |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | static void case_scan_rejects(void) { | 145 | static void case_scan_rejects(void) { |
| 146 | char ip[16]; | 146 | char ip[16]; |
| 147 | 147 | ||
| 148 | test_case("an empty or junk string is refused"); | 148 | test_case("an empty or junk string is refused"); |
| 149 | CHECK_INT(ip_scan("", ip), 0); | 149 | CHECK_INT(ip_parse("", ip), 0); |
| 150 | CHECK_INT(ip_scan("x", ip), 0); | 150 | CHECK_INT(ip_parse("x", ip), 0); |
| 151 | CHECK_INT(ip_scan(":", ip), 0); | 151 | CHECK_INT(ip_parse(":", ip), 0); |
| 152 | CHECK_INT(ip_scan(":1:2", ip), 0); | 152 | CHECK_INT(ip_parse(":1:2", ip), 0); |
| 153 | 153 | ||
| 154 | test_case("too few groups without :: are refused"); | 154 | test_case("too few groups without :: are refused"); |
| 155 | CHECK_INT(ip_scan("1:2:3:4:5:6:7", ip), 0); | 155 | CHECK_INT(ip_parse("1:2:3:4:5:6:7", ip), 0); |
| 156 | CHECK_INT(ip_scan("2001:db8:1", ip), 0); | 156 | CHECK_INT(ip_parse("2001:db8:1", ip), 0); |
| 157 | 157 | ||
| 158 | /* libowfat accepted a prefix of each of these and carried on. Taking only | 158 | /* libowfat accepted a prefix of each of these and carried on. Taking only |
| 159 | part of a malformed address is worse than refusing it, especially where | 159 | part of a malformed address is worse than refusing it, especially where |
| 160 | the text comes from a config file or an X-Forwarded-For header. */ | 160 | the text comes from a config file or an X-Forwarded-For header. */ |
| 161 | test_case("a second :: is refused rather than truncated"); | 161 | test_case("a second :: is refused rather than truncated"); |
| 162 | CHECK_INT(ip_scan("1::2::3", ip), 0); | 162 | CHECK_INT(ip_parse("1::2::3", ip), 0); |
| 163 | CHECK_INT(ip_scan("::1::", ip), 0); | 163 | CHECK_INT(ip_parse("::1::", ip), 0); |
| 164 | CHECK_INT(ip_scan("::::", ip), 0); | 164 | CHECK_INT(ip_parse("::::", ip), 0); |
| 165 | 165 | ||
| 166 | test_case("more than eight groups is refused"); | 166 | test_case("more than eight groups is refused"); |
| 167 | CHECK_INT(ip_scan("1:2:3:4:5:6:7:8:9", ip), 0); | 167 | CHECK_INT(ip_parse("1:2:3:4:5:6:7:8:9", ip), 0); |
| 168 | 168 | ||
| 169 | test_case("a group of more than four digits is refused, not truncated"); | 169 | test_case("a group of more than four digits is refused, not truncated"); |
| 170 | CHECK_INT(ip_scan("12345::1", ip), 0); | 170 | CHECK_INT(ip_parse("12345::1", ip), 0); |
| 171 | CHECK_INT(ip_scan("fffff::1", ip), 0); | 171 | CHECK_INT(ip_parse("fffff::1", ip), 0); |
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | static void case_round_trip(void) { | 174 | static void case_round_trip(void) { |
| @@ -187,7 +187,7 @@ static void case_round_trip(void) { | |||
| 187 | 187 | ||
| 188 | test_case("every formatted address scans back to the same bytes"); | 188 | test_case("every formatted address scans back to the same bytes"); |
| 189 | for (i = 0; samples[i]; ++i) { | 189 | for (i = 0; samples[i]; ++i) { |
| 190 | size_t n = ip_scan(samples[i], ip); | 190 | size_t n = ip_parse(samples[i], ip); |
| 191 | if (n != strlen(samples[i])) { | 191 | if (n != strlen(samples[i])) { |
| 192 | test_failf(__FILE__, __LINE__, "\"%s\" consumed %zu of %zu", samples[i], n, strlen(samples[i])); | 192 | test_failf(__FILE__, __LINE__, "\"%s\" consumed %zu of %zu", samples[i], n, strlen(samples[i])); |
| 193 | return; | 193 | return; |
| @@ -197,7 +197,7 @@ static void case_round_trip(void) { | |||
| 197 | test_failf(__FILE__, __LINE__, "\"%s\" formatted back as \"%s\"", samples[i], text); | 197 | test_failf(__FILE__, __LINE__, "\"%s\" formatted back as \"%s\"", samples[i], text); |
| 198 | return; | 198 | return; |
| 199 | } | 199 | } |
| 200 | if (!ip_scan(text, again) || memcmp(ip, again, 16)) { | 200 | if (!ip_parse(text, again) || memcmp(ip, again, 16)) { |
| 201 | test_failf(__FILE__, __LINE__, "\"%s\" did not survive a second pass", samples[i]); | 201 | test_failf(__FILE__, __LINE__, "\"%s\" did not survive a second pass", samples[i]); |
| 202 | return; | 202 | return; |
| 203 | } | 203 | } |
diff --git a/tests/t_scan.c b/tests/t_scan.c index 89f1590..aab4413 100644 --- a/tests/t_scan.c +++ b/tests/t_scan.c | |||
| @@ -15,13 +15,13 @@ static void case_plain_numbers(void) { | |||
| 15 | uint64_t value; | 15 | uint64_t value; |
| 16 | 16 | ||
| 17 | test_case("a decimal number is read and its length reported"); | 17 | test_case("a decimal number is read and its length reported"); |
| 18 | CHECK_INT(scan_udec("0", &value, 65535), 1); | 18 | CHECK_INT(scan_uint("0", &value, 65535), 1); |
| 19 | CHECK_INT(value, 0); | 19 | CHECK_INT(value, 0); |
| 20 | CHECK_INT(scan_udec("7", &value, 65535), 1); | 20 | CHECK_INT(scan_uint("7", &value, 65535), 1); |
| 21 | CHECK_INT(value, 7); | 21 | CHECK_INT(value, 7); |
| 22 | CHECK_INT(scan_udec("6969", &value, 65535), 4); | 22 | CHECK_INT(scan_uint("6969", &value, 65535), 4); |
| 23 | CHECK_INT(value, 6969); | 23 | CHECK_INT(value, 6969); |
| 24 | CHECK_INT(scan_udec("65535", &value, 65535), 5); | 24 | CHECK_INT(scan_uint("65535", &value, 65535), 5); |
| 25 | CHECK_INT(value, 65535); | 25 | CHECK_INT(value, 65535); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| @@ -29,11 +29,11 @@ static void case_stops_at_non_digit(void) { | |||
| 29 | uint64_t value; | 29 | uint64_t value; |
| 30 | 30 | ||
| 31 | test_case("scanning stops at the first character that is not a digit"); | 31 | test_case("scanning stops at the first character that is not a digit"); |
| 32 | CHECK_INT(scan_udec("6969:rest", &value, 65535), 4); | 32 | CHECK_INT(scan_uint("6969:rest", &value, 65535), 4); |
| 33 | CHECK_INT(value, 6969); | 33 | CHECK_INT(value, 6969); |
| 34 | CHECK_INT(scan_udec("128/8", &value, 65535), 3); | 34 | CHECK_INT(scan_uint("128/8", &value, 65535), 3); |
| 35 | CHECK_INT(value, 128); | 35 | CHECK_INT(value, 128); |
| 36 | CHECK_INT(scan_udec("42\n", &value, 65535), 2); | 36 | CHECK_INT(scan_uint("42\n", &value, 65535), 2); |
| 37 | CHECK_INT(value, 42); | 37 | CHECK_INT(value, 42); |
| 38 | } | 38 | } |
| 39 | 39 | ||
| @@ -41,9 +41,9 @@ static void case_rejects_whitespace(void) { | |||
| 41 | uint64_t value = 0xdead; | 41 | uint64_t value = 0xdead; |
| 42 | 42 | ||
| 43 | test_case("leading whitespace is not skipped"); | 43 | test_case("leading whitespace is not skipped"); |
| 44 | CHECK_INT(scan_udec(" 1", &value, 65535), 0); | 44 | CHECK_INT(scan_uint(" 1", &value, 65535), 0); |
| 45 | CHECK_INT(scan_udec("\t1", &value, 65535), 0); | 45 | CHECK_INT(scan_uint("\t1", &value, 65535), 0); |
| 46 | CHECK_INT(scan_udec("\n1", &value, 65535), 0); | 46 | CHECK_INT(scan_uint("\n1", &value, 65535), 0); |
| 47 | CHECK_INT(value, 0xdead); | 47 | CHECK_INT(value, 0xdead); |
| 48 | } | 48 | } |
| 49 | 49 | ||
| @@ -51,9 +51,9 @@ static void case_rejects_sign(void) { | |||
| 51 | uint64_t value = 0xdead; | 51 | uint64_t value = 0xdead; |
| 52 | 52 | ||
| 53 | test_case("a sign is not accepted"); | 53 | test_case("a sign is not accepted"); |
| 54 | CHECK_INT(scan_udec("+1", &value, 65535), 0); | 54 | CHECK_INT(scan_uint("+1", &value, 65535), 0); |
| 55 | CHECK_INT(scan_udec("-1", &value, 65535), 0); | 55 | CHECK_INT(scan_uint("-1", &value, 65535), 0); |
| 56 | CHECK_INT(scan_udec("-0", &value, 65535), 0); | 56 | CHECK_INT(scan_uint("-0", &value, 65535), 0); |
| 57 | CHECK_INT(value, 0xdead); | 57 | CHECK_INT(value, 0xdead); |
| 58 | } | 58 | } |
| 59 | 59 | ||
| @@ -61,9 +61,9 @@ static void case_rejects_empty_and_junk(void) { | |||
| 61 | uint64_t value = 0xdead; | 61 | uint64_t value = 0xdead; |
| 62 | 62 | ||
| 63 | test_case("a string with no leading digit yields nothing"); | 63 | test_case("a string with no leading digit yields nothing"); |
| 64 | CHECK_INT(scan_udec("", &value, 65535), 0); | 64 | CHECK_INT(scan_uint("", &value, 65535), 0); |
| 65 | CHECK_INT(scan_udec("x", &value, 65535), 0); | 65 | CHECK_INT(scan_uint("x", &value, 65535), 0); |
| 66 | CHECK_INT(scan_udec(":80", &value, 65535), 0); | 66 | CHECK_INT(scan_uint(":80", &value, 65535), 0); |
| 67 | CHECK_INT(value, 0xdead); | 67 | CHECK_INT(value, 0xdead); |
| 68 | } | 68 | } |
| 69 | 69 | ||
| @@ -71,9 +71,9 @@ static void case_no_alternative_bases(void) { | |||
| 71 | uint64_t value; | 71 | uint64_t value; |
| 72 | 72 | ||
| 73 | test_case("hexadecimal and octal notations are read as plain decimal"); | 73 | test_case("hexadecimal and octal notations are read as plain decimal"); |
| 74 | CHECK_INT(scan_udec("0x10", &value, 65535), 1); | 74 | CHECK_INT(scan_uint("0x10", &value, 65535), 1); |
| 75 | CHECK_INT(value, 0); | 75 | CHECK_INT(value, 0); |
| 76 | CHECK_INT(scan_udec("010", &value, 65535), 3); | 76 | CHECK_INT(scan_uint("010", &value, 65535), 3); |
| 77 | CHECK_INT(value, 10); | 77 | CHECK_INT(value, 10); |
| 78 | } | 78 | } |
| 79 | 79 | ||
| @@ -81,13 +81,13 @@ static void case_refuses_out_of_range(void) { | |||
| 81 | uint64_t value = 0xdead; | 81 | uint64_t value = 0xdead; |
| 82 | 82 | ||
| 83 | test_case("a value above max is refused, not truncated"); | 83 | test_case("a value above max is refused, not truncated"); |
| 84 | CHECK_INT(scan_udec("65536", &value, 65535), 0); | 84 | CHECK_INT(scan_uint("65536", &value, 65535), 0); |
| 85 | CHECK_INT(scan_udec("99999", &value, 65535), 0); | 85 | CHECK_INT(scan_uint("99999", &value, 65535), 0); |
| 86 | CHECK_INT(scan_udec("129", &value, 128), 0); | 86 | CHECK_INT(scan_uint("129", &value, 128), 0); |
| 87 | CHECK_INT(value, 0xdead); | 87 | CHECK_INT(value, 0xdead); |
| 88 | 88 | ||
| 89 | test_case("the boundary itself is accepted"); | 89 | test_case("the boundary itself is accepted"); |
| 90 | CHECK_INT(scan_udec("128", &value, 128), 3); | 90 | CHECK_INT(scan_uint("128", &value, 128), 3); |
| 91 | CHECK_INT(value, 128); | 91 | CHECK_INT(value, 128); |
| 92 | } | 92 | } |
| 93 | 93 | ||
| @@ -95,21 +95,21 @@ static void case_sixty_four_bit_range(void) { | |||
| 95 | uint64_t value; | 95 | uint64_t value; |
| 96 | 96 | ||
| 97 | test_case("the full unsigned 64 bit range is available"); | 97 | test_case("the full unsigned 64 bit range is available"); |
| 98 | CHECK_INT(scan_udec("18446744073709551615", &value, UINT64_MAX), 20); | 98 | CHECK_INT(scan_uint("18446744073709551615", &value, UINT64_MAX), 20); |
| 99 | CHECK(value == UINT64_MAX); | 99 | CHECK(value == UINT64_MAX); |
| 100 | 100 | ||
| 101 | test_case("a number too large for 64 bits is refused"); | 101 | test_case("a number too large for 64 bits is refused"); |
| 102 | CHECK_INT(scan_udec("18446744073709551616", &value, UINT64_MAX), 0); | 102 | CHECK_INT(scan_uint("18446744073709551616", &value, UINT64_MAX), 0); |
| 103 | CHECK_INT(scan_udec("99999999999999999999999999", &value, UINT64_MAX), 0); | 103 | CHECK_INT(scan_uint("99999999999999999999999999", &value, UINT64_MAX), 0); |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | static void case_leading_zeros_allowed(void) { | 106 | static void case_leading_zeros_allowed(void) { |
| 107 | uint64_t value; | 107 | uint64_t value; |
| 108 | 108 | ||
| 109 | test_case("leading zeros are accepted outside bencode"); | 109 | test_case("leading zeros are accepted outside bencode"); |
| 110 | CHECK_INT(scan_udec("007", &value, 65535), 3); | 110 | CHECK_INT(scan_uint("007", &value, 65535), 3); |
| 111 | CHECK_INT(value, 7); | 111 | CHECK_INT(value, 7); |
| 112 | CHECK_INT(scan_udec("0000", &value, 65535), 4); | 112 | CHECK_INT(scan_uint("0000", &value, 65535), 4); |
| 113 | CHECK_INT(value, 0); | 113 | CHECK_INT(value, 0); |
| 114 | } | 114 | } |
| 115 | 115 | ||
| @@ -117,9 +117,9 @@ static void case_bencode_rejects_leading_zeros(void) { | |||
| 117 | uint64_t value = 0xdead; | 117 | uint64_t value = 0xdead; |
| 118 | 118 | ||
| 119 | test_case("bencode refuses a leading zero on anything but zero"); | 119 | test_case("bencode refuses a leading zero on anything but zero"); |
| 120 | CHECK_INT(scan_udec_bencode("007", &value, 65535), 0); | 120 | CHECK_INT(scan_uint_bencode("007", &value, 65535), 0); |
| 121 | CHECK_INT(scan_udec_bencode("0000", &value, 65535), 0); | 121 | CHECK_INT(scan_uint_bencode("0000", &value, 65535), 0); |
| 122 | CHECK_INT(scan_udec_bencode("01", &value, 65535), 0); | 122 | CHECK_INT(scan_uint_bencode("01", &value, 65535), 0); |
| 123 | CHECK_INT(value, 0xdead); | 123 | CHECK_INT(value, 0xdead); |
| 124 | } | 124 | } |
| 125 | 125 | ||
| @@ -127,11 +127,11 @@ static void case_bencode_accepts_canonical(void) { | |||
| 127 | uint64_t value; | 127 | uint64_t value; |
| 128 | 128 | ||
| 129 | test_case("bencode accepts a canonically written number"); | 129 | test_case("bencode accepts a canonically written number"); |
| 130 | CHECK_INT(scan_udec_bencode("0", &value, 65535), 1); | 130 | CHECK_INT(scan_uint_bencode("0", &value, 65535), 1); |
| 131 | CHECK_INT(value, 0); | 131 | CHECK_INT(value, 0); |
| 132 | CHECK_INT(scan_udec_bencode("0:", &value, 65535), 1); | 132 | CHECK_INT(scan_uint_bencode("0:", &value, 65535), 1); |
| 133 | CHECK_INT(value, 0); | 133 | CHECK_INT(value, 0); |
| 134 | CHECK_INT(scan_udec_bencode("160:", &value, 65535), 3); | 134 | CHECK_INT(scan_uint_bencode("160:", &value, 65535), 3); |
| 135 | CHECK_INT(value, 160); | 135 | CHECK_INT(value, 160); |
| 136 | } | 136 | } |
| 137 | 137 | ||
| @@ -139,14 +139,64 @@ static void case_bencode_inherits_the_rest(void) { | |||
| 139 | uint64_t value = 0xdead; | 139 | uint64_t value = 0xdead; |
| 140 | 140 | ||
| 141 | test_case("bencode is no laxer than the plain scanner"); | 141 | test_case("bencode is no laxer than the plain scanner"); |
| 142 | CHECK_INT(scan_udec_bencode(" 1", &value, 65535), 0); | 142 | CHECK_INT(scan_uint_bencode(" 1", &value, 65535), 0); |
| 143 | CHECK_INT(scan_udec_bencode("-1", &value, 65535), 0); | 143 | CHECK_INT(scan_uint_bencode("-1", &value, 65535), 0); |
| 144 | CHECK_INT(scan_udec_bencode("+1", &value, 65535), 0); | 144 | CHECK_INT(scan_uint_bencode("+1", &value, 65535), 0); |
| 145 | CHECK_INT(scan_udec_bencode("65536", &value, 65535), 0); | 145 | CHECK_INT(scan_uint_bencode("65536", &value, 65535), 0); |
| 146 | CHECK_INT(value, 0xdead); | 146 | CHECK_INT(value, 0xdead); |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | /* ------------------------------------------- fixed width signed integers */ | ||
| 150 | |||
| 151 | /* A value lifted out of a query string arrives as a pointer and a length | ||
| 152 | with nothing terminating it, and the whole of it has to be the number. */ | ||
| 153 | |||
| 154 | static void case_int_whole_field(void) { | ||
| 155 | int64_t value = 0; | ||
| 156 | |||
| 157 | test_case("the whole field is read, sign and all"); | ||
| 158 | CHECK_INT(scan_int("123", 3, &value), 0); | ||
| 159 | CHECK_INT(value, 123); | ||
| 160 | CHECK_INT(scan_int("-45", 3, &value), 0); | ||
| 161 | CHECK_INT(value, -45); | ||
| 162 | |||
| 163 | test_case("and only as far as the length given"); | ||
| 164 | CHECK_INT(scan_int("12345", 3, &value), 0); | ||
| 165 | CHECK_INT(value, 123); | ||
| 166 | } | ||
| 167 | |||
| 168 | static void case_int_rejects_the_rest(void) { | ||
| 169 | int64_t value = 4711; | ||
| 170 | |||
| 171 | test_case("anything that is not all digits is refused"); | ||
| 172 | CHECK_INT(scan_int("12a", 3, &value), -1); | ||
| 173 | CHECK_INT(scan_int("", 0, &value), -1); | ||
| 174 | CHECK_INT(scan_int("-", 1, &value), -1); | ||
| 175 | CHECK_INT(scan_int(" 12", 3, &value), -1); | ||
| 176 | CHECK_INT(scan_int("+12", 3, &value), -1); | ||
| 177 | |||
| 178 | test_case("and a refused field leaves the number alone"); | ||
| 179 | CHECK_INT(value, 4711); | ||
| 180 | } | ||
| 181 | |||
| 182 | static void case_int_overflow(void) { | ||
| 183 | int64_t value = 0; | ||
| 184 | |||
| 185 | test_case("a field too long to fit is refused rather than wrapped"); | ||
| 186 | CHECK_INT(scan_int("9223372036854775807", 19, &value), 0); | ||
| 187 | CHECK_INT(value, INT64_MAX); | ||
| 188 | CHECK_INT(scan_int("9223372036854775808", 19, &value), -1); | ||
| 189 | CHECK_INT(scan_int("99999999999999999999", 20, &value), -1); | ||
| 190 | |||
| 191 | test_case("including the negative side"); | ||
| 192 | CHECK_INT(scan_int("-9223372036854775807", 20, &value), 0); | ||
| 193 | CHECK_INT(value, -INT64_MAX); | ||
| 194 | } | ||
| 195 | |||
| 149 | void test_scan(void) { | 196 | void test_scan(void) { |
| 197 | case_int_whole_field(); | ||
| 198 | case_int_rejects_the_rest(); | ||
| 199 | case_int_overflow(); | ||
| 150 | case_plain_numbers(); | 200 | case_plain_numbers(); |
| 151 | case_stops_at_non_digit(); | 201 | case_stops_at_non_digit(); |
| 152 | case_rejects_whitespace(); | 202 | case_rejects_whitespace(); |
diff --git a/tests/t_urlencoded.c b/tests/t_urlencoded.c index 1239e6b..6e0ee31 100644 --- a/tests/t_urlencoded.c +++ b/tests/t_urlencoded.c | |||
| @@ -228,33 +228,6 @@ static void case_find_keywords(void) { | |||
| 228 | CHECK_INT(urlencoded_match_keyword(keywords, &p, SCAN_SEARCHPATH_PARAM), -3); | 228 | CHECK_INT(urlencoded_match_keyword(keywords, &p, SCAN_SEARCHPATH_PARAM), -3); |
| 229 | } | 229 | } |
| 230 | 230 | ||
| 231 | static void case_fixed_int(void) { | ||
| 232 | char positive[] = "123"; | ||
| 233 | char negative[] = "-45"; | ||
| 234 | char trailing[] = "12a"; | ||
| 235 | int value; | ||
| 236 | |||
| 237 | test_case("scan_fixed_int returns the count of unparsed bytes"); | ||
| 238 | CHECK_INT(scan_fixed_int(positive, 3, &value), 0); | ||
| 239 | CHECK_INT(value, 123); | ||
| 240 | |||
| 241 | CHECK_INT(scan_fixed_int(negative, 3, &value), 0); | ||
| 242 | CHECK_INT(value, -45); | ||
| 243 | |||
| 244 | CHECK_INT(scan_fixed_int(trailing, 3, &value), 1); | ||
| 245 | CHECK_INT(value, 12); | ||
| 246 | } | ||
| 247 | |||
| 248 | static void case_fixed_int_bounded(void) { | ||
| 249 | char digits[] = "12345"; | ||
| 250 | int value; | ||
| 251 | |||
| 252 | test_case("scan_fixed_int honours the length it is given"); | ||
| 253 | CHECK_INT(scan_fixed_int(digits, 3, &value), 0); | ||
| 254 | CHECK_INT(value, 123); | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | /* The encoding direction. Which bytes survive unescaped is fixed by what | 231 | /* The encoding direction. Which bytes survive unescaped is fixed by what |
| 259 | existing parsers of the urlencoded full scrape expect, so these cases pin | 232 | existing parsers of the urlencoded full scrape expect, so these cases pin |
| 260 | the set rather than describe a preference. */ | 233 | the set rather than describe a preference. */ |
| @@ -361,8 +334,6 @@ void test_urlencoded(void) { | |||
| 361 | case_raw_high_byte(); | 334 | case_raw_high_byte(); |
| 362 | case_skipvalue(); | 335 | case_skipvalue(); |
| 363 | case_find_keywords(); | 336 | case_find_keywords(); |
| 364 | case_fixed_int(); | ||
| 365 | case_fixed_int_bounded(); | ||
| 366 | case_fmt_passes_safe_bytes(); | 337 | case_fmt_passes_safe_bytes(); |
| 367 | case_fmt_escapes_the_rest(); | 338 | case_fmt_escapes_the_rest(); |
| 368 | case_fmt_hex_is_lower_case(); | 339 | case_fmt_hex_is_lower_case(); |
