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");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 67 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 68 | bool result = true;
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 69 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 70 | 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.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 78 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 79 | uint8_t * hash1 = new uint8_t[hashbytes];
|
| 80 | uint8_t * hash2 = new uint8_t[hashbytes];
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 81 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 82 | //----------
|
| 83 |
|
| 84 | for(int irep = 0; irep < reps; irep++)
|
| 85 | {
|
| 86 | if(irep % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 87 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 88 | 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.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 94 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 95 | rand_p(buffer1,buflen);
|
| 96 | rand_p(buffer2,buflen);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 97 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 98 | memcpy(key2,key1,len);
|
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 | hash(key1,len,0,hash1);
|
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 | for(int bit = 0; bit < (len * 8); bit++)
|
| 103 | {
|
| 104 | // 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] | 105 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 106 | flipbit(key2,len,bit);
|
| 107 | hash(key2,len,0,hash2);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 108 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 109 | if(memcmp(hash1,hash2,hashbytes) == 0)
|
| 110 | {
|
| 111 | result = false;
|
| 112 | }
|
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 | // Flip it back, hash again -> we should get the original result.
|
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 | flipbit(key2,len,bit);
|
| 117 | hash(key2,len,0,hash2);
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 118 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 119 | if(memcmp(hash1,hash2,hashbytes) != 0)
|
| 120 | {
|
| 121 | result = false;
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 127 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 128 | if(result == false)
|
| 129 | {
|
| 130 | printf("*********FAIL*********\n");
|
| 131 | }
|
| 132 | else
|
| 133 | {
|
| 134 | printf("PASS\n");
|
| 135 | }
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 136 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 137 | delete [] hash1;
|
| 138 | delete [] hash2;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 139 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 140 | return result;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 141 | }
|
| 142 |
|
| 143 | //----------------------------------------------------------------------------
|
| 144 | // Appending zero bytes to a key should always cause it to produce a different
|
| 145 | // hash value
|
| 146 |
|
| 147 | void AppendedZeroesTest ( pfHash hash, const int hashbits )
|
| 148 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 149 | printf("Running sanity check 2");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 150 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 151 | const int hashbytes = hashbits/8;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 152 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 153 | for(int rep = 0; rep < 100; rep++)
|
| 154 | {
|
| 155 | if(rep % 10 == 0) printf(".");
|
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 | unsigned char key[256];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 158 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 159 | memset(key,0,sizeof(key));
|
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 | rand_p(key,32);
|
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 | uint32_t h1[16];
|
| 164 | uint32_t h2[16];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 165 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 166 | memset(h1,0,hashbytes);
|
| 167 | memset(h2,0,hashbytes);
|
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 | for(int i = 0; i < 32; i++)
|
| 170 | {
|
| 171 | hash(key,32+i,0,h1);
|
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 | if(memcmp(h1,h2,hashbytes) == 0)
|
| 174 | {
|
| 175 | printf("\n*********FAIL*********\n");
|
| 176 | return;
|
| 177 | }
|
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 | memcpy(h2,h1,hashbytes);
|
| 180 | }
|
| 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 | printf("PASS\n");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 184 | }
|
| 185 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 186 | //-----------------------------------------------------------------------------
|