tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 1 | #include "KeysetTest.h"
|
| 2 |
|
| 3 | #include "Random.h"
|
| 4 |
|
| 5 | //-----------------------------------------------------------------------------
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 6 | // This should hopefully be a thorough and uambiguous test of whether a hash
|
| 7 | // is correctly implemented on a given platform
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 8 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 9 | bool VerificationTest ( pfHash hash, const int hashbits, uint32_t expected, bool verbose )
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 10 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 11 | const int hashbytes = hashbits / 8;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 12 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 13 | 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.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 16 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 17 | memset(key,0,256);
|
| 18 | memset(hashes,0,hashbytes*256);
|
| 19 | memset(final,0,hashbytes);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 20 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 21 | 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.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 27 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 28 | //----------
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 29 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 30 | hash(hashes,hashbytes*256,0,final);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 31 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 32 | 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.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 50 | }
|
| 51 |
|
| 52 | //----------------------------------------------------------------------------
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 53 | // Basic sanity checks -
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 54 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 55 | // A hash function should not be reading outside the bounds of the key.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 56 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 57 | // Flipping a bit of a key should, with overwhelmingly high probability,
|
| 58 | // result in a different hash.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 59 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 60 | // Hashing the same key twice should always produce the same result.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 61 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 62 | // The memory alignment of the key should not affect the hash result.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 63 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 64 | bool SanityTest ( pfHash hash, const int hashbits )
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 65 | {
|
| 66 | printf("Running sanity check 1");
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame^] | 67 |
|
| 68 | Rand r(883741);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 69 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 70 | bool result = true;
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 71 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 72 | const int hashbytes = hashbits/8;
|
| 73 | const int reps = 10;
|
| 74 | const int keymax = 128;
|
| 75 | const int pad = 16;
|
| 76 | const int buflen = keymax + pad*3;
|
| 77 |
|
| 78 | uint8_t * buffer1 = new uint8_t[buflen];
|
| 79 | uint8_t * buffer2 = new uint8_t[buflen];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 80 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 81 | uint8_t * hash1 = new uint8_t[hashbytes];
|
| 82 | uint8_t * hash2 = new uint8_t[hashbytes];
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 83 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 84 | //----------
|
| 85 |
|
| 86 | for(int irep = 0; irep < reps; irep++)
|
| 87 | {
|
| 88 | if(irep % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 89 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 90 | for(int len = 4; len <= keymax; len++)
|
| 91 | {
|
| 92 | for(int offset = pad; offset < pad*2; offset++)
|
| 93 | {
|
| 94 | uint8_t * key1 = &buffer1[pad];
|
| 95 | uint8_t * key2 = &buffer2[pad+offset];
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 96 |
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame^] | 97 | r.rand_p(buffer1,buflen);
|
| 98 | r.rand_p(buffer2,buflen);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 99 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 100 | memcpy(key2,key1,len);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 101 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 102 | hash(key1,len,0,hash1);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 103 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 104 | for(int bit = 0; bit < (len * 8); bit++)
|
| 105 | {
|
| 106 | // Flip a bit, hash the key -> we should get a different result.
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 107 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 108 | flipbit(key2,len,bit);
|
| 109 | hash(key2,len,0,hash2);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 110 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 111 | if(memcmp(hash1,hash2,hashbytes) == 0)
|
| 112 | {
|
| 113 | result = false;
|
| 114 | }
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 115 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 116 | // Flip it back, hash again -> we should get the original result.
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 117 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 118 | flipbit(key2,len,bit);
|
| 119 | hash(key2,len,0,hash2);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 120 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 121 | if(memcmp(hash1,hash2,hashbytes) != 0)
|
| 122 | {
|
| 123 | result = false;
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 129 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 130 | if(result == false)
|
| 131 | {
|
| 132 | printf("*********FAIL*********\n");
|
| 133 | }
|
| 134 | else
|
| 135 | {
|
| 136 | printf("PASS\n");
|
| 137 | }
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 138 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 139 | delete [] hash1;
|
| 140 | delete [] hash2;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 141 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 142 | return result;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 143 | }
|
| 144 |
|
| 145 | //----------------------------------------------------------------------------
|
| 146 | // Appending zero bytes to a key should always cause it to produce a different
|
| 147 | // hash value
|
| 148 |
|
| 149 | void AppendedZeroesTest ( pfHash hash, const int hashbits )
|
| 150 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 151 | printf("Running sanity check 2");
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame^] | 152 |
|
| 153 | Rand r(173994);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 154 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 155 | const int hashbytes = hashbits/8;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 156 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 157 | for(int rep = 0; rep < 100; rep++)
|
| 158 | {
|
| 159 | if(rep % 10 == 0) printf(".");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 160 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 161 | unsigned char key[256];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 162 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 163 | memset(key,0,sizeof(key));
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 164 |
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame^] | 165 | r.rand_p(key,32);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 166 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 167 | uint32_t h1[16];
|
| 168 | uint32_t h2[16];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 169 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 170 | memset(h1,0,hashbytes);
|
| 171 | memset(h2,0,hashbytes);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 172 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 173 | for(int i = 0; i < 32; i++)
|
| 174 | {
|
| 175 | hash(key,32+i,0,h1);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 176 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 177 | if(memcmp(h1,h2,hashbytes) == 0)
|
| 178 | {
|
| 179 | printf("\n*********FAIL*********\n");
|
| 180 | return;
|
| 181 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 182 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 183 | memcpy(h2,h1,hashbytes);
|
| 184 | }
|
| 185 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 186 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 187 | printf("PASS\n");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 188 | }
|
| 189 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 190 | //-----------------------------------------------------------------------------
|