]> git.proxmox.com Git - libgit2.git/blob - tests/object/lookup.c
a7b1ceeb4ed9d05d34bf0b2407c89d74d4cf07c8
[libgit2.git] / tests / object / lookup.c
1 #include "clar_libgit2.h"
2
3 #include "repository.h"
4
5 static git_repository *g_repo;
6
7 void test_object_lookup__initialize(void)
8 {
9 g_repo = cl_git_sandbox_init("testrepo.git");
10 }
11
12 void test_object_lookup__cleanup(void)
13 {
14 cl_git_sandbox_cleanup();
15 }
16
17 void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
18 {
19 const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
20 git_oid oid;
21 git_object *object;
22
23 cl_git_pass(git_oid_fromstr(&oid, commit));
24 cl_assert_equal_i(
25 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG));
26 }
27
28 void test_object_lookup__lookup_nonexisting_returns_enotfound(void)
29 {
30 const char *unknown = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
31 git_oid oid;
32 git_object *object;
33
34 cl_git_pass(git_oid_fromstr(&oid, unknown));
35 cl_assert_equal_i(
36 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_ANY));
37 }
38
39 void 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_OBJECT_TAG));
48 }
49
50 void 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_OBJECT_COMMIT));
59 git_object_free(object);
60
61 cl_assert_equal_i(
62 GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG));
63 }
64
65 void test_object_lookup__lookup_corrupt_object_returns_error(void)
66 {
67 const char *commit = "8e73b769e97678d684b809b163bebdae2911720f",
68 *file = "objects/8e/73b769e97678d684b809b163bebdae2911720f";
69 git_str path = GIT_STR_INIT, contents = GIT_STR_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_str_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_OBJECT_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_OBJECT_COMMIT));
89
90 git_object_free(object);
91 git_str_dispose(&path);
92 git_str_dispose(&contents);
93 }
94
95 void 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_str oldpath = GIT_STR_INIT, newpath = GIT_STR_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_str_joinpath(&oldpath, git_repository_path(g_repo), oldloose));
108 cl_git_pass(git_str_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_OBJECT_COMMIT));
113
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_OBJECT_COMMIT));
117 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
118
119 git_object_free(object);
120 git_str_dispose(&oldpath);
121 git_str_dispose(&newpath);
122 }