summaryrefslogtreecommitdiff
path: root/tests/object
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2017-04-10 09:00:51 +0200
committerPatrick Steinhardt <ps@pks.im>2017-04-28 14:05:45 +0200
commitd59dabe5cbee52035810216ab118fa3f63047f2f (patch)
treef2224513f01282b2cb7ab19d7752550285ff2209 /tests/object
parent86c035526dd9eb4131ce2a3e25926c5a136b8318 (diff)
downloadlibgit2-d59dabe5cbee52035810216ab118fa3f63047f2f.tar.gz
tests: object: test looking up corrupted objects
We currently have no tests which check whether we fail reading corrupted objects. Add one which modifies contents of an object stored on disk and then tries to read the object.
Diffstat (limited to 'tests/object')
-rw-r--r--tests/object/lookup.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/object/lookup.c b/tests/object/lookup.c
index 0e492b428..0116ee44d 100644
--- a/tests/object/lookup.c
+++ b/tests/object/lookup.c
@@ -62,3 +62,33 @@ void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG));
}
+void test_object_lookup__lookup_corrupt_object_returns_error(void)
+{
+ const char *commit = "8e73b769e97678d684b809b163bebdae2911720f",
+ *file = "objects/8e/73b769e97678d684b809b163bebdae2911720f";
+ git_buf path = GIT_BUF_INIT, contents = GIT_BUF_INIT;
+ git_oid oid;
+ git_object *object;
+ size_t i;
+
+ cl_git_pass(git_oid_fromstr(&oid, commit));
+ cl_git_pass(git_buf_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_OBJ_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_OBJ_COMMIT));
+
+ git_object_free(object);
+ git_buf_free(&path);
+ git_buf_free(&contents);
+}
+