]> git.proxmox.com Git - libgit2.git/blob - tests/submodule/escape.c
08eb7680972d34296561c4c9e24324f662107343
[libgit2.git] / tests / submodule / escape.c
1 #include "clar_libgit2.h"
2 #include "posix.h"
3 #include "path.h"
4 #include "submodule_helpers.h"
5 #include "futils.h"
6 #include "repository.h"
7
8 static git_repository *g_repo = NULL;
9
10 void test_submodule_escape__cleanup(void)
11 {
12 cl_git_sandbox_cleanup();
13 }
14
15 #define EVIL_SM_NAME "../../modules/evil"
16 #define EVIL_SM_NAME_WINDOWS "..\\\\..\\\\modules\\\\evil"
17 #define EVIL_SM_NAME_WINDOWS_UNESC "..\\..\\modules\\evil"
18
19 static int find_evil(git_submodule *sm, const char *name, void *payload)
20 {
21 int *foundit = (int *) payload;
22
23 GIT_UNUSED(sm);
24
25 if (!git__strcmp(EVIL_SM_NAME, name) ||
26 !git__strcmp(EVIL_SM_NAME_WINDOWS_UNESC, name))
27 *foundit = true;
28
29 return 0;
30 }
31
32 void test_submodule_escape__from_gitdir(void)
33 {
34 int foundit;
35 git_submodule *sm;
36 git_buf buf = GIT_BUF_INIT;
37 unsigned int sm_location;
38
39 g_repo = setup_fixture_submodule_simple();
40
41 cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
42 cl_git_rewritefile(buf.ptr,
43 "[submodule \"" EVIL_SM_NAME "\"]\n"
44 " path = testrepo\n"
45 " url = ../testrepo.git\n");
46 git_buf_dispose(&buf);
47
48 /* Find it all the different ways we know about it */
49 foundit = 0;
50 cl_git_pass(git_submodule_foreach(g_repo, find_evil, &foundit));
51 cl_assert_equal_i(0, foundit);
52 cl_git_fail_with(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, EVIL_SM_NAME));
53 /*
54 * We do know about this as it's in the index and HEAD, but the data is
55 * incomplete as there is no configured data for it (we pretend it
56 * doesn't exist). This leaves us with an odd situation but it's
57 * consistent with what we would do if we did add a submodule with no
58 * configuration.
59 */
60 cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
61 cl_git_pass(git_submodule_location(&sm_location, sm));
62 cl_assert_equal_i(GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_HEAD, sm_location);
63 git_submodule_free(sm);
64 }
65
66 void test_submodule_escape__from_gitdir_windows(void)
67 {
68 int foundit;
69 git_submodule *sm;
70 git_buf buf = GIT_BUF_INIT;
71 unsigned int sm_location;
72
73 g_repo = setup_fixture_submodule_simple();
74
75 cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
76 cl_git_rewritefile(buf.ptr,
77 "[submodule \"" EVIL_SM_NAME_WINDOWS "\"]\n"
78 " path = testrepo\n"
79 " url = ../testrepo.git\n");
80 git_buf_dispose(&buf);
81
82 /* Find it all the different ways we know about it */
83 foundit = 0;
84 cl_git_pass(git_submodule_foreach(g_repo, find_evil, &foundit));
85 cl_assert_equal_i(0, foundit);
86 cl_git_fail_with(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, EVIL_SM_NAME_WINDOWS_UNESC));
87 /*
88 * We do know about this as it's in the index and HEAD, but the data is
89 * incomplete as there is no configured data for it (we pretend it
90 * doesn't exist). This leaves us with an odd situation but it's
91 * consistent with what we would do if we did add a submodule with no
92 * configuration.
93 */
94 cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
95 cl_git_pass(git_submodule_location(&sm_location, sm));
96 cl_assert_equal_i(GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_HEAD, sm_location);
97 git_submodule_free(sm);
98 }