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 | //----------------------------------------------------------------------------
|
| 32 | // Alignment of the keys should not affect the hash value - if it does,
|
| 33 | // something is broken.
|
| 34 |
|
| 35 | void AlignmentTest ( pfHash hash, const int hashbits )
|
| 36 | {
|
| 37 | const int hashbytes = hashbits / 8;
|
| 38 |
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 39 | printf("Testing alignment handling..........");
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 40 |
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 41 | char testblob[512];
|
| 42 | rand_p(testblob,512);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 43 |
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 44 | char * bufs[16];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 45 | char * strings[16];
|
| 46 |
|
| 47 | for(int i = 0; i < 16; i++)
|
| 48 | {
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 49 | bufs[i] = new char[1024];
|
| 50 | uint32_t b = uint32_t(bufs[i]);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 51 |
|
| 52 | b = (b+15)&(~15);
|
| 53 |
|
| 54 | strings[i] = (char*)(b + i);
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 55 |
|
| 56 | memcpy(strings[i],testblob,512);
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 57 | }
|
| 58 |
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 59 | bool result = true;
|
| 60 |
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 61 | uint32_t hash1[64];
|
| 62 | uint32_t hash2[64];
|
| 63 |
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 64 | for(int k = 1; k <= 512; k++)
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 65 | for(int j = 0; j < 15; j++)
|
| 66 | for(int i = j+1; i < 16; i++)
|
| 67 | {
|
| 68 | const char * s1 = strings[i];
|
| 69 | const char * s2 = strings[j];
|
| 70 |
|
| 71 | hash(s1,k,0,hash1);
|
| 72 | hash(s2,k,0,hash2);
|
| 73 |
|
| 74 | if(memcmp(hash1,hash2,hashbytes) != 0)
|
| 75 | {
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 76 | result = false;
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 77 | }
|
| 78 | }
|
| 79 |
|
tanjent@gmail.com | 9d17d0b | 2010-11-17 04:07:51 +0000 | [diff] [blame^] | 80 | if(!result)
|
| 81 | {
|
| 82 | printf("*********FAIL*********\n");
|
| 83 | }
|
| 84 | else
|
| 85 | {
|
| 86 | printf("PASS\n");
|
| 87 | }
|
| 88 |
|
| 89 | for(int i = 0; i < 16; i++) delete [] bufs[i];
|
tanjent@gmail.com | ad4b363 | 2010-11-05 01:20:58 +0000 | [diff] [blame] | 90 | }
|
| 91 |
|
| 92 | //----------------------------------------------------------------------------
|
| 93 | // Appending zero bytes to a key should always cause it to produce a different
|
| 94 | // hash value
|
| 95 |
|
| 96 | void AppendedZeroesTest ( pfHash hash, const int hashbits )
|
| 97 | {
|
| 98 | const int hashbytes = hashbits/8;
|
| 99 |
|
| 100 | printf("Testing zero-appending");
|
| 101 |
|
| 102 | for(int rep = 0; rep < 100; rep++)
|
| 103 | {
|
| 104 | if(rep % 10 == 0) printf(".");
|
| 105 |
|
| 106 | unsigned char key[256];
|
| 107 |
|
| 108 | memset(key,0,sizeof(key));
|
| 109 |
|
| 110 | rand_p(key,32);
|
| 111 |
|
| 112 | uint32_t h1[16];
|
| 113 | uint32_t h2[16];
|
| 114 |
|
| 115 | memset(h1,0,hashbytes);
|
| 116 | memset(h2,0,hashbytes);
|
| 117 |
|
| 118 | for(int i = 0; i < 32; i++)
|
| 119 | {
|
| 120 | hash(key,32+i,0,h1);
|
| 121 |
|
| 122 | if(memcmp(h1,h2,hashbytes) == 0)
|
| 123 | {
|
| 124 | printf("\n*********FAIL*********\n");
|
| 125 | return;
|
| 126 | }
|
| 127 |
|
| 128 | memcpy(h2,h1,hashbytes);
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | printf("PASS\n");
|
| 133 | }
|
| 134 |
|
| 135 | //----------------------------------------------------------------------------
|
| 136 | // Basic sanity checks -
|
| 137 |
|
| 138 | // A hash function should not be reading outside the bounds of the key.
|
| 139 |
|
| 140 | // Flipping a bit of a key should, with overwhelmingly high probability,
|
| 141 | // result in a different hash.
|
| 142 |
|
| 143 | // Hashing the same key twice should always produce the same result.
|
| 144 |
|
| 145 | bool SanityTest ( pfHash hash, const int hashbits )
|
| 146 | {
|
| 147 | bool result = true;
|
| 148 |
|
| 149 | const int hashbytes = hashbits/8;
|
| 150 | const int reps = 100;
|
| 151 |
|
| 152 | printf("Testing bit twiddling");
|
| 153 |
|
| 154 | uint8_t buffer[256];
|
| 155 | uint8_t * key = &buffer[64];
|
| 156 |
|
| 157 | uint8_t * h1 = new uint8_t[hashbytes];
|
| 158 | uint8_t * h2 = new uint8_t[hashbytes];
|
| 159 |
|
| 160 | for(int irep = 0; irep < reps; irep++)
|
| 161 | {
|
| 162 | if(irep % (reps/10) == 0) printf(".");
|
| 163 |
|
| 164 | for(int len = 1; len <= 128; len++)
|
| 165 | {
|
| 166 | // Generate a random key in the middle of the buffer, hash it,
|
| 167 | // and then fill the space around the key with garbage. If a
|
| 168 | // broken hash function reads past the ends of the key, it should
|
| 169 | // fail the "did we get the same hash?" test below.
|
| 170 |
|
| 171 | rand_p(key,len);
|
| 172 | hash(key,len,0,h1);
|
| 173 |
|
| 174 | rand_p(buffer,64);
|
| 175 | rand_p(key+len,64);
|
| 176 |
|
| 177 | // Flip a bit, hash the key -> we should get a different result.
|
| 178 | // Flip it back, hash again -> we should get the same result.
|
| 179 |
|
| 180 | for(int bit = 0; bit < (len * 8); bit++)
|
| 181 | {
|
| 182 | flipbit(key,len,bit);
|
| 183 | hash(key,len,0,h2);
|
| 184 |
|
| 185 | if(memcmp(h1,h2,hashbytes) == 0)
|
| 186 | {
|
| 187 | result = false;
|
| 188 | }
|
| 189 |
|
| 190 | flipbit(key,len,bit);
|
| 191 | hash(key,len,0,h2);
|
| 192 |
|
| 193 | if(memcmp(h1,h2,hashbytes) != 0)
|
| 194 | {
|
| 195 | result = false;
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | if(result == false)
|
| 202 | {
|
| 203 | printf("*********FAIL*********\n");
|
| 204 | }
|
| 205 | else
|
| 206 | {
|
| 207 | printf("PASS\n");
|
| 208 | }
|
| 209 |
|
| 210 | delete [] h1;
|
| 211 | delete [] h2;
|
| 212 |
|
| 213 | return result;
|
| 214 | }
|
| 215 |
|
| 216 | //-----------------------------------------------------------------------------
|