Exemplo n.º 1
0
void explodeImplodeTest()
{
	uint8 testIndex;
	BNetworkCookie cookie;

	for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
	{
		cookie.ParseCookieString(kTestExplode[testIndex].cookieString);

		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.name) == BString(cookie.Name()));
		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.value) == BString(cookie.Value()));
		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.domain) == BString(cookie.Domain()));
		ASSERT(testIndex, BString(kTestExplode[testIndex].expected.path) == BString(cookie.Path()));
		ASSERT(testIndex, kTestExplode[testIndex].expected.secure == cookie.Secure());
		ASSERT(testIndex, kTestExplode[testIndex].expected.discard == cookie.Discard());
		ASSERT(testIndex, kTestExplode[testIndex].expected.session == cookie.IsSessionCookie());

		if (!cookie.IsSessionCookie())
			ASSERT(testIndex, kTestExplode[testIndex].expected.maxAge == cookie.MaxAge());
	}
}
Exemplo n.º 2
0
void
CookieTest::ExplodeTest()
{
	struct Test {
		const char* cookieString;
		const char*	url;
		struct
		{
			bool		valid;
			const char* name;
			const char* value;
			const char* domain;
			const char* path;
			bool 		secure;
			bool 		httponly;
			bool		session;
			BDateTime	expire;
		} expected;
	};

	Test tests[] = {
	//     Cookie string      URL
	//     ------------- -------------
	//		   Valid     Name     Value	       Domain         Path      Secure  HttpOnly Session  Expiration
	//       --------- -------- --------- ----------------- ---------  -------- -------- -------  ----------
		// Normal cookies
		{ "name=value", "http://www.example.com/path/path",
			{  true,    "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		{ "name=value; domain=example.com; path=/; secure", "http://www.example.com/path/path",
			{  true,    "name",  "value",   "example.com",   "/"    ,   true,    false,   true,   BDateTime() } },
		{ "name=value; httponly; secure", "http://www.example.com/path/path",
			{  true,    "name",  "value", "www.example.com", "/path",   true,    true,    true,   BDateTime() } },
		{ "name=value; expires=Wed, 20-Feb-2013 20:00:00 UTC", "http://www.example.com/path/path",
			{  true,    "name",  "value", "www.example.com", "/path",   false,   false,   false,
				BDateTime(BDate(2013, 2, 20), BTime(20, 0, 0, 0)) } },
		// Valid cookie with bad form
		{ "name=  ;  domain   =example.com  ;path=/;  secure = yup  ; blahblah ;)", "http://www.example.com/path/path",
			{  true,    "name",  "",   "example.com",   "/"    ,   true,    false,   true,   BDateTime() } },
		// Invalid path
		{ "name=value; path=invalid", "http://www.example.com/path/path",
			{  false,    "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		// Setting for other subdomain (invalid)
		{ "name=value; domain=subdomain.example.com", "http://www.example.com/path/path",
			{  false,   "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		// Various invalid cookies
		{ "name", "http://www.example.com/path/path",
			{  false,   "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } },
		{ "; domain=example.com", "http://www.example.com/path/path",
			{  false,   "name",  "value", "www.example.com", "/path",   false,   false,   true,   BDateTime() } }
	};
	
	BNetworkCookie cookie;

	for (uint32 i = 0; i < (sizeof(tests) / sizeof(Test)); i++) {
		NextSubTest();

		BUrl url(tests[i].url);
		cookie.ParseCookieString(tests[i].cookieString, url);

		CPPUNIT_ASSERT(tests[i].expected.valid == cookie.IsValid());

		if (!tests[i].expected.valid)
			continue;

		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.name), cookie.Name());
		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.value), cookie.Value());
		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.domain),
			cookie.Domain());
		CPPUNIT_ASSERT_EQUAL(BString(tests[i].expected.path), cookie.Path());
		CPPUNIT_ASSERT(tests[i].expected.secure == cookie.Secure());
		CPPUNIT_ASSERT(tests[i].expected.httponly == cookie.HttpOnly());
		CPPUNIT_ASSERT(tests[i].expected.session == cookie.IsSessionCookie());

		if (!cookie.IsSessionCookie())
			CPPUNIT_ASSERT_EQUAL(tests[i].expected.expire.Time_t(),
				cookie.ExpirationDate());
	}
}
Exemplo n.º 3
0
void
CookieTest::ExpireParsingTest()
{
	BUrl url("http://testsuites.opera.com/cookies/301.php");
	BNetworkCookie cookie;
	status_t result;

	BString bigData("301-16=1; Expires=\"");
	for (int i = 0; i < 1500; i++)
		bigData << "abcdefghijklmnopqrstuvwxyz";
	bigData << "\";";

	struct Test {
		const char* cookieString;
		bool canParse;
		bool sessionOnly;
		bool expired;
	};

	Test tests[] = {
		{ "301-01=1; Expires=\"notAValidValue\";",
			true, true, false }, // Obviously invalid date
		{ "301-02=1; Expires=\"Wed, 08-Nov-2035 01:04:33\";",
			true, false, false }, // Valid date
		{ "301-03=1; Expires=\"Tue, 19-Jan-2038 03:14:06\";",
			true, false, false }, // Valid date, after year 2038 time_t overflow
		{ "301-04=1; Expires=\"Fri, 13-Dec-1901 20:45:51\";",
			true, false, true }, // Valid date, in the past
		{ "301-05=1; Expires=\"Thu, 33-Nov-2035 01:04:33\";",
			true, true, false }, // Invalid day
		{ "301-06=1; Expires=\"Wed, 08-Siz-2035 01:04:33\";",
			true, true, false }, // Invalid month
		{ "301-07=1; Expires=\"Wed, 08-Nov-9035 01:04:33\";",
			true, false, false }, // Very far in the future
			// NOTE: Opera testsuite considers it should be a session cookie.
		{ "301-08=1; Expires=\"Wed, 08-Nov-2035 75:04:33\";",
			true, true, false }, // Invalid hour
		{ "301-09=1; Expires=\"Wed, 08-Nov-2035 01:75:33\";",
			true, true, false }, // Invalid minute
		{ "301-10=1; Expires=\"Wed, 08-Nov-2035 01:04:75\";",
			true, true, false }, // Invalid second
		{ "301-11=1; Expires=\"XXX, 08-Nov-2035 01:04:33\";",
			true, true, false }, // Invalid weekday
		{ "301-12=1; Expires=\"Thu, XX-Nov-2035 01:04:33\";",
			true, true, false }, // Non-digit day
		{ "301-13=1; Expires=\"Thu, 08-Nov-XXXX 01:04:33\";",
			true, true, false }, // Non-digit year
		{ "301-14=1; Expires=\"Thu, 08-Nov-2035 XX:XX:33\";",
			true, true, false }, // Non-digit hour and minute
		{ "301-15=1; Expires=\"Thu, 08-Nov-2035 01:04:XX\";",
			true, true, false }, // Non-digit second
		{ bigData.String(),
			true, true, false }, // Very long invalid string
			// NOTE: Opera doesn't register the cookie at all.
		{ "301-17=1; Expires=\"Thu, 99999-Nov-2035 01:04:33\";",
			true, true, false }, // Day with many digits
		{ "301-18=1; Expires=\"Thu, 25-Nov-99999 01:04:33\";",
			true, true, false }, // Year with many digits
			// NOTE: Opera tests 301-17 twice and misses this test.
		{ "301-19=1; Expires=\"Thu, 25-Nov-2035 99999:04:33\";",
			true, true, false }, // Hour with many digits
		{ "301-20=1; Expires=\"Thu, 25-Nov-2035 01:99999:33\";",
			true, true, false }, // Minute with many digits
		{ "301-21=1; Expires=\"Thu, 25-Nov-2035 01:04:99999\";",
			true, true, false }, // Second with many digits
		{ "301-22=1; Expires=\"99999999999999999999\";",
			true, true, false }, // Huge number
		{ "301-23=1; Expires=\"Fri, 13-Dec-101 20:45:51\";",
			true, false, true }, // Very far in the past
		{ "301-24=1; EXPiReS=\"Wed, 08-Nov-2035 01:04:33\";",
			true, false, false }, // Case insensitive key parsing
		{ "301-25=1; Expires=Wed, 08-Nov-2035 01:04:33\";",
			true, true, false }, // Missing opening quote
			// NOTE: unlike Opera, we accept badly quoted values for cookie
			// attributes. This allows to handle unquoted values from the early
			// cookie spec properly. However, a date with a quote inside it
			// should not be accepted, so this will be a session cookie.
		{ "301-26=1; Expires=\"Wed, 08-Nov-2035 01:04:33;",
			true, true, false }, // Missing closing quote
		{ "301-27=1; Expires:\"Wed, 08-Nov-2035 01:04:33\";",
			true, true, false }, // Uses : instead of =
			// NOTE: unlike Opera, we allow this as a cookie with a strange
			// name and no value
		{ "301-28=1; Expires;=\"Wed, 08-Nov-2035 01:04:33\";",
			true, true, false }, // Extra ; after name
		{ "301-29=1; Expired=\"Wed, 08-Nov-2035 01:04:33\";",
			true, true, false }, // Expired instead of Expires
		{ "301-30=1; Expires=\"Wed; 08-Nov-2035 01:04:33\";",
			true, true, false }, // ; in value
		{ "301-31=1; Expires=\"Wed,\\r\\n 08-Nov-2035 01:04:33\";",
			true, true, false }, // escaped newline in value
			// NOTE: Only here for completeness. This is what the Opera
			// testsuite sends in test 31, but I suspect they were trying to
			// test the following case.
		{ "301-31b=1; Expires=\"Wed,\r\n 08-Nov-2035 01:04:33\";",
			true, false, false }, // newline in value
			// NOTE: This can't really happen when we get cookies from HTTP
			// headers. It could happen when the cookie is set from meta html
			// tags or from JS.
	};

	for (unsigned int i = 0; i < sizeof(tests) / sizeof(Test); i++)
	{
		NextSubTest();

		result = cookie.ParseCookieString(tests[i].cookieString, url);
		CPPUNIT_ASSERT_EQUAL_MESSAGE("Cookie can be parsed",
			tests[i].canParse, result == B_OK);
		CPPUNIT_ASSERT_EQUAL_MESSAGE("Cookie is session only",
			tests[i].sessionOnly, cookie.IsSessionCookie());
		CPPUNIT_ASSERT_EQUAL_MESSAGE("Cookie has expired",
			tests[i].expired, cookie.ShouldDeleteNow());
	}
}