Fix possibly undefined values
[libisds.git] / test / compute_hash.c
blob06e8252e0d9dda386388810659d872bdf3eaa8f6
1 #include "test.h"
2 #include "crypto.h"
3 #include <string.h>
5 static int cmp_hash (const struct isds_hash *h1, const struct isds_hash *h2) {
6 if (h1 == NULL && h2 == NULL) PASS_TEST;
7 if (!(h1 != NULL && h2 != NULL))
8 FAIL_TEST("Hash structure is NULL and shouldn't be or vice versa");
9 if (h1->algorithm != h2->algorithm)
10 FAIL_TEST("Wrong hash algorithm");
12 if (h1->length != h2->length)
13 FAIL_TEST("Wrong hash length");
14 for (int i = 0; i < h1->length; i++) {
15 if (((uint8_t *) (h1->value))[i] != ((uint8_t *) (h2->value))[i])
16 FAIL_TEST("Wrong hash value");
18 PASS_TEST;
21 static int test_compute_hash(const isds_error error,
22 const struct isds_hash *correct, struct isds_hash *test, const void *input,
23 const size_t input_length) {
24 isds_error err;
25 int status;
27 if (!correct || !test) return 1;
29 err = compute_hash(input, input_length, test);
30 if (err != error) {
31 free(test->value); test->value = NULL;
32 FAIL_TEST("Wrong return value");
35 if (!err) {
36 status = cmp_hash(correct, test);
37 } else {
38 status = 0;
41 free(test->value); test->value = NULL;
42 return (status);
45 int main(int argc, char **argv) {
47 INIT_TEST("compute_hash");
49 if (init_gcrypt())
50 ABORT_UNIT("init_gcrypt() failed");
52 char input[] = "42";
53 struct isds_hash test = {
54 .algorithm = HASH_ALGORITHM_SHA_1,
55 .length = 0,
56 .value = NULL
58 struct isds_hash correct = {
59 .algorithm = HASH_ALGORITHM_SHA_1,
60 .length = 20,
61 .value = (uint8_t[]) {
62 0x27, 0x45, 0x34, 0x43, 0x27, 0x4a, 0x2d, 0x9c, 0xe2, 0xca,
63 0xfe, 0x6f, 0xe0, 0x16, 0x3a, 0x4a, 0x32, 0x74, 0x62, 0xaa
66 TEST("standard SHA1", test_compute_hash, IE_SUCCESS, &correct, &test,
67 input, sizeof(input));
69 TEST("NULL input", test_compute_hash, IE_INVAL, &correct, &test, NULL, 1);
71 correct.value = (uint8_t[]) {
72 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
73 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09
75 test.algorithm = HASH_ALGORITHM_SHA_1;
76 TEST("zero length with NULL input",
77 test_compute_hash, IE_SUCCESS, &correct, &test, NULL, 0);
79 correct.algorithm = HASH_ALGORITHM_MD5;
80 correct.length = 16;
81 correct.value = (uint8_t[]) {
82 0x67, 0x54, 0x80, 0xc6, 0x5a, 0x65, 0x57, 0x6e,
83 0xdf, 0xf9, 0x26, 0xa7, 0xf0, 0xa8, 0x25, 0x70
85 test.algorithm = HASH_ALGORITHM_MD5;
86 TEST("MD5", test_compute_hash, IE_SUCCESS, &correct, &test, input,
87 sizeof(input));
89 correct.algorithm = HASH_ALGORITHM_SHA_256;
90 correct.length = 32;
91 correct.value = (uint8_t[]) {
92 0xe0, 0x8d, 0xe2, 0x65, 0x65, 0x32, 0xc1, 0x05,
93 0x88, 0xb5, 0xbf, 0x37, 0x06, 0x0f, 0x4c, 0xc6,
94 0xec, 0xff, 0x71, 0x7a, 0xa3, 0x93, 0x54, 0x59,
95 0x4e, 0x92, 0x57, 0xc1, 0x8b, 0x47, 0xb7, 0x5a
97 test.algorithm = HASH_ALGORITHM_SHA_256;
98 TEST("SHA-256", test_compute_hash, IE_SUCCESS, &correct, &test, input,
99 sizeof(input));
101 correct.algorithm = HASH_ALGORITHM_SHA_512;
102 correct.length = 64;
103 correct.value = (uint8_t[]) {
104 0x37, 0x39, 0x43, 0x2c, 0x23, 0x33, 0x2b, 0x88,
105 0x8d, 0x09, 0xfb, 0xab, 0xf5, 0x51, 0x8f, 0xb5,
106 0x0b, 0xf1, 0xc8, 0x7a, 0x7f, 0x28, 0x23, 0xcb,
107 0xf2, 0x64, 0xf3, 0xae, 0x73, 0xa8, 0xf1, 0x75,
108 0x88, 0xc6, 0x3f, 0xc4, 0xfd, 0x25, 0x5b, 0x4c,
109 0xbc, 0xdb, 0x0d, 0x0d, 0xb1, 0x05, 0x15, 0x4f,
110 0x7c, 0x63, 0xd8, 0xb0, 0xe2, 0x6f, 0x31, 0x81,
111 0x41, 0xec, 0xec, 0xd1, 0x02, 0x45, 0x6b, 0xf6
113 test.algorithm = HASH_ALGORITHM_SHA_512;
114 TEST("SHA-512", test_compute_hash, IE_SUCCESS, &correct, &test, input,
115 sizeof(input));
117 SUM_TEST();