netwerk/test/TestCookie.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "TestCommon.h"
michael@0 7 #include "nsIServiceManager.h"
michael@0 8 #include "nsICookieService.h"
michael@0 9 #include "nsICookieManager.h"
michael@0 10 #include "nsICookieManager2.h"
michael@0 11 #include "nsICookie2.h"
michael@0 12 #include <stdio.h>
michael@0 13 #include "plstr.h"
michael@0 14 #include "prprf.h"
michael@0 15 #include "nsNetUtil.h"
michael@0 16 #include "nsNetCID.h"
michael@0 17 #include "nsStringAPI.h"
michael@0 18 #include "nsIPrefBranch.h"
michael@0 19 #include "nsIPrefService.h"
michael@0 20
michael@0 21 static NS_DEFINE_CID(kCookieServiceCID, NS_COOKIESERVICE_CID);
michael@0 22 static NS_DEFINE_CID(kPrefServiceCID, NS_PREFSERVICE_CID);
michael@0 23
michael@0 24 // various pref strings
michael@0 25 static const char kCookiesPermissions[] = "network.cookie.cookieBehavior";
michael@0 26 static const char kCookiesLifetimeEnabled[] = "network.cookie.lifetime.enabled";
michael@0 27 static const char kCookiesLifetimeDays[] = "network.cookie.lifetime.days";
michael@0 28 static const char kCookiesLifetimeCurrentSession[] = "network.cookie.lifetime.behavior";
michael@0 29 static const char kCookiesAskPermission[] = "network.cookie.warnAboutCookies";
michael@0 30 static const char kCookiesMaxPerHost[] = "network.cookie.maxPerHost";
michael@0 31
michael@0 32 static char *sBuffer;
michael@0 33
michael@0 34 nsresult
michael@0 35 SetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSpec2, const char* aCookieString, const char *aServerTime)
michael@0 36 {
michael@0 37 nsCOMPtr<nsIURI> uri1, uri2;
michael@0 38 NS_NewURI(getter_AddRefs(uri1), aSpec1);
michael@0 39 if (aSpec2)
michael@0 40 NS_NewURI(getter_AddRefs(uri2), aSpec2);
michael@0 41
michael@0 42 sBuffer = PR_sprintf_append(sBuffer, " for host \"%s\": SET ", aSpec1);
michael@0 43 nsresult rv = aCookieService->SetCookieStringFromHttp(uri1, uri2, nullptr, (char *)aCookieString, aServerTime, nullptr);
michael@0 44 // the following code is useless. the cookieservice blindly returns NS_OK
michael@0 45 // from SetCookieString. we have to call GetCookie to see if the cookie was
michael@0 46 // set correctly...
michael@0 47 if (NS_FAILED(rv)) {
michael@0 48 sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
michael@0 49 } else {
michael@0 50 sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", aCookieString);
michael@0 51 }
michael@0 52 return rv;
michael@0 53 }
michael@0 54
michael@0 55 nsresult
michael@0 56 SetACookieNoHttp(nsICookieService *aCookieService, const char *aSpec, const char* aCookieString)
michael@0 57 {
michael@0 58 nsCOMPtr<nsIURI> uri;
michael@0 59 NS_NewURI(getter_AddRefs(uri), aSpec);
michael@0 60
michael@0 61 sBuffer = PR_sprintf_append(sBuffer, " for host \"%s\": SET ", aSpec);
michael@0 62 nsresult rv = aCookieService->SetCookieString(uri, nullptr, (char *)aCookieString, nullptr);
michael@0 63 // the following code is useless. the cookieservice blindly returns NS_OK
michael@0 64 // from SetCookieString. we have to call GetCookie to see if the cookie was
michael@0 65 // set correctly...
michael@0 66 if (NS_FAILED(rv)) {
michael@0 67 sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
michael@0 68 } else {
michael@0 69 sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", aCookieString);
michael@0 70 }
michael@0 71 return rv;
michael@0 72 }
michael@0 73
michael@0 74 // returns true if cookie(s) for the given host were found; else false.
michael@0 75 // the cookie string is returned via aCookie.
michael@0 76 bool
michael@0 77 GetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSpec2, char **aCookie)
michael@0 78 {
michael@0 79 nsCOMPtr<nsIURI> uri1, uri2;
michael@0 80 NS_NewURI(getter_AddRefs(uri1), aSpec1);
michael@0 81 if (aSpec2)
michael@0 82 NS_NewURI(getter_AddRefs(uri2), aSpec2);
michael@0 83
michael@0 84 sBuffer = PR_sprintf_append(sBuffer, " \"%s\": GOT ", aSpec1);
michael@0 85 nsresult rv = aCookieService->GetCookieStringFromHttp(uri1, uri2, nullptr, aCookie);
michael@0 86 if (NS_FAILED(rv)) {
michael@0 87 sBuffer = PR_sprintf_append(sBuffer, "XXX GetCookieString() failed!\n");
michael@0 88 }
michael@0 89 if (!*aCookie) {
michael@0 90 sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
michael@0 91 } else {
michael@0 92 sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", *aCookie);
michael@0 93 }
michael@0 94 return *aCookie != nullptr;
michael@0 95 }
michael@0 96
michael@0 97 // returns true if cookie(s) for the given host were found; else false.
michael@0 98 // the cookie string is returned via aCookie.
michael@0 99 bool
michael@0 100 GetACookieNoHttp(nsICookieService *aCookieService, const char *aSpec, char **aCookie)
michael@0 101 {
michael@0 102 nsCOMPtr<nsIURI> uri;
michael@0 103 NS_NewURI(getter_AddRefs(uri), aSpec);
michael@0 104
michael@0 105 sBuffer = PR_sprintf_append(sBuffer, " \"%s\": GOT ", aSpec);
michael@0 106 nsresult rv = aCookieService->GetCookieString(uri, nullptr, aCookie);
michael@0 107 if (NS_FAILED(rv)) {
michael@0 108 sBuffer = PR_sprintf_append(sBuffer, "XXX GetCookieString() failed!\n");
michael@0 109 }
michael@0 110 if (!*aCookie) {
michael@0 111 sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
michael@0 112 } else {
michael@0 113 sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", *aCookie);
michael@0 114 }
michael@0 115 return *aCookie != nullptr;
michael@0 116 }
michael@0 117
michael@0 118 // some #defines for comparison rules
michael@0 119 #define MUST_BE_NULL 0
michael@0 120 #define MUST_EQUAL 1
michael@0 121 #define MUST_CONTAIN 2
michael@0 122 #define MUST_NOT_CONTAIN 3
michael@0 123 #define MUST_NOT_EQUAL 4
michael@0 124
michael@0 125 // a simple helper function to improve readability:
michael@0 126 // takes one of the #defined rules above, and performs the appropriate test.
michael@0 127 // true means the test passed; false means the test failed.
michael@0 128 static inline bool
michael@0 129 CheckResult(const char *aLhs, uint32_t aRule, const char *aRhs = nullptr)
michael@0 130 {
michael@0 131 switch (aRule) {
michael@0 132 case MUST_BE_NULL:
michael@0 133 return !aLhs || !*aLhs;
michael@0 134
michael@0 135 case MUST_EQUAL:
michael@0 136 return !PL_strcmp(aLhs, aRhs);
michael@0 137
michael@0 138 case MUST_NOT_EQUAL:
michael@0 139 return PL_strcmp(aLhs, aRhs);
michael@0 140
michael@0 141 case MUST_CONTAIN:
michael@0 142 return PL_strstr(aLhs, aRhs) != nullptr;
michael@0 143
michael@0 144 case MUST_NOT_CONTAIN:
michael@0 145 return PL_strstr(aLhs, aRhs) == nullptr;
michael@0 146
michael@0 147 default:
michael@0 148 return false; // failure
michael@0 149 }
michael@0 150 }
michael@0 151
michael@0 152 // helper function that ensures the first aSize elements of aResult are
michael@0 153 // true (i.e. all tests succeeded). prints the result of the tests (if any
michael@0 154 // tests failed, it prints the zero-based index of each failed test).
michael@0 155 bool
michael@0 156 PrintResult(const bool aResult[], uint32_t aSize)
michael@0 157 {
michael@0 158 bool failed = false;
michael@0 159 sBuffer = PR_sprintf_append(sBuffer, "*** tests ");
michael@0 160 for (uint32_t i = 0; i < aSize; ++i) {
michael@0 161 if (!aResult[i]) {
michael@0 162 failed = true;
michael@0 163 sBuffer = PR_sprintf_append(sBuffer, "%d ", i);
michael@0 164 }
michael@0 165 }
michael@0 166 if (failed) {
michael@0 167 sBuffer = PR_sprintf_append(sBuffer, "FAILED!\a\n");
michael@0 168 } else {
michael@0 169 sBuffer = PR_sprintf_append(sBuffer, "passed.\n");
michael@0 170 }
michael@0 171 return !failed;
michael@0 172 }
michael@0 173
michael@0 174 void
michael@0 175 InitPrefs(nsIPrefBranch *aPrefBranch)
michael@0 176 {
michael@0 177 // init some relevant prefs, so the tests don't go awry.
michael@0 178 // we use the most restrictive set of prefs we can;
michael@0 179 // however, we don't test third party blocking here.
michael@0 180 aPrefBranch->SetIntPref(kCookiesPermissions, 0); // accept all
michael@0 181 aPrefBranch->SetBoolPref(kCookiesLifetimeEnabled, true);
michael@0 182 aPrefBranch->SetIntPref(kCookiesLifetimeCurrentSession, 0);
michael@0 183 aPrefBranch->SetIntPref(kCookiesLifetimeDays, 1);
michael@0 184 aPrefBranch->SetBoolPref(kCookiesAskPermission, false);
michael@0 185 // Set the base domain limit to 50 so we have a known value.
michael@0 186 aPrefBranch->SetIntPref(kCookiesMaxPerHost, 50);
michael@0 187 }
michael@0 188
michael@0 189 class ScopedXPCOM
michael@0 190 {
michael@0 191 public:
michael@0 192 ScopedXPCOM() : rv(NS_InitXPCOM2(nullptr, nullptr, nullptr)) { }
michael@0 193 ~ScopedXPCOM()
michael@0 194 {
michael@0 195 if (NS_SUCCEEDED(rv))
michael@0 196 NS_ShutdownXPCOM(nullptr);
michael@0 197 }
michael@0 198
michael@0 199 nsresult rv;
michael@0 200 };
michael@0 201
michael@0 202 int
michael@0 203 main(int32_t argc, char *argv[])
michael@0 204 {
michael@0 205 if (test_common_init(&argc, &argv) != 0)
michael@0 206 return -1;
michael@0 207
michael@0 208 bool allTestsPassed = true;
michael@0 209
michael@0 210 ScopedXPCOM xpcom;
michael@0 211 if (NS_FAILED(xpcom.rv))
michael@0 212 return -1;
michael@0 213
michael@0 214 {
michael@0 215 nsresult rv0;
michael@0 216
michael@0 217 nsCOMPtr<nsICookieService> cookieService =
michael@0 218 do_GetService(kCookieServiceCID, &rv0);
michael@0 219 if (NS_FAILED(rv0)) return -1;
michael@0 220
michael@0 221 nsCOMPtr<nsIPrefBranch> prefBranch =
michael@0 222 do_GetService(kPrefServiceCID, &rv0);
michael@0 223 if (NS_FAILED(rv0)) return -1;
michael@0 224
michael@0 225 InitPrefs(prefBranch);
michael@0 226
michael@0 227 bool rv[20];
michael@0 228 nsCString cookie;
michael@0 229
michael@0 230 /* The basic idea behind these tests is the following:
michael@0 231 *
michael@0 232 * we set() some cookie, then try to get() it in various ways. we have
michael@0 233 * several possible tests we perform on the cookie string returned from
michael@0 234 * get():
michael@0 235 *
michael@0 236 * a) check whether the returned string is null (i.e. we got no cookies
michael@0 237 * back). this is used e.g. to ensure a given cookie was deleted
michael@0 238 * correctly, or to ensure a certain cookie wasn't returned to a given
michael@0 239 * host.
michael@0 240 * b) check whether the returned string exactly matches a given string.
michael@0 241 * this is used where we want to make sure our cookie service adheres to
michael@0 242 * some strict spec (e.g. ordering of multiple cookies), or where we
michael@0 243 * just know exactly what the returned string should be.
michael@0 244 * c) check whether the returned string contains/does not contain a given
michael@0 245 * string. this is used where we don't know/don't care about the
michael@0 246 * ordering of multiple cookies - we just want to make sure the cookie
michael@0 247 * string contains them all, in some order.
michael@0 248 *
michael@0 249 * the results of each individual testing operation from CheckResult() is
michael@0 250 * stored in an array of bools, which is then checked against the expected
michael@0 251 * outcomes (all successes), by PrintResult(). the overall result of all
michael@0 252 * tests to date is kept in |allTestsPassed|, for convenient display at the
michael@0 253 * end.
michael@0 254 *
michael@0 255 * Interpreting the output:
michael@0 256 * each setting/getting operation will print output saying exactly what
michael@0 257 * it's doing and the outcome, respectively. this information is only
michael@0 258 * useful for debugging purposes; the actual result of the tests is
michael@0 259 * printed at the end of each block of tests. this will either be "all
michael@0 260 * tests passed" or "tests X Y Z failed", where X, Y, Z are the indexes
michael@0 261 * of rv (i.e. zero-based). at the conclusion of all tests, the overall
michael@0 262 * passed/failed result is printed.
michael@0 263 *
michael@0 264 * NOTE: this testsuite is not yet comprehensive or complete, and is
michael@0 265 * somewhat contrived - still under development, and needs improving!
michael@0 266 */
michael@0 267
michael@0 268 // *** basic tests
michael@0 269 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning basic tests...\n");
michael@0 270
michael@0 271 // test some basic variations of the domain & path
michael@0 272 SetACookie(cookieService, "http://www.basic.com", nullptr, "test=basic", nullptr);
michael@0 273 GetACookie(cookieService, "http://www.basic.com", nullptr, getter_Copies(cookie));
michael@0 274 rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=basic");
michael@0 275 GetACookie(cookieService, "http://www.basic.com/testPath/testfile.txt", nullptr, getter_Copies(cookie));
michael@0 276 rv[1] = CheckResult(cookie.get(), MUST_EQUAL, "test=basic");
michael@0 277 GetACookie(cookieService, "http://www.basic.com./", nullptr, getter_Copies(cookie));
michael@0 278 rv[2] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 279 GetACookie(cookieService, "http://www.basic.com.", nullptr, getter_Copies(cookie));
michael@0 280 rv[3] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 281 GetACookie(cookieService, "http://www.basic.com./testPath/testfile.txt", nullptr, getter_Copies(cookie));
michael@0 282 rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 283 GetACookie(cookieService, "http://www.basic2.com/", nullptr, getter_Copies(cookie));
michael@0 284 rv[5] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 285 SetACookie(cookieService, "http://www.basic.com", nullptr, "test=basic; max-age=-1", nullptr);
michael@0 286 GetACookie(cookieService, "http://www.basic.com/", nullptr, getter_Copies(cookie));
michael@0 287 rv[6] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 288
michael@0 289 allTestsPassed = PrintResult(rv, 7) && allTestsPassed;
michael@0 290
michael@0 291
michael@0 292 // *** domain tests
michael@0 293 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning domain tests...\n");
michael@0 294
michael@0 295 // test some variations of the domain & path, for different domains of
michael@0 296 // a domain cookie
michael@0 297 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=domain.com", nullptr);
michael@0 298 GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
michael@0 299 rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
michael@0 300 GetACookie(cookieService, "http://domain.com.", nullptr, getter_Copies(cookie));
michael@0 301 rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 302 GetACookie(cookieService, "http://www.domain.com", nullptr, getter_Copies(cookie));
michael@0 303 rv[2] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
michael@0 304 GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
michael@0 305 rv[3] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
michael@0 306 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=domain.com; max-age=-1", nullptr);
michael@0 307 GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
michael@0 308 rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 309
michael@0 310 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=.domain.com", nullptr);
michael@0 311 GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
michael@0 312 rv[5] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
michael@0 313 GetACookie(cookieService, "http://www.domain.com", nullptr, getter_Copies(cookie));
michael@0 314 rv[6] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
michael@0 315 GetACookie(cookieService, "http://bah.domain.com", nullptr, getter_Copies(cookie));
michael@0 316 rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
michael@0 317 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=.domain.com; max-age=-1", nullptr);
michael@0 318 GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
michael@0 319 rv[8] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 320
michael@0 321 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=.foo.domain.com", nullptr);
michael@0 322 GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
michael@0 323 rv[9] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 324
michael@0 325 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=moose.com", nullptr);
michael@0 326 GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
michael@0 327 rv[10] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 328
michael@0 329 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=domain.com.", nullptr);
michael@0 330 GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
michael@0 331 rv[11] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 332
michael@0 333 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=..domain.com", nullptr);
michael@0 334 GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
michael@0 335 rv[12] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 336
michael@0 337 SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=..domain.com.", nullptr);
michael@0 338 GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
michael@0 339 rv[13] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 340
michael@0 341 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=taco; path=\"/bogus\"", nullptr);
michael@0 342 GetACookie(cookieService, "http://path.net/path/file", nullptr, getter_Copies(cookie));
michael@0 343 rv[14] = CheckResult(cookie.get(), MUST_EQUAL, "test=taco");
michael@0 344 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=taco; max-age=-1", nullptr);
michael@0 345 GetACookie(cookieService, "http://path.net/path/file", nullptr, getter_Copies(cookie));
michael@0 346 rv[15] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 347
michael@0 348 allTestsPassed = PrintResult(rv, 16) && allTestsPassed;
michael@0 349
michael@0 350
michael@0 351 // *** path tests
michael@0 352 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning path tests...\n");
michael@0 353
michael@0 354 // test some variations of the domain & path, for different paths of
michael@0 355 // a path cookie
michael@0 356 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path", nullptr);
michael@0 357 GetACookie(cookieService, "http://path.net/path", nullptr, getter_Copies(cookie));
michael@0 358 rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
michael@0 359 GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
michael@0 360 rv[1] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
michael@0 361 GetACookie(cookieService, "http://path.net/path/hithere.foo", nullptr, getter_Copies(cookie));
michael@0 362 rv[2] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
michael@0 363 GetACookie(cookieService, "http://path.net/path?hithere/foo", nullptr, getter_Copies(cookie));
michael@0 364 rv[3] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
michael@0 365 GetACookie(cookieService, "http://path.net/path2", nullptr, getter_Copies(cookie));
michael@0 366 rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 367 GetACookie(cookieService, "http://path.net/path2/", nullptr, getter_Copies(cookie));
michael@0 368 rv[5] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 369 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path; max-age=-1", nullptr);
michael@0 370 GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
michael@0 371 rv[6] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 372
michael@0 373 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path/", nullptr);
michael@0 374 GetACookie(cookieService, "http://path.net/path", nullptr, getter_Copies(cookie));
michael@0 375 rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
michael@0 376 GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
michael@0 377 rv[8] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
michael@0 378 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path/; max-age=-1", nullptr);
michael@0 379 GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
michael@0 380 rv[9] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 381
michael@0 382 // note that a site can set a cookie for a path it's not on.
michael@0 383 // this is an intentional deviation from spec (see comments in
michael@0 384 // nsCookieService::CheckPath()), so we test this functionality too
michael@0 385 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/foo/", nullptr);
michael@0 386 GetACookie(cookieService, "http://path.net/path", nullptr, getter_Copies(cookie));
michael@0 387 rv[10] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 388 GetACookie(cookieService, "http://path.net/foo", nullptr, getter_Copies(cookie));
michael@0 389 rv[11] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
michael@0 390 SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/foo/; max-age=-1", nullptr);
michael@0 391 GetACookie(cookieService, "http://path.net/foo/", nullptr, getter_Copies(cookie));
michael@0 392 rv[12] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 393
michael@0 394 // bug 373228: make sure cookies with paths longer than 1024 bytes,
michael@0 395 // and cookies with paths or names containing tabs, are rejected.
michael@0 396 // the following cookie has a path > 1024 bytes explicitly specified in the cookie
michael@0 397 SetACookie(cookieService, "http://path.net/", nullptr, "test=path; path=/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890/", nullptr);
michael@0 398 GetACookie(cookieService, "http://path.net/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", nullptr, getter_Copies(cookie));
michael@0 399 rv[13] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 400 // the following cookie has a path > 1024 bytes implicitly specified by the uri path
michael@0 401 SetACookie(cookieService, "http://path.net/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890/", nullptr, "test=path", nullptr);
michael@0 402 GetACookie(cookieService, "http://path.net/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890/", nullptr, getter_Copies(cookie));
michael@0 403 rv[14] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 404 // the following cookie includes a tab in the path
michael@0 405 SetACookie(cookieService, "http://path.net/", nullptr, "test=path; path=/foo\tbar/", nullptr);
michael@0 406 GetACookie(cookieService, "http://path.net/foo\tbar/", nullptr, getter_Copies(cookie));
michael@0 407 rv[15] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 408 // the following cookie includes a tab in the name
michael@0 409 SetACookie(cookieService, "http://path.net/", nullptr, "test\ttabs=tab", nullptr);
michael@0 410 GetACookie(cookieService, "http://path.net/", nullptr, getter_Copies(cookie));
michael@0 411 rv[16] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 412 // the following cookie includes a tab in the value - allowed
michael@0 413 SetACookie(cookieService, "http://path.net/", nullptr, "test=tab\ttest", nullptr);
michael@0 414 GetACookie(cookieService, "http://path.net/", nullptr, getter_Copies(cookie));
michael@0 415 rv[17] = CheckResult(cookie.get(), MUST_EQUAL, "test=tab\ttest");
michael@0 416 SetACookie(cookieService, "http://path.net/", nullptr, "test=tab\ttest; max-age=-1", nullptr);
michael@0 417 GetACookie(cookieService, "http://path.net/", nullptr, getter_Copies(cookie));
michael@0 418 rv[18] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 419
michael@0 420 allTestsPassed = PrintResult(rv, 19) && allTestsPassed;
michael@0 421
michael@0 422
michael@0 423 // *** expiry & deletion tests
michael@0 424 // XXX add server time str parsing tests here
michael@0 425 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning expiry & deletion tests...\n");
michael@0 426
michael@0 427 // test some variations of the expiry time,
michael@0 428 // and test deletion of previously set cookies
michael@0 429 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=-1", nullptr);
michael@0 430 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 431 rv[0] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 432 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=0", nullptr);
michael@0 433 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 434 rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 435 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=bad", nullptr);
michael@0 436 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 437 rv[2] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
michael@0 438 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nullptr);
michael@0 439 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 440 rv[3] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 441 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=\"Thu, 10 Apr 1980 16:33:12 GMT", nullptr);
michael@0 442 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 443 rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 444 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=\"Thu, 10 Apr 1980 16:33:12 GMT\"", nullptr);
michael@0 445 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 446 rv[5] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 447
michael@0 448 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=60", nullptr);
michael@0 449 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 450 rv[6] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
michael@0 451 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=-20", nullptr);
michael@0 452 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 453 rv[7] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 454 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=60", nullptr);
michael@0 455 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 456 rv[8] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
michael@0 457 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nullptr);
michael@0 458 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 459 rv[9] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 460 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=60", nullptr);
michael@0 461 SetACookie(cookieService, "http://expireme.org/", nullptr, "newtest=expiry; max-age=60", nullptr);
michael@0 462 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 463 rv[10] = CheckResult(cookie.get(), MUST_CONTAIN, "test=expiry");
michael@0 464 rv[11] = CheckResult(cookie.get(), MUST_CONTAIN, "newtest=expiry");
michael@0 465 SetACookie(cookieService, "http://expireme.org/", nullptr, "test=differentvalue; max-age=0", nullptr);
michael@0 466 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 467 rv[12] = CheckResult(cookie.get(), MUST_EQUAL, "newtest=expiry");
michael@0 468 SetACookie(cookieService, "http://expireme.org/", nullptr, "newtest=evendifferentvalue; max-age=0", nullptr);
michael@0 469 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 470 rv[13] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 471
michael@0 472 SetACookie(cookieService, "http://foo.expireme.org/", nullptr, "test=expiry; domain=.expireme.org; max-age=60", nullptr);
michael@0 473 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 474 rv[14] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
michael@0 475 SetACookie(cookieService, "http://bar.expireme.org/", nullptr, "test=differentvalue; domain=.expireme.org; max-age=0", nullptr);
michael@0 476 GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
michael@0 477 rv[15] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 478
michael@0 479 allTestsPassed = PrintResult(rv, 16) && allTestsPassed;
michael@0 480
michael@0 481
michael@0 482 // *** multiple cookie tests
michael@0 483 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning multiple cookie tests...\n");
michael@0 484
michael@0 485 // test the setting of multiple cookies, and test the order of precedence
michael@0 486 // (a later cookie overwriting an earlier one, in the same header string)
michael@0 487 SetACookie(cookieService, "http://multiple.cookies/", nullptr, "test=multiple; domain=.multiple.cookies \n test=different \n test=same; domain=.multiple.cookies \n newtest=ciao \n newtest=foo; max-age=-6 \n newtest=reincarnated", nullptr);
michael@0 488 GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
michael@0 489 rv[0] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test=multiple");
michael@0 490 rv[1] = CheckResult(cookie.get(), MUST_CONTAIN, "test=different");
michael@0 491 rv[2] = CheckResult(cookie.get(), MUST_CONTAIN, "test=same");
michael@0 492 rv[3] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "newtest=ciao");
michael@0 493 rv[4] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "newtest=foo");
michael@0 494 rv[5] = CheckResult(cookie.get(), MUST_CONTAIN, "newtest=reincarnated");
michael@0 495 SetACookie(cookieService, "http://multiple.cookies/", nullptr, "test=expiry; domain=.multiple.cookies; max-age=0", nullptr);
michael@0 496 GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
michael@0 497 rv[6] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test=same");
michael@0 498 SetACookie(cookieService, "http://multiple.cookies/", nullptr, "\n test=different; max-age=0 \n", nullptr);
michael@0 499 GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
michael@0 500 rv[7] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test=different");
michael@0 501 SetACookie(cookieService, "http://multiple.cookies/", nullptr, "newtest=dead; max-age=0", nullptr);
michael@0 502 GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
michael@0 503 rv[8] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 504
michael@0 505 allTestsPassed = PrintResult(rv, 9) && allTestsPassed;
michael@0 506
michael@0 507
michael@0 508 // *** parser tests
michael@0 509 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning parser tests...\n");
michael@0 510
michael@0 511 // test the cookie header parser, under various circumstances.
michael@0 512 SetACookie(cookieService, "http://parser.test/", nullptr, "test=parser; domain=.parser.test; ;; ;=; ,,, ===,abc,=; abracadabra! max-age=20;=;;", nullptr);
michael@0 513 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 514 rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=parser");
michael@0 515 SetACookie(cookieService, "http://parser.test/", nullptr, "test=parser; domain=.parser.test; max-age=0", nullptr);
michael@0 516 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 517 rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 518 SetACookie(cookieService, "http://parser.test/", nullptr, "test=\"fubar! = foo;bar\\\";\" parser; domain=.parser.test; max-age=6\nfive; max-age=2.63,", nullptr);
michael@0 519 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 520 rv[2] = CheckResult(cookie.get(), MUST_CONTAIN, "test=\"fubar! = foo");
michael@0 521 rv[3] = CheckResult(cookie.get(), MUST_CONTAIN, "five");
michael@0 522 SetACookie(cookieService, "http://parser.test/", nullptr, "test=kill; domain=.parser.test; max-age=0 \n five; max-age=0", nullptr);
michael@0 523 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 524 rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 525
michael@0 526 // test the handling of VALUE-only cookies (see bug 169091),
michael@0 527 // i.e. "six" should assume an empty NAME, which allows other VALUE-only
michael@0 528 // cookies to overwrite it
michael@0 529 SetACookie(cookieService, "http://parser.test/", nullptr, "six", nullptr);
michael@0 530 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 531 rv[5] = CheckResult(cookie.get(), MUST_EQUAL, "six");
michael@0 532 SetACookie(cookieService, "http://parser.test/", nullptr, "seven", nullptr);
michael@0 533 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 534 rv[6] = CheckResult(cookie.get(), MUST_EQUAL, "seven");
michael@0 535 SetACookie(cookieService, "http://parser.test/", nullptr, " =eight", nullptr);
michael@0 536 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 537 rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "eight");
michael@0 538 SetACookie(cookieService, "http://parser.test/", nullptr, "test=six", nullptr);
michael@0 539 GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
michael@0 540 rv[9] = CheckResult(cookie.get(), MUST_CONTAIN, "test=six");
michael@0 541
michael@0 542 allTestsPassed = PrintResult(rv, 10) && allTestsPassed;
michael@0 543
michael@0 544
michael@0 545 // *** path ordering tests
michael@0 546 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning path ordering tests...\n");
michael@0 547
michael@0 548 // test that cookies are returned in path order - longest to shortest.
michael@0 549 // if the header doesn't specify a path, it's taken from the host URI.
michael@0 550 SetACookie(cookieService, "http://multi.path.tests/", nullptr, "test1=path; path=/one/two/three", nullptr);
michael@0 551 SetACookie(cookieService, "http://multi.path.tests/", nullptr, "test2=path; path=/one \n test3=path; path=/one/two/three/four \n test4=path; path=/one/two \n test5=path; path=/one/two/", nullptr);
michael@0 552 SetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/", nullptr, "test6=path", nullptr);
michael@0 553 SetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/six/", nullptr, "test7=path; path=", nullptr);
michael@0 554 SetACookie(cookieService, "http://multi.path.tests/", nullptr, "test8=path; path=/", nullptr);
michael@0 555 GetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/six/", nullptr, getter_Copies(cookie));
michael@0 556 rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test7=path; test6=path; test3=path; test1=path; test5=path; test4=path; test2=path; test8=path");
michael@0 557
michael@0 558 allTestsPassed = PrintResult(rv, 1) && allTestsPassed;
michael@0 559
michael@0 560
michael@0 561 // *** httponly tests
michael@0 562 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning httponly tests...\n");
michael@0 563
michael@0 564 // Since this cookie is NOT set via http, setting it fails
michael@0 565 SetACookieNoHttp(cookieService, "http://httponly.test/", "test=httponly; httponly");
michael@0 566 GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
michael@0 567 rv[0] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 568 // Since this cookie is set via http, it can be retrieved
michael@0 569 SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
michael@0 570 GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
michael@0 571 rv[1] = CheckResult(cookie.get(), MUST_EQUAL, "test=httponly");
michael@0 572 // ... but not by web content
michael@0 573 GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
michael@0 574 rv[2] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 575 // Non-Http cookies should not replace HttpOnly cookies
michael@0 576 SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
michael@0 577 SetACookieNoHttp(cookieService, "http://httponly.test/", "test=not-httponly");
michael@0 578 GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
michael@0 579 rv[3] = CheckResult(cookie.get(), MUST_EQUAL, "test=httponly");
michael@0 580 // ... and, if an HttpOnly cookie already exists, should not be set at all
michael@0 581 GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
michael@0 582 rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 583 // Non-Http cookies should not delete HttpOnly cookies
michael@0 584 SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
michael@0 585 SetACookieNoHttp(cookieService, "http://httponly.test/", "test=httponly; max-age=-1");
michael@0 586 GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
michael@0 587 rv[5] = CheckResult(cookie.get(), MUST_EQUAL, "test=httponly");
michael@0 588 // ... but HttpOnly cookies should
michael@0 589 SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly; max-age=-1", nullptr);
michael@0 590 GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
michael@0 591 rv[6] = CheckResult(cookie.get(), MUST_BE_NULL);
michael@0 592 // Non-Httponly cookies can replace HttpOnly cookies when set over http
michael@0 593 SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
michael@0 594 SetACookie(cookieService, "http://httponly.test/", nullptr, "test=not-httponly", nullptr);
michael@0 595 GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
michael@0 596 rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "test=not-httponly");
michael@0 597 // scripts should not be able to set httponly cookies by replacing an existing non-httponly cookie
michael@0 598 SetACookie(cookieService, "http://httponly.test/", nullptr, "test=not-httponly", nullptr);
michael@0 599 SetACookieNoHttp(cookieService, "http://httponly.test/", "test=httponly; httponly");
michael@0 600 GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
michael@0 601 rv[8] = CheckResult(cookie.get(), MUST_EQUAL, "test=not-httponly");
michael@0 602
michael@0 603 allTestsPassed = PrintResult(rv, 9) && allTestsPassed;
michael@0 604
michael@0 605
michael@0 606 // *** nsICookieManager{2} interface tests
michael@0 607 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning nsICookieManager{2} interface tests...\n");
michael@0 608 nsCOMPtr<nsICookieManager> cookieMgr = do_GetService(NS_COOKIEMANAGER_CONTRACTID, &rv0);
michael@0 609 if (NS_FAILED(rv0)) return -1;
michael@0 610 nsCOMPtr<nsICookieManager2> cookieMgr2 = do_QueryInterface(cookieMgr);
michael@0 611 if (!cookieMgr2) return -1;
michael@0 612
michael@0 613 // first, ensure a clean slate
michael@0 614 rv[0] = NS_SUCCEEDED(cookieMgr->RemoveAll());
michael@0 615 // add some cookies
michael@0 616 rv[1] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
michael@0 617 NS_LITERAL_CSTRING("/foo"), // path
michael@0 618 NS_LITERAL_CSTRING("test1"), // name
michael@0 619 NS_LITERAL_CSTRING("yes"), // value
michael@0 620 false, // is secure
michael@0 621 false, // is httponly
michael@0 622 true, // is session
michael@0 623 INT64_MAX)); // expiry time
michael@0 624 rv[2] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
michael@0 625 NS_LITERAL_CSTRING("/foo"), // path
michael@0 626 NS_LITERAL_CSTRING("test2"), // name
michael@0 627 NS_LITERAL_CSTRING("yes"), // value
michael@0 628 false, // is secure
michael@0 629 true, // is httponly
michael@0 630 true, // is session
michael@0 631 PR_Now() / PR_USEC_PER_SEC + 2)); // expiry time
michael@0 632 rv[3] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("new.domain"), // domain
michael@0 633 NS_LITERAL_CSTRING("/rabbit"), // path
michael@0 634 NS_LITERAL_CSTRING("test3"), // name
michael@0 635 NS_LITERAL_CSTRING("yes"), // value
michael@0 636 false, // is secure
michael@0 637 false, // is httponly
michael@0 638 true, // is session
michael@0 639 INT64_MAX)); // expiry time
michael@0 640 // confirm using enumerator
michael@0 641 nsCOMPtr<nsISimpleEnumerator> enumerator;
michael@0 642 rv[4] = NS_SUCCEEDED(cookieMgr->GetEnumerator(getter_AddRefs(enumerator)));
michael@0 643 int32_t i = 0;
michael@0 644 bool more;
michael@0 645 nsCOMPtr<nsICookie2> expiredCookie, newDomainCookie;
michael@0 646 while (NS_SUCCEEDED(enumerator->HasMoreElements(&more)) && more) {
michael@0 647 nsCOMPtr<nsISupports> cookie;
michael@0 648 if (NS_FAILED(enumerator->GetNext(getter_AddRefs(cookie)))) break;
michael@0 649 ++i;
michael@0 650
michael@0 651 // keep tabs on the second and third cookies, so we can check them later
michael@0 652 nsCOMPtr<nsICookie2> cookie2(do_QueryInterface(cookie));
michael@0 653 if (!cookie2) break;
michael@0 654 nsAutoCString name;
michael@0 655 cookie2->GetName(name);
michael@0 656 if (name == NS_LITERAL_CSTRING("test2"))
michael@0 657 expiredCookie = cookie2;
michael@0 658 else if (name == NS_LITERAL_CSTRING("test3"))
michael@0 659 newDomainCookie = cookie2;
michael@0 660 }
michael@0 661 rv[5] = i == 3;
michael@0 662 // check the httpOnly attribute of the second cookie is honored
michael@0 663 GetACookie(cookieService, "http://cookiemgr.test/foo/", nullptr, getter_Copies(cookie));
michael@0 664 rv[6] = CheckResult(cookie.get(), MUST_CONTAIN, "test2=yes");
michael@0 665 GetACookieNoHttp(cookieService, "http://cookiemgr.test/foo/", getter_Copies(cookie));
michael@0 666 rv[7] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test2=yes");
michael@0 667 // check CountCookiesFromHost()
michael@0 668 uint32_t hostCookies = 0;
michael@0 669 rv[8] = NS_SUCCEEDED(cookieMgr2->CountCookiesFromHost(NS_LITERAL_CSTRING("cookiemgr.test"), &hostCookies)) &&
michael@0 670 hostCookies == 2;
michael@0 671 // check CookieExists() using the third cookie
michael@0 672 bool found;
michael@0 673 rv[9] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && found;
michael@0 674 // remove the cookie, block it, and ensure it can't be added again
michael@0 675 rv[10] = NS_SUCCEEDED(cookieMgr->Remove(NS_LITERAL_CSTRING("new.domain"), // domain
michael@0 676 NS_LITERAL_CSTRING("test3"), // name
michael@0 677 NS_LITERAL_CSTRING("/rabbit"), // path
michael@0 678 true)); // is blocked
michael@0 679 rv[11] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
michael@0 680 rv[12] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("new.domain"), // domain
michael@0 681 NS_LITERAL_CSTRING("/rabbit"), // path
michael@0 682 NS_LITERAL_CSTRING("test3"), // name
michael@0 683 NS_LITERAL_CSTRING("yes"), // value
michael@0 684 false, // is secure
michael@0 685 false, // is httponly
michael@0 686 true, // is session
michael@0 687 INT64_MIN)); // expiry time
michael@0 688 rv[13] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
michael@0 689 // sleep four seconds, to make sure the second cookie has expired
michael@0 690 PR_Sleep(4 * PR_TicksPerSecond());
michael@0 691 // check that both CountCookiesFromHost() and CookieExists() count the
michael@0 692 // expired cookie
michael@0 693 rv[14] = NS_SUCCEEDED(cookieMgr2->CountCookiesFromHost(NS_LITERAL_CSTRING("cookiemgr.test"), &hostCookies)) &&
michael@0 694 hostCookies == 2;
michael@0 695 rv[15] = NS_SUCCEEDED(cookieMgr2->CookieExists(expiredCookie, &found)) && found;
michael@0 696 // double-check RemoveAll() using the enumerator
michael@0 697 rv[16] = NS_SUCCEEDED(cookieMgr->RemoveAll());
michael@0 698 rv[17] = NS_SUCCEEDED(cookieMgr->GetEnumerator(getter_AddRefs(enumerator))) &&
michael@0 699 NS_SUCCEEDED(enumerator->HasMoreElements(&more)) &&
michael@0 700 !more;
michael@0 701
michael@0 702 allTestsPassed = PrintResult(rv, 18) && allTestsPassed;
michael@0 703
michael@0 704
michael@0 705 // *** eviction and creation ordering tests
michael@0 706 sBuffer = PR_sprintf_append(sBuffer, "*** Beginning eviction and creation ordering tests...\n");
michael@0 707
michael@0 708 // test that cookies are
michael@0 709 // a) returned by order of creation time (oldest first, newest last)
michael@0 710 // b) evicted by order of lastAccessed time, if the limit on cookies per host (50) is reached
michael@0 711 nsAutoCString name;
michael@0 712 nsAutoCString expected;
michael@0 713 for (int32_t i = 0; i < 60; ++i) {
michael@0 714 name = NS_LITERAL_CSTRING("test");
michael@0 715 name.AppendInt(i);
michael@0 716 name += NS_LITERAL_CSTRING("=creation");
michael@0 717 SetACookie(cookieService, "http://creation.ordering.tests/", nullptr, name.get(), nullptr);
michael@0 718
michael@0 719 if (i >= 10) {
michael@0 720 expected += name;
michael@0 721 if (i < 59)
michael@0 722 expected += NS_LITERAL_CSTRING("; ");
michael@0 723 }
michael@0 724 }
michael@0 725 GetACookie(cookieService, "http://creation.ordering.tests/", nullptr, getter_Copies(cookie));
michael@0 726 rv[0] = CheckResult(cookie.get(), MUST_EQUAL, expected.get());
michael@0 727
michael@0 728 allTestsPassed = PrintResult(rv, 1) && allTestsPassed;
michael@0 729
michael@0 730
michael@0 731 // XXX the following are placeholders: add these tests please!
michael@0 732 // *** "noncompliant cookie" tests
michael@0 733 // *** IP address tests
michael@0 734 // *** speed tests
michael@0 735
michael@0 736
michael@0 737 sBuffer = PR_sprintf_append(sBuffer, "\n*** Result: %s!\n\n", allTestsPassed ? "all tests passed" : "TEST(S) FAILED");
michael@0 738 }
michael@0 739
michael@0 740 if (!allTestsPassed) {
michael@0 741 // print the entire log
michael@0 742 printf("%s", sBuffer);
michael@0 743 return 1;
michael@0 744 }
michael@0 745
michael@0 746 PR_smprintf_free(sBuffer);
michael@0 747 sBuffer = nullptr;
michael@0 748
michael@0 749 return 0;
michael@0 750 }

mercurial