diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2022-09-21 05:09:46 -0400 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-02-12 22:02:00 +0000 |
| commit | fe2ee3a018286b04cd0c64f84d437c7317c8f138 (patch) | |
| tree | 196a29fc72ef0a27e0277b4d142cc93c28fa4cb7 /tests | |
| parent | 6204499242101d0f93cd7e56e259c900828e41e1 (diff) | |
| download | libgit2-fe2ee3a018286b04cd0c64f84d437c7317c8f138.tar.gz | |
object: lookup sha256 objects
This is much of the plumbing for the object database to support SHA256,
and for objects to be able to parse SHA256 versions of themselves.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/libgit2/commit/parse.c | 2 | ||||
| -rw-r--r-- | tests/libgit2/object/commit/parse.c | 298 | ||||
| -rw-r--r-- | tests/libgit2/object/lookup256.c | 153 | ||||
| -rw-r--r-- | tests/libgit2/object/tag/parse.c | 4 | ||||
| -rw-r--r-- | tests/libgit2/object/tree/parse.c | 4 | ||||
| -rw-r--r-- | tests/libgit2/object/validate.c | 154 | ||||
| -rw-r--r-- | tests/libgit2/pack/indexer.c | 53 | ||||
| -rw-r--r-- | tests/libgit2/pack/packbuilder.c | 17 |
8 files changed, 626 insertions, 59 deletions
diff --git a/tests/libgit2/commit/parse.c b/tests/libgit2/commit/parse.c index b313fc308..3a1fc3d26 100644 --- a/tests/libgit2/commit/parse.c +++ b/tests/libgit2/commit/parse.c @@ -287,7 +287,7 @@ static int parse_commit(git_commit **out, const char *buffer) fake_odb_object.buffer = (char *)buffer; fake_odb_object.cached.size = strlen(fake_odb_object.buffer); - error = git_commit__parse(commit, &fake_odb_object); + error = git_commit__parse(commit, &fake_odb_object, GIT_OID_SHA1); *out = commit; return error; diff --git a/tests/libgit2/object/commit/parse.c b/tests/libgit2/object/commit/parse.c index 28a3ccd85..6f9a65507 100644 --- a/tests/libgit2/object/commit/parse.c +++ b/tests/libgit2/object/commit/parse.c @@ -3,7 +3,10 @@ #include "object.h" #include "signature.h" -static void assert_commit_parses(const char *data, size_t datalen, +static void assert_commit_parses( + const char *data, + size_t datalen, + git_oid_t oid_type, const char *expected_treeid, const char *expected_author, const char *expected_committer, @@ -14,7 +17,7 @@ static void assert_commit_parses(const char *data, size_t datalen, git_commit *commit; if (!datalen) datalen = strlen(data); - cl_git_pass(git_object__from_raw((git_object **) &commit, data, datalen, GIT_OBJECT_COMMIT)); + cl_git_pass(git_object__from_raw((git_object **) &commit, data, datalen, GIT_OBJECT_COMMIT, oid_type)); if (expected_author) { git_signature *author; @@ -51,7 +54,7 @@ static void assert_commit_parses(const char *data, size_t datalen, if (expected_treeid) { git_oid tree_oid; - cl_git_pass(git_oid__fromstr(&tree_oid, expected_treeid, GIT_OID_SHA1)); + cl_git_pass(git_oid__fromstr(&tree_oid, expected_treeid, oid_type)); cl_assert_equal_oid(&tree_oid, &commit->tree_id); } @@ -60,15 +63,18 @@ static void assert_commit_parses(const char *data, size_t datalen, git_object__free(&commit->object); } -static void assert_commit_fails(const char *data, size_t datalen) +static void assert_commit_fails( + const char *data, + size_t datalen, + git_oid_t oid_type) { git_object *object; if (!datalen) datalen = strlen(data); - cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_COMMIT)); + cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_COMMIT, oid_type)); } -void test_object_commit_parse__parsing_commit_succeeds(void) +void test_object_commit_parse__sha1_parsing_commit_succeeds(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" @@ -77,7 +83,7 @@ void test_object_commit_parse__parsing_commit_succeeds(void) "encoding Encoding\n" "\n" "Message"; - assert_commit_parses(commit, 0, + assert_commit_parses(commit, 0, GIT_OID_SHA1, "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8", "Author <author@example.com>", "Committer <committer@example.com>", @@ -85,7 +91,7 @@ void test_object_commit_parse__parsing_commit_succeeds(void) "Message", 0); } -void test_object_commit_parse__parsing_commit_without_encoding_succeeds(void) +void test_object_commit_parse__sha1_parsing_commit_without_encoding_succeeds(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" @@ -93,7 +99,7 @@ void test_object_commit_parse__parsing_commit_without_encoding_succeeds(void) "committer Committer <committer@example.com>\n" "\n" "Message"; - assert_commit_parses(commit, 0, + assert_commit_parses(commit, 0, GIT_OID_SHA1, "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8", "Author <author@example.com>", "Committer <committer@example.com>", @@ -101,7 +107,7 @@ void test_object_commit_parse__parsing_commit_without_encoding_succeeds(void) "Message", 0); } -void test_object_commit_parse__parsing_commit_with_multiple_authors_succeeds(void) +void test_object_commit_parse__sha1_parsing_commit_with_multiple_authors_succeeds(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" @@ -112,7 +118,7 @@ void test_object_commit_parse__parsing_commit_with_multiple_authors_succeeds(voi "committer Committer <committer@example.com>\n" "\n" "Message"; - assert_commit_parses(commit, 0, + assert_commit_parses(commit, 0, GIT_OID_SHA1, "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8", "Author1 <author@example.com>", "Committer <committer@example.com>", @@ -120,7 +126,7 @@ void test_object_commit_parse__parsing_commit_with_multiple_authors_succeeds(voi "Message", 0); } -void test_object_commit_parse__parsing_commit_with_multiple_committers_succeeds(void) +void test_object_commit_parse__sha1_parsing_commit_with_multiple_committers_succeeds(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" @@ -131,7 +137,7 @@ void test_object_commit_parse__parsing_commit_with_multiple_committers_succeeds( "committer Committer4 <committer@example.com>\n" "\n" "Message"; - assert_commit_parses(commit, 0, + assert_commit_parses(commit, 0, GIT_OID_SHA1, "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8", "Author <author@example.com>", "Committer1 <committer@example.com>", @@ -139,13 +145,13 @@ void test_object_commit_parse__parsing_commit_with_multiple_committers_succeeds( "Message", 0); } -void test_object_commit_parse__parsing_commit_without_message_succeeds(void) +void test_object_commit_parse__sha1_parsing_commit_without_message_succeeds(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" "author Author <author@example.com>\n" "committer Committer <committer@example.com>\n"; - assert_commit_parses(commit, 0, + assert_commit_parses(commit, 0, GIT_OID_SHA1, "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8", "Author <author@example.com>", "Committer <committer@example.com>", @@ -153,7 +159,7 @@ void test_object_commit_parse__parsing_commit_without_message_succeeds(void) "", 0); } -void test_object_commit_parse__parsing_commit_with_unknown_fields_succeeds(void) +void test_object_commit_parse__sha1_parsing_commit_with_unknown_fields_succeeds(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" @@ -163,7 +169,7 @@ void test_object_commit_parse__parsing_commit_with_unknown_fields_succeeds(void) "more garbage\n" "\n" "Message"; - assert_commit_parses(commit, 0, + assert_commit_parses(commit, 0, GIT_OID_SHA1, "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8", "Author <author@example.com>", "Committer <committer@example.com>", @@ -171,7 +177,7 @@ void test_object_commit_parse__parsing_commit_with_unknown_fields_succeeds(void) "Message", 0); } -void test_object_commit_parse__parsing_commit_with_invalid_tree_fails(void) +void test_object_commit_parse__sha1_parsing_commit_with_invalid_tree_fails(void) { const char *commit = "tree 3e7ac388cadacccdf1xxx5f3445895b71d9cb0f8\n" @@ -179,40 +185,51 @@ void test_object_commit_parse__parsing_commit_with_invalid_tree_fails(void) "committer Committer <committer@example.com>\n" "\n" "Message"; - assert_commit_fails(commit, 0); + assert_commit_fails(commit, 0, GIT_OID_SHA1); } -void test_object_commit_parse__parsing_commit_without_tree_fails(void) +void test_object_commit_parse__sha1_parsing_commit_with_sha256_tree_fails(void) { const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" "author Author <author@example.com>\n" "committer Committer <committer@example.com>\n" "\n" "Message"; - assert_commit_fails(commit, 0); + assert_commit_fails(commit, 0, GIT_OID_SHA1); } -void test_object_commit_parse__parsing_commit_without_author_fails(void) +void test_object_commit_parse__sha1_parsing_commit_without_tree_fails(void) +{ + const char *commit = + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "\n" + "Message"; + assert_commit_fails(commit, 0, GIT_OID_SHA1); +} + +void test_object_commit_parse__sha1_parsing_commit_without_author_fails(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" "committer Committer <committer@example.com>\n" "\n" "Message"; - assert_commit_fails(commit, 0); + assert_commit_fails(commit, 0, GIT_OID_SHA1); } -void test_object_commit_parse__parsing_commit_without_committer_fails(void) +void test_object_commit_parse__sha1_parsing_commit_without_committer_fails(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" "author Author <author@example.com>\n" "\n" "Message"; - assert_commit_fails(commit, 0); + assert_commit_fails(commit, 0, GIT_OID_SHA1); } -void test_object_commit_parse__parsing_encoding_will_not_cause_oob_read(void) +void test_object_commit_parse__sha1_parsing_encoding_will_not_cause_oob_read(void) { const char *commit = "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" @@ -223,10 +240,237 @@ void test_object_commit_parse__parsing_encoding_will_not_cause_oob_read(void) * As we ignore unknown fields, the cut-off encoding field will be * parsed just fine. */ - assert_commit_parses(commit, strlen(commit) - strlen("ncoding foo\n"), + assert_commit_parses( + commit, strlen(commit) - strlen("ncoding foo\n"), + GIT_OID_SHA1, "3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8", "<>", "<>", NULL, "", 0); } + + +void test_object_commit_parse__sha256_parsing_commit_succeeds(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "encoding Encoding\n" + "\n" + "Message"; + assert_commit_parses(commit, 0, GIT_OID_SHA256, + "f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736", + "Author <author@example.com>", + "Committer <committer@example.com>", + "Encoding", + "Message", 0); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_without_encoding_succeeds(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "\n" + "Message"; + assert_commit_parses(commit, 0, GIT_OID_SHA256, + "f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736", + "Author <author@example.com>", + "Committer <committer@example.com>", + NULL, + "Message", 0); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_with_multiple_authors_succeeds(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author1 <author@example.com>\n" + "author Author2 <author@example.com>\n" + "author Author3 <author@example.com>\n" + "author Author4 <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "\n" + "Message"; + assert_commit_parses(commit, 0, GIT_OID_SHA256, + "f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736", + "Author1 <author@example.com>", + "Committer <committer@example.com>", + NULL, + "Message", 0); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_with_multiple_committers_succeeds(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author <author@example.com>\n" + "committer Committer1 <committer@example.com>\n" + "committer Committer2 <committer@example.com>\n" + "committer Committer3 <committer@example.com>\n" + "committer Committer4 <committer@example.com>\n" + "\n" + "Message"; + assert_commit_parses(commit, 0, GIT_OID_SHA256, + "f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736", + "Author <author@example.com>", + "Committer1 <committer@example.com>", + NULL, + "Message", 0); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_without_message_succeeds(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n"; + assert_commit_parses(commit, 0, GIT_OID_SHA256, + "f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736", + "Author <author@example.com>", + "Committer <committer@example.com>", + NULL, + "", 0); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_with_unknown_fields_succeeds(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "foo bar\n" + "more garbage\n" + "\n" + "Message"; + assert_commit_parses(commit, 0, GIT_OID_SHA256, + "f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736", + "Author <author@example.com>", + "Committer <committer@example.com>", + NULL, + "Message", 0); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_with_invalid_tree_fails(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9adxxxd55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "\n" + "Message"; + assert_commit_fails(commit, 0, GIT_OID_SHA256); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_with_sha1_tree_fails(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n" + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "\n" + "Message"; + assert_commit_fails(commit, 0, GIT_OID_SHA256); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_without_tree_fails(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "author Author <author@example.com>\n" + "committer Committer <committer@example.com>\n" + "\n" + "Message"; + assert_commit_fails(commit, 0, GIT_OID_SHA256); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_without_author_fails(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "committer Committer <committer@example.com>\n" + "\n" + "Message"; + assert_commit_fails(commit, 0, GIT_OID_SHA256); +#endif +} + +void test_object_commit_parse__sha256_parsing_commit_without_committer_fails(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author Author <author@example.com>\n" + "\n" + "Message"; + assert_commit_fails(commit, 0, GIT_OID_SHA256); +#endif +} + +void test_object_commit_parse__sha256_parsing_encoding_will_not_cause_oob_read(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = + "tree f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736\n" + "author <>\n" + "committer <>\n" + "encoding foo\n"; + /* + * As we ignore unknown fields, the cut-off encoding field will be + * parsed just fine. + */ + assert_commit_parses( + commit, strlen(commit) - strlen("ncoding foo\n"), + GIT_OID_SHA256, + "f2a108f86a3b4fd9ad75ed55e9cb3cb46e348fca3b9dba3db64f7c9f64b8a736", + "<>", + "<>", + NULL, + "", 0); +#endif +} diff --git a/tests/libgit2/object/lookup256.c b/tests/libgit2/object/lookup256.c new file mode 100644 index 000000000..3e1dab661 --- /dev/null +++ b/tests/libgit2/object/lookup256.c @@ -0,0 +1,153 @@ +#include "clar_libgit2.h" + +#include "repository.h" + +#ifdef GIT_EXPERIMENTAL_SHA256 +static git_repository *g_repo; +#endif + +void test_object_lookup256__initialize(void) +{ +#ifdef GIT_EXPERIMENTAL_SHA256 + g_repo = cl_git_sandbox_init("testrepo_256.git"); +#endif +} + +void test_object_lookup256__cleanup(void) +{ +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_sandbox_cleanup(); +#endif +} + +void test_object_lookup256__lookup_wrong_type_returns_enotfound(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = "4d46d9719e425ef2dfb5bfba098d0b62e21b2b92d0731892eef70db0870e3744"; + git_oid oid; + git_object *object; + + cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA256)); + cl_assert_equal_i( + GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG)); +#endif +} + +void test_object_lookup256__lookup_nonexisting_returns_enotfound(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *unknown = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; + git_oid oid; + git_object *object; + + cl_git_pass(git_oid__fromstr(&oid, unknown, GIT_OID_SHA256)); + cl_assert_equal_i( + GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_ANY)); +#endif +} + +void test_object_lookup256__lookup_wrong_type_by_abbreviated_id_returns_enotfound(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = "4d46d97"; + git_oid oid; + git_object *object; + + cl_git_pass(git_oid__fromstrn(&oid, commit, strlen(commit), GIT_OID_SHA256)); + cl_assert_equal_i( + GIT_ENOTFOUND, git_object_lookup_prefix(&object, g_repo, &oid, strlen(commit), GIT_OBJECT_TAG)); +#endif +} + +void test_object_lookup256__lookup_wrong_type_eventually_returns_enotfound(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = "4d46d9719e425ef2dfb5bfba098d0b62e21b2b92d0731892eef70db0870e3744"; + git_oid oid; + git_object *object; + + cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA256)); + + cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT)); + git_object_free(object); + + cl_assert_equal_i( + GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG)); +#endif +} + +void test_object_lookup256__lookup_corrupt_object_returns_error(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *commit = "5ca8959deb2b8327458e0344523eb1ddeeef4bce03e35864640b452f84d26848", + *file = "objects/5c/a8959deb2b8327458e0344523eb1ddeeef4bce03e35864640b452f84d26848"; + git_str path = GIT_STR_INIT, contents = GIT_STR_INIT; + git_oid oid; + git_object *object; + size_t i; + + cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA256)); + cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), file)); + cl_git_pass(git_futils_readbuffer(&contents, path.ptr)); + + /* Corrupt and try to read the object */ + for (i = 0; i < contents.size; i++) { + contents.ptr[i] ^= 0x1; + cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644)); + cl_git_fail(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT)); + contents.ptr[i] ^= 0x1; + } + + /* Restore original content and assert we can read the object */ + cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644)); + cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT)); + + git_object_free(object); + git_str_dispose(&path); + git_str_dispose(&contents); +#endif +} + +void test_object_lookup256__lookup_object_with_wrong_hash_returns_error(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + const char *oldloose = "objects/5c/a8959deb2b8327458e0344523eb1ddeeef4bce03e35864640b452f84d26848", + *newloose = "objects/5c/a8959deb2b8327458e0344523eb1ddeeef4bce03e35864640b452f84d26840", + *commit = "5ca8959deb2b8327458e0344523eb1ddeeef4bce03e35864640b452f84d26840"; + + git_str oldpath = GIT_STR_INIT, newpath = GIT_STR_INIT; + git_object *object; + git_oid oid; + + cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA256)); + + /* Copy object to another location with wrong hash */ + cl_git_pass(git_str_joinpath(&oldpath, git_repository_path(g_repo), oldloose)); + cl_git_pass(git_str_joinpath(&newpath, git_repository_path(g_repo), newloose)); + cl_git_pass(git_futils_cp(oldpath.ptr, newpath.ptr, 0644)); + + /* Verify that lookup fails due to a hashsum mismatch */ + cl_git_fail_with(GIT_EMISMATCH, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT)); + + /* Disable verification and try again */ + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0)); + cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT)); + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1)); + + git_object_free(object); + git_str_dispose(&oldpath); + git_str_dispose(&newpath); +#endif +} diff --git a/tests/libgit2/object/tag/parse.c b/tests/libgit2/object/tag/parse.c index 92298b5ce..d7a4d85bf 100644 --- a/tests/libgit2/object/tag/parse.c +++ b/tests/libgit2/object/tag/parse.c @@ -14,7 +14,7 @@ static void assert_tag_parses(const char *data, size_t datalen, if (!datalen) datalen = strlen(data); - cl_git_pass(git_object__from_raw((git_object **) &tag, data, datalen, GIT_OBJECT_TAG)); + cl_git_pass(git_object__from_raw((git_object **) &tag, data, datalen, GIT_OBJECT_TAG, GIT_OID_SHA1)); cl_assert_equal_i(tag->type, GIT_OBJECT_TAG); if (expected_oid) { @@ -54,7 +54,7 @@ static void assert_tag_fails(const char *data, size_t datalen) git_object *object; if (!datalen) datalen = strlen(data); - cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_TAG)); + cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_TAG, GIT_OID_SHA1)); } void test_object_tag_parse__valid_tag_parses(void) diff --git a/tests/libgit2/object/tree/parse.c b/tests/libgit2/object/tree/parse.c index d6d6e5302..fc985d672 100644 --- a/tests/libgit2/object/tree/parse.c +++ b/tests/libgit2/object/tree/parse.c @@ -26,7 +26,7 @@ static void assert_tree_parses(const char *data, size_t datalen, if (!datalen) datalen = strlen(data); - cl_git_pass(git_object__from_raw((git_object **) &tree, data, datalen, GIT_OBJECT_TREE)); + cl_git_pass(git_object__from_raw((git_object **) &tree, data, datalen, GIT_OBJECT_TREE, GIT_OID_SHA1)); cl_assert_equal_i(git_tree_entrycount(tree), expected_nentries); @@ -51,7 +51,7 @@ static void assert_tree_fails(const char *data, size_t datalen) git_object *object; if (!datalen) datalen = strlen(data); - cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_TREE)); + cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJECT_TREE, GIT_OID_SHA1)); } void test_object_tree_parse__single_blob_parses(void) diff --git a/tests/libgit2/object/validate.c b/tests/libgit2/object/validate.c index 87193deb6..e11038115 100644 --- a/tests/libgit2/object/validate.c +++ b/tests/libgit2/object/validate.c @@ -1,50 +1,154 @@ #include "clar_libgit2.h" -#define VALID_COMMIT "tree bdd24e358576f1baa275df98cdcaf3ac9a3f4233\n" \ - "parent d6d956f1d66210bfcd0484166befab33b5987a39\n" \ - "author Edward Thomson <ethomson@edwardthomson.com> 1638286404 -0500\n" \ - "committer Edward Thomson <ethomson@edwardthomson.com> 1638324642 -0500\n" \ - "\n" \ - "commit go here.\n" -#define VALID_TREE "100644 HEADER\0\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" - -#define INVALID_COMMIT "tree bdd24e358576f1baa275df98cdcaf3ac9a3f4233\n" \ - "parent d6d956f1d66210bfcd0484166befab33b5987a39\n" \ - "committer Edward Thomson <ethomson@edwardthomson.com> 1638324642 -0500\n" \ - "\n" \ - "commit go here.\n" -#define INVALID_TREE "100644 HEADER \x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" - -void test_object_validate__valid(void) +#define VALID_COMMIT_SHA1 \ + "tree bdd24e358576f1baa275df98cdcaf3ac9a3f4233\n" \ + "parent d6d956f1d66210bfcd0484166befab33b5987a39\n" \ + "author Edward Thomson <ethomson@edwardthomson.com> 1638286404 -0500\n" \ + "committer Edward Thomson <ethomson@edwardthomson.com> 1638324642 -0500\n" \ + "\n" \ + "commit go here.\n" + +#define VALID_TREE_SHA1 \ + "100644 HEADER\0\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" + +#define INVALID_COMMIT_SHA1 \ + "tree bdd24e358576f1baa275df98cdcaf3ac9a3f4233\n" \ + "parent d6d956f1d66210bfcd0484166befab33b5987a39\n" \ + "committer Edward Thomson <ethomson@edwardthomson.com> 1638324642 -0500\n" \ + "\n" \ + "commit go here.\n" + +#define INVALID_TREE_SHA1 \ + "100644 HEADER \x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" + +#define VALID_COMMIT_SHA256 \ + "tree d0fc7f52dc42358506e7f3f3be72f5271994abb104b9397ab3e19bb42361504d\n" \ + "parent 652412419a24ba62a1d897f40aeb80eecbf873797b04a1bbb8d71918653ef65b\n" \ + "author Edward Thomson <ethomson@edwardthomson.com> 1638286404 -0500\n" \ + "committer Edward Thomson <ethomson@edwardthomson.com> 1638324642 -0500\n" \ + "\n" \ + "commit go here.\n" + +#define VALID_TREE_SHA256 \ + "100644 HEADER\0\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" + +#define INVALID_COMMIT_SHA256 \ + "tree d0fc7f52dc42358506e7f3f3be72f5271994abb104b9397ab3e19bb42361504d\n" \ + "parent 652412419a24ba62a1d897f40aeb80eecbf873797b04a1bbb8d71918653ef65b\n" \ + "committer Edward Thomson <ethomson@edwardthomson.com> 1638324642 -0500\n" \ + "\n" \ + "commit go here.\n" + +#define INVALID_TREE_SHA256 \ + "100644 HEADER \x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42\x42" + +#ifdef GIT_EXPERIMENTAL_SHA256 +# define sha1_rawcontent_is_valid(v, c, l, t) \ + git_object_rawcontent_is_valid(v, c, l, t, GIT_OID_SHA1) +#else +# define sha1_rawcontent_is_valid(v, c, l, t) \ + git_object_rawcontent_is_valid(v, c, l, t) +#endif + +void test_object_validate__valid_sha1(void) +{ + int valid; + + cl_git_pass(sha1_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_BLOB)); + cl_assert_equal_i(1, valid); + + cl_git_pass(sha1_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_BLOB)); + cl_assert_equal_i(1, valid); + + cl_git_pass(sha1_rawcontent_is_valid(&valid, VALID_COMMIT_SHA1, CONST_STRLEN(VALID_COMMIT_SHA1), GIT_OBJECT_COMMIT)); + cl_assert_equal_i(1, valid); + + cl_git_pass(sha1_rawcontent_is_valid(&valid, VALID_TREE_SHA1, CONST_STRLEN(VALID_TREE_SHA1), GIT_OBJECT_TREE)); + cl_assert_equal_i(1, valid); +} + +void test_object_validate__cannot_parse_sha256_as_sha1(void) +{ + int valid; + + cl_git_pass(sha1_rawcontent_is_valid(&valid, VALID_COMMIT_SHA256, CONST_STRLEN(INVALID_COMMIT_SHA256), GIT_OBJECT_COMMIT)); + cl_assert_equal_i(0, valid); + + cl_git_pass(sha1_rawcontent_is_valid(&valid, INVALID_TREE_SHA256, CONST_STRLEN(INVALID_TREE_SHA256), GIT_OBJECT_TREE)); + cl_assert_equal_i(0, valid); +} + +void test_object_validate__invalid_sha1(void) { int valid; - cl_git_pass(git_object_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_BLOB)); + cl_git_pass(sha1_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_COMMIT)); + cl_assert_equal_i(0, valid); + + cl_git_pass(sha1_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_COMMIT)); + cl_assert_equal_i(0, valid); + + cl_git_pass(sha1_rawcontent_is_valid(&valid, INVALID_COMMIT_SHA1, CONST_STRLEN(INVALID_COMMIT_SHA1), GIT_OBJECT_COMMIT)); + cl_assert_equal_i(0, valid); + + cl_git_pass(sha1_rawcontent_is_valid(&valid, INVALID_TREE_SHA1, CONST_STRLEN(INVALID_TREE_SHA1), GIT_OBJECT_TREE)); + cl_assert_equal_i(0, valid); +} + + +void test_object_validate__valid_sha256(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + int valid; + + cl_git_pass(git_object_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_BLOB, GIT_OID_SHA256)); cl_assert_equal_i(1, valid); - cl_git_pass(git_object_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_BLOB)); + cl_git_pass(git_object_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_BLOB, GIT_OID_SHA256)); cl_assert_equal_i(1, valid); - cl_git_pass(git_object_rawcontent_is_valid(&valid, VALID_COMMIT, CONST_STRLEN(VALID_COMMIT), GIT_OBJECT_COMMIT)); + cl_git_pass(git_object_rawcontent_is_valid(&valid, VALID_COMMIT_SHA256, CONST_STRLEN(VALID_COMMIT_SHA256), GIT_OBJECT_COMMIT, GIT_OID_SHA256)); cl_assert_equal_i(1, valid); - cl_git_pass(git_object_rawcontent_is_valid(&valid, VALID_TREE, CONST_STRLEN(VALID_TREE), GIT_OBJECT_TREE)); + cl_git_pass(git_object_rawcontent_is_valid(&valid, VALID_TREE_SHA256, CONST_STRLEN(VALID_TREE_SHA256), GIT_OBJECT_TREE, GIT_OID_SHA256)); cl_assert_equal_i(1, valid); +#endif } -void test_object_validate__invalid(void) +void test_object_validate__invalid_sha256(void) { +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else int valid; - cl_git_pass(git_object_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_COMMIT)); + cl_git_pass(git_object_rawcontent_is_valid(&valid, "", 0, GIT_OBJECT_COMMIT, GIT_OID_SHA256)); + cl_assert_equal_i(0, valid); + + cl_git_pass(git_object_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_COMMIT, GIT_OID_SHA256)); cl_assert_equal_i(0, valid); - cl_git_pass(git_object_rawcontent_is_valid(&valid, "foobar", 0, GIT_OBJECT_COMMIT)); + cl_git_pass(git_object_rawcontent_is_valid(&valid, INVALID_COMMIT_SHA256, CONST_STRLEN(INVALID_COMMIT_SHA256), GIT_OBJECT_COMMIT, GIT_OID_SHA256)); cl_assert_equal_i(0, valid); - cl_git_pass(git_object_rawcontent_is_valid(&valid, INVALID_COMMIT, CONST_STRLEN(INVALID_COMMIT), GIT_OBJECT_COMMIT)); + cl_git_pass(git_object_rawcontent_is_valid(&valid, INVALID_TREE_SHA256, CONST_STRLEN(INVALID_TREE_SHA256), GIT_OBJECT_TREE, GIT_OID_SHA256)); + cl_assert_equal_i(0, valid); +#endif +} + +void test_object_validate__cannot_parse_sha1_as_sha256(void) +{ +#ifndef GIT_EXPERIMENTAL_SHA256 + cl_skip(); +#else + int valid; + + cl_git_pass(git_object_rawcontent_is_valid(&valid, VALID_COMMIT_SHA1, CONST_STRLEN(INVALID_COMMIT_SHA1), GIT_OBJECT_COMMIT, GIT_OID_SHA256)); cl_assert_equal_i(0, valid); - cl_git_pass(git_object_rawcontent_is_valid(&valid, INVALID_TREE, CONST_STRLEN(INVALID_TREE), GIT_OBJECT_TREE)); + cl_git_pass(git_object_rawcontent_is_valid(&valid, INVALID_TREE_SHA1, CONST_STRLEN(INVALID_TREE_SHA1), GIT_OBJECT_TREE, GIT_OID_SHA256)); cl_assert_equal_i(0, valid); +#endif } diff --git a/tests/libgit2/pack/indexer.c b/tests/libgit2/pack/indexer.c index 2ac287255..9722decaf 100644 --- a/tests/libgit2/pack/indexer.c +++ b/tests/libgit2/pack/indexer.c @@ -100,7 +100,12 @@ void test_pack_indexer__out_of_order(void) git_indexer *idx = 0; git_indexer_progress stats = { 0 }; +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); +#endif + cl_git_pass(git_indexer_append( idx, out_of_order_pack, out_of_order_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); @@ -117,7 +122,12 @@ void test_pack_indexer__missing_trailer(void) git_indexer *idx = 0; git_indexer_progress stats = { 0 }; +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); +#endif + cl_git_pass(git_indexer_append( idx, missing_trailer_pack, missing_trailer_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); @@ -133,7 +143,12 @@ void test_pack_indexer__leaky(void) git_indexer *idx = 0; git_indexer_progress stats = { 0 }; +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); +#endif + cl_git_pass(git_indexer_append( idx, leaky_pack, leaky_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); @@ -151,6 +166,7 @@ void test_pack_indexer__fix_thin(void) git_repository *repo; git_odb *odb; git_oid id, should_id; + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; cl_git_pass(git_repository_init(&repo, "thin.git", true)); cl_git_pass(git_repository_odb(&odb, repo)); @@ -160,7 +176,13 @@ void test_pack_indexer__fix_thin(void) git_oid__fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18", GIT_OID_SHA1); cl_assert_equal_oid(&should_id, &id); - cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL)); +#ifdef GIT_EXPERIMENTAL_SHA256 + opts.odb = odb; + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); +#else + cl_git_pass(git_indexer_new(&idx, ".", 0, odb, &opts)); +#endif + cl_git_pass(git_indexer_append(idx, thin_pack, thin_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); @@ -192,7 +214,12 @@ void test_pack_indexer__fix_thin(void) cl_git_pass(p_stat(name, &st)); +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); +#endif + read = p_read(fd, buffer, sizeof(buffer)); cl_assert(read != -1); p_close(fd); @@ -216,6 +243,7 @@ void test_pack_indexer__corrupt_length(void) git_repository *repo; git_odb *odb; git_oid id, should_id; + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; cl_git_pass(git_repository_init(&repo, "thin.git", true)); cl_git_pass(git_repository_odb(&odb, repo)); @@ -225,7 +253,13 @@ void test_pack_indexer__corrupt_length(void) git_oid__fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18", GIT_OID_SHA1); cl_assert_equal_oid(&should_id, &id); - cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL)); +#ifdef GIT_EXPERIMENTAL_SHA256 + opts.odb = odb; + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); +#else + cl_git_pass(git_indexer_new(&idx, ".", 0, odb, &opts)); +#endif + cl_git_pass(git_indexer_append( idx, corrupt_thin_pack, corrupt_thin_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); @@ -246,7 +280,12 @@ void test_pack_indexer__incomplete_pack_fails_with_strict(void) opts.verify = 1; +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); +#endif + cl_git_pass(git_indexer_append( idx, incomplete_pack, incomplete_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); @@ -266,7 +305,12 @@ void test_pack_indexer__out_of_order_with_connectivity_checks(void) opts.verify = 1; +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, &opts)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, &opts)); +#endif + cl_git_pass(git_indexer_append( idx, out_of_order_pack, out_of_order_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); @@ -309,7 +353,12 @@ void test_pack_indexer__no_tmp_files(void) git_str_dispose(&path); cl_assert(git_str_len(&first_tmp_file) == 0); +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); +#endif + git_indexer_free(idx); cl_git_pass(git_str_sets(&path, clar_sandbox_path())); diff --git a/tests/libgit2/pack/packbuilder.c b/tests/libgit2/pack/packbuilder.c index 0889f46ed..ff3dc1f68 100644 --- a/tests/libgit2/pack/packbuilder.c +++ b/tests/libgit2/pack/packbuilder.c @@ -104,7 +104,12 @@ void test_pack_packbuilder__create_pack(void) seed_packbuilder(); +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&_indexer, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&_indexer, ".", 0, NULL, NULL)); +#endif + cl_git_pass(git_packbuilder_foreach(_packbuilder, feed_indexer, &stats)); cl_git_pass(git_indexer_commit(_indexer, &stats)); @@ -244,7 +249,13 @@ void test_pack_packbuilder__foreach(void) git_indexer *idx; seed_packbuilder(); + +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); +#endif + cl_git_pass(git_packbuilder_foreach(_packbuilder, foreach_cb, idx)); cl_git_pass(git_indexer_commit(idx, &_stats)); git_indexer_free(idx); @@ -262,7 +273,13 @@ void test_pack_packbuilder__foreach_with_cancel(void) git_indexer *idx; seed_packbuilder(); + +#ifdef GIT_EXPERIMENTAL_SHA256 + cl_git_pass(git_indexer_new(&idx, ".", GIT_OID_SHA1, NULL)); +#else cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL)); +#endif + cl_git_fail_with( git_packbuilder_foreach(_packbuilder, foreach_cancel_cb, idx), -1111); git_indexer_free(idx); |
