tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 1 | #include "KeysetTest.h"
|
| 2 |
|
| 3 | #include "Random.h"
|
| 4 |
|
| 5 | //-----------------------------------------------------------------------------
|
| 6 |
|
| 7 | void 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.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 32 | // Basic sanity checks -
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 33 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 34 | // 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] | 35 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 36 | // Flipping a bit of a key should, with overwhelmingly high probability,
|
| 37 | // result in a different hash.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 38 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 39 | // Hashing the same key twice should always produce the same result.
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 40 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 41 | // 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] | 42 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 43 | bool SanityTest ( pfHash hash, const int hashbits )
|
| 44 | { printf("Testing bit twiddling");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 45 |
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 46 | bool result = true;
|
| 47 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 48 | 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.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 | 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.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 | if(irep % (reps/10) == 0) printf(".");
|
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 | for(int len = 4; len <= keymax; len++)
|
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 | 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.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 103 | }
|
| 104 | }
|
| 105 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 106 | if(result == false)
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame] | 107 | {
|
| 108 | printf("*********FAIL*********\n");
|
| 109 | }
|
| 110 | else
|
| 111 | {
|
| 112 | printf("PASS\n");
|
| 113 | }
|
| 114 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame^] | 115 | delete [] hash1;
|
| 116 | delete [] hash2;
|
| 117 |
|
| 118 | return result;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 119 | }
|
| 120 |
|
| 121 | //----------------------------------------------------------------------------
|
| 122 | // Appending zero bytes to a key should always cause it to produce a different
|
| 123 | // hash value
|
| 124 |
|
| 125 | void 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.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 164 | //-----------------------------------------------------------------------------
|