]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/object/lookup.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / 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 24 cl_assert_equal_i(
ac3d33df 25 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_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 35 cl_assert_equal_i(
ac3d33df 36 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_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(
ac3d33df 47 GIT_ENOTFOUND, git_object_lookup_prefix(&object, g_repo, &oid, strlen(commit), GIT_OBJECT_TAG));
e28c3776 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
ac3d33df 58 cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
e28c3776 59 git_object_free(object);
60
61 cl_assert_equal_i(
ac3d33df 62 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG));
e28c3776 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";
e579e0f7 69 git_str path = GIT_STR_INIT, contents = GIT_STR_INIT;
d59dabe5
PS
70 git_oid oid;
71 git_object *object;
72 size_t i;
73
74 cl_git_pass(git_oid_fromstr(&oid, commit));
e579e0f7 75 cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), file));
d59dabe5
PS
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));
ac3d33df 82 cl_git_fail(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
d59dabe5
PS
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));
ac3d33df 88 cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
d59dabe5
PS
89
90 git_object_free(object);
e579e0f7
MB
91 git_str_dispose(&path);
92 git_str_dispose(&contents);
d59dabe5
PS
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";
e579e0f7 100 git_str oldpath = GIT_STR_INIT, newpath = GIT_STR_INIT;
28a0741f
PS
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 */
e579e0f7
MB
107 cl_git_pass(git_str_joinpath(&oldpath, git_repository_path(g_repo), oldloose));
108 cl_git_pass(git_str_joinpath(&newpath, git_repository_path(g_repo), newloose));
28a0741f
PS
109 cl_git_pass(git_futils_cp(oldpath.ptr, newpath.ptr, 0644));
110
111 /* Verify that lookup fails due to a hashsum mismatch */
ac3d33df 112 cl_git_fail_with(GIT_EMISMATCH, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
28a0741f 113
35079f50
PS
114 /* Disable verification and try again */
115 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
ac3d33df 116 cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
35079f50
PS
117 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
118
1dc89aab 119 git_object_free(object);
e579e0f7
MB
120 git_str_dispose(&oldpath);
121 git_str_dispose(&newpath);
28a0741f 122}