blob: 6436826a45b78a07096727a8f24b5ebde6d4e626 [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//----------------------------------------------------------------------------
tanjent@gmail.combabb5532011-02-28 06:03:12 +000032// Basic sanity checks -
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000033
tanjent@gmail.combabb5532011-02-28 06:03:12 +000034// A hash function should not be reading outside the bounds of the key.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000035
tanjent@gmail.combabb5532011-02-28 06:03:12 +000036// Flipping a bit of a key should, with overwhelmingly high probability,
37// result in a different hash.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000038
tanjent@gmail.combabb5532011-02-28 06:03:12 +000039// Hashing the same key twice should always produce the same result.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000040
tanjent@gmail.combabb5532011-02-28 06:03:12 +000041// The memory alignment of the key should not affect the hash result.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000042
tanjent@gmail.combabb5532011-02-28 06:03:12 +000043bool SanityTest ( pfHash hash, const int hashbits )
44{ printf("Testing bit twiddling");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000045
tanjent@gmail.com9d17d0b2010-11-17 04:07:51 +000046 bool result = true;
47
tanjent@gmail.combabb5532011-02-28 06:03:12 +000048 const int hashbytes = hashbits/8;
49 const int reps = 10;
50 const int keymax = 128;
51 const int pad = 16;
52 const int buflen = keymax + pad*3;
53
54 uint8_t * buffer1 = new uint8_t[buflen];
55 uint8_t * buffer2 = new uint8_t[buflen];
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000056
tanjent@gmail.combabb5532011-02-28 06:03:12 +000057 uint8_t * hash1 = new uint8_t[hashbytes];
58 uint8_t * hash2 = new uint8_t[hashbytes];
59
60 //----------
61
62 for(int irep = 0; irep < reps; irep++)
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000063 {
tanjent@gmail.combabb5532011-02-28 06:03:12 +000064 if(irep % (reps/10) == 0) printf(".");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000065
tanjent@gmail.combabb5532011-02-28 06:03:12 +000066 for(int len = 4; len <= keymax; len++)
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000067 {
tanjent@gmail.combabb5532011-02-28 06:03:12 +000068 for(int offset = pad; offset < pad*2; offset++)
69 {
70 uint8_t * key1 = &buffer1[pad];
71 uint8_t * key2 = &buffer2[pad+offset];
72
73 rand_p(buffer1,buflen);
74 rand_p(buffer2,buflen);
75
76 memcpy(key2,key1,len);
77
78 hash(key1,len,0,hash1);
79
80 for(int bit = 0; bit < (len * 8); bit++)
81 {
82 // Flip a bit, hash the key -> we should get a different result.
83
84 flipbit(key2,len,bit);
85 hash(key2,len,0,hash2);
86
87 if(memcmp(hash1,hash2,hashbytes) == 0)
88 {
89 result = false;
90 }
91
92 // Flip it back, hash again -> we should get the original result.
93
94 flipbit(key2,len,bit);
95 hash(key2,len,0,hash2);
96
97 if(memcmp(hash1,hash2,hashbytes) != 0)
98 {
99 result = false;
100 }
101 }
102 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000103 }
104 }
105
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000106 if(result == false)
tanjent@gmail.com9d17d0b2010-11-17 04:07:51 +0000107 {
108 printf("*********FAIL*********\n");
109 }
110 else
111 {
112 printf("PASS\n");
113 }
114
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000115 delete [] hash1;
116 delete [] hash2;
117
118 return result;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000119}
120
121//----------------------------------------------------------------------------
122// Appending zero bytes to a key should always cause it to produce a different
123// hash value
124
125void AppendedZeroesTest ( pfHash hash, const int hashbits )
126{
127 const int hashbytes = hashbits/8;
128
129 printf("Testing zero-appending");
130
131 for(int rep = 0; rep < 100; rep++)
132 {
133 if(rep % 10 == 0) printf(".");
134
135 unsigned char key[256];
136
137 memset(key,0,sizeof(key));
138
139 rand_p(key,32);
140
141 uint32_t h1[16];
142 uint32_t h2[16];
143
144 memset(h1,0,hashbytes);
145 memset(h2,0,hashbytes);
146
147 for(int i = 0; i < 32; i++)
148 {
149 hash(key,32+i,0,h1);
150
151 if(memcmp(h1,h2,hashbytes) == 0)
152 {
153 printf("\n*********FAIL*********\n");
154 return;
155 }
156
157 memcpy(h2,h1,hashbytes);
158 }
159 }
160
161 printf("PASS\n");
162}
163
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000164//-----------------------------------------------------------------------------