1 
2 #include "clar_libgit2.h"
3 
4 #include "odb.h"
5 #include "hash.h"
6 
7 #include "data.h"
8 
hash_object_pass(git_oid * oid,git_rawobj * obj)9 static void hash_object_pass(git_oid *oid, git_rawobj *obj)
10 {
11 	cl_git_pass(git_odb_hash(oid, obj->data, obj->len, obj->type));
12 }
hash_object_fail(git_oid * oid,git_rawobj * obj)13 static void hash_object_fail(git_oid *oid, git_rawobj *obj)
14 {
15 	cl_git_fail(git_odb_hash(oid, obj->data, obj->len, obj->type));
16 }
17 
18 static char *hello_id = "22596363b3de40b06f981fb85d82312e8c0ed511";
19 static char *hello_text = "hello world\n";
20 
21 static char *bye_id = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
22 static char *bye_text = "bye world\n";
23 
test_object_raw_hash__hash_by_blocks(void)24 void test_object_raw_hash__hash_by_blocks(void)
25 {
26 	git_hash_ctx ctx;
27 	git_oid id1, id2;
28 
29 	cl_git_pass(git_hash_ctx_init(&ctx));
30 
31 	/* should already be init'd */
32 	cl_git_pass(git_hash_update(&ctx, hello_text, strlen(hello_text)));
33 	cl_git_pass(git_hash_final(&id2, &ctx));
34 	cl_git_pass(git_oid_fromstr(&id1, hello_id));
35 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
36 
37 	/* reinit should permit reuse */
38 	cl_git_pass(git_hash_init(&ctx));
39 	cl_git_pass(git_hash_update(&ctx, bye_text, strlen(bye_text)));
40 	cl_git_pass(git_hash_final(&id2, &ctx));
41 	cl_git_pass(git_oid_fromstr(&id1, bye_id));
42 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
43 
44 	git_hash_ctx_cleanup(&ctx);
45 }
46 
test_object_raw_hash__hash_buffer_in_single_call(void)47 void test_object_raw_hash__hash_buffer_in_single_call(void)
48 {
49 	git_oid id1, id2;
50 
51 	cl_git_pass(git_oid_fromstr(&id1, hello_id));
52 	git_hash_buf(&id2, hello_text, strlen(hello_text));
53 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
54 }
55 
test_object_raw_hash__hash_vector(void)56 void test_object_raw_hash__hash_vector(void)
57 {
58 	git_oid id1, id2;
59 	git_buf_vec vec[2];
60 
61 	cl_git_pass(git_oid_fromstr(&id1, hello_id));
62 
63 	vec[0].data = hello_text;
64 	vec[0].len  = 4;
65 	vec[1].data = hello_text+4;
66 	vec[1].len  = strlen(hello_text)-4;
67 
68 	git_hash_vec(&id2, vec, 2);
69 
70 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
71 }
72 
test_object_raw_hash__hash_junk_data(void)73 void test_object_raw_hash__hash_junk_data(void)
74 {
75 	git_oid id, id_zero;
76 
77 	cl_git_pass(git_oid_fromstr(&id_zero, zero_id));
78 
79 	/* invalid types: */
80 	junk_obj.data = some_data;
81 	hash_object_fail(&id, &junk_obj);
82 
83 	junk_obj.type = 0; /* EXT1 */
84 	hash_object_fail(&id, &junk_obj);
85 
86 	junk_obj.type = 5; /* EXT2 */
87 	hash_object_fail(&id, &junk_obj);
88 
89 	junk_obj.type = GIT_OBJECT_OFS_DELTA;
90 	hash_object_fail(&id, &junk_obj);
91 
92 	junk_obj.type = GIT_OBJECT_REF_DELTA;
93 	hash_object_fail(&id, &junk_obj);
94 
95 	junk_obj.type = 42;
96 	hash_object_fail(&id, &junk_obj);
97 
98 	/* data can be NULL only if len is zero: */
99 	junk_obj.type = GIT_OBJECT_BLOB;
100 	junk_obj.data = NULL;
101 	hash_object_pass(&id, &junk_obj);
102 	cl_assert(git_oid_cmp(&id, &id_zero) == 0);
103 
104 	junk_obj.len = 1;
105 	hash_object_fail(&id, &junk_obj);
106 }
107 
test_object_raw_hash__hash_commit_object(void)108 void test_object_raw_hash__hash_commit_object(void)
109 {
110 	git_oid id1, id2;
111 
112 	cl_git_pass(git_oid_fromstr(&id1, commit_id));
113 	hash_object_pass(&id2, &commit_obj);
114 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
115 }
116 
test_object_raw_hash__hash_tree_object(void)117 void test_object_raw_hash__hash_tree_object(void)
118 {
119 	git_oid id1, id2;
120 
121 	cl_git_pass(git_oid_fromstr(&id1, tree_id));
122 	hash_object_pass(&id2, &tree_obj);
123 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
124 }
125 
test_object_raw_hash__hash_tag_object(void)126 void test_object_raw_hash__hash_tag_object(void)
127 {
128 	git_oid id1, id2;
129 
130 	cl_git_pass(git_oid_fromstr(&id1, tag_id));
131 	hash_object_pass(&id2, &tag_obj);
132 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
133 }
134 
test_object_raw_hash__hash_zero_length_object(void)135 void test_object_raw_hash__hash_zero_length_object(void)
136 {
137 	git_oid id1, id2;
138 
139 	cl_git_pass(git_oid_fromstr(&id1, zero_id));
140 	hash_object_pass(&id2, &zero_obj);
141 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
142 }
143 
test_object_raw_hash__hash_one_byte_object(void)144 void test_object_raw_hash__hash_one_byte_object(void)
145 {
146 	git_oid id1, id2;
147 
148 	cl_git_pass(git_oid_fromstr(&id1, one_id));
149 	hash_object_pass(&id2, &one_obj);
150 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
151 }
152 
test_object_raw_hash__hash_two_byte_object(void)153 void test_object_raw_hash__hash_two_byte_object(void)
154 {
155 	git_oid id1, id2;
156 
157 	cl_git_pass(git_oid_fromstr(&id1, two_id));
158 	hash_object_pass(&id2, &two_obj);
159 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
160 }
161 
test_object_raw_hash__hash_multi_byte_object(void)162 void test_object_raw_hash__hash_multi_byte_object(void)
163 {
164 	git_oid id1, id2;
165 
166 	cl_git_pass(git_oid_fromstr(&id1, some_id));
167 	hash_object_pass(&id2, &some_obj);
168 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
169 }
170