]> git.proxmox.com Git - libgit2.git/blob - tests/core/sha1.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / sha1.c
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(unsigned char *out, 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, GIT_HASH_ALGORITHM_SHA1));
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(out, &ctx);
35 git_hash_ctx_cleanup(&ctx);
36
37 return ret;
38 }
39
40 void test_core_sha1__sum(void)
41 {
42 unsigned char expected[GIT_HASH_SHA1_SIZE] = {
43 0x4e, 0x72, 0x67, 0x9e, 0x3e, 0xa4, 0xd0, 0x4e, 0x0c, 0x64,
44 0x2f, 0x02, 0x9e, 0x61, 0xeb, 0x80, 0x56, 0xc7, 0xed, 0x94
45 };
46 unsigned char actual[GIT_HASH_SHA1_SIZE];
47
48 cl_git_pass(sha1_file(actual, FIXTURE_DIR "/hello_c"));
49 cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA1_SIZE));
50 }
51
52 /* test that sha1 collision detection works when enabled */
53 void test_core_sha1__detect_collision_attack(void)
54 {
55 unsigned char actual[GIT_HASH_SHA1_SIZE];
56 unsigned char expected[GIT_HASH_SHA1_SIZE] = {
57 0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17,
58 0x9a, 0xe6, 0xa4, 0xc8, 0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a
59 };
60
61 #ifdef GIT_SHA1_COLLISIONDETECT
62 GIT_UNUSED(&expected);
63 cl_git_fail(sha1_file(actual, FIXTURE_DIR "/shattered-1.pdf"));
64 cl_assert_equal_s("SHA1 collision attack detected", git_error_last()->message);
65 #else
66 cl_git_pass(sha1_file(actual, FIXTURE_DIR "/shattered-1.pdf"));
67 cl_assert_equal_i(0, memcmp(expected, actual, GIT_HASH_SHA1_SIZE));
68 #endif
69 }
70