Git fork

t/unit-tests: convert urlmatch-normalization test to clar

Adapt urlmatch-normalization test file to use clar testing framework by
using clar assertions where necessary.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Seyi Kuforiji and committed by
Junio C Hamano
7d4212b8 bc934277

+20 -44
+1 -1
Makefile
··· 1362 CLAR_TEST_SUITES += u-strcmp-offset 1363 CLAR_TEST_SUITES += u-strvec 1364 CLAR_TEST_SUITES += u-trailer 1365 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) 1366 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) 1367 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o ··· 1378 UNIT_TEST_PROGRAMS += t-reftable-readwrite 1379 UNIT_TEST_PROGRAMS += t-reftable-record 1380 UNIT_TEST_PROGRAMS += t-reftable-stack 1381 - UNIT_TEST_PROGRAMS += t-urlmatch-normalization 1382 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) 1383 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o 1384 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
··· 1362 CLAR_TEST_SUITES += u-strcmp-offset 1363 CLAR_TEST_SUITES += u-strvec 1364 CLAR_TEST_SUITES += u-trailer 1365 + CLAR_TEST_SUITES += u-urlmatch-normalization 1366 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) 1367 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) 1368 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o ··· 1379 UNIT_TEST_PROGRAMS += t-reftable-readwrite 1380 UNIT_TEST_PROGRAMS += t-reftable-record 1381 UNIT_TEST_PROGRAMS += t-reftable-stack 1382 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) 1383 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o 1384 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
+1 -1
t/meson.build
··· 10 'unit-tests/u-strcmp-offset.c', 11 'unit-tests/u-strvec.c', 12 'unit-tests/u-trailer.c', 13 ] 14 15 clar_sources = [ ··· 60 'unit-tests/t-reftable-readwrite.c', 61 'unit-tests/t-reftable-record.c', 62 'unit-tests/t-reftable-stack.c', 63 - 'unit-tests/t-urlmatch-normalization.c', 64 ] 65 66 foreach unit_test_program : unit_test_programs
··· 10 'unit-tests/u-strcmp-offset.c', 11 'unit-tests/u-strvec.c', 12 'unit-tests/u-trailer.c', 13 + 'unit-tests/u-urlmatch-normalization.c', 14 ] 15 16 clar_sources = [ ··· 61 'unit-tests/t-reftable-readwrite.c', 62 'unit-tests/t-reftable-record.c', 63 'unit-tests/t-reftable-stack.c', 64 ] 65 66 foreach unit_test_program : unit_test_programs
+18 -42
t/unit-tests/t-urlmatch-normalization.c t/unit-tests/u-urlmatch-normalization.c
··· 1 - #include "test-lib.h" 2 #include "urlmatch.h" 3 4 static void check_url_normalizable(const char *url, unsigned int normalizable) 5 { 6 char *url_norm = url_normalize(url, NULL); 7 8 - if (!check_int(normalizable, ==, url_norm ? 1 : 0)) 9 - test_msg("input url: %s", url); 10 free(url_norm); 11 } 12 ··· 14 { 15 char *url_norm = url_normalize(url, NULL); 16 17 - if (!check_str(url_norm, expect)) 18 - test_msg("input url: %s", url); 19 free(url_norm); 20 } 21 ··· 26 char *url2_norm = url_normalize(url2, NULL); 27 28 if (equal) { 29 - if (!check_str(url1_norm, url2_norm)) 30 - test_msg("input url1: %s\n input url2: %s", url1, 31 - url2); 32 - } else if (!check_int(strcmp(url1_norm, url2_norm), !=, 0)) { 33 - test_msg(" normalized url1: %s\n normalized url2: %s\n" 34 - " input url1: %s\n input url2: %s", 35 - url1_norm, url2_norm, url1, url2); 36 } 37 free(url1_norm); 38 free(url2_norm); ··· 43 struct url_info info; 44 char *url_norm = url_normalize(url, &info); 45 46 - if (!check_int(info.url_len, ==, len)) 47 - test_msg(" input url: %s\n normalized url: %s", url, 48 - url_norm); 49 free(url_norm); 50 } 51 52 /* Note that only "file:" URLs should be allowed without a host */ 53 - static void t_url_scheme(void) 54 { 55 check_url_normalizable("", 0); 56 check_url_normalizable("_", 0); ··· 73 check_normalized_url("AbCdeF://x.Y", "abcdef://x.y/"); 74 } 75 76 - static void t_url_authority(void) 77 { 78 check_url_normalizable("scheme://user:pass@", 0); 79 check_url_normalizable("scheme://?", 0); ··· 109 check_url_normalizable("scheme://invalid....:[", 0); 110 } 111 112 - static void t_url_port(void) 113 { 114 check_url_normalizable("xyz://q@some.host:", 1); 115 check_url_normalizable("xyz://q@some.host:456/", 1); ··· 139 check_url_normalizable("xyz://[::1]:030f/", 0); 140 } 141 142 - static void t_url_port_normalization(void) 143 { 144 check_normalized_url("http://x:800", "http://x:800/"); 145 check_normalized_url("http://x:0800", "http://x:800/"); ··· 154 check_normalized_url("https://x:000000443", "https://x/"); 155 } 156 157 - static void t_url_general_escape(void) 158 { 159 check_url_normalizable("http://x.y?%fg", 0); 160 check_normalized_url("X://W/%7e%41^%3a", "x://w/~A%5E%3A"); ··· 164 check_normalized_url("X://W?!", "x://w/?!"); 165 } 166 167 - static void t_url_high_bit(void) 168 { 169 check_normalized_url( 170 "x://q/\x01\x02\x03\x04\x05\x06\x07\x08\x0e\x0f\x10\x11\x12", ··· 198 "x://q/%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF"); 199 } 200 201 - static void t_url_utf8_escape(void) 202 { 203 check_normalized_url( 204 "x://q/\xc2\x80\xdf\xbf\xe0\xa0\x80\xef\xbf\xbd\xf0\x90\x80\x80\xf0\xaf\xbf\xbd", 205 "x://q/%C2%80%DF%BF%E0%A0%80%EF%BF%BD%F0%90%80%80%F0%AF%BF%BD"); 206 } 207 208 - static void t_url_username_pass(void) 209 { 210 check_normalized_url("x://%41%62(^):%70+d@foo", "x://Ab(%5E):p+d@foo/"); 211 } 212 213 - static void t_url_length(void) 214 { 215 check_normalized_url_length("Http://%4d%65:%4d^%70@The.Host", 25); 216 check_normalized_url_length("http://%41:%42@x.y/%61/", 17); 217 check_normalized_url_length("http://@x.y/^", 15); 218 } 219 220 - static void t_url_dots(void) 221 { 222 check_normalized_url("x://y/.", "x://y/"); 223 check_normalized_url("x://y/./", "x://y/"); ··· 244 * "http://foo" specifies neither a user name nor a password. 245 * So they should not be equivalent. 246 */ 247 - static void t_url_equivalents(void) 248 { 249 compare_normalized_urls("httP://x", "Http://X/", 1); 250 compare_normalized_urls("Http://%4d%65:%4d^%70@The.Host", "hTTP://Me:%4D^p@the.HOST:80/", 1); ··· 253 compare_normalized_urls("https://@x.y/^/../abc", "httpS://@x.y:0443/abc", 1); 254 compare_normalized_urls("https://@x.y/^/..", "httpS://@x.y:0443/", 1); 255 } 256 - 257 - int cmd_main(int argc UNUSED, const char **argv UNUSED) 258 - { 259 - TEST(t_url_scheme(), "url scheme"); 260 - TEST(t_url_authority(), "url authority"); 261 - TEST(t_url_port(), "url port checks"); 262 - TEST(t_url_port_normalization(), "url port normalization"); 263 - TEST(t_url_general_escape(), "url general escapes"); 264 - TEST(t_url_high_bit(), "url high-bit escapes"); 265 - TEST(t_url_utf8_escape(), "url utf8 escapes"); 266 - TEST(t_url_username_pass(), "url username/password escapes"); 267 - TEST(t_url_length(), "url normalized lengths"); 268 - TEST(t_url_dots(), "url . and .. segments"); 269 - TEST(t_url_equivalents(), "url equivalents"); 270 - return test_done(); 271 - }
··· 1 + #include "unit-test.h" 2 #include "urlmatch.h" 3 4 static void check_url_normalizable(const char *url, unsigned int normalizable) 5 { 6 char *url_norm = url_normalize(url, NULL); 7 8 + cl_assert_equal_i(normalizable, url_norm ? 1 : 0); 9 free(url_norm); 10 } 11 ··· 13 { 14 char *url_norm = url_normalize(url, NULL); 15 16 + cl_assert_equal_s(url_norm, expect); 17 free(url_norm); 18 } 19 ··· 24 char *url2_norm = url_normalize(url2, NULL); 25 26 if (equal) { 27 + cl_assert_equal_s(url1_norm, url2_norm); 28 + } else { 29 + cl_assert(strcmp(url1_norm, url2_norm) != 0); 30 } 31 free(url1_norm); 32 free(url2_norm); ··· 37 struct url_info info; 38 char *url_norm = url_normalize(url, &info); 39 40 + cl_assert_equal_i(info.url_len, len); 41 free(url_norm); 42 } 43 44 /* Note that only "file:" URLs should be allowed without a host */ 45 + void test_urlmatch_normalization__scheme(void) 46 { 47 check_url_normalizable("", 0); 48 check_url_normalizable("_", 0); ··· 65 check_normalized_url("AbCdeF://x.Y", "abcdef://x.y/"); 66 } 67 68 + void test_urlmatch_normalization__authority(void) 69 { 70 check_url_normalizable("scheme://user:pass@", 0); 71 check_url_normalizable("scheme://?", 0); ··· 101 check_url_normalizable("scheme://invalid....:[", 0); 102 } 103 104 + void test_urlmatch_normalization__port(void) 105 { 106 check_url_normalizable("xyz://q@some.host:", 1); 107 check_url_normalizable("xyz://q@some.host:456/", 1); ··· 131 check_url_normalizable("xyz://[::1]:030f/", 0); 132 } 133 134 + void test_urlmatch_normalization__port_normalization(void) 135 { 136 check_normalized_url("http://x:800", "http://x:800/"); 137 check_normalized_url("http://x:0800", "http://x:800/"); ··· 146 check_normalized_url("https://x:000000443", "https://x/"); 147 } 148 149 + void test_urlmatch_normalization__general_escape(void) 150 { 151 check_url_normalizable("http://x.y?%fg", 0); 152 check_normalized_url("X://W/%7e%41^%3a", "x://w/~A%5E%3A"); ··· 156 check_normalized_url("X://W?!", "x://w/?!"); 157 } 158 159 + void test_urlmatch_normalization__high_bit(void) 160 { 161 check_normalized_url( 162 "x://q/\x01\x02\x03\x04\x05\x06\x07\x08\x0e\x0f\x10\x11\x12", ··· 190 "x://q/%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF"); 191 } 192 193 + void test_urlmatch_normalization__utf8_escape(void) 194 { 195 check_normalized_url( 196 "x://q/\xc2\x80\xdf\xbf\xe0\xa0\x80\xef\xbf\xbd\xf0\x90\x80\x80\xf0\xaf\xbf\xbd", 197 "x://q/%C2%80%DF%BF%E0%A0%80%EF%BF%BD%F0%90%80%80%F0%AF%BF%BD"); 198 } 199 200 + void test_urlmatch_normalization__username_pass(void) 201 { 202 check_normalized_url("x://%41%62(^):%70+d@foo", "x://Ab(%5E):p+d@foo/"); 203 } 204 205 + void test_urlmatch_normalization__length(void) 206 { 207 check_normalized_url_length("Http://%4d%65:%4d^%70@The.Host", 25); 208 check_normalized_url_length("http://%41:%42@x.y/%61/", 17); 209 check_normalized_url_length("http://@x.y/^", 15); 210 } 211 212 + void test_urlmatch_normalization__dots(void) 213 { 214 check_normalized_url("x://y/.", "x://y/"); 215 check_normalized_url("x://y/./", "x://y/"); ··· 236 * "http://foo" specifies neither a user name nor a password. 237 * So they should not be equivalent. 238 */ 239 + void test_urlmatch_normalization__equivalents(void) 240 { 241 compare_normalized_urls("httP://x", "Http://X/", 1); 242 compare_normalized_urls("Http://%4d%65:%4d^%70@The.Host", "hTTP://Me:%4D^p@the.HOST:80/", 1); ··· 245 compare_normalized_urls("https://@x.y/^/../abc", "httpS://@x.y:0443/abc", 1); 246 compare_normalized_urls("https://@x.y/^/..", "httpS://@x.y:0443/", 1); 247 }