Remove unnecessary stripping of quotes prior to calling PR_ParseTimeString. b=542974, r=abarth

This commit is contained in:
Dan Witte 2010-02-10 12:22:49 -08:00
Родитель 3913348852
Коммит a081d5eeed
2 изменённых файлов: 67 добавлений и 69 удалений

Просмотреть файл

@ -2237,24 +2237,14 @@ nsCookieService::GetExpiry(nsCookieAttributes &aCookieAttributes,
// check for expires attribute
} else if (!aCookieAttributes.expires.IsEmpty()) {
PRTime tempExpires;
PRInt64 expires;
// For Expires, we trim leading and trailing " characters to maximize
// the compatibility of our date parsing. In principle, this processsing
// should be done in our date parser.
nsCString& expiresAttr = aCookieAttributes.expires;
if (!expiresAttr.IsEmpty() && expiresAttr.First() == '"' && expiresAttr.Last() == '"')
expiresAttr = Substring(expiresAttr.BeginReading() + 1, expiresAttr.EndReading() - 1);
PRTime expires;
// parse expiry time
if (PR_ParseTimeString(expiresAttr.get(), PR_TRUE, &tempExpires) == PR_SUCCESS) {
expires = tempExpires / PR_USEC_PER_SEC;
} else {
if (PR_ParseTimeString(aCookieAttributes.expires.get(), PR_TRUE, &expires) != PR_SUCCESS) {
return PR_TRUE;
}
delta = expires - aServerTime;
delta = expires / PR_USEC_PER_SEC - aServerTime;
// default to session cookie if no attributes found
} else {

Просмотреть файл

@ -62,6 +62,8 @@ static const char kCookiesLifetimeCurrentSession[] = "network.cookie.lifetime.be
static const char kCookiesP3PString[] = "network.cookie.p3p";
static const char kCookiesAskPermission[] = "network.cookie.warnAboutCookies";
static char *sBuffer;
nsresult
SetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSpec2, const char* aCookieString, const char *aServerTime)
{
@ -70,15 +72,15 @@ SetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSp
if (aSpec2)
NS_NewURI(getter_AddRefs(uri2), aSpec2);
printf(" for host \"%s\": SET ", aSpec1);
sBuffer = PR_sprintf_append(sBuffer, " for host \"%s\": SET ", aSpec1);
nsresult rv = aCookieService->SetCookieStringFromHttp(uri1, uri2, nsnull, (char *)aCookieString, aServerTime, nsnull);
// the following code is useless. the cookieservice blindly returns NS_OK
// from SetCookieString. we have to call GetCookie to see if the cookie was
// set correctly...
if (NS_FAILED(rv)) {
printf("nothing\n");
sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
} else {
printf("\"%s\"\n", aCookieString);
sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", aCookieString);
}
return rv;
}
@ -89,15 +91,15 @@ SetACookieNoHttp(nsICookieService *aCookieService, const char *aSpec, const char
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), aSpec);
printf(" for host \"%s\": SET ", aSpec);
sBuffer = PR_sprintf_append(sBuffer, " for host \"%s\": SET ", aSpec);
nsresult rv = aCookieService->SetCookieString(uri, nsnull, (char *)aCookieString, nsnull);
// the following code is useless. the cookieservice blindly returns NS_OK
// from SetCookieString. we have to call GetCookie to see if the cookie was
// set correctly...
if (NS_FAILED(rv)) {
printf("nothing\n");
sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
} else {
printf("\"%s\"\n", aCookieString);
sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", aCookieString);
}
return rv;
}
@ -112,13 +114,15 @@ GetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSp
if (aSpec2)
NS_NewURI(getter_AddRefs(uri2), aSpec2);
printf(" \"%s\": GOT ", aSpec1);
sBuffer = PR_sprintf_append(sBuffer, " \"%s\": GOT ", aSpec1);
nsresult rv = aCookieService->GetCookieStringFromHttp(uri1, uri2, nsnull, aCookie);
if (NS_FAILED(rv)) printf("XXX GetCookieString() failed!\n");
if (NS_FAILED(rv)) {
sBuffer = PR_sprintf_append(sBuffer, "XXX GetCookieString() failed!\n");
}
if (!*aCookie) {
printf("nothing\n");
sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
} else {
printf("\"%s\"\n", *aCookie);
sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", *aCookie);
}
return *aCookie != nsnull;
}
@ -131,13 +135,15 @@ GetACookieNoHttp(nsICookieService *aCookieService, const char *aSpec, char **aCo
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), aSpec);
printf(" \"%s\": GOT ", aSpec);
sBuffer = PR_sprintf_append(sBuffer, " \"%s\": GOT ", aSpec);
nsresult rv = aCookieService->GetCookieString(uri, nsnull, aCookie);
if (NS_FAILED(rv)) printf("XXX GetCookieString() failed!\n");
if (NS_FAILED(rv)) {
sBuffer = PR_sprintf_append(sBuffer, "XXX GetCookieString() failed!\n");
}
if (!*aCookie) {
printf("nothing\n");
sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
} else {
printf("\"%s\"\n", *aCookie);
sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", *aCookie);
}
return *aCookie != nsnull;
}
@ -183,17 +189,17 @@ PRBool
PrintResult(const PRBool aResult[], PRUint32 aSize)
{
PRBool failed = PR_FALSE;
printf("*** tests ");
sBuffer = PR_sprintf_append(sBuffer, "*** tests ");
for (PRUint32 i = 0; i < aSize; ++i) {
if (!aResult[i]) {
failed = PR_TRUE;
printf("%d ", i);
sBuffer = PR_sprintf_append(sBuffer, "%d ", i);
}
}
if (failed) {
printf("FAILED!\a\n");
sBuffer = PR_sprintf_append(sBuffer, "FAILED!\a\n");
} else {
printf("passed.\n");
sBuffer = PR_sprintf_append(sBuffer, "passed.\n");
}
return !failed;
}
@ -252,15 +258,6 @@ main(PRInt32 argc, char *argv[])
PRBool rv[20];
nsCString cookie;
// call NS_NewURI just to force chrome registrations, so all our
// printf'ed messages are together.
{
nsCOMPtr<nsIURI> foo;
NS_NewURI(getter_AddRefs(foo), "http://foo.com");
}
printf("\n");
/* The basic idea behind these tests is the following:
*
* we set() some cookie, then try to get() it in various ways. we have
@ -300,7 +297,7 @@ main(PRInt32 argc, char *argv[])
*/
// *** basic tests
printf("*** Beginning basic tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning basic tests...\n");
// test some basic variations of the domain & path
SetACookie(cookieService, "http://www.basic.com", nsnull, "test=basic", nsnull);
@ -324,7 +321,7 @@ main(PRInt32 argc, char *argv[])
// *** domain tests
printf("*** Beginning domain tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning domain tests...\n");
// test some variations of the domain & path, for different domains of
// a domain cookie
@ -376,7 +373,7 @@ main(PRInt32 argc, char *argv[])
// *** path tests
printf("*** Beginning path tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning path tests...\n");
// test some variations of the domain & path, for different paths of
// a path cookie
@ -449,7 +446,7 @@ main(PRInt32 argc, char *argv[])
// *** expiry & deletion tests
// XXX add server time str parsing tests here
printf("*** Beginning expiry & deletion tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning expiry & deletion tests...\n");
// test some variations of the expiry time,
// and test deletion of previously set cookies
@ -459,52 +456,55 @@ main(PRInt32 argc, char *argv[])
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=0", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; expires=bad", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[2] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[2] = CheckResult(cookie.get(), MUST_BE_NULL);
rv[3] = CheckResult(cookie.get(), MUST_BE_NULL);
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; expires=\"Thu, 10 Apr 1980 16:33:12 GMT", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[3] = CheckResult(cookie.get(), MUST_BE_NULL);
rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; expires=\"Thu, 10 Apr 1980 16:33:12 GMT\"", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
rv[5] = CheckResult(cookie.get(), MUST_BE_NULL);
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=60", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[5] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
rv[6] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=-20", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[6] = CheckResult(cookie.get(), MUST_BE_NULL);
rv[7] = CheckResult(cookie.get(), MUST_BE_NULL);
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=60", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
rv[8] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[8] = CheckResult(cookie.get(), MUST_BE_NULL);
rv[9] = CheckResult(cookie.get(), MUST_BE_NULL);
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=60", nsnull);
SetACookie(cookieService, "http://expireme.org/", nsnull, "newtest=expiry; max-age=60", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[9] = CheckResult(cookie.get(), MUST_CONTAIN, "test=expiry");
rv[10] = CheckResult(cookie.get(), MUST_CONTAIN, "newtest=expiry");
rv[10] = CheckResult(cookie.get(), MUST_CONTAIN, "test=expiry");
rv[11] = CheckResult(cookie.get(), MUST_CONTAIN, "newtest=expiry");
SetACookie(cookieService, "http://expireme.org/", nsnull, "test=differentvalue; max-age=0", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[11] = CheckResult(cookie.get(), MUST_EQUAL, "newtest=expiry");
rv[12] = CheckResult(cookie.get(), MUST_EQUAL, "newtest=expiry");
SetACookie(cookieService, "http://expireme.org/", nsnull, "newtest=evendifferentvalue; max-age=0", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[12] = CheckResult(cookie.get(), MUST_BE_NULL);
rv[13] = CheckResult(cookie.get(), MUST_BE_NULL);
SetACookie(cookieService, "http://foo.expireme.org/", nsnull, "test=expiry; domain=.expireme.org; max-age=60", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[13] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
rv[14] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
SetACookie(cookieService, "http://bar.expireme.org/", nsnull, "test=differentvalue; domain=.expireme.org; max-age=0", nsnull);
GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
rv[14] = CheckResult(cookie.get(), MUST_BE_NULL);
rv[15] = CheckResult(cookie.get(), MUST_BE_NULL);
allTestsPassed = PrintResult(rv, 15) && allTestsPassed;
allTestsPassed = PrintResult(rv, 16) && allTestsPassed;
// *** multiple cookie tests
printf("*** Beginning multiple cookie tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning multiple cookie tests...\n");
// test the setting of multiple cookies, and test the order of precedence
// (a later cookie overwriting an earlier one, in the same header string)
@ -530,7 +530,7 @@ main(PRInt32 argc, char *argv[])
// *** parser tests
printf("*** Beginning parser tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning parser tests...\n");
// test the cookie header parser, under various circumstances.
SetACookie(cookieService, "http://parser.test/", nsnull, "test=parser; domain=.parser.test; ;; ;=; ,,, ===,abc,=; abracadabra! max-age=20;=;;", nsnull);
@ -567,7 +567,7 @@ main(PRInt32 argc, char *argv[])
// *** mailnews tests
printf("*** Beginning mailnews tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning mailnews tests...\n");
// test some mailnews cookies to ensure blockage.
// we use null firstURI's deliberately, since we have hacks to deal with
@ -590,7 +590,7 @@ main(PRInt32 argc, char *argv[])
// *** path ordering tests
printf("*** Beginning path ordering tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning path ordering tests...\n");
// test that cookies are returned in path order - longest to shortest.
// if the header doesn't specify a path, it's taken from the host URI.
@ -606,7 +606,7 @@ main(PRInt32 argc, char *argv[])
// *** httponly tests
printf("*** Beginning httponly tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning httponly tests...\n");
// Since this cookie is NOT set via http, setting it fails
SetACookieNoHttp(cookieService, "http://httponly.test/", "test=httponly; httponly");
@ -651,7 +651,7 @@ main(PRInt32 argc, char *argv[])
// *** nsICookieManager{2} interface tests
printf("*** Beginning nsICookieManager{2} interface tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning nsICookieManager{2} interface tests...\n");
nsCOMPtr<nsICookieManager> cookieMgr = do_GetService(NS_COOKIEMANAGER_CONTRACTID, &rv0);
if (NS_FAILED(rv0)) return -1;
nsCOMPtr<nsICookieManager2> cookieMgr2 = do_QueryInterface(cookieMgr);
@ -749,7 +749,7 @@ main(PRInt32 argc, char *argv[])
// *** eviction and creation ordering tests
printf("*** Beginning eviction and creation ordering tests...\n");
sBuffer = PR_sprintf_append(sBuffer, "*** Beginning eviction and creation ordering tests...\n");
// test that cookies are
// a) returned by order of creation time (oldest first, newest last)
@ -786,9 +786,17 @@ main(PRInt32 argc, char *argv[])
// *** speed tests
printf("\n*** Result: %s!\n\n", allTestsPassed ? "all tests passed" : "TEST(S) FAILED");
sBuffer = PR_sprintf_append(sBuffer, "\n*** Result: %s!\n\n", allTestsPassed ? "all tests passed" : "TEST(S) FAILED");
}
return allTestsPassed ? 0 : 1;
if (!allTestsPassed) {
// print the entire log
printf("%s", sBuffer);
return 1;
}
PR_smprintf_free(sBuffer);
sBuffer = nsnull;
return 0;
}