blob: a59fda432d2cba034be3401f0ba7a664cbb29158 [file] [log] [blame]
tanjent@gmail.comad4b3632010-11-05 01:20:58 +00001#include "KeysetTest.h"
2
3#include "Random.h"
4
5//-----------------------------------------------------------------------------
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +00006// This should hopefully be a thorough and uambiguous test of whether a hash
7// is correctly implemented on a given platform
tanjent@gmail.comad4b3632010-11-05 01:20:58 +00008
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +00009bool VerificationTest ( pfHash hash, const int hashbits, uint32_t expected, bool verbose )
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000010{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000011 const int hashbytes = hashbits / 8;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000012
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000013 uint8_t * key = new uint8_t[256];
14 uint8_t * hashes = new uint8_t[hashbytes * 256];
15 uint8_t * final = new uint8_t[hashbytes];
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000016
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000017 memset(key,0,256);
18 memset(hashes,0,hashbytes*256);
19 memset(final,0,hashbytes);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000020
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000021 for(int i = 0; i < 256; i++)
22 {
23 key[i] = (uint8_t)i;
24
25 hash(key,i,0,&hashes[i*hashbytes]);
26 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000027
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000028 //----------
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000029
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000030 hash(hashes,hashbytes*256,0,final);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000031
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000032 uint32_t verification = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) | (final[3] << 24);
33
34 delete [] key;
35 delete [] hashes;
36 delete [] final;
37
38 //----------
39
40 if(expected != verification)
41 {
42 if(verbose) printf("Verification value 0x%08X : Failed! (Expected 0x%08x)\n",verification,expected);
43 return false;
44 }
45 else
46 {
47 if(verbose) printf("Verification value 0x%08X : Passed!\n",verification);
48 return true;
49 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000050}
51
52//----------------------------------------------------------------------------
tanjent@gmail.combabb5532011-02-28 06:03:12 +000053// Basic sanity checks -
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000054
tanjent@gmail.combabb5532011-02-28 06:03:12 +000055// A hash function should not be reading outside the bounds of the key.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000056
tanjent@gmail.combabb5532011-02-28 06:03:12 +000057// Flipping a bit of a key should, with overwhelmingly high probability,
58// result in a different hash.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000059
tanjent@gmail.combabb5532011-02-28 06:03:12 +000060// Hashing the same key twice should always produce the same result.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000061
tanjent@gmail.combabb5532011-02-28 06:03:12 +000062// The memory alignment of the key should not affect the hash result.
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000063
tanjent@gmail.combabb5532011-02-28 06:03:12 +000064bool SanityTest ( pfHash hash, const int hashbits )
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000065{
66 printf("Running sanity check 1");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000067
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000068 bool result = true;
tanjent@gmail.com9d17d0b2010-11-17 04:07:51 +000069
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000070 const int hashbytes = hashbits/8;
71 const int reps = 10;
72 const int keymax = 128;
73 const int pad = 16;
74 const int buflen = keymax + pad*3;
75
76 uint8_t * buffer1 = new uint8_t[buflen];
77 uint8_t * buffer2 = new uint8_t[buflen];
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000078
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000079 uint8_t * hash1 = new uint8_t[hashbytes];
80 uint8_t * hash2 = new uint8_t[hashbytes];
tanjent@gmail.combabb5532011-02-28 06:03:12 +000081
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000082 //----------
83
84 for(int irep = 0; irep < reps; irep++)
85 {
86 if(irep % (reps/10) == 0) printf(".");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000087
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000088 for(int len = 4; len <= keymax; len++)
89 {
90 for(int offset = pad; offset < pad*2; offset++)
91 {
92 uint8_t * key1 = &buffer1[pad];
93 uint8_t * key2 = &buffer2[pad+offset];
tanjent@gmail.combabb5532011-02-28 06:03:12 +000094
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000095 rand_p(buffer1,buflen);
96 rand_p(buffer2,buflen);
tanjent@gmail.combabb5532011-02-28 06:03:12 +000097
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000098 memcpy(key2,key1,len);
tanjent@gmail.combabb5532011-02-28 06:03:12 +000099
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000100 hash(key1,len,0,hash1);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000101
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000102 for(int bit = 0; bit < (len * 8); bit++)
103 {
104 // Flip a bit, hash the key -> we should get a different result.
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000105
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000106 flipbit(key2,len,bit);
107 hash(key2,len,0,hash2);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000108
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000109 if(memcmp(hash1,hash2,hashbytes) == 0)
110 {
111 result = false;
112 }
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000113
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000114 // Flip it back, hash again -> we should get the original result.
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000115
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000116 flipbit(key2,len,bit);
117 hash(key2,len,0,hash2);
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000118
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000119 if(memcmp(hash1,hash2,hashbytes) != 0)
120 {
121 result = false;
122 }
123 }
124 }
125 }
126 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000127
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000128 if(result == false)
129 {
130 printf("*********FAIL*********\n");
131 }
132 else
133 {
134 printf("PASS\n");
135 }
tanjent@gmail.com9d17d0b2010-11-17 04:07:51 +0000136
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000137 delete [] hash1;
138 delete [] hash2;
tanjent@gmail.combabb5532011-02-28 06:03:12 +0000139
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000140 return result;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000141}
142
143//----------------------------------------------------------------------------
144// Appending zero bytes to a key should always cause it to produce a different
145// hash value
146
147void AppendedZeroesTest ( pfHash hash, const int hashbits )
148{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000149 printf("Running sanity check 2");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000150
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000151 const int hashbytes = hashbits/8;
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000152
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000153 for(int rep = 0; rep < 100; rep++)
154 {
155 if(rep % 10 == 0) printf(".");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000156
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000157 unsigned char key[256];
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000158
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000159 memset(key,0,sizeof(key));
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000160
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000161 rand_p(key,32);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000162
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000163 uint32_t h1[16];
164 uint32_t h2[16];
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000165
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000166 memset(h1,0,hashbytes);
167 memset(h2,0,hashbytes);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000168
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000169 for(int i = 0; i < 32; i++)
170 {
171 hash(key,32+i,0,h1);
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000172
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000173 if(memcmp(h1,h2,hashbytes) == 0)
174 {
175 printf("\n*********FAIL*********\n");
176 return;
177 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000178
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000179 memcpy(h2,h1,hashbytes);
180 }
181 }
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000182
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +0000183 printf("PASS\n");
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000184}
185
tanjent@gmail.comad4b3632010-11-05 01:20:58 +0000186//-----------------------------------------------------------------------------