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/t_ip.c | |
| parent | 6837c5ac80e3624c6dc3a82d03e50d68b2f5a989 (diff) | |
Third run of making variable and function names more readable, integer scans and ip parsers
Diffstat (limited to 'tests/t_ip.c')
| -rw-r--r-- | tests/t_ip.c | 54 |
1 files changed, 27 insertions, 27 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 | } |
