diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/commit/write.c | 111 | ||||
-rw-r--r-- | tests/diff/index.c | 8 | ||||
-rw-r--r-- | tests/diff/workdir.c | 8 | ||||
-rw-r--r-- | tests/index/add.c | 84 | ||||
-rw-r--r-- | tests/index/bypath.c | 3 | ||||
-rw-r--r-- | tests/index/cache.c | 6 | ||||
-rw-r--r-- | tests/index/conflicts.c | 34 | ||||
-rw-r--r-- | tests/index/filemodes.c | 7 | ||||
-rw-r--r-- | tests/index/racy.c | 4 | ||||
-rw-r--r-- | tests/object/tree/write.c | 86 | ||||
-rw-r--r-- | tests/refs/create.c | 26 | ||||
-rw-r--r-- | tests/resources/status/.gitted/objects/2b/d0a343aeef7a2cf0d158478966a6e587ff3863 | bin | 0 -> 56 bytes | |||
-rw-r--r-- | tests/resources/status/.gitted/objects/d4/27e0b2e138501a3d15cc376077a3631e15bd46 | bin | 0 -> 38 bytes | |||
-rw-r--r-- | tests/resources/status/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf | bin | 0 -> 64 bytes | |||
-rw-r--r-- | tests/resources/testrepo/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf | bin | 0 -> 64 bytes |
15 files changed, 326 insertions, 51 deletions
diff --git a/tests/commit/write.c b/tests/commit/write.c index 176965cbd..96b7cc321 100644 --- a/tests/commit/write.c +++ b/tests/commit/write.c @@ -1,10 +1,12 @@ #include "clar_libgit2.h" +#include "git2/sys/commit.h" static const char *committer_name = "Vicent Marti"; static const char *committer_email = "vicent@github.com"; static const char *commit_message = "This commit has been created in memory\n\ This is a commit created in memory and it will be written back to disk\n"; -static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd"; +static const char *tree_id_str = "1810dff58d8a660512d4832e740f692884338ccd"; +static const char *parent_id_str = "8496071c1b46c854b31185ea97743be6a8774479"; static const char *root_commit_message = "This is a root commit\n\ This is a root commit and should be the only one in this branch\n"; static const char *root_reflog_message = "commit (initial): This is a root commit \ @@ -35,6 +37,8 @@ void test_commit_write__cleanup(void) head_old = NULL; cl_git_sandbox_cleanup(); + + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1)); } @@ -46,12 +50,11 @@ void test_commit_write__from_memory(void) const git_signature *author1, *committer1; git_commit *parent; git_tree *tree; - const char *commit_id_str = "8496071c1b46c854b31185ea97743be6a8774479"; - git_oid_fromstr(&tree_id, tree_oid); + git_oid_fromstr(&tree_id, tree_id_str); cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); - git_oid_fromstr(&parent_id, commit_id_str); + git_oid_fromstr(&parent_id, parent_id_str); cl_git_pass(git_commit_lookup(&parent, g_repo, &parent_id)); /* create signatures */ @@ -106,7 +109,7 @@ void test_commit_write__root(void) git_reflog *log; const git_reflog_entry *entry; - git_oid_fromstr(&tree_id, tree_oid); + git_oid_fromstr(&tree_id, tree_id_str); cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); /* create signatures */ @@ -158,3 +161,101 @@ void test_commit_write__root(void) git_signature_free(committer); git_reflog_free(log); } + +static int create_commit_from_ids( + git_oid *result, + const git_oid *tree_id, + const git_oid *parent_id) +{ + git_signature *author, *committer; + const git_oid *parent_ids[1]; + int ret; + + cl_git_pass(git_signature_new( + &committer, committer_name, committer_email, 123456789, 60)); + cl_git_pass(git_signature_new( + &author, committer_name, committer_email, 987654321, 90)); + + parent_ids[0] = parent_id; + + ret = git_commit_create_from_ids( + result, + g_repo, + NULL, + author, + committer, + NULL, + root_commit_message, + tree_id, + 1, + parent_ids); + + git_signature_free(committer); + git_signature_free(author); + + return ret; +} + +void test_commit_write__can_write_invalid_objects(void) +{ + git_oid expected_id, tree_id, parent_id, commit_id; + + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0)); + + /* this is a valid tree and parent */ + git_oid_fromstr(&tree_id, tree_id_str); + git_oid_fromstr(&parent_id, parent_id_str); + + git_oid_fromstr(&expected_id, "c8571bbec3a72c4bcad31648902e5a453f1adece"); + cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); + cl_assert_equal_oid(&expected_id, &commit_id); + + /* this is a wholly invented tree id */ + git_oid_fromstr(&tree_id, "1234567890123456789012345678901234567890"); + git_oid_fromstr(&parent_id, parent_id_str); + + git_oid_fromstr(&expected_id, "996008340b8e68d69bf3c28d7c57fb7ec3c8e202"); + cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); + cl_assert_equal_oid(&expected_id, &commit_id); + + /* this is a wholly invented parent id */ + git_oid_fromstr(&tree_id, tree_id_str); + git_oid_fromstr(&parent_id, "1234567890123456789012345678901234567890"); + + git_oid_fromstr(&expected_id, "d78f660cab89d9791ca6714b57978bf2a7e709fd"); + cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); + cl_assert_equal_oid(&expected_id, &commit_id); + + /* these are legitimate objects, but of the wrong type */ + git_oid_fromstr(&tree_id, parent_id_str); + git_oid_fromstr(&parent_id, tree_id_str); + + git_oid_fromstr(&expected_id, "5d80c07414e3f18792949699dfcacadf7748f361"); + cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); + cl_assert_equal_oid(&expected_id, &commit_id); +} + +void test_commit_write__can_validate_objects(void) +{ + git_oid tree_id, parent_id, commit_id; + + /* this is a valid tree and parent */ + git_oid_fromstr(&tree_id, tree_id_str); + git_oid_fromstr(&parent_id, parent_id_str); + cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); + + /* this is a wholly invented tree id */ + git_oid_fromstr(&tree_id, "1234567890123456789012345678901234567890"); + git_oid_fromstr(&parent_id, parent_id_str); + cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); + + /* this is a wholly invented parent id */ + git_oid_fromstr(&tree_id, tree_id_str); + git_oid_fromstr(&parent_id, "1234567890123456789012345678901234567890"); + cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); + + /* these are legitimate objects, but of the wrong type */ + git_oid_fromstr(&tree_id, parent_id_str); + git_oid_fromstr(&parent_id, tree_id_str); + cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id)); +} diff --git a/tests/diff/index.c b/tests/diff/index.c index df45ad236..0293b7821 100644 --- a/tests/diff/index.c +++ b/tests/diff/index.c @@ -185,9 +185,9 @@ static void do_conflicted_diff(diff_expects *exp, unsigned long flags) ancestor.path = ours.path = theirs.path = "staged_changes"; ancestor.mode = ours.mode = theirs.mode = GIT_FILEMODE_BLOB; - git_oid_fromstr(&ancestor.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); - git_oid_fromstr(&ours.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); - git_oid_fromstr(&theirs.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + git_oid_fromstr(&ancestor.id, "d427e0b2e138501a3d15cc376077a3631e15bd46"); + git_oid_fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"); + git_oid_fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863"); cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, &opts)); @@ -255,7 +255,7 @@ void test_diff_index__not_in_head_conflicted(void) theirs.path = "file_not_in_head"; theirs.mode = GIT_FILEMODE_BLOB; - git_oid_fromstr(&theirs.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + git_oid_fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863"); cl_git_pass(git_index_conflict_add(index, NULL, NULL, &theirs)); cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, NULL)); diff --git a/tests/diff/workdir.c b/tests/diff/workdir.c index 892c7b72d..e1bbce8fb 100644 --- a/tests/diff/workdir.c +++ b/tests/diff/workdir.c @@ -85,9 +85,11 @@ void test_diff_workdir__to_index_with_conflicts(void) /* Adding an entry that represents a rename gets two files in conflict */ our_entry.path = "subdir/modified_file"; our_entry.mode = 0100644; + git_oid_fromstr(&our_entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"); their_entry.path = "subdir/rename_conflict"; their_entry.mode = 0100644; + git_oid_fromstr(&their_entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863"); cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_index_conflict_add(index, NULL, &our_entry, &their_entry)); @@ -1975,9 +1977,9 @@ void test_diff_workdir__to_index_conflicted(void) { ancestor.path = ours.path = theirs.path = "_file"; ancestor.mode = ours.mode = theirs.mode = 0100644; - git_oid_fromstr(&ancestor.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); - git_oid_fromstr(&ours.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); - git_oid_fromstr(&theirs.id, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"); + git_oid_fromstr(&ancestor.id, "d427e0b2e138501a3d15cc376077a3631e15bd46"); + git_oid_fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"); + git_oid_fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863"); cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs)); cl_git_pass(git_diff_tree_to_index(&diff1, g_repo, a, index, NULL)); diff --git a/tests/index/add.c b/tests/index/add.c new file mode 100644 index 000000000..f101ea266 --- /dev/null +++ b/tests/index/add.c @@ -0,0 +1,84 @@ +#include "clar_libgit2.h" + +static git_repository *g_repo = NULL; +static git_index *g_index = NULL; + +static const char *valid_blob_id = "fa49b077972391ad58037050f2a75f74e3671e92"; +static const char *valid_tree_id = "181037049a54a1eb5fab404658a3a250b44335d7"; +static const char *valid_commit_id = "763d71aadf09a7951596c9746c024e7eece7c7af"; +static const char *invalid_id = "1234567890123456789012345678901234567890"; + +void test_index_add__initialize(void) +{ + g_repo = cl_git_sandbox_init("testrepo"); + cl_git_pass(git_repository_index(&g_index, g_repo)); +} + +void test_index_add__cleanup(void) +{ + git_index_free(g_index); + cl_git_sandbox_cleanup(); + g_repo = NULL; + + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1)); +} + +static void test_add_entry( + bool should_succeed, const char *idstr, git_filemode_t mode) +{ + git_index_entry entry = {{0}}; + + cl_git_pass(git_oid_fromstr(&entry.id, idstr)); + + entry.path = mode == GIT_FILEMODE_TREE ? "test_folder" : "test_file"; + entry.mode = mode; + + if (should_succeed) + cl_git_pass(git_index_add(g_index, &entry)); + else + cl_git_fail(git_index_add(g_index, &entry)); +} + +void test_index_add__invalid_entries_succeeds_by_default(void) +{ + /* + * Ensure that there is validation on object ids by default + */ + + /* ensure that we can add some actually good entries */ + test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB); + test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB_EXECUTABLE); + test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK); + + /* test that we fail to add some invalid (missing) blobs and trees */ + test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB); + test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE); + test_add_entry(false, invalid_id, GIT_FILEMODE_LINK); + + /* test that we validate the types of objects */ + test_add_entry(false, valid_commit_id, GIT_FILEMODE_BLOB); + test_add_entry(false, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE); + test_add_entry(false, valid_commit_id, GIT_FILEMODE_LINK); + + /* + * Ensure that there we can disable validation + */ + + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0)); + + /* ensure that we can add some actually good entries */ + test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB); + test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB_EXECUTABLE); + test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK); + + /* test that we can now add some invalid (missing) blobs and trees */ + test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB); + test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE); + test_add_entry(true, invalid_id, GIT_FILEMODE_LINK); + + /* test that we do not validate the types of objects */ + test_add_entry(true, valid_commit_id, GIT_FILEMODE_BLOB); + test_add_entry(true, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE); + test_add_entry(true, valid_commit_id, GIT_FILEMODE_LINK); +} + diff --git a/tests/index/bypath.c b/tests/index/bypath.c index 88a76178a..34a7412a8 100644 --- a/tests/index/bypath.c +++ b/tests/index/bypath.c @@ -134,6 +134,7 @@ void test_index_bypath__add_honors_existing_case_2(void) clar__skip(); dummy.mode = GIT_FILEMODE_BLOB; + cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b")); /* note that `git_index_add` does no checking to canonical directories */ dummy.path = "Just_a_dir/file0.txt"; @@ -189,6 +190,7 @@ void test_index_bypath__add_honors_existing_case_3(void) clar__skip(); dummy.mode = GIT_FILEMODE_BLOB; + cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b")); dummy.path = "just_a_dir/filea.txt"; cl_git_pass(git_index_add(g_idx, &dummy)); @@ -219,6 +221,7 @@ void test_index_bypath__add_honors_existing_case_4(void) clar__skip(); dummy.mode = GIT_FILEMODE_BLOB; + cl_git_pass(git_oid_fromstr(&dummy.id, "f990a25a74d1a8281ce2ab018ea8df66795cd60b")); dummy.path = "just_a_dir/a/b/c/d/e/file1.txt"; cl_git_pass(git_index_add(g_idx, &dummy)); diff --git a/tests/index/cache.c b/tests/index/cache.c index 3982bf183..56885aff7 100644 --- a/tests/index/cache.c +++ b/tests/index/cache.c @@ -111,7 +111,7 @@ void test_index_cache__read_tree_no_children(void) memset(&entry, 0x0, sizeof(git_index_entry)); entry.path = "new.txt"; entry.mode = GIT_FILEMODE_BLOB; - git_oid_fromstr(&entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); + git_oid_fromstr(&entry.id, "d4bcc68acd4410bf836a39f20afb2c2ece09584e"); cl_git_pass(git_index_add(index, &entry)); cl_assert_equal_i(-1, index->tree->entry_count); @@ -191,7 +191,7 @@ void test_index_cache__read_tree_children(void) memset(&entry, 0x0, sizeof(git_index_entry)); entry.path = "top-level"; entry.mode = GIT_FILEMODE_BLOB; - git_oid_fromstr(&entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057"); + git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"); cl_git_pass(git_index_add(index, &entry)); @@ -217,7 +217,7 @@ void test_index_cache__read_tree_children(void) /* override with a slightly different id, also dummy */ entry.path = "subdir/some-file"; - git_oid_fromstr(&entry.id, "45b983be36b73c0788dc9cbcb76cbb80fc7bb058"); + git_oid_fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf"); cl_git_pass(git_index_add(index, &entry)); cl_assert_equal_i(-1, index->tree->entry_count); diff --git a/tests/index/conflicts.c b/tests/index/conflicts.c index 8e94cd441..d4004686f 100644 --- a/tests/index/conflicts.c +++ b/tests/index/conflicts.c @@ -16,11 +16,6 @@ static git_index *repo_index; #define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2" #define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e" -#define TEST_STAGED_OID "beefdadafeedabedcafedeedbabedeadbeaddeaf" -#define TEST_ANCESTOR_OID "f00ff00ff00ff00ff00ff00ff00ff00ff00ff00f" -#define TEST_OUR_OID "b44bb44bb44bb44bb44bb44bb44bb44bb44bb44b" -#define TEST_THEIR_OID "0123456789abcdef0123456789abcdef01234567" - // Fixture setup and teardown void test_index_conflicts__initialize(void) { @@ -49,17 +44,17 @@ void test_index_conflicts__add(void) ancestor_entry.path = "test-one.txt"; ancestor_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 1); - git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID); + git_oid_fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID); our_entry.path = "test-one.txt"; our_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&our_entry, 2); - git_oid_fromstr(&our_entry.id, TEST_OUR_OID); + git_oid_fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID); their_entry.path = "test-one.txt"; their_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 2); - git_oid_fromstr(&their_entry.id, TEST_THEIR_OID); + git_oid_fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID); cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); @@ -80,17 +75,17 @@ void test_index_conflicts__add_fixes_incorrect_stage(void) ancestor_entry.path = "test-one.txt"; ancestor_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 3); - git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID); + git_oid_fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID); our_entry.path = "test-one.txt"; our_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&our_entry, 1); - git_oid_fromstr(&our_entry.id, TEST_OUR_OID); + git_oid_fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID); their_entry.path = "test-one.txt"; their_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&their_entry, 2); - git_oid_fromstr(&their_entry.id, TEST_THEIR_OID); + git_oid_fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID); cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); @@ -105,36 +100,33 @@ void test_index_conflicts__add_fixes_incorrect_stage(void) void test_index_conflicts__add_removes_stage_zero(void) { - git_index_entry staged, ancestor_entry, our_entry, their_entry; + git_index_entry ancestor_entry, our_entry, their_entry; const git_index_entry *conflict_entry[3]; cl_assert(git_index_entrycount(repo_index) == 8); - memset(&staged, 0x0, sizeof(git_index_entry)); memset(&ancestor_entry, 0x0, sizeof(git_index_entry)); memset(&our_entry, 0x0, sizeof(git_index_entry)); memset(&their_entry, 0x0, sizeof(git_index_entry)); - staged.path = "test-one.txt"; - staged.mode = 0100644; - git_oid_fromstr(&staged.id, TEST_STAGED_OID); - cl_git_pass(git_index_add(repo_index, &staged)); + cl_git_mkfile("./mergedrepo/test-one.txt", "new-file\n"); + cl_git_pass(git_index_add_bypath(repo_index, "test-one.txt")); cl_assert(git_index_entrycount(repo_index) == 9); ancestor_entry.path = "test-one.txt"; ancestor_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 3); - git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID); + git_oid_fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID); our_entry.path = "test-one.txt"; our_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&our_entry, 1); - git_oid_fromstr(&our_entry.id, TEST_OUR_OID); + git_oid_fromstr(&our_entry.id, CONFLICTS_ONE_OUR_OID); their_entry.path = "test-one.txt"; their_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&their_entry, 2); - git_oid_fromstr(&their_entry.id, TEST_THEIR_OID); + git_oid_fromstr(&their_entry.id, CONFLICTS_ONE_THEIR_OID); cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry)); @@ -330,7 +322,7 @@ void test_index_conflicts__partial(void) ancestor_entry.path = "test-one.txt"; ancestor_entry.mode = 0100644; GIT_IDXENTRY_STAGE_SET(&ancestor_entry, 1); - git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID); + git_oid_fromstr(&ancestor_entry.id, CONFLICTS_ONE_ANCESTOR_OID); cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL)); cl_assert(git_index_entrycount(repo_index) == 9); diff --git a/tests/index/filemodes.c b/tests/index/filemodes.c index 6442d7755..2efad5b33 100644 --- a/tests/index/filemodes.c +++ b/tests/index/filemodes.c @@ -236,12 +236,19 @@ void test_index_filemodes__invalid(void) { git_index *index; git_index_entry entry; + const git_index_entry *dummy; cl_git_pass(git_repository_index(&index, g_repo)); + /* add a dummy file so that we have a valid id */ + cl_git_mkfile("./filemodes/dummy-file.txt", "new-file\n"); + cl_git_pass(git_index_add_bypath(index, "dummy-file.txt")); + cl_assert((dummy = git_index_get_bypath(index, "dummy-file.txt", 0))); + GIT_IDXENTRY_STAGE_SET(&entry, 0); entry.path = "foo"; entry.mode = GIT_OBJ_BLOB; + git_oid_cpy(&entry.id, &dummy->id); cl_git_fail(git_index_add(index, &entry)); entry.mode = GIT_FILEMODE_BLOB; diff --git a/tests/index/racy.c b/tests/index/racy.c index ace84d585..68aa46007 100644 --- a/tests/index/racy.c +++ b/tests/index/racy.c @@ -178,6 +178,7 @@ static void setup_uptodate_files(void) { git_buf path = GIT_BUF_INIT; git_index *index; + const git_index_entry *a_entry; git_index_entry new_entry = {{0}}; cl_git_pass(git_repository_index(&index, g_repo)); @@ -188,9 +189,12 @@ static void setup_uptodate_files(void) /* Put 'A' into the index */ cl_git_pass(git_index_add_bypath(index, "A")); + cl_assert((a_entry = git_index_get_bypath(index, "A", 0))); + /* Put 'B' into the index */ new_entry.path = "B"; new_entry.mode = GIT_FILEMODE_BLOB; + git_oid_cpy(&new_entry.id, &a_entry->id); cl_git_pass(git_index_add(index, &new_entry)); /* Put 'C' into the index */ diff --git a/tests/object/tree/write.c b/tests/object/tree/write.c index 5433e5f03..341f5db72 100644 --- a/tests/object/tree/write.c +++ b/tests/object/tree/write.c @@ -18,6 +18,8 @@ void test_object_tree_write__initialize(void) void test_object_tree_write__cleanup(void) { cl_git_sandbox_cleanup(); + + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1)); } void test_object_tree_write__from_memory(void) @@ -131,15 +133,18 @@ void test_object_tree_write__sorted_subtrees(void) { GIT_FILEMODE_TREE, "vendors"} }; - git_oid blank_oid, tree_oid; + git_oid bid, tid, tree_oid; - memset(&blank_oid, 0x0, sizeof(blank_oid)); + cl_git_pass(git_oid_fromstr(&bid, blob_oid)); + cl_git_pass(git_oid_fromstr(&tid, first_tree)); cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL)); for (i = 0; i < ARRAY_SIZE(entries); ++i) { + git_oid *id = entries[i].attr == GIT_FILEMODE_TREE ? &tid : &bid; + cl_git_pass(git_treebuilder_insert(NULL, - builder, entries[i].filename, &blank_oid, entries[i].attr)); + builder, entries[i].filename, id, entries[i].attr)); } cl_git_pass(git_treebuilder_write(&tree_oid, builder)); @@ -187,10 +192,10 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void) { git_treebuilder *builder; int i, aardvark_i, apple_i, apple_after_i, apple_extra_i, last_i; - git_oid blank_oid, tree_oid; + git_oid entry_oid, tree_oid; git_tree *tree; - memset(&blank_oid, 0x0, sizeof(blank_oid)); + cl_git_pass(git_oid_fromstr(&entry_oid, blob_oid)); cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL)); @@ -198,7 +203,7 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void) for (i = 0; _entries[i].filename; ++i) cl_git_pass(git_treebuilder_insert(NULL, - builder, _entries[i].filename, &blank_oid, _entries[i].attr)); + builder, _entries[i].filename, &entry_oid, _entries[i].attr)); cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder)); @@ -209,12 +214,12 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void) cl_assert_equal_i(4, (int)git_treebuilder_entrycount(builder)); cl_git_pass(git_treebuilder_insert( - NULL, builder, "before_last", &blank_oid, GIT_FILEMODE_BLOB)); + NULL, builder, "before_last", &entry_oid, GIT_FILEMODE_BLOB)); cl_assert_equal_i(5, (int)git_treebuilder_entrycount(builder)); /* reinsert apple_after */ cl_git_pass(git_treebuilder_insert( - NULL, builder, "apple_after", &blank_oid, GIT_FILEMODE_BLOB)); + NULL, builder, "apple_after", &entry_oid, GIT_FILEMODE_BLOB)); cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder)); cl_git_pass(git_treebuilder_remove(builder, "last")); @@ -222,11 +227,11 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void) /* reinsert last */ cl_git_pass(git_treebuilder_insert( - NULL, builder, "last", &blank_oid, GIT_FILEMODE_BLOB)); + NULL, builder, "last", &entry_oid, GIT_FILEMODE_BLOB)); cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder)); cl_git_pass(git_treebuilder_insert( - NULL, builder, "apple_extra", &blank_oid, GIT_FILEMODE_BLOB)); + NULL, builder, "apple_extra", &entry_oid, GIT_FILEMODE_BLOB)); cl_assert_equal_i(7, (int)git_treebuilder_entrycount(builder)); cl_git_pass(git_treebuilder_write(&tree_oid, builder)); @@ -278,16 +283,16 @@ void test_object_tree_write__filtering(void) { git_treebuilder *builder; int i; - git_oid blank_oid, tree_oid; + git_oid entry_oid, tree_oid; git_tree *tree; - memset(&blank_oid, 0x0, sizeof(blank_oid)); + git_oid_fromstr(&entry_oid, blob_oid); cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL)); for (i = 0; _entries[i].filename; ++i) cl_git_pass(git_treebuilder_insert(NULL, - builder, _entries[i].filename, &blank_oid, _entries[i].attr)); + builder, _entries[i].filename, &entry_oid, _entries[i].attr)); cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder)); @@ -406,6 +411,8 @@ void test_object_tree_write__protect_filesystems(void) git_treebuilder *builder; git_oid bid; + cl_git_pass(git_oid_fromstr(&bid, "fa49b077972391ad58037050f2a75f74e3671e92")); + /* Ensure that (by default) we can write objects with funny names on * platforms that are not affected. */ @@ -440,3 +447,56 @@ void test_object_tree_write__protect_filesystems(void) git_treebuilder_free(builder); } + +static void test_invalid_objects(bool should_allow_invalid) +{ + git_treebuilder *builder; + git_oid valid_blob_id, invalid_blob_id, valid_tree_id, invalid_tree_id; + +#define assert_allowed(expr) \ + clar__assert(!(expr) == should_allow_invalid, __FILE__, __LINE__, \ + (should_allow_invalid ? \ + "Expected function call to succeed: " #expr : \ + "Expected function call to fail: " #expr), \ + NULL, 1) + + cl_git_pass(git_oid_fromstr(&valid_blob_id, blob_oid)); + cl_git_pass(git_oid_fromstr(&invalid_blob_id, + "1234567890123456789012345678901234567890")); + cl_git_pass(git_oid_fromstr(&valid_tree_id, first_tree)); + cl_git_pass(git_oid_fromstr(&invalid_tree_id, + "0000000000111111111122222222223333333333")); + + cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL)); + + /* test valid blobs and trees (these should always pass) */ + cl_git_pass(git_treebuilder_insert(NULL, builder, "file.txt", &valid_blob_id, GIT_FILEMODE_BLOB)); + cl_git_pass(git_treebuilder_insert(NULL, builder, "folder", &valid_tree_id, GIT_FILEMODE_TREE)); + + /* replace valid files and folders with invalid ones */ + assert_allowed(git_treebuilder_insert(NULL, builder, "file.txt", &invalid_blob_id, GIT_FILEMODE_BLOB)); + assert_allowed(git_treebuilder_insert(NULL, builder, "folder", &invalid_blob_id, GIT_FILEMODE_BLOB)); + + /* insert new invalid files and folders */ + assert_allowed(git_treebuilder_insert(NULL, builder, "invalid_file.txt", &invalid_blob_id, GIT_FILEMODE_BLOB)); + assert_allowed(git_treebuilder_insert(NULL, builder, "invalid_folder", &invalid_blob_id, GIT_FILEMODE_BLOB)); + + /* insert valid blobs as trees and trees as blobs */ + assert_allowed(git_treebuilder_insert(NULL, builder, "file_as_folder", &valid_blob_id, GIT_FILEMODE_TREE)); + assert_allowed(git_treebuilder_insert(NULL, builder, "folder_as_file.txt", &valid_tree_id, GIT_FILEMODE_BLOB)); + +#undef assert_allowed + + git_treebuilder_free(builder); +} + +void test_object_tree_write__object_validity(void) +{ + /* Ensure that we cannot add invalid objects by default */ + test_invalid_objects(false); + + /* Ensure that we can turn off validation */ + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0)); + test_invalid_objects(true); +} + diff --git a/tests/refs/create.c b/tests/refs/create.c index 48194ae3b..a0bc78014 100644 --- a/tests/refs/create.c +++ b/tests/refs/create.c @@ -18,6 +18,8 @@ void test_refs_create__initialize(void) void test_refs_create__cleanup(void) { cl_git_sandbox_cleanup(); + + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0)); } void test_refs_create__symbolic(void) @@ -119,9 +121,27 @@ void test_refs_create__oid(void) git_reference_free(looked_up_ref); } -void test_refs_create__oid_unknown(void) +/* Can by default create a reference that targets at an unknown id */ +void test_refs_create__oid_unknown_succeeds_by_default(void) +{ + git_reference *new_reference, *looked_up_ref; + git_oid id; + + const char *new_head = "refs/heads/new-head"; + + git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644"); + + /* Create and write the new object id reference */ + cl_git_pass(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL)); + + /* Ensure the reference can't be looked-up... */ + cl_git_pass(git_reference_lookup(&looked_up_ref, g_repo, new_head)); + git_reference_free(looked_up_ref); +} + +/* Strict object enforcement enforces valid object id */ +void test_refs_create__oid_unknown_fails_strict_mode(void) { - // Can not create a new OID reference which targets at an unknown id git_reference *new_reference, *looked_up_ref; git_oid id; @@ -129,6 +149,8 @@ void test_refs_create__oid_unknown(void) git_oid_fromstr(&id, "deadbeef3f795b2b4353bcce3a527ad0a4f7f644"); + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1)); + /* Create and write the new object id reference */ cl_git_fail(git_reference_create(&new_reference, g_repo, new_head, &id, 0, NULL)); diff --git a/tests/resources/status/.gitted/objects/2b/d0a343aeef7a2cf0d158478966a6e587ff3863 b/tests/resources/status/.gitted/objects/2b/d0a343aeef7a2cf0d158478966a6e587ff3863 Binary files differnew file mode 100644 index 000000000..d10ca636b --- /dev/null +++ b/tests/resources/status/.gitted/objects/2b/d0a343aeef7a2cf0d158478966a6e587ff3863 diff --git a/tests/resources/status/.gitted/objects/d4/27e0b2e138501a3d15cc376077a3631e15bd46 b/tests/resources/status/.gitted/objects/d4/27e0b2e138501a3d15cc376077a3631e15bd46 Binary files differnew file mode 100644 index 000000000..0b3611ae4 --- /dev/null +++ b/tests/resources/status/.gitted/objects/d4/27e0b2e138501a3d15cc376077a3631e15bd46 diff --git a/tests/resources/status/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf b/tests/resources/status/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf Binary files differnew file mode 100644 index 000000000..974b72dfd --- /dev/null +++ b/tests/resources/status/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf diff --git a/tests/resources/testrepo/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf b/tests/resources/testrepo/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf Binary files differnew file mode 100644 index 000000000..974b72dfd --- /dev/null +++ b/tests/resources/testrepo/.gitted/objects/ee/3fa1b8c00aff7fe02065fdb50864bb0d932ccf |