Skip to content

Commit d59dabe

Browse files
committed
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.
1 parent 86c0355 commit d59dabe

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/object/lookup.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,33 @@ void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
6262
GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG));
6363
}
6464

65+
void test_object_lookup__lookup_corrupt_object_returns_error(void)
66+
{
67+
const char *commit = "8e73b769e97678d684b809b163bebdae2911720f",
68+
*file = "objects/8e/73b769e97678d684b809b163bebdae2911720f";
69+
git_buf path = GIT_BUF_INIT, contents = GIT_BUF_INIT;
70+
git_oid oid;
71+
git_object *object;
72+
size_t i;
73+
74+
cl_git_pass(git_oid_fromstr(&oid, commit));
75+
cl_git_pass(git_buf_joinpath(&path, git_repository_path(g_repo), file));
76+
cl_git_pass(git_futils_readbuffer(&contents, path.ptr));
77+
78+
/* Corrupt and try to read the object */
79+
for (i = 0; i < contents.size; i++) {
80+
contents.ptr[i] ^= 0x1;
81+
cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
82+
cl_git_fail(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
83+
contents.ptr[i] ^= 0x1;
84+
}
85+
86+
/* Restore original content and assert we can read the object */
87+
cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
88+
cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
89+
90+
git_object_free(object);
91+
git_buf_free(&path);
92+
git_buf_free(&contents);
93+
}
94+

0 commit comments

Comments
 (0)