aboutsummaryrefslogtreecommitdiffstats
path: root/common.h
blob: e70d922a58b43b976ba7c66cb77638fe531a9bb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* See LICENSE file for copyright and license details. */
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
# pragma clang diagnostic ignored "-Wpadded"
# pragma clang diagnostic ignored "-Wcomma"
# pragma clang diagnostic ignored "-Wcovered-switch-default"
#endif
#include "libhashsum.h"
#include <errno.h>
#include <limits.h>
#include <string.h>


#if defined(__GNUC__)
__attribute__((__const__))
#endif
uint8_t libhashsum_reverse_byte__(uint8_t);


#ifdef TEST
# include <stdlib.h>
# include <stdio.h>

struct testcase {
	size_t input_repeat;
	unsigned extra_bits;
	const char *input;
	const char *output;
};

static void
hex(char *out, const unsigned char *in, size_t n)
{
        for (; n--; in++) {
                *out++ = "0123456789abcdef"[(*in >> 4) & 15];
                *out++ = "0123456789abcdef"[(*in >> 0) & 15];
        }
        *out = '\0';
}

static char *
escape(const char *s, size_t n)
{
	char *ret, *p;

	ret = malloc(n * 4U + 1U);
	if (!ret) {
		perror("malloc");
		exit(2);
	}

	for (p = ret; n--; s++) {
		if (*s == '"' || *s == '\\') {
			*p++ = '\\';
			*p++ = *s;
		} else if (*s == '\n') {
			*p++ = '\\';
			*p++ = 'n';
		} else if (*s < ' ' || *s >= 127) {
			*p++ = '\\';
			*p++ = 'x';
			*p++ = "0123456789abcdef"[(*s >> 4) & 15];
			*p++ = "0123456789abcdef"[(*s >> 0) & 15];
		} else {
			*p++ = *s;
		}
	}
	*p = '\0';

	return ret;
}

static int
run_tests(const char *name, enum libhashsum_algorithm algorithm, size_t hash_size,
          struct testcase *testcases, size_t ntestcases, char hexsum[])
{
	struct libhashsum_hasher hasher;
	char *input, *p, bitstr[sizeof(" + b\"1234567\"")];
	unsigned char extra_bit;
	int ok = 1, caseok;
	size_t i, j, input_string_len, input_total_len, input_size;
	size_t bits;
	for (i = 0; i < ntestcases; i++) {
		if (libhashsum_init_hasher(&hasher, algorithm)) {
			perror("libhashsum_init_hasher");
			return 2;
		}
		if (hasher.algorithm != algorithm) {
			fprintf(stderr, "libhashsum_init_hasher returned unexpected value in .algorithm\n");
			return 2;
		}
		if (hasher.hash_size != hash_size) {
			fprintf(stderr, "libhashsum_init_hasher returned unexpected value in .hash_size\n");
			return 2;
		}
		if (hasher.hash_output) {
			fprintf(stderr, "libhashsum_init_hasher returned non-NULL pointer in .hash_output\n");
			return 2;
		}
		if (!hasher.process) {
			fprintf(stderr, "libhashsum_init_hasher returned NULL pointer in .process\n");
			return 2;
		}
		if (!hasher.finalise_const) {
			fprintf(stderr, "libhashsum_init_hasher returned NULL pointer in .finalise_const\n");
			return 2;
		}
		if (!hasher.finalise) {
			fprintf(stderr, "libhashsum_init_hasher returned NULL pointer in .finalise\n");
			return 2;
		}
		input_string_len = strlen(testcases[i].input);
		bits = testcases[i].extra_bits;
		if (bits) {
			if (input_string_len < (bits + 7U) / 8U)
				input_string_len = (bits + 7U) / 8U;
			bits %= 8U;
			if (bits)
				input_string_len -= 1U;
		}
		input_total_len = testcases[i].input_repeat * input_string_len;
		input_size = input_total_len + hasher.input_block_size + 1U;
		p = input = malloc(input_size);
		if (!input) {
			perror("malloc");
			return 2;
		}
		for (j = 0; j < testcases[i].input_repeat; j++) {
			p = memcpy(p, testcases[i].input, input_string_len);
			p = &p[input_string_len];
		}
		if (hasher.finalise(&hasher, input, input_total_len, (unsigned)bits, input_size)) {
			perror("hasher.finalise");
			return 2;
		}
		if (!hasher.hash_output) {
			fprintf(stderr, "hasher.finalise did not set hasher.hash_output\n");
			return 2;
		}
		free(input);
		hex(hexsum, hasher.hash_output, hasher.hash_size);
		ok &= caseok = !testcases[i].output || !strcmp(hexsum, testcases[i].output);
		input = escape(testcases[i].input, input_string_len);
		bitstr[0] = '\0';
		if (bits) {
			extra_bit = (unsigned char)testcases[i].input[input_string_len];
			p = bitstr;
			*p++ = ' ';
			*p++ = '+';
			*p++ = ' ';
			*p++ = 'b';
			*p++ = '"';
			while (bits--) {
				*p++ = "01"[(extra_bit >> 7) & 1U];
				extra_bit <<= 1;
			}
			*p++ = '"';
			*p = '\0';
		}
		if (testcases[i].input_repeat == 1)
			printf("[\033[1;%s\033[m] %s(\"%s\"%s) = %s\n",
			       caseok ? "32mPASS" : "31mFAIL", name, input, bitstr, hexsum);
		else
			printf("[\033[1;%s\033[m] %s(%zu * \"%s\"%s) = %s\n",
			       caseok ? "32mPASS" : "31mFAIL", name, testcases[i].input_repeat, input, bitstr, hexsum);
		free(input);
	}
	return !ok;
}

# define TEST_MAIN(NAME, ID)\
	char hexsum[LIBHASHSUM_##ID##_HASH_SIZE * 2 + 1];\
	return run_tests(NAME, LIBHASHSUM_##ID, LIBHASHSUM_##ID##_HASH_SIZE,\
	                 testcases, sizeof(testcases) / sizeof(*testcases), hexsum)

#endif