]>
git.proxmox.com Git - libgit2.git/blob - tests/core/sha1.c
f81d40854481fc025e82372e4bc51b386861f2e8
1 #include "clar_libgit2.h"
4 #define FIXTURE_DIR "sha1"
6 void test_core_sha1__initialize(void)
8 cl_fixture_sandbox(FIXTURE_DIR
);
11 void test_core_sha1__cleanup(void)
13 cl_fixture_cleanup(FIXTURE_DIR
);
16 static int sha1_file(git_oid
*oid
, const char *filename
)
23 fd
= p_open(filename
, O_RDONLY
);
26 cl_git_pass(git_hash_ctx_init(&ctx
));
28 while ((read_len
= p_read(fd
, buf
, 2048)) > 0)
29 cl_git_pass(git_hash_update(&ctx
, buf
, (size_t)read_len
));
31 cl_assert_equal_i(0, read_len
);
34 ret
= git_hash_final(oid
, &ctx
);
35 git_hash_ctx_cleanup(&ctx
);
40 void test_core_sha1__sum(void)
42 git_oid oid
, expected
;
44 cl_git_pass(sha1_file(&oid
, FIXTURE_DIR
"/hello_c"));
45 git_oid_fromstr(&expected
, "4e72679e3ea4d04e0c642f029e61eb8056c7ed94");
46 cl_assert_equal_oid(&expected
, &oid
);
49 /* test that sha1 collision detection works when enabled */
50 void test_core_sha1__detect_collision_attack(void)
52 git_oid oid
, expected
;
54 #ifdef GIT_SHA1_COLLISIONDETECT
56 cl_git_fail(sha1_file(&oid
, FIXTURE_DIR
"/shattered-1.pdf"));
57 cl_assert_equal_s("SHA1 collision attack detected", git_error_last()->message
);
59 cl_git_pass(sha1_file(&oid
, FIXTURE_DIR
"/shattered-1.pdf"));
60 git_oid_fromstr(&expected
, "38762cf7f55934b34d179ae6a4c80cadccbb7f0a");
61 cl_assert_equal_oid(&expected
, &oid
);