From 4be2aa57c9b512d45b4071b71d0b93f3bf7e18d6 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 16 Feb 2016 18:50:08 +0000 Subject: [PATCH] win32: tests around handling forbidden paths Introduce a repository that contains some paths that were illegal on PC-DOS circa 1981 (like `aux`, `con`, `com1`) and that in a bizarre fit of retrocomputing, remain illegal on some "modern" computers, despite being "new technology". Introduce some aspirational tests that suggest that we should be able to cope with trees and indexes that contain paths that would be illegal on the filesystem, so that we can at least diff them. Further ensure that checkout will not write a repository with forbidden paths. --- tests/resources/win32-forbidden/.gitted/HEAD | 1 + .../resources/win32-forbidden/.gitted/config | 7 + tests/resources/win32-forbidden/.gitted/index | Bin 0 -> 577 bytes .../win32-forbidden/.gitted/info/exclude | 6 + .../10/68072702a28a82c78902cf5bf82c3864cf4356 | Bin 0 -> 143 bytes .../17/6a458f94e0ea5272ce67c36bf30b6be9caf623 | Bin 0 -> 28 bytes .../2d/7445a749d25269f32724aa621cb70b196bcc40 | Bin 0 -> 105 bytes .../34/96991d72d500af36edef68bbfcccd1661d88db | 3 + .../8f/45aad6f23b9509f8786c617e19c127ae76609a | 2 + .../da/623abd956bb2fd8052c708c7ed43f05d192d37 | Bin 0 -> 59 bytes .../ea/c7621a652e5261ef1c1d3e7ae31b0d84fcbaba | 3 + .../win32-forbidden/.gitted/refs/heads/master | 1 + tests/win32/forbidden.c | 183 ++++++++++++++++++ 13 files changed, 206 insertions(+) create mode 100644 tests/resources/win32-forbidden/.gitted/HEAD create mode 100644 tests/resources/win32-forbidden/.gitted/config create mode 100644 tests/resources/win32-forbidden/.gitted/index create mode 100644 tests/resources/win32-forbidden/.gitted/info/exclude create mode 100644 tests/resources/win32-forbidden/.gitted/objects/10/68072702a28a82c78902cf5bf82c3864cf4356 create mode 100644 tests/resources/win32-forbidden/.gitted/objects/17/6a458f94e0ea5272ce67c36bf30b6be9caf623 create mode 100644 tests/resources/win32-forbidden/.gitted/objects/2d/7445a749d25269f32724aa621cb70b196bcc40 create mode 100644 tests/resources/win32-forbidden/.gitted/objects/34/96991d72d500af36edef68bbfcccd1661d88db create mode 100644 tests/resources/win32-forbidden/.gitted/objects/8f/45aad6f23b9509f8786c617e19c127ae76609a create mode 100644 tests/resources/win32-forbidden/.gitted/objects/da/623abd956bb2fd8052c708c7ed43f05d192d37 create mode 100644 tests/resources/win32-forbidden/.gitted/objects/ea/c7621a652e5261ef1c1d3e7ae31b0d84fcbaba create mode 100644 tests/resources/win32-forbidden/.gitted/refs/heads/master create mode 100644 tests/win32/forbidden.c diff --git a/tests/resources/win32-forbidden/.gitted/HEAD b/tests/resources/win32-forbidden/.gitted/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/tests/resources/win32-forbidden/.gitted/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests/resources/win32-forbidden/.gitted/config b/tests/resources/win32-forbidden/.gitted/config new file mode 100644 index 000000000..6c9406b7d --- /dev/null +++ b/tests/resources/win32-forbidden/.gitted/config @@ -0,0 +1,7 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true diff --git a/tests/resources/win32-forbidden/.gitted/index b/tests/resources/win32-forbidden/.gitted/index new file mode 100644 index 0000000000000000000000000000000000000000..1202dd9f4ae5b5246426b4b7bdc787a12af44248 GIT binary patch literal 577 zcmZ?q402{*U|<4b_OQdze}OaujAmqDU}3rwx0->WaR~zh<5!@R2oUp#XSw!IdGIQz z=v?~Y?9bfUFHe0_X5iCH&n!tSDJjZKDlJJZhL|Jy2bqSLXZjY+JiS{5F=8+@FfcPQQP4}zEJ-XWDauSLElDkA5YKY$pYq^UP|>;c z!`Yv?vtOS2rVLf-?C-~LE6Hl_)a*@v8-k8=9DnQlAy!h?94hToP-2KrJ~1&-ucV@c xq2G1YwNKVlIe%2-B-TkDR9{z?Fbir#Vrd0F6VftsQVAJRRaJ#k2>{B;WqZ3*KK=jz literal 0 HcmV?d00001 diff --git a/tests/resources/win32-forbidden/.gitted/objects/17/6a458f94e0ea5272ce67c36bf30b6be9caf623 b/tests/resources/win32-forbidden/.gitted/objects/17/6a458f94e0ea5272ce67c36bf30b6be9caf623 new file mode 100644 index 0000000000000000000000000000000000000000..ef83166706c43aabd3c68d155be9c92a23960f0f GIT binary patch literal 28 kcmb5Fkmn=FfcPQQE>M6W4M)MwRdXvroRnA$2pF_b^Z`5scR0E z_9-Ya#3!Gan5b7$QNqygy6V~|>#3YSDsmF*BoC^uD@&LKH6pRJ0-p(KnK`M1jHs%r L!l?uR{HIf1PD3w2 literal 0 HcmV?d00001 diff --git a/tests/resources/win32-forbidden/.gitted/objects/34/96991d72d500af36edef68bbfcccd1661d88db b/tests/resources/win32-forbidden/.gitted/objects/34/96991d72d500af36edef68bbfcccd1661d88db new file mode 100644 index 000000000..71b6172c6 --- /dev/null +++ b/tests/resources/win32-forbidden/.gitted/objects/34/96991d72d500af36edef68bbfcccd1661d88db @@ -0,0 +1,3 @@ +x +![whӢ}/8B*8N~@gu՜S ͡7a +f2"s e.%Q ؽ຾m[kjmGq_N3LBJŔFG:B VRO ͿҖj!= \ No newline at end of file diff --git a/tests/resources/win32-forbidden/.gitted/objects/8f/45aad6f23b9509f8786c617e19c127ae76609a b/tests/resources/win32-forbidden/.gitted/objects/8f/45aad6f23b9509f8786c617e19c127ae76609a new file mode 100644 index 000000000..8bcd980c4 --- /dev/null +++ b/tests/resources/win32-forbidden/.gitted/objects/8f/45aad6f23b9509f8786c617e19c127ae76609a @@ -0,0 +1,2 @@ +xA + ֝/k. ]`h޾"= ˕e* U+M%O4c˱ \ No newline at end of file diff --git a/tests/resources/win32-forbidden/.gitted/objects/da/623abd956bb2fd8052c708c7ed43f05d192d37 b/tests/resources/win32-forbidden/.gitted/objects/da/623abd956bb2fd8052c708c7ed43f05d192d37 new file mode 100644 index 0000000000000000000000000000000000000000..923462306f0fb2612820702c6b18f53f3c95b479 GIT binary patch literal 59 zcmbFMi#hOzsp>a%59Po8VGq(58w PWV-n4LPmy_m?BgqVJ =FdJTqDdBN'6Rp S+kpGrAR*Tz! vVGn5l_;U>H \ No newline at end of file diff --git a/tests/resources/win32-forbidden/.gitted/refs/heads/master b/tests/resources/win32-forbidden/.gitted/refs/heads/master new file mode 100644 index 000000000..47fce0fe3 --- /dev/null +++ b/tests/resources/win32-forbidden/.gitted/refs/heads/master @@ -0,0 +1 @@ +3496991d72d500af36edef68bbfcccd1661d88db diff --git a/tests/win32/forbidden.c b/tests/win32/forbidden.c new file mode 100644 index 000000000..e02f41179 --- /dev/null +++ b/tests/win32/forbidden.c @@ -0,0 +1,183 @@ +#include "clar_libgit2.h" + +#include "repository.h" +#include "buffer.h" +#include "submodule.h" + +static const char *repo_name = "win32-forbidden"; +static git_repository *repo; + +void test_win32_forbidden__initialize(void) +{ + repo = cl_git_sandbox_init(repo_name); +} + +void test_win32_forbidden__cleanup(void) +{ + cl_git_sandbox_cleanup(); +} + +void test_win32_forbidden__can_open_index(void) +{ + git_index *index; + cl_git_pass(git_repository_index(&index, repo)); + cl_assert_equal_i(7, git_index_entrycount(index)); + + /* ensure we can even write the unmodified index */ + cl_git_pass(git_index_write(index)); + + git_index_free(index); +} + +void test_win32_forbidden__can_add_forbidden_filename_with_entry(void) +{ + git_index *index; + git_index_entry entry = {{0}}; + + cl_git_pass(git_repository_index(&index, repo)); + + entry.path = "aux"; + entry.mode = GIT_FILEMODE_BLOB; + git_oid_fromstr(&entry.id, "da623abd956bb2fd8052c708c7ed43f05d192d37"); + + cl_git_pass(git_index_add(index, &entry)); + + git_index_free(index); +} + +void test_win32_forbidden__cannot_add_dot_git_even_with_entry(void) +{ + git_index *index; + git_index_entry entry = {{0}}; + + cl_git_pass(git_repository_index(&index, repo)); + + entry.path = "foo/.git"; + entry.mode = GIT_FILEMODE_BLOB; + git_oid_fromstr(&entry.id, "da623abd956bb2fd8052c708c7ed43f05d192d37"); + + cl_git_fail(git_index_add(index, &entry)); + + git_index_free(index); +} + +void test_win32_forbidden__cannot_add_forbidden_filename_from_filesystem(void) +{ + git_index *index; + + /* since our function calls are very low-level, we can create `aux.`, + * but we should not be able to add it to the index + */ + cl_git_pass(git_repository_index(&index, repo)); + cl_git_write2file("win32-forbidden/aux.", "foo\n", 4, O_RDWR | O_CREAT, 0666); + +#ifdef GIT_WIN32 + cl_git_fail(git_index_add_bypath(index, "aux.")); +#else + cl_git_pass(git_index_add_bypath(index, "aux.")); +#endif + + cl_must_pass(p_unlink("win32-forbidden/aux.")); + git_index_free(index); +} + +static int dummy_submodule_cb( + git_submodule *sm, const char *name, void *payload) +{ + GIT_UNUSED(sm); + GIT_UNUSED(name); + GIT_UNUSED(payload); + return 0; +} + +void test_win32_forbidden__can_diff_tree_to_index(void) +{ + git_diff *diff; + git_tree *tree; + + cl_git_pass(git_repository_head_tree(&tree, repo)); + cl_git_pass(git_diff_tree_to_index(&diff, repo, tree, NULL, NULL)); + cl_assert_equal_i(0, git_diff_num_deltas(diff)); + git_diff_free(diff); + git_tree_free(tree); +} + +void test_win32_forbidden__can_diff_tree_to_tree(void) +{ + git_diff *diff; + git_tree *tree; + + cl_git_pass(git_repository_head_tree(&tree, repo)); + cl_git_pass(git_diff_tree_to_tree(&diff, repo, tree, tree, NULL)); + cl_assert_equal_i(0, git_diff_num_deltas(diff)); + git_diff_free(diff); + git_tree_free(tree); +} + +void test_win32_forbidden__can_diff_index_to_workdir(void) +{ + git_index *index; + git_diff *diff; + const git_diff_delta *delta; + git_tree *tree; + size_t i; + + cl_git_pass(git_repository_index(&index, repo)); + cl_git_pass(git_repository_head_tree(&tree, repo)); + cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL)); + + for (i = 0; i < git_diff_num_deltas(diff); i++) { + delta = git_diff_get_delta(diff, i); + cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); + } + + git_diff_free(diff); + git_tree_free(tree); + git_index_free(index); +} + +void test_win32_forbidden__checking_out_forbidden_index_fails(void) +{ +#ifdef GIT_WIN32 + git_index *index; + git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT; + git_diff *diff; + const git_diff_delta *delta; + git_tree *tree; + size_t num_deltas, i; + + opts.checkout_strategy = GIT_CHECKOUT_FORCE; + + cl_git_pass(git_repository_index(&index, repo)); + cl_git_fail(git_checkout_index(repo, index, &opts)); + + cl_git_pass(git_repository_head_tree(&tree, repo)); + cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL)); + + num_deltas = git_diff_num_deltas(diff); + + cl_assert(num_deltas > 0); + + for (i = 0; i < num_deltas; i++) { + delta = git_diff_get_delta(diff, i); + cl_assert_equal_i(GIT_DELTA_DELETED, delta->status); + } + + git_diff_free(diff); + git_tree_free(tree); + git_index_free(index); +#endif +} + +void test_win32_forbidden__can_query_submodules(void) +{ + cl_git_pass(git_submodule_foreach(repo, dummy_submodule_cb, NULL)); +} + +void test_win32_forbidden__can_blame_file(void) +{ + git_blame *blame; + + cl_git_pass(git_blame_file(&blame, repo, "aux", NULL)); + git_blame_free(blame); +}