xref: /haiku/src/tests/kits/net/cookie/cookie_test.cpp (revision 71452e98334eaac603bf542d159e24788a46bebb)
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 
11 using std::cout;
12 using std::endl;
13 
14 
15 void stressTest(int32 domainNumber, int32 totalCookies, char** flat, ssize_t* size)
16 {
17 	char **domains = new char*[domainNumber];
18 
19 	cout << "Creating random domains" << endl;
20 	srand(time(NULL));
21 	for (int32 i = 0; i < domainNumber; i++) {
22 		int16 charNum = (rand() % 16) + 1;
23 
24 		domains[i] = new char[charNum + 5];
25 
26 		// Random domain
27 		for (int32 c = 0; c < charNum; c++)
28 			domains[i][c] = (rand() % 26) + 'a';
29 
30 		domains[i][charNum] = '.';
31 
32 		// Random tld
33 		for (int32 c = 0; c < 3; c++)
34 			domains[i][charNum+1+c] = (rand() % 26) + 'a';
35 
36 		domains[i][charNum+4] = 0;
37 	}
38 
39 	BNetworkCookieJar j;
40 	BStopWatch* watch = new BStopWatch("Cookie insertion");
41 	for (int32 i = 0; i < totalCookies; i++) {
42 		BNetworkCookie c;
43 		int16 domain = (rand() % domainNumber);
44 		BString name("Foo");
45 		name << i;
46 
47 		c.SetName(name);
48 		c.SetValue("Bar");
49 		c.SetDomain(domains[domain]);
50 		c.SetPath("/");
51 
52 		j.AddCookie(c);
53 	}
54 	delete watch;
55 
56 	const BNetworkCookie* c;
57 	int16 domain = (rand() % domainNumber);
58 	BString host("http://");
59 	host << domains[domain] << "/";
60 
61 	watch = new BStopWatch("Cookie filtering");
62 	BUrl url(host);
63 	int32 count = 0;
64 	for (BNetworkCookieJar::UrlIterator it(j.GetUrlIterator(url)); (c = it.Next()); ) {
65 	//for (BNetworkCookieJar::Iterator it(j.GetIterator()); c = it.Next(); ) {
66 		count++;
67 	}
68 	delete watch;
69 	cout << "Count for " << host << ": " << count << endl;
70 
71 
72 	cout << "Flat view of cookie jar is " << j.FlattenedSize() << " bytes large." << endl;
73 	*flat = new char[j.FlattenedSize()];
74 	*size = j.FlattenedSize();
75 
76 	if (j.Flatten(*flat, j.FlattenedSize()) == B_OK)
77 		cout << "Flatten() success!" << endl;
78 	else
79 		cout << "Flatten() error!" << endl;
80 
81 	delete[] domains;
82 }
83 
84 
85 int
86 main(int, char**)
87 {
88 	cout << "Running stressTest:" << endl;
89 	char* flatJar;
90 	ssize_t size;
91 	stressTest(10000, 40000, &flatJar, &size);
92 
93 	BNetworkCookieJar j;
94 	j.Unflatten(B_ANY_TYPE, flatJar, size);
95 
96 	int32 count = 0;
97 	const BNetworkCookie* c;
98 	for (BNetworkCookieJar::Iterator it(j.GetIterator()); (c = it.Next()); )
99 		count++;
100 	cout << "Count : " << count << endl;
101 
102 	delete[] flatJar;
103 
104 	return EXIT_SUCCESS;
105 }
106