|
| 1 | +#include "clar_libgit2.h" |
| 2 | +#include "hash.h" |
| 3 | + |
| 4 | +#define FIXTURE_DIR "sha1" |
| 5 | + |
| 6 | +void test_core_sha1__initialize(void) |
| 7 | +{ |
| 8 | + cl_fixture_sandbox(FIXTURE_DIR); |
| 9 | +} |
| 10 | + |
| 11 | +void test_core_sha1__cleanup(void) |
| 12 | +{ |
| 13 | + cl_fixture_cleanup(FIXTURE_DIR); |
| 14 | +} |
| 15 | + |
| 16 | +static int sha1_file(git_oid *oid, const char *filename) |
| 17 | +{ |
| 18 | + git_hash_ctx ctx; |
| 19 | + char buf[2048]; |
| 20 | + int fd, ret; |
| 21 | + ssize_t read_len; |
| 22 | + |
| 23 | + fd = p_open(filename, O_RDONLY); |
| 24 | + cl_assert(fd >= 0); |
| 25 | + |
| 26 | + cl_git_pass(git_hash_ctx_init(&ctx)); |
| 27 | + |
| 28 | + while ((read_len = p_read(fd, buf, 2048)) > 0) |
| 29 | + cl_git_pass(git_hash_update(&ctx, buf, (size_t)read_len)); |
| 30 | + |
| 31 | + cl_assert_equal_i(0, read_len); |
| 32 | + p_close(fd); |
| 33 | + |
| 34 | + ret = git_hash_final(oid, &ctx); |
| 35 | + git_hash_ctx_cleanup(&ctx); |
| 36 | + |
| 37 | + return ret; |
| 38 | +} |
| 39 | + |
| 40 | +void test_core_sha1__sum(void) |
| 41 | +{ |
| 42 | + git_oid oid, expected; |
| 43 | + |
| 44 | + cl_git_pass(sha1_file(&oid, FIXTURE_DIR "/hello_c")); |
| 45 | + git_oid_fromstr(&expected, "4e72679e3ea4d04e0c642f029e61eb8056c7ed94"); |
| 46 | + cl_assert_equal_oid(&expected, &oid); |
| 47 | +} |
| 48 | + |
| 49 | +/* test that sha1 collision detection works when enabled */ |
| 50 | +void test_core_sha1__detect_collision_attack(void) |
| 51 | +{ |
| 52 | + git_oid oid, expected; |
| 53 | + |
| 54 | +#ifdef GIT_SHA1_COLLISIONDETECT |
| 55 | + GIT_UNUSED(expected); |
| 56 | + cl_git_fail(sha1_file(&oid, FIXTURE_DIR "/shattered-1.pdf")); |
| 57 | + cl_assert_equal_s("SHA1 collision attack detected", giterr_last()->message); |
| 58 | +#else |
| 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); |
| 62 | +#endif |
| 63 | +} |
| 64 | + |
0 commit comments