t7450: test submodule urls

Add tests to 't7450-bad-git-dotfiles.sh' to check the validity of different submodule URLs. To verify this directly (without setting up test repositories & submodules), add a 'check-url' subcommand to 'test-tool submodule' that calls 'check_submodule_url' in the same way that 'check-name' calls 'check_submodule_name'. Add two tests to separately address cases where the URL check correctly filters out invalid URLs and cases where the check misses invalid URLs. Mark the latter ("url check misses invalid cases") with 'test_expect_failure' to indicate that this is currently broken, which will be fixed in the next step. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Jan 18, 2024 at 01:55 UTC 7e2fc39d8c02048e9dddcba1b1b6786a8088a1a8
2 files changed +66 -4
t/helper/test-submodule.c
+31 -4
@@ -14,6 +14,13 @@ static const char *submodule_check_name_usage[] = {
14 NULL
15 };
16
17 +#define TEST_TOOL_CHECK_URL_USAGE \
18 + "test-tool submodule check-url"
19 +static const char *submodule_check_url_usage[] = {
20 + TEST_TOOL_CHECK_URL_USAGE,
21 + NULL
22 +};
23 +
24 #define TEST_TOOL_IS_ACTIVE_USAGE \
25 "test-tool submodule is-active <name>"
26 static const char *submodule_is_active_usage[] = {
@@ -30,17 +37,23 @@ static const char *submodule_resolve_relative_url_usage[] = {
37
38 static const char *submodule_usage[] = {
39 TEST_TOOL_CHECK_NAME_USAGE,
40 + TEST_TOOL_CHECK_URL_USAGE,
41 TEST_TOOL_IS_ACTIVE_USAGE,
42 TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
43 NULL
44 };
45
38 -/* Filter stdin to print only valid names. */
39 -static int check_name(void)
46 +typedef int (*check_fn_t)(const char *);
47 +
48 +/*
49 + * Apply 'check_fn' to each line of stdin, printing values that pass the check
50 + * to stdout.
51 + */
52 +static int check_submodule(check_fn_t check_fn)
53 {
54 struct strbuf buf = STRBUF_INIT;
55 while (strbuf_getline(&buf, stdin) != EOF) {
43 - if (!check_submodule_name(buf.buf))
56 + if (!check_fn(buf.buf))
57 printf("%s\n", buf.buf);
58 }
59 strbuf_release(&buf);
@@ -57,7 +70,20 @@ static int cmd__submodule_check_name(int argc, const char **argv)
70 if (argc)
71 usage_with_options(submodule_check_name_usage, options);
72
60 - return check_name();
73 + return check_submodule(check_submodule_name);
74 +}
75 +
76 +static int cmd__submodule_check_url(int argc, const char **argv)
77 +{
78 + struct option options[] = {
79 + OPT_END()
80 + };
81 + argc = parse_options(argc, argv, "test-tools", options,
82 + submodule_check_url_usage, 0);
83 + if (argc)
84 + usage_with_options(submodule_check_url_usage, options);
85 +
86 + return check_submodule(check_submodule_url);
87 }
88
89 static int cmd__submodule_is_active(int argc, const char **argv)
@@ -183,6 +209,7 @@ static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
209
210 static struct test_cmd cmds[] = {
211 { "check-name", cmd__submodule_check_name },
212 + { "check-url", cmd__submodule_check_url },
213 { "is-active", cmd__submodule_is_active },
214 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
215 { "config-list", cmd__submodule_config_list },
t/t7450-bad-git-dotfiles.sh
+35
@@ -45,6 +45,41 @@ test_expect_success 'check names' '
45 test_cmp expect actual
46 '
47
48 +test_expect_success 'check urls' '
49 + cat >expect <<-\EOF &&
50 + ./bar/baz/foo.git
51 + https://example.com/foo.git
52 + http://example.com:80/deeper/foo.git
53 + EOF
54 +
55 + test-tool submodule check-url >actual <<-\EOF &&
56 + ./bar/baz/foo.git
57 + https://example.com/foo.git
58 + http://example.com:80/deeper/foo.git
59 + -a./foo
60 + ../../..//test/foo.git
61 + ../../../../../:localhost:8080/foo.git
62 + ..\../.\../:example.com/foo.git
63 + ./%0ahost=example.com/foo.git
64 + https://one.example.com/evil?%0ahost=two.example.com
65 + https:///example.com/foo.git
66 + https::example.com/foo.git
67 + http:::example.com/foo.git
68 + EOF
69 +
70 + test_cmp expect actual
71 +'
72 +
73 +# NEEDSWORK: the URL checked here is not valid (and will not work as a remote if
74 +# a user attempts to clone it), but the fsck check passes.
75 +test_expect_failure 'url check misses invalid cases' '
76 + test-tool submodule check-url >actual <<-\EOF &&
77 + http://example.com:test/foo.git
78 + EOF
79 +
80 + test_must_be_empty actual
81 +'
82 +
83 test_expect_success 'create innocent subrepo' '
84 git init innocent &&
85 git -C innocent commit --allow-empty -m foo