aboutsummaryrefslogtreecommitdiff
path: root/test/validation/api/hash/hash.c
blob: d3b01548f1b6ba998a550d66427e12c5b188c107 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* Copyright (c) 2015-2018, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include "config.h"

#include <odp_api.h>
#include <odp_cunit_common.h>

#define CRC32C_INIT 0xffffffff
#define CRC32C_XOR  0xffffffff
#define CRC32_INIT  0xffffffff
#define CRC32_XOR   0xffffffff

typedef struct hash_test_vector_t {
	const uint8_t  *data;
	uint32_t  len;

	union {
		uint32_t u32;
		uint8_t  u8[4];
	} result;

} hash_test_vector_t;

/*
 * Test vectors 0-4 from RFC 7143.
 */
static const uint8_t test_data_0[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static const uint8_t test_data_1[] = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

static const uint8_t test_data_2[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};

static const uint8_t test_data_3[] = {
	0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
	0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
	0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
	0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
};

static const uint8_t test_data_4[] = {
	0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
	0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18,
	0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

/* Various length strings. Terminating null character is not included into
 * crc calculation. */
static const uint8_t test_data_5[] = "abcd";

static const uint8_t test_data_6[] = "abcdefgh";

static const uint8_t test_data_7[] =
	"The quick brown fox jumps over the lazy dog.";

static const uint8_t test_data_8[]  = "a";

static const uint8_t test_data_9[]  = "ab";

static const uint8_t test_data_10[] = "abc";

static const uint8_t test_data_11[] = "abcdefg";

static const uint8_t test_data_12[] = "The five boxing wizards jump quickly.";

/* String of the common "check" value. */
static const uint8_t test_data_13[] = "123456789";

static const hash_test_vector_t crc32c_test_vector[] = {
	{ .data = test_data_0,
	  .len = sizeof(test_data_0),
	  .result.u32 = 0x8a9136aa
	},
	{ .data = test_data_1,
	  .len = sizeof(test_data_1),
	  .result.u32 = 0x62a8ab43
	},
	{ .data = test_data_2,
	  .len = sizeof(test_data_2),
	  .result.u32 = 0x46dd794e
	},
	{ .data = test_data_3,
	  .len = sizeof(test_data_3),
	  .result.u32 = 0x113fdb5c
	},
	{ .data = test_data_4,
	  .len = sizeof(test_data_4),
	  .result.u32 = 0xd9963a56
	},
	{ .data = test_data_5,
	  .len = sizeof(test_data_5) - 1,
	  .result.u32 = 0x92c80a31
	},
	{ .data = test_data_6,
	  .len = sizeof(test_data_6) - 1,
	  .result.u32 = 0x0a9421b7
	},
	{ .data = test_data_7,
	  .len = sizeof(test_data_7) - 1,
	  .result.u32 = 0x190097b3
	},
	{ .data = test_data_8,
	  .len = sizeof(test_data_8) - 1,
	  .result.u32 = 0xc1d04330
	},
	{ .data = test_data_9,
	  .len = sizeof(test_data_9) - 1,
	  .result.u32 = 0xe2a22936
	},
	{ .data = test_data_10,
	  .len = sizeof(test_data_10) - 1,
	  .result.u32 = 0x364b3fb7
	},
	{ .data = test_data_11,
	  .len = sizeof(test_data_11) - 1,
	  .result.u32 = 0xe627f441
	},
	{ .data = test_data_12,
	  .len = sizeof(test_data_12) - 1,
	  .result.u32 = 0xded3059a
	},
	{ .data = test_data_13,
	  .len = sizeof(test_data_13) - 1,
	  .result.u32 = 0xe3069283
	}
};

static const hash_test_vector_t crc32_test_vector[] = {
	{ .data = test_data_0,
	  .len = sizeof(test_data_0),
	  .result.u32 = 0x190a55ad
	},
	{ .data = test_data_1,
	  .len = sizeof(test_data_1),
	  .result.u32 = 0xff6cab0b
	},
	{ .data = test_data_2,
	  .len = sizeof(test_data_2),
	  .result.u32 = 0x91267e8a
	},
	{ .data = test_data_3,
	  .len = sizeof(test_data_3),
	  .result.u32 = 0x9ab0ef72
	},
	{ .data = test_data_4,
	  .len = sizeof(test_data_4),
	  .result.u32 = 0x51e17412
	},
	{ .data = test_data_5,
	  .len = sizeof(test_data_5) - 1,
	  .result.u32 = 0xed82cd11
	},
	{ .data = test_data_6,
	  .len = sizeof(test_data_6) - 1,
	  .result.u32 = 0xaeef2a50
	},
	{ .data = test_data_7,
	  .len = sizeof(test_data_7) - 1,
	  .result.u32 = 0x519025e9
	},
	{ .data = test_data_8,
	  .len = sizeof(test_data_8) - 1,
	  .result.u32 = 0xe8b7be43
	},
	{ .data = test_data_9,
	  .len = sizeof(test_data_9) - 1,
	  .result.u32 = 0x9e83486d
	},
	{ .data = test_data_10,
	  .len = sizeof(test_data_10) - 1,
	  .result.u32 = 0x352441c2
	},
	{ .data = test_data_11,
	  .len = sizeof(test_data_11) - 1,
	  .result.u32 = 0x312a6aa6
	},
	{ .data = test_data_12,
	  .len = sizeof(test_data_12) - 1,
	  .result.u32 = 0xde912acd
	},
	{ .data = test_data_13,
	  .len = sizeof(test_data_13) - 1,
	  .result.u32 = 0xcbf43926
	}
};

static void hash_test_crc32c(void)
{
	uint32_t ret, result;
	int i;
	int num = sizeof(crc32c_test_vector) / sizeof(hash_test_vector_t);

	for (i = 0; i < num; i++) {
		ret = odp_hash_crc32c(crc32c_test_vector[i].data,
				      crc32c_test_vector[i].len,
				      CRC32C_INIT);

		result = CRC32C_XOR ^ ret;
		CU_ASSERT(result == crc32c_test_vector[i].result.u32);
	}
}

static void hash_test_crc32(void)
{
	uint32_t ret, result;
	int i;
	int num = sizeof(crc32_test_vector) / sizeof(hash_test_vector_t);

	for (i = 0; i < num; i++) {
		ret = odp_hash_crc32(crc32_test_vector[i].data,
				     crc32_test_vector[i].len,
				     CRC32_INIT);

		result = CRC32_XOR ^ ret;
		CU_ASSERT(result == crc32_test_vector[i].result.u32);
	}
}

odp_testinfo_t hash_suite[] = {
	ODP_TEST_INFO(hash_test_crc32c),
	ODP_TEST_INFO(hash_test_crc32),
	ODP_TEST_INFO_NULL,
};

odp_suiteinfo_t hash_suites[] = {
	{"Hash", NULL, NULL, hash_suite},
	ODP_SUITE_INFO_NULL
};

int main(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	ret = odp_cunit_register(hash_suites);

	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}