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>
Seyi Kuforiji committed
Mar 4, 2025 at 12:33 UTC
7d4212b8f376ddf1aa403e51b46c92106d079b2d
3 files changed
+20
-44
Makefile
+1
-1
@@ -1362,6 +1362,7 @@ CLAR_TEST_SUITES += u-strbuf
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
@@ -1378,7 +1379,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
1379
UNIT_TEST_PROGRAMS += t-reftable-readwrite
1380
UNIT_TEST_PROGRAMS += t-reftable-record
1381
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
t/meson.build
+1
-1
@@ -10,6 +10,7 @@ clar_test_suites = [
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 = [
@@ -60,7 +61,6 @@ unit_test_programs = [
61
'unit-tests/t-reftable-readwrite.c',
62
'unit-tests/t-reftable-record.c',
63
'unit-tests/t-reftable-stack.c',
63
- 'unit-tests/t-urlmatch-normalization.c',
64
]
65
66
foreach unit_test_program : unit_test_programs
t/unit-tests/u-urlmatch-normalization.c
renamed
+18
-42
@@ -1,12 +1,11 @@
1
-#include "test-lib.h"
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
- if (!check_int(normalizable, ==, url_norm ? 1 : 0))
9
- test_msg("input url: %s", url);
8
+ cl_assert_equal_i(normalizable, url_norm ? 1 : 0);
9
free(url_norm);
10
}
11
@@ -14,8 +13,7 @@ static void check_normalized_url(const char *url, const char *expect)
13
{
14
char *url_norm = url_normalize(url, NULL);
15
17
- if (!check_str(url_norm, expect))
18
- test_msg("input url: %s", url);
16
+ cl_assert_equal_s(url_norm, expect);
17
free(url_norm);
18
}
19
@@ -26,13 +24,9 @@ static void compare_normalized_urls(const char *url1, const char *url2,
24
char *url2_norm = url_normalize(url2, NULL);
25
26
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);
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);
@@ -43,14 +37,12 @@ static void check_normalized_url_length(const char *url, size_t len)
37
struct url_info info;
38
char *url_norm = url_normalize(url, &info);
39
46
- if (!check_int(info.url_len, ==, len))
47
- test_msg(" input url: %s\n normalized url: %s", url,
48
- url_norm);
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 */
53
-static void t_url_scheme(void)
45
+void test_urlmatch_normalization__scheme(void)
46
{
47
check_url_normalizable("", 0);
48
check_url_normalizable("_", 0);
@@ -73,7 +65,7 @@ static void t_url_scheme(void)
65
check_normalized_url("AbCdeF://x.Y", "abcdef://x.y/");
66
}
67
76
-static void t_url_authority(void)
68
+void test_urlmatch_normalization__authority(void)
69
{
70
check_url_normalizable("scheme://user:pass@", 0);
71
check_url_normalizable("scheme://?", 0);
@@ -109,7 +101,7 @@ static void t_url_authority(void)
101
check_url_normalizable("scheme://invalid....:[", 0);
102
}
103
112
-static void t_url_port(void)
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);
@@ -139,7 +131,7 @@ static void t_url_port(void)
131
check_url_normalizable("xyz://[::1]:030f/", 0);
132
}
133
142
-static void t_url_port_normalization(void)
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/");
@@ -154,7 +146,7 @@ static void t_url_port_normalization(void)
146
check_normalized_url("https://x:000000443", "https://x/");
147
}
148
157
-static void t_url_general_escape(void)
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");
@@ -164,7 +156,7 @@ static void t_url_general_escape(void)
156
check_normalized_url("X://W?!", "x://w/?!");
157
}
158
167
-static void t_url_high_bit(void)
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",
@@ -198,26 +190,26 @@ static void t_url_high_bit(void)
190
"x://q/%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF");
191
}
192
201
-static void t_url_utf8_escape(void)
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
208
-static void t_url_username_pass(void)
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
213
-static void t_url_length(void)
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
220
-static void t_url_dots(void)
212
+void test_urlmatch_normalization__dots(void)
213
{
214
check_normalized_url("x://y/.", "x://y/");
215
check_normalized_url("x://y/./", "x://y/");
@@ -244,7 +236,7 @@ static void t_url_dots(void)
236
* "http://foo" specifies neither a user name nor a password.
237
* So they should not be equivalent.
238
*/
247
-static void t_url_equivalents(void)
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);
@@ -253,19 +245,3 @@ static void t_url_equivalents(void)
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
}
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
-}