Exemplo n.º 1
0
void
CookieTest::OverwriteTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/015/015.php");
	result = jar.AddCookie("015-01=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/015-1.php");
	result = jar.AddCookie("015-01=1", url);
	result = jar.AddCookie("015-02=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/015/015-2.php");
	result = jar.AddCookie("015-01=1", url);
	result = jar.AddCookie("015-02=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/015/015-3.php");
	BNetworkCookieJar::UrlIterator it = jar.GetUrlIterator(url);
	int count = 0;

	while (it.HasNext()) {
		it.Next();
		count++;
	}

	CPPUNIT_ASSERT_EQUAL(4, count);
}
Exemplo n.º 2
0
void
CookieTest::OrderTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/016.php");
	result = jar.AddCookie("016-01=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/016/016-1.php");
	result = jar.AddCookie("016-02=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/016/016-2.php");
	BNetworkCookieJar::UrlIterator it = jar.GetUrlIterator(url);
	int count = 0;

	// Check that the cookie with the most specific path is sent first
	while (it.HasNext()) {
		const BNetworkCookie* cookie = it.Next();
		switch(count)
		{
			case 0:
				CPPUNIT_ASSERT_EQUAL(BString("016-02"), cookie->Name());
				break;
			case 1:
				CPPUNIT_ASSERT_EQUAL(BString("016-01"), cookie->Name());
				break;
		}
		count++;
	}

	CPPUNIT_ASSERT_EQUAL(2, count);
}
Exemplo n.º 3
0
void
CookieTest::PersistantTest()
{
	BNetworkCookieJar jar;
	status_t result;
	char buffer[256];

	time_t t = time(NULL) + 100000;
	struct tm* now = gmtime(&t);

	NextSubTest();

	BUrl url("http://testsuites.opera.com/cookies/013.php");

	strftime(buffer, sizeof(buffer),
		"013-1=1; Expires=%a, %d-%b-%Y %H:%M:%S", now);
	result = jar.AddCookie(buffer, url);
	CPPUNIT_ASSERT(result == B_OK);

	result = jar.AddCookie("013-2=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	NextSubTest();

	url.SetUrlString("http://testsuites.opera.com/cookies/013-1.php");

	const BNetworkCookie* cookie = _GetCookie(jar, url, "013-1");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT(cookie->Value() == "1");
	CPPUNIT_ASSERT(cookie->HasExpirationDate());

	cookie = _GetCookie(jar, url, "013-2");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT(cookie->Value() == "1");
	CPPUNIT_ASSERT(!cookie->HasExpirationDate());

	// Simulate exit
	jar.PurgeForExit();

	NextSubTest();

	cookie = _GetCookie(jar, url, "013-1");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT(cookie->Value() == "1");
	CPPUNIT_ASSERT(cookie->HasExpirationDate());

	cookie = _GetCookie(jar, url, "013-2");
	CPPUNIT_ASSERT(cookie == NULL);
}
Exemplo n.º 4
0
void
CookieTest::MaxNumberTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/007.php");
	BString cookieString;

	for (int i = 1; i <= 20; i++)
	{
		cookieString.SetToFormat("007-%d=1", i);
		result = jar.AddCookie(cookieString, url);
		CPPUNIT_ASSERT(result == B_OK);
	}

	url.SetUrlString("http://testsuites.opera.com/cookies/007-1.php");
	for (int i = 1; i <= 20; i++)
	{
		cookieString.SetToFormat("007-%d", i);
		const BNetworkCookie* cookie = _GetCookie(jar, url, cookieString.String());
		CPPUNIT_ASSERT(cookie != NULL);
		CPPUNIT_ASSERT(cookie->Value() == "1");
	}
}
Exemplo n.º 5
0
void
CookieTest::ExpireTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/003.php");
	result = jar.AddCookie("003=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/003-1.php");
	result = jar.AddCookie("003=1; expires=Thu, 01-Jan-1970 00:00:10 GMT", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/003-2.php");
	// The cookie should be expired.
	CPPUNIT_ASSERT(_GetCookie(jar, url, "003") == NULL);
}
Exemplo n.º 6
0
void
CookieTest::SimpleTest()
{
	BNetworkCookieJar jar;
	char buffer[256];
	BUrl url("http://www.chipchapin.com/WebTools/cookietest.php");

	time_t t = time(NULL) + 6400; // Cookies expire in 1h45
	struct tm* now = gmtime(&t);
	status_t result;

	// Add various cookies
	result = jar.AddCookie("testcookie1=present;", url);
	CPPUNIT_ASSERT(result == B_OK);

	strftime(buffer, sizeof(buffer),
		"testcookie2=present; expires=%A, %d-%b-%Y %H:%M:%S", now);
	result = jar.AddCookie(buffer, url);
	CPPUNIT_ASSERT(result == B_OK);

	strftime(buffer, sizeof(buffer),
		"testcookie3=present; expires=%A, %d-%b-%Y %H:%M:%S; path=/", now);
	result = jar.AddCookie(buffer, url);
	CPPUNIT_ASSERT(result == B_OK);

	t += 3200; // expire in 2h40
	now = gmtime(&t);
	strftime(buffer, sizeof(buffer),
		"testcookie4=present; path=/; domain=www.chipchapin.com; "
		"expires=%A, %d-%b-%Y %H:%M:%S", now);
	result = jar.AddCookie(buffer, url);
	CPPUNIT_ASSERT(result == B_OK);

	// Now check they were all properly added
	BNetworkCookieJar::Iterator it = jar.GetIterator();

	int count = 0;
	while (it.HasNext()) {
		count ++;
		it.Next();
	}

	CPPUNIT_ASSERT_EQUAL(4, count);
}
Exemplo n.º 7
0
void
CookieTest::UpdateTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/008.php");
	result = jar.AddCookie("008=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/008-1.php");
	result = jar.AddCookie("008=2", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/008-2.php");
	const BNetworkCookie* cookie = _GetCookie(jar, url, "008");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT(cookie->Value() == "2");
}
Exemplo n.º 8
0
void
CookieTest::DomainTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/012.php");
	result = jar.AddCookie("012-1=1; Domain=\"opera.com\"", url);
	CPPUNIT_ASSERT(result == B_OK);
	result = jar.AddCookie("012-1=1; Domain=\"example.com\"", url);
	CPPUNIT_ASSERT(result != B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/012-1.php");
	const BNetworkCookie* cookie = _GetCookie(jar, url, "012-1");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT(cookie->Value() == "1");

	cookie = _GetCookie(jar, url, "012-2");
	CPPUNIT_ASSERT(cookie == NULL);
}
Exemplo n.º 9
0
void
CookieTest::EncodingTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/011.php");
	result = jar.AddCookie("011=UTF-8 \303\246\303\270\303\245 \346\227\245\346\234\254;", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/011-1.php");
	const BNetworkCookie* cookie = _GetCookie(jar, url, "011");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT_EQUAL(
		BString("UTF-8 \303\246\303\270\303\245 \346\227\245\346\234\254"),
		cookie->Value());
}
Exemplo n.º 10
0
void
CookieTest::HttpOnlyTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/010.php");
	result = jar.AddCookie("010=1; Httponly; Max-age=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/010-1.php");
	const BNetworkCookie* cookie = _GetCookie(jar, url, "010");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT(cookie->Value() == "1");
	CPPUNIT_ASSERT(cookie->HttpOnly());
	CPPUNIT_ASSERT(cookie->HasExpirationDate());
}
Exemplo n.º 11
0
void
CookieTest::PathTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/004/004.php");
	result = jar.AddCookie("004=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	// Page in the same path can access the cookie
	url.SetUrlString("http://testsuites.opera.com/cookies/004/004-1.php");
	CPPUNIT_ASSERT(_GetCookie(jar, url, "004") != NULL);

	// Page in parent directory cannot access the cookie
	url.SetUrlString("http://testsuites.opera.com/cookies/004-2.php");
	CPPUNIT_ASSERT(_GetCookie(jar, url, "004") == NULL);
}
Exemplo n.º 12
0
void
CookieTest::MaxSizeTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/006.php");
	BString cookieString("006=");
	for (int i = 0; i < 128; i++) {
		cookieString << "00xxxxxxxxxxxxxx16xxxxxxxxxxxxxx";
	}
	result = jar.AddCookie(cookieString, url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/006-1.php");
	const BNetworkCookie* cookie = _GetCookie(jar, url, "006");
	CPPUNIT_ASSERT(cookie != NULL);
	CPPUNIT_ASSERT(cookie->Value().Length() == 4096);
}
Exemplo n.º 13
0
void
CookieTest::StandardTest()
{
	BNetworkCookieJar jar;
	status_t result;

	BUrl url("http://testsuites.opera.com/cookies/001.php");
	result = jar.AddCookie("001=1", url);
	CPPUNIT_ASSERT(result == B_OK);

	url.SetUrlString("http://testsuites.opera.com/cookies/001-1.php");
	const BNetworkCookie* cookie = _GetCookie(jar, url, "001");

	CPPUNIT_ASSERT(cookie != NULL);

	CPPUNIT_ASSERT(cookie->HasValue());
	CPPUNIT_ASSERT_EQUAL(BString("1"), cookie->Value());
	CPPUNIT_ASSERT(cookie->IsSessionCookie());
	CPPUNIT_ASSERT(cookie->ShouldDeleteAtExit());

	CPPUNIT_ASSERT(!cookie->HasExpirationDate());
	CPPUNIT_ASSERT(!cookie->Secure());
	CPPUNIT_ASSERT(!cookie->HttpOnly());
}
Exemplo n.º 14
0
void stressTest(int32 domainNumber, int32 totalCookies, char** flat, ssize_t* size)
{
	char **domains = new char*[domainNumber];
	
	cout << "Creating random domains" << endl;
	srand(time(NULL));
	for (int32 i = 0; i < domainNumber; i++) {
		int16 charNum = (rand() % 16) + 1;
		
		domains[i] = new char[charNum + 5];
		
		// Random domain
		for (int32 c = 0; c < charNum; c++) 
			domains[i][c] = (rand() % 26) + 'a';
		
		domains[i][charNum] = '.';
		
		// Random tld
		for (int32 c = 0; c < 3; c++) 
			domains[i][charNum+1+c] = (rand() % 26) + 'a';
			
		domains[i][charNum+4] = 0;
	}
	
	BNetworkCookieJar j;
	BStopWatch* watch = new BStopWatch("Cookie insertion");
	for (int32 i = 0; i < totalCookies; i++) {
		BNetworkCookie c;
		int16 domain = (rand() % domainNumber);
		BString name("Foo");
		name << i;
		
		c.SetName(name);
		c.SetValue("Bar");
		c.SetDomain(domains[domain]);
		c.SetPath("/");
		
		j.AddCookie(c);
	}
	delete watch;
	
	BNetworkCookie* c;
	int16 domain = (rand() % domainNumber);
	BString host("http://");
	host << domains[domain] << "/";
	
	watch = new BStopWatch("Cookie filtering");
	BUrl url(host);
	int32 count = 0;
	for (BNetworkCookieJar::UrlIterator it(j.GetUrlIterator(url)); (c = it.Next()); ) {
	//for (BNetworkCookieJar::Iterator it(j.GetIterator()); c = it.Next(); ) {
		count++;
	}
	delete watch;
	cout << "Count for " << host << ": " << count << endl;
	
	
	cout << "Flat view of cookie jar is " << j.FlattenedSize() << " bytes large." << endl;
	*flat = new char[j.FlattenedSize()];
	*size = j.FlattenedSize();
	
	if (j.Flatten(*flat, j.FlattenedSize()) == B_OK)
		cout << "Flatten() success!" << endl;
	else
		cout << "Flatten() error!" << endl;
	
	delete[] domains;
}