blob: 3424ce6ef1dca65d3acbd9be564d28a719e2d0ef [file] [log] [blame]
tanjent@gmail.comad4b3632010-11-05 01:20:58 +00001#include "KeysetTest.h"
2
3#include "Random.h"
4
5//-----------------------------------------------------------------------------
6
7void QuickBrownFox ( pfHash hash, const int hashbits )
8{
9 const int hashbytes = hashbits / 8;
10
11 const char * text1 = "The quick brown fox jumps over the lazy dog";
12 const char * text2 = "The quick brown fox jumps over the lazy cog";
13
14 uint8_t h1[128];
15 uint8_t h2[128];
16
17 hash(text1,(int)strlen(text1),0,h1);
18 hash(text2,(int)strlen(text2),0,h2);
19
20 printf("\"%s\" => ",text1);
21 printhex32(h1,hashbytes);
22 printf("\n");
23
24 printf("\"%s\" => ",text2);
25 printhex32(h2,hashbytes);
26 printf("\n");
27
28 printf("\n");
29}
30
31//----------------------------------------------------------------------------
32// Alignment of the keys should not affect the hash value - if it does,
33// something is broken.
34
35void AlignmentTest ( pfHash hash, const int hashbits )
36{
37 const int hashbytes = hashbits / 8;
38
39 printf("Testing alignment handling on small keys..........");
40
41 char bufs[16][64];
42
43 char * strings[16];
44
45 for(int i = 0; i < 16; i++)
46 {
47 uint32_t b = uint32_t(&bufs[i][0]);
48
49 b = (b+15)&(~15);
50
51 strings[i] = (char*)(b + i);
52
53 strcpy_s(strings[i],32,"DeadBeefDeadBeef");
54 }
55
56 uint32_t hash1[64];
57 uint32_t hash2[64];
58
59 for(int k = 1; k <= 16; k++)
60 for(int j = 0; j < 15; j++)
61 for(int i = j+1; i < 16; i++)
62 {
63 const char * s1 = strings[i];
64 const char * s2 = strings[j];
65
66 hash(s1,k,0,hash1);
67 hash(s2,k,0,hash2);
68
69 if(memcmp(hash1,hash2,hashbytes) != 0)
70 {
71 printf("*********FAIL*********\n");
72 return;
73 }
74 }
75
76 printf("PASS\n");
77}
78
79//----------------------------------------------------------------------------
80// Appending zero bytes to a key should always cause it to produce a different
81// hash value
82
83void AppendedZeroesTest ( pfHash hash, const int hashbits )
84{
85 const int hashbytes = hashbits/8;
86
87 printf("Testing zero-appending");
88
89 for(int rep = 0; rep < 100; rep++)
90 {
91 if(rep % 10 == 0) printf(".");
92
93 unsigned char key[256];
94
95 memset(key,0,sizeof(key));
96
97 rand_p(key,32);
98
99 uint32_t h1[16];
100 uint32_t h2[16];
101
102 memset(h1,0,hashbytes);
103 memset(h2,0,hashbytes);
104
105 for(int i = 0; i < 32; i++)
106 {
107 hash(key,32+i,0,h1);
108
109 if(memcmp(h1,h2,hashbytes) == 0)
110 {
111 printf("\n*********FAIL*********\n");
112 return;
113 }
114
115 memcpy(h2,h1,hashbytes);
116 }
117 }
118
119 printf("PASS\n");
120}
121
122//----------------------------------------------------------------------------
123// Basic sanity checks -
124
125// A hash function should not be reading outside the bounds of the key.
126
127// Flipping a bit of a key should, with overwhelmingly high probability,
128// result in a different hash.
129
130// Hashing the same key twice should always produce the same result.
131
132bool SanityTest ( pfHash hash, const int hashbits )
133{
134 bool result = true;
135
136 const int hashbytes = hashbits/8;
137 const int reps = 100;
138
139 printf("Testing bit twiddling");
140
141 uint8_t buffer[256];
142 uint8_t * key = &buffer[64];
143
144 uint8_t * h1 = new uint8_t[hashbytes];
145 uint8_t * h2 = new uint8_t[hashbytes];
146
147 for(int irep = 0; irep < reps; irep++)
148 {
149 if(irep % (reps/10) == 0) printf(".");
150
151 for(int len = 1; len <= 128; len++)
152 {
153 // Generate a random key in the middle of the buffer, hash it,
154 // and then fill the space around the key with garbage. If a
155 // broken hash function reads past the ends of the key, it should
156 // fail the "did we get the same hash?" test below.
157
158 rand_p(key,len);
159 hash(key,len,0,h1);
160
161 rand_p(buffer,64);
162 rand_p(key+len,64);
163
164 // Flip a bit, hash the key -> we should get a different result.
165 // Flip it back, hash again -> we should get the same result.
166
167 for(int bit = 0; bit < (len * 8); bit++)
168 {
169 flipbit(key,len,bit);
170 hash(key,len,0,h2);
171
172 if(memcmp(h1,h2,hashbytes) == 0)
173 {
174 result = false;
175 }
176
177 flipbit(key,len,bit);
178 hash(key,len,0,h2);
179
180 if(memcmp(h1,h2,hashbytes) != 0)
181 {
182 result = false;
183 }
184 }
185 }
186 }
187
188 if(result == false)
189 {
190 printf("*********FAIL*********\n");
191 }
192 else
193 {
194 printf("PASS\n");
195 }
196
197 delete [] h1;
198 delete [] h2;
199
200 return result;
201}
202
203//-----------------------------------------------------------------------------