]>
git.proxmox.com Git - libgit2.git/blob - tests/object/raw/hash.c
5a3e818552abc9d95d47ab9b3cf83174acc219da
2 #include "clar_libgit2.h"
9 static void hash_object_pass(git_oid
*oid
, git_rawobj
*obj
)
11 cl_git_pass(git_odb_hash(oid
, obj
->data
, obj
->len
, obj
->type
));
13 static void hash_object_fail(git_oid
*oid
, git_rawobj
*obj
)
15 cl_git_fail(git_odb_hash(oid
, obj
->data
, obj
->len
, obj
->type
));
18 static char *hello_id
= "22596363b3de40b06f981fb85d82312e8c0ed511";
19 static char *hello_text
= "hello world\n";
21 static char *bye_id
= "ce08fe4884650f067bd5703b6a59a8b3b3c99a09";
22 static char *bye_text
= "bye world\n";
24 void test_object_raw_hash__hash_by_blocks(void)
29 cl_git_pass(git_hash_ctx_init(&ctx
, GIT_HASH_ALGORITHM_SHA1
));
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
.id
, &ctx
));
34 cl_git_pass(git_oid_fromstr(&id1
, hello_id
));
35 cl_assert(git_oid_cmp(&id1
, &id2
) == 0);
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
.id
, &ctx
));
41 cl_git_pass(git_oid_fromstr(&id1
, bye_id
));
42 cl_assert(git_oid_cmp(&id1
, &id2
) == 0);
44 git_hash_ctx_cleanup(&ctx
);
47 void test_object_raw_hash__hash_buffer_in_single_call(void)
51 cl_git_pass(git_oid_fromstr(&id1
, hello_id
));
52 git_hash_buf(id2
.id
, hello_text
, strlen(hello_text
), GIT_HASH_ALGORITHM_SHA1
);
53 cl_assert(git_oid_cmp(&id1
, &id2
) == 0);
56 void test_object_raw_hash__hash_vector(void)
61 cl_git_pass(git_oid_fromstr(&id1
, hello_id
));
63 vec
[0].data
= hello_text
;
65 vec
[1].data
= hello_text
+4;
66 vec
[1].len
= strlen(hello_text
)-4;
68 git_hash_vec(id2
.id
, vec
, 2, GIT_HASH_ALGORITHM_SHA1
);
70 cl_assert(git_oid_cmp(&id1
, &id2
) == 0);
73 void test_object_raw_hash__hash_junk_data(void)
77 cl_git_pass(git_oid_fromstr(&id_zero
, zero_id
));
80 junk_obj
.data
= some_data
;
81 hash_object_fail(&id
, &junk_obj
);
83 junk_obj
.type
= 0; /* EXT1 */
84 hash_object_fail(&id
, &junk_obj
);
86 junk_obj
.type
= 5; /* EXT2 */
87 hash_object_fail(&id
, &junk_obj
);
89 junk_obj
.type
= GIT_OBJECT_OFS_DELTA
;
90 hash_object_fail(&id
, &junk_obj
);
92 junk_obj
.type
= GIT_OBJECT_REF_DELTA
;
93 hash_object_fail(&id
, &junk_obj
);
96 hash_object_fail(&id
, &junk_obj
);
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);
105 hash_object_fail(&id
, &junk_obj
);
108 void test_object_raw_hash__hash_commit_object(void)
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);
117 void test_object_raw_hash__hash_tree_object(void)
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);
126 void test_object_raw_hash__hash_tag_object(void)
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);
135 void test_object_raw_hash__hash_zero_length_object(void)
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);
144 void test_object_raw_hash__hash_one_byte_object(void)
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);
153 void test_object_raw_hash__hash_two_byte_object(void)
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);
162 void test_object_raw_hash__hash_multi_byte_object(void)
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);