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 | 623590d | 2011-03-28 18:19:31 +0000 | [diff] [blame] | 21 | // Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as
|
| 22 | // the seed
|
| 23 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 24 | for(int i = 0; i < 256; i++)
|
| 25 | {
|
| 26 | key[i] = (uint8_t)i;
|
tanjent@gmail.com | 623590d | 2011-03-28 18:19:31 +0000 | [diff] [blame] | 27 |
|
| 28 | hash(key,i,256-i,&hashes[i*hashbytes]);
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 29 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 30 |
|
tanjent@gmail.com | 623590d | 2011-03-28 18:19:31 +0000 | [diff] [blame] | 31 | // Then hash the result array
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 32 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 33 | hash(hashes,hashbytes*256,0,final);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 34 |
|
tanjent@gmail.com | 623590d | 2011-03-28 18:19:31 +0000 | [diff] [blame] | 35 | // The first four bytes of that hash, interpreted as a little-endian integer, is our
|
| 36 | // verification value
|
| 37 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 38 | uint32_t verification = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) | (final[3] << 24);
|
| 39 |
|
| 40 | delete [] key;
|
| 41 | delete [] hashes;
|
| 42 | delete [] final;
|
| 43 |
|
| 44 | //----------
|
| 45 |
|
| 46 | if(expected != verification)
|
| 47 | {
|
| 48 | if(verbose) printf("Verification value 0x%08X : Failed! (Expected 0x%08x)\n",verification,expected);
|
| 49 | return false;
|
| 50 | }
|
| 51 | else
|
| 52 | {
|
| 53 | if(verbose) printf("Verification value 0x%08X : Passed!\n",verification);
|
| 54 | return true;
|
| 55 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 56 | }
|
| 57 |
|
| 58 | //----------------------------------------------------------------------------
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 59 | // Basic sanity checks -
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 60 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 61 | // 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] | 62 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 63 | // Flipping a bit of a key should, with overwhelmingly high probability,
|
| 64 | // result in a different hash.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 65 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 66 | // Hashing the same key twice should always produce the same result.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 67 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 68 | // 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] | 69 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 70 | bool SanityTest ( pfHash hash, const int hashbits )
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 71 | {
|
| 72 | printf("Running sanity check 1");
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 73 |
|
| 74 | Rand r(883741);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 75 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 76 | bool result = true;
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 77 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 78 | const int hashbytes = hashbits/8;
|
| 79 | const int reps = 10;
|
| 80 | const int keymax = 128;
|
| 81 | const int pad = 16;
|
| 82 | const int buflen = keymax + pad*3;
|
| 83 |
|
| 84 | uint8_t * buffer1 = new uint8_t[buflen];
|
| 85 | uint8_t * buffer2 = new uint8_t[buflen];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 86 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 87 | uint8_t * hash1 = new uint8_t[hashbytes];
|
| 88 | uint8_t * hash2 = new uint8_t[hashbytes];
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 89 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 90 | //----------
|
| 91 |
|
| 92 | for(int irep = 0; irep < reps; irep++)
|
| 93 | {
|
| 94 | if(irep % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 95 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 96 | for(int len = 4; len <= keymax; len++)
|
| 97 | {
|
| 98 | for(int offset = pad; offset < pad*2; offset++)
|
| 99 | {
|
| 100 | uint8_t * key1 = &buffer1[pad];
|
| 101 | uint8_t * key2 = &buffer2[pad+offset];
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 102 |
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 103 | r.rand_p(buffer1,buflen);
|
| 104 | r.rand_p(buffer2,buflen);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 105 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 106 | memcpy(key2,key1,len);
|
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 | hash(key1,len,0,hash1);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 109 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 110 | for(int bit = 0; bit < (len * 8); bit++)
|
| 111 | {
|
| 112 | // 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] | 113 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 114 | flipbit(key2,len,bit);
|
| 115 | hash(key2,len,0,hash2);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 116 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 117 | if(memcmp(hash1,hash2,hashbytes) == 0)
|
| 118 | {
|
| 119 | result = false;
|
| 120 | }
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 121 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 122 | // Flip it back, hash again -> we should get the original result.
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 123 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 124 | flipbit(key2,len,bit);
|
| 125 | hash(key2,len,0,hash2);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 126 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 127 | if(memcmp(hash1,hash2,hashbytes) != 0)
|
| 128 | {
|
| 129 | result = false;
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 135 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 136 | if(result == false)
|
| 137 | {
|
| 138 | printf("*********FAIL*********\n");
|
| 139 | }
|
| 140 | else
|
| 141 | {
|
| 142 | printf("PASS\n");
|
| 143 | }
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 144 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 145 | delete [] hash1;
|
| 146 | delete [] hash2;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 147 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 148 | return result;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 149 | }
|
| 150 |
|
| 151 | //----------------------------------------------------------------------------
|
| 152 | // Appending zero bytes to a key should always cause it to produce a different
|
| 153 | // hash value
|
| 154 |
|
| 155 | void AppendedZeroesTest ( pfHash hash, const int hashbits )
|
| 156 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 157 | printf("Running sanity check 2");
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 158 |
|
| 159 | Rand r(173994);
|
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 | const int hashbytes = hashbits/8;
|
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 | for(int rep = 0; rep < 100; rep++)
|
| 164 | {
|
| 165 | if(rep % 10 == 0) printf(".");
|
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 | unsigned char key[256];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 168 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 169 | memset(key,0,sizeof(key));
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 170 |
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 171 | r.rand_p(key,32);
|
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 | uint32_t h1[16];
|
| 174 | uint32_t h2[16];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 175 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 176 | memset(h1,0,hashbytes);
|
| 177 | memset(h2,0,hashbytes);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 178 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 179 | for(int i = 0; i < 32; i++)
|
| 180 | {
|
| 181 | hash(key,32+i,0,h1);
|
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 | if(memcmp(h1,h2,hashbytes) == 0)
|
| 184 | {
|
| 185 | printf("\n*********FAIL*********\n");
|
| 186 | return;
|
| 187 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 188 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 189 | memcpy(h2,h1,hashbytes);
|
| 190 | }
|
| 191 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 192 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 193 | printf("PASS\n");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 194 | }
|
| 195 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 196 | //-----------------------------------------------------------------------------
|