1 #include <cstdlib> 2 #include <cstring> 3 #include <cstdio> 4 #include <ctime> 5 #include <iostream> 6 7 #include <NetworkKit.h> 8 #include <SupportKit.h> 9 10 #ifdef ASSERT 11 #undef ASSERT 12 #endif 13 14 #define REPORT(i, assert, line) cout << "ASSERT() failed at line " << line \ 15 << ": " << #assert << " (test #" << i << ")" << endl 16 #define ASSERT(index, assertion) { if (!(assertion)) { REPORT(index, assertion, __LINE__ ); } } 17 18 19 using std::cout; 20 using std::endl; 21 22 23 typedef struct 24 { 25 const char* cookieString; 26 27 struct 28 { 29 const char* name; 30 const char* value; 31 const char* domain; 32 const char* path; 33 bool secure; 34 bool discard; 35 bool session; 36 int32 maxAge; 37 } expected; 38 } ExplodeTest; 39 40 const ExplodeTest kTestExplode[] = 41 // Cookie string 42 // Name Value Domain Path Secure Discard Session maxAge 43 // -------- --------- --------- --------- ------ ------- -------- ------- 44 { 45 { "name=value", 46 { "name", "value", "", "", false, false, true, 0 } }, 47 { "name=value;secure=true", 48 { "name", "value", "", "", true, false, true, 0 } }, 49 { "name=value;secure=false;maxage=5", 50 { "name", "value", "", "", false, false, false, 5 } }, 51 { "name=value;discard=true", 52 { "name", "value", "", "", false, true, true, 0 } }, 53 }; 54 55 56 void explodeImplodeTest() 57 { 58 uint8 testIndex; 59 BNetworkCookie cookie; 60 61 for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++) 62 { 63 cookie.ParseCookieString(kTestExplode[testIndex].cookieString); 64 65 ASSERT(testIndex, BString(kTestExplode[testIndex].expected.name) == BString(cookie.Name())); 66 ASSERT(testIndex, BString(kTestExplode[testIndex].expected.value) == BString(cookie.Value())); 67 ASSERT(testIndex, BString(kTestExplode[testIndex].expected.domain) == BString(cookie.Domain())); 68 ASSERT(testIndex, BString(kTestExplode[testIndex].expected.path) == BString(cookie.Path())); 69 ASSERT(testIndex, kTestExplode[testIndex].expected.secure == cookie.Secure()); 70 ASSERT(testIndex, kTestExplode[testIndex].expected.discard == cookie.Discard()); 71 ASSERT(testIndex, kTestExplode[testIndex].expected.session == cookie.IsSessionCookie()); 72 73 if (!cookie.IsSessionCookie()) 74 ASSERT(testIndex, kTestExplode[testIndex].expected.maxAge == cookie.MaxAge()); 75 } 76 } 77 78 79 void stressTest(int32 domainNumber, int32 totalCookies, char** flat, ssize_t* size) 80 { 81 char **domains = new char*[domainNumber]; 82 83 cout << "Creating random domains" << endl; 84 srand(time(NULL)); 85 for (int32 i = 0; i < domainNumber; i++) { 86 int16 charNum = (rand() % 16) + 1; 87 88 domains[i] = new char[charNum + 5]; 89 90 // Random domain 91 for (int32 c = 0; c < charNum; c++) 92 domains[i][c] = (rand() % 26) + 'a'; 93 94 domains[i][charNum] = '.'; 95 96 // Random tld 97 for (int32 c = 0; c < 3; c++) 98 domains[i][charNum+1+c] = (rand() % 26) + 'a'; 99 100 domains[i][charNum+4] = 0; 101 } 102 103 BNetworkCookieJar j; 104 BStopWatch* watch = new BStopWatch("Cookie insertion"); 105 for (int32 i = 0; i < totalCookies; i++) { 106 BNetworkCookie c; 107 int16 domain = (rand() % domainNumber); 108 BString name("Foo"); 109 name << i; 110 111 c.SetName(name); 112 c.SetValue("Bar"); 113 c.SetDomain(domains[domain]); 114 c.SetPath("/"); 115 116 j.AddCookie(c); 117 } 118 delete watch; 119 120 BNetworkCookie* c; 121 int16 domain = (rand() % domainNumber); 122 BString host("http://"); 123 host << domains[domain] << "/"; 124 125 watch = new BStopWatch("Cookie filtering"); 126 BUrl url(host); 127 int32 count = 0; 128 for (BNetworkCookieJar::UrlIterator it(j.GetUrlIterator(url)); (c = it.Next()); ) { 129 //for (BNetworkCookieJar::Iterator it(j.GetIterator()); c = it.Next(); ) { 130 count++; 131 } 132 delete watch; 133 cout << "Count for " << host << ": " << count << endl; 134 135 136 cout << "Flat view of cookie jar is " << j.FlattenedSize() << " bytes large." << endl; 137 *flat = new char[j.FlattenedSize()]; 138 *size = j.FlattenedSize(); 139 140 if (j.Flatten(*flat, j.FlattenedSize()) == B_OK) 141 cout << "Flatten() success!" << endl; 142 else 143 cout << "Flatten() error!" << endl; 144 145 delete[] domains; 146 } 147 148 149 int 150 main(int, char**) 151 { 152 cout << "Running explodeImplodeTest:" << endl; 153 explodeImplodeTest(); 154 cout << endl << endl; 155 156 cout << "Running stressTest:" << endl; 157 char* flatJar; 158 ssize_t size; 159 stressTest(10000, 40000, &flatJar, &size); 160 161 BNetworkCookieJar j; 162 j.Unflatten(B_ANY_TYPE, flatJar, size); 163 164 int32 count = 0; 165 BNetworkCookie* c; 166 for (BNetworkCookieJar::Iterator it(j.GetIterator()); (c = it.Next()); ) 167 count++; 168 cout << "Count : " << count << endl; 169 170 delete[] flatJar; 171 172 return EXIT_SUCCESS; 173 } 174