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