#include "KeysetTest.h" #include "Platform.h" #include "Random.h" #include #include //----------------------------------------------------------------------------- // This should hopefully be a thorough and uambiguous test of whether a hash // is correctly implemented on a given platform bool VerificationTest ( pfHash hash, const int hashbits, uint32_t expected, bool verbose ) { const int hashbytes = hashbits / 8; uint8_t * key = new uint8_t[256]; uint8_t * hashes = new uint8_t[hashbytes * 256]; uint8_t * final = new uint8_t[hashbytes]; memset(key,0,256); memset(hashes,0,hashbytes*256); memset(final,0,hashbytes); // Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as // the seed for(int i = 0; i < 256; i++) { key[i] = (uint8_t)i; hash(key,i,256-i,&hashes[i*hashbytes]); } // Then hash the result array hash(hashes,hashbytes*256,0,final); // The first four bytes of that hash, interpreted as a little-endian integer, is our // verification value uint32_t verification = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) | (final[3] << 24); delete [] key; delete [] hashes; delete [] final; //---------- if(expected != verification) { if(verbose) fprintf(stderr,"Verification value 0x%08X : Failed! (Expected 0x%08x)\n",verification,expected); return false; } else { if(verbose) fprintf(stderr,"Verification value 0x%08X : Passed!\n",verification); return true; } } //---------------------------------------------------------------------------- // Basic sanity checks - // A hash function should not be reading outside the bounds of the key. // Flipping a bit of a key should, with overwhelmingly high probability, // result in a different hash. // Hashing the same key twice should always produce the same result. // The memory alignment of the key should not affect the hash result. bool SanityTest ( pfHash hash, const int hashbits ) { // return true; fprintf(stderr,"Running sanity check 1"); Rand r(883741); bool result = true; bool diff = true; bool same = true; const int hashbytes = hashbits/8; // const int reps = 10; const int reps = 1; const int keymax = 256; const int pad = 16; const int buflen = keymax + pad*3; uint8_t * buffer1 = new uint8_t[buflen]; uint8_t * buffer2 = new uint8_t[buflen]; uint8_t * hash1 = new uint8_t[hashbytes]; uint8_t * hash2 = new uint8_t[hashbytes]; //---------- for(int irep = 0; irep < reps; irep++) { // if(irep % (reps/10) == 0) fprintf(stderr,"."); for(int len = 4; len <= keymax; len++) { for(int offset = pad; offset < pad*2; offset++) { uint8_t * key1 = &buffer1[pad]; uint8_t * key2 = &buffer2[pad+offset]; // uint8_t * key2 = &buffer2[pad]; // no offset r.rand_p(buffer1,buflen); r.rand_p(buffer2,buflen); memcpy(key2,key1,len); hash(key1,len,0,hash1); for(int bit = 0; bit < (len * 8); bit++) { // Flip a bit, hash the key -> we should get a different result. flipbit(key2,len,bit); hash(key2,len,0,hash2); if(memcmp(hash1,hash2,hashbytes) == 0) { fprintf(stderr,"\nkey1 = "); for (int m=0; m hash1 = "); for (int m=0; m hash2 = "); for (int m=0; m we should get the original result. // flipbit(key2,len,bit); // hash(key2,len,0,hash2); // somethings wrong with back flipping so use key1 instead hash(key1,len,0,hash2); if (memcmp(hash1,hash2,hashbytes) != 0) { fprintf(stderr,"\nkey1 = "); for (int m=0; m void DumpCollisionMap ( CollisionMap & cmap ) { typedef CollisionMap cmap_t; for(typename cmap_t::iterator it = cmap.begin(); it != cmap.end(); ++it) { const hashtype & hash = (*it).first; fprintf(stderr,"Hash - "); printbytes(&hash,sizeof(hashtype)); fprintf(stderr,"\n"); std::vector & keys = (*it).second; for(int i = 0; i < (int)keys.size(); i++) { ByteVec & key = keys[i]; fprintf(stderr,"Key - "); printbytes(&key[0],(int)key.size()); fprintf(stderr,"\n"); } fprintf(stderr,"\n"); } } // test code void ReportCollisions ( pfHash hash ) { fprintf(stderr,"Hashing keyset\n"); std::vector hashes; HashCallback c(hash,hashes); TwoBytesKeygen(20,c); fprintf(stderr,"%d hashes\n",(int)hashes.size()); fprintf(stderr,"Finding collisions\n"); HashSet collisions; FindCollisions(hashes,collisions,1000); fprintf(stderr,"%d collisions\n",(int)collisions.size()); fprintf(stderr,"Mapping collisions\n"); CollisionMap cmap; CollisionCallback c2(hash,collisions,cmap); TwoBytesKeygen(20,c2); fprintf(stderr,"Dumping collisions\n"); DumpCollisionMap(cmap); }