]> git.proxmox.com Git - libgit2.git/blame - tests/object/lookup.c
signature: free dup'd buffers on parse error
[libgit2.git] / tests / object / lookup.c
CommitLineData
cb0ce16b 1#include "clar_libgit2.h"
2
3#include "repository.h"
4
5static git_repository *g_repo;
6
7void test_object_lookup__initialize(void)
8{
86c03552 9 g_repo = cl_git_sandbox_init("testrepo.git");
cb0ce16b 10}
11
12void test_object_lookup__cleanup(void)
13{
86c03552 14 cl_git_sandbox_cleanup();
cb0ce16b 15}
16
19579847 17void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
cb0ce16b 18{
19 const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
20 git_oid oid;
21 git_object *object;
22
23 cl_git_pass(git_oid_fromstr(&oid, commit));
19579847
RB
24 cl_assert_equal_i(
25 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG));
cb0ce16b 26}
27
19579847 28void test_object_lookup__lookup_nonexisting_returns_enotfound(void)
cb0ce16b 29{
30 const char *unknown = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
31 git_oid oid;
32 git_object *object;
33
34 cl_git_pass(git_oid_fromstr(&oid, unknown));
19579847
RB
35 cl_assert_equal_i(
36 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_ANY));
cb0ce16b 37}
e28c3776 38
39void test_object_lookup__lookup_wrong_type_by_abbreviated_id_returns_enotfound(void)
40{
41 const char *commit = "e90810b";
42 git_oid oid;
43 git_object *object;
44
45 cl_git_pass(git_oid_fromstrn(&oid, commit, strlen(commit)));
46 cl_assert_equal_i(
47 GIT_ENOTFOUND, git_object_lookup_prefix(&object, g_repo, &oid, strlen(commit), GIT_OBJ_TAG));
48}
49
50void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
51{
52 const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
53 git_oid oid;
54 git_object *object;
55
56 cl_git_pass(git_oid_fromstr(&oid, commit));
57
58 cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
59 git_object_free(object);
60
61 cl_assert_equal_i(
62 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG));
63}
613d5eb9 64
d59dabe5
PS
65void 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
28a0741f
PS
95void test_object_lookup__lookup_object_with_wrong_hash_returns_error(void)
96{
97 const char *oldloose = "objects/8e/73b769e97678d684b809b163bebdae2911720f",
98 *newloose = "objects/8e/73b769e97678d684b809b163bebdae2911720e",
99 *commit = "8e73b769e97678d684b809b163bebdae2911720e";
100 git_buf oldpath = GIT_BUF_INIT, newpath = GIT_BUF_INIT;
101 git_object *object;
102 git_oid oid;
103
104 cl_git_pass(git_oid_fromstr(&oid, commit));
105
106 /* Copy object to another location with wrong hash */
107 cl_git_pass(git_buf_joinpath(&oldpath, git_repository_path(g_repo), oldloose));
108 cl_git_pass(git_buf_joinpath(&newpath, git_repository_path(g_repo), newloose));
109 cl_git_pass(git_futils_cp(oldpath.ptr, newpath.ptr, 0644));
110
111 /* Verify that lookup fails due to a hashsum mismatch */
112 cl_git_fail_with(GIT_EMISMATCH, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
113
35079f50
PS
114 /* Disable verification and try again */
115 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
116 cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
117 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
118
28a0741f
PS
119 git_buf_free(&oldpath);
120 git_buf_free(&newpath);
121}