summaryrefslogtreecommitdiffstats
path: root/src/crypto/isa-l/isa-l_crypto/rolling_hash/rolling_hash2_test.c
blob: ee45c120d9e4cd34e6bce95536a003480aeb8f57 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**********************************************************************
  Copyright(c) 2011-2017 Intel Corporation All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the
      distribution.
    * Neither the name of Intel Corporation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************/

#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "rolling_hashx.h"

#ifndef FUT_run
# define FUT_run rolling_hash2_run
#endif
#ifndef FUT_init
# define FUT_init rolling_hash2_init
#endif
#ifndef FUT_reset
# define FUT_reset rolling_hash2_reset
#endif
#ifndef FUT_ref
# define FUT_ref rolling_hash2_ref
#endif

#define str(s) #s
#define xstr(s) str(s)

#define MAX_BUFFER_SIZE 128*1024*1024
#define MAX_ROLLING_HASH_WIDTH 32

#ifndef RANDOMS
# define RANDOMS 200
#endif
#ifndef TEST_SEED
# define TEST_SEED 0x1234
#endif

static
uint64_t rolling_hash2_ref(struct rh_state2 *state, unsigned char *p, int len,
			   uint64_t hash_init)
{
	int i;
	uint64_t h = hash_init;

	for (i = 0; i < len; i++) {
		h = (h << 1) | (h >> (64 - 1));
		h ^= state->table1[*p++];
	}
	return h;
}

int ones_in_mask(uint32_t in)
{
	int count;

	for (count = 0; in != 0; in &= (in - 1))
		count++;

	return count;
}

/*
 * Utility function to pick a random mask.  Not uniform in number of bits.
 */
uint32_t pick_rand_mask_in_range(int min_bits, int max_bits)
{
	uint32_t mask = 0;
	int ones;

	do {
		mask = rand();
#if defined(_WIN32) || defined(_WIN64)
		mask = (mask << 16) ^ rand();
#endif
		ones = ones_in_mask(mask);
	} while (ones < min_bits || ones > max_bits);

	return mask;
}

int main(void)
{
	uint8_t *buffer;
	uint64_t hash;
	uint32_t mask, trigger, offset = 0;
	int i, w, r, ret, max, errors = 0;
	uint32_t offset_fut;
	struct rh_state2 state;

	printf(xstr(FUT_run) ": " xstr(MAX_BUFFER_SIZE));

	buffer = malloc(MAX_BUFFER_SIZE);
	if (buffer == NULL) {
		printf("cannot allocate mem\n");
		return -1;
	}
	srand(TEST_SEED);

	// Test case 1, compare trigger case at boundary with reference hash
	w = 32;
	mask = 0xffff0;
	trigger = 0x3df0;
	trigger &= mask;

	for (i = 0; i < MAX_BUFFER_SIZE; i++)
		buffer[i] = rand();

	FUT_init(&state, w);
	FUT_reset(&state, buffer);

	uint8_t *p = buffer;
	int remain = MAX_BUFFER_SIZE;
	ret = FINGERPRINT_RET_HIT;

	while ((ret == FINGERPRINT_RET_HIT) && (remain > 0)) {
		ret = FUT_run(&state, p, remain, mask, trigger, &offset);

		if (offset > remain) {
			printf(" error offset past remaining limit\n");
			errors++;
		}

		if ((ret == FINGERPRINT_RET_HIT) && (&p[offset] > &buffer[w])) {
			hash = FUT_ref(&state, &p[offset] - w, w, 0);
			if ((hash & mask) != trigger) {
				printf("   mismatch chunk from ref");
				printf(" hit: offset=%d %lx %lx\n", offset, state.hash, hash);
				errors++;
			}
		}
		p += offset;
		remain -= offset;
		putchar('.');
	}

	putchar('.');		// Finished test 1

	// Test case 2, check if reference function hits same chunk boundary as test

	w = 32;
	mask = 0xffff;
	trigger = rand();
	trigger &= mask;
	p = buffer;

	// Function under test
	FUT_init(&state, w);
	FUT_reset(&state, p);
	ret = FUT_run(&state, p + w, MAX_BUFFER_SIZE - w, mask, trigger, &offset_fut);
	offset_fut += w;

	// Reference
	for (p++, offset = w + 1; offset < MAX_BUFFER_SIZE; offset++) {
		hash = FUT_ref(&state, p++, w, 0);
		if ((hash & mask) == trigger)
			break;
	}

	if (offset != offset_fut) {
		printf("\ncase 2, offset of chunk different from ref\n");
		printf("  case 2: stop fut at offset=%d\n", offset_fut);
		printf("  case 2: stop ref at offset=%d\n", offset);
		errors++;
		return errors;
	}
	putchar('.');		// Finished test 2

	// Do case 2 above with random args

	for (r = 0; r < RANDOMS; r++) {
		w = rand() % MAX_ROLLING_HASH_WIDTH;
		if (w < 3)
			continue;

		mask = pick_rand_mask_in_range(4, 20);
		trigger = rand() & mask;
		p = buffer;

		// Function under test
		FUT_init(&state, w);
		FUT_reset(&state, p);
		ret = FUT_run(&state, p + w, MAX_BUFFER_SIZE - w, mask, trigger, &offset_fut);
		offset_fut += w;

		// Reference
		for (p++, offset = w + 1; offset < MAX_BUFFER_SIZE; offset++) {
			hash = FUT_ref(&state, p++, w, 0);
			if ((hash & mask) == trigger)
				break;
		}

		if (offset != offset_fut) {
			printf("\nrand case 2 #%d: w=%d, mask=0x%x, trigger=0x%x\n", r, w,
			       mask, trigger);
			printf("  offset of chunk different from ref\n");
			printf("  case 2r: stop fut at offset=%d\n", offset_fut);
			printf("  case 2r: stop ref at offset=%d\n", offset);
			errors++;
			return errors;
		}
		putchar('.');
	}

	// Test case 3, check if max bound is same

	w = 32;
	mask = 0xfffff;
	trigger = rand();
	trigger &= mask;
	putchar('|');

	for (max = w + 1; max < 500; max++) {
		p = buffer;
		FUT_init(&state, w);
		FUT_reset(&state, p);

		ret = FUT_run(&state, p + w, max - w, mask, trigger, &offset_fut);
		offset_fut += w;

		int ret_ref = FINGERPRINT_RET_MAX;
		for (p++, offset = w + 1; offset < max; offset++) {
			hash = FUT_ref(&state, p++, w, 0);
			if ((hash & mask) == trigger) {
				ret_ref = FINGERPRINT_RET_HIT;
				break;
			}
		}

		if (offset != offset_fut || ret != ret_ref) {
			printf("\ncase 3 max=%d, offset of chunk different from ref\n", max);
			printf("  case 3: stop fut at offset=%d\n", offset_fut);
			printf("  case 3: stop ref at offset=%d\n", offset);
			printf("  case 3: ret_fut=%d ret_ref=%d\n", ret, ret_ref);
			errors++;
			return errors;
		}
		putchar('.');	// Finished test 3
	}

	// Test case 4, check if max bound is same under random params

	for (r = 0; r < RANDOMS; r++) {
		p = buffer;
		mask = pick_rand_mask_in_range(24, 30);	// Pick an unlikely mask
		trigger = rand() & mask;
		w = rand() % MAX_ROLLING_HASH_WIDTH;
		max = rand() % 1024;

		if (w < 3 || max < 2 * MAX_ROLLING_HASH_WIDTH)
			continue;

		FUT_init(&state, w);
		FUT_reset(&state, p);

		ret = FUT_run(&state, p, max, mask, trigger, &offset_fut);

		if (offset_fut <= w)
			continue;

		int ret_ref = FINGERPRINT_RET_MAX;
		for (p++, offset = w + 1; offset < max; offset++) {
			hash = FUT_ref(&state, p++, w, 0);
			if ((hash & mask) == trigger) {
				ret_ref = FINGERPRINT_RET_HIT;
				break;
			}
		}

		if (offset != offset_fut || ret != ret_ref) {
			printf("\ncase 4 rand case different from ref, max=%d w=%d\n", max, w);
			printf("  case 4: stop fut at offset=%d\n", offset_fut);
			printf("  case 4: stop ref at offset=%d\n", offset);
			printf("  case 4: ret_fut=%d ret_ref=%d\n", ret, ret_ref);
			errors++;
			return errors;
		}
		putchar('.');	// Finished test 4

		if (ret == FINGERPRINT_RET_HIT) {
			p[-1] = rand();	// Keep hits from repeating
		}
	}

	if (errors > 0)
		printf(" Fail: %d\n", errors);
	else
		printf(" Pass\n");
	return errors;
}