From 490d20e8fb1c936324e3501196004592f46b3ff4 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 16 Jun 2016 14:45:36 -0700 Subject: Self test for dictionary, and fixes. --- node/Dictionary.hpp | 24 +++++++++++++++++++++--- selftest.cpp | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/node/Dictionary.hpp b/node/Dictionary.hpp index e4c7ff20..dca8ac67 100644 --- a/node/Dictionary.hpp +++ b/node/Dictionary.hpp @@ -27,7 +27,7 @@ #include // Can be increased if it's ever needed, but not too much. -#define ZT_DICTIONARY_MAX_SIZE 16384 +#define ZT_DICTIONARY_MAX_SIZE 8194 namespace ZeroTier { @@ -279,6 +279,15 @@ public: for(unsigned int i=0;i 0) { + _d[j++] = '\n'; + if (j == ZT_DICTIONARY_MAX_SIZE) { + _d[i] = (char)0; + return false; + } + } + const char *p = key; while (*p) { _d[j++] = *(p++); @@ -287,9 +296,16 @@ public: return false; } } + + _d[j++] = '='; + if (j == ZT_DICTIONARY_MAX_SIZE) { + _d[i] = (char)0; + return false; + } + p = value; int k = 0; - while ((*p)&&((vlen < 0)||(k < vlen))) { + while ( ((*p)&&(vlen < 0)) || (k < vlen) ) { switch(*p) { case 0: case '\r': @@ -326,7 +342,9 @@ public: ++p; ++k; } - _d[j++] = (char)0; + + _d[j] = (char)0; + return true; } } diff --git a/selftest.cpp b/selftest.cpp index 9e6e077a..eeffb9bd 100644 --- a/selftest.cpp +++ b/selftest.cpp @@ -764,21 +764,22 @@ static int testOther() } std::cout << "PASS" << std::endl; - std::cout << "[other] Testing Dictionary... "; std::cout.flush(); + std::cout << "[other] Testing/fuzzing Dictionary... "; std::cout.flush(); for(int k=0;k<1000;++k) { - Dictionary td; - char key[128][16]; - char value[128][128]; - for(unsigned int q=0;q<128;++q) { + Dictionary test; + char key[32][16]; + char value[32][128]; + for(unsigned int q=0;q<32;++q) { Utils::snprintf(key[q],16,"%.8lx",(unsigned long)rand()); int r = rand() % 128; for(int x=0;x= 0) { if (strcmp(value[r],tmp)) { @@ -786,21 +787,33 @@ static int testOther() return -1; } } else { - std::cout << "FAILED (can't find key)!" << std::endl; + std::cout << "FAILED (can't find key '" << key[r] << "')!" << std::endl; + return -1; + } + } + for(unsigned int q=0;q<31;++q) { + char tmp[128]; + test.erase(key[q]); + if (test.get(key[q],tmp,sizeof(tmp)) >= 0) { + std::cout << "FAILED (key should have been erased)!" << std::endl; + return -1; + } + if (test.get(key[q+1],tmp,sizeof(tmp)) < 0) { + std::cout << "FAILED (key should NOT have been erased)!" << std::endl; return -1; } } } int foo = 0; volatile int *volatile bar = &foo; // force compiler not to optimize out test.get() below - for(int k=0;k<100000;++k) { - int r = rand() % 16384; - unsigned char tmp[16384]; + for(int k=0;k<100;++k) { + int r = rand() % ZT_DICTIONARY_MAX_SIZE; + unsigned char tmp[ZT_DICTIONARY_MAX_SIZE]; for(int q=0;q requestHeaders,responseHeaders; @@ -1055,6 +1069,7 @@ static int testHttp() return 0; } +*/ #ifdef __WINDOWS__ int _tmain(int argc, _TCHAR* argv[]) -- cgit v1.2.3