netwerk/test/TestCookie.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/TestCookie.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,750 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "TestCommon.h"
    1.10 +#include "nsIServiceManager.h"
    1.11 +#include "nsICookieService.h"
    1.12 +#include "nsICookieManager.h"
    1.13 +#include "nsICookieManager2.h"
    1.14 +#include "nsICookie2.h"
    1.15 +#include <stdio.h>
    1.16 +#include "plstr.h"
    1.17 +#include "prprf.h"
    1.18 +#include "nsNetUtil.h"
    1.19 +#include "nsNetCID.h"
    1.20 +#include "nsStringAPI.h"
    1.21 +#include "nsIPrefBranch.h"
    1.22 +#include "nsIPrefService.h"
    1.23 +
    1.24 +static NS_DEFINE_CID(kCookieServiceCID, NS_COOKIESERVICE_CID);
    1.25 +static NS_DEFINE_CID(kPrefServiceCID,   NS_PREFSERVICE_CID);
    1.26 +
    1.27 +// various pref strings
    1.28 +static const char kCookiesPermissions[] = "network.cookie.cookieBehavior";
    1.29 +static const char kCookiesLifetimeEnabled[] = "network.cookie.lifetime.enabled";
    1.30 +static const char kCookiesLifetimeDays[] = "network.cookie.lifetime.days";
    1.31 +static const char kCookiesLifetimeCurrentSession[] = "network.cookie.lifetime.behavior";
    1.32 +static const char kCookiesAskPermission[] = "network.cookie.warnAboutCookies";
    1.33 +static const char kCookiesMaxPerHost[] = "network.cookie.maxPerHost";
    1.34 +
    1.35 +static char *sBuffer;
    1.36 +
    1.37 +nsresult
    1.38 +SetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSpec2, const char* aCookieString, const char *aServerTime)
    1.39 +{
    1.40 +    nsCOMPtr<nsIURI> uri1, uri2;
    1.41 +    NS_NewURI(getter_AddRefs(uri1), aSpec1);
    1.42 +    if (aSpec2)
    1.43 +        NS_NewURI(getter_AddRefs(uri2), aSpec2);
    1.44 +
    1.45 +    sBuffer = PR_sprintf_append(sBuffer, "    for host \"%s\": SET ", aSpec1);
    1.46 +    nsresult rv = aCookieService->SetCookieStringFromHttp(uri1, uri2, nullptr, (char *)aCookieString, aServerTime, nullptr);
    1.47 +    // the following code is useless. the cookieservice blindly returns NS_OK
    1.48 +    // from SetCookieString. we have to call GetCookie to see if the cookie was
    1.49 +    // set correctly...
    1.50 +    if (NS_FAILED(rv)) {
    1.51 +        sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
    1.52 +    } else {
    1.53 +        sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", aCookieString);
    1.54 +    }
    1.55 +    return rv;
    1.56 +}
    1.57 +
    1.58 +nsresult
    1.59 +SetACookieNoHttp(nsICookieService *aCookieService, const char *aSpec, const char* aCookieString)
    1.60 +{
    1.61 +    nsCOMPtr<nsIURI> uri;
    1.62 +    NS_NewURI(getter_AddRefs(uri), aSpec);
    1.63 +
    1.64 +    sBuffer = PR_sprintf_append(sBuffer, "    for host \"%s\": SET ", aSpec);
    1.65 +    nsresult rv = aCookieService->SetCookieString(uri, nullptr, (char *)aCookieString, nullptr);
    1.66 +    // the following code is useless. the cookieservice blindly returns NS_OK
    1.67 +    // from SetCookieString. we have to call GetCookie to see if the cookie was
    1.68 +    // set correctly...
    1.69 +    if (NS_FAILED(rv)) {
    1.70 +        sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
    1.71 +    } else {
    1.72 +        sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", aCookieString);
    1.73 +    }
    1.74 +    return rv;
    1.75 +}
    1.76 +
    1.77 +// returns true if cookie(s) for the given host were found; else false.
    1.78 +// the cookie string is returned via aCookie.
    1.79 +bool
    1.80 +GetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSpec2, char **aCookie)
    1.81 +{
    1.82 +    nsCOMPtr<nsIURI> uri1, uri2;
    1.83 +    NS_NewURI(getter_AddRefs(uri1), aSpec1);
    1.84 +    if (aSpec2)
    1.85 +        NS_NewURI(getter_AddRefs(uri2), aSpec2);
    1.86 +
    1.87 +    sBuffer = PR_sprintf_append(sBuffer, "             \"%s\": GOT ", aSpec1);
    1.88 +    nsresult rv = aCookieService->GetCookieStringFromHttp(uri1, uri2, nullptr, aCookie);
    1.89 +    if (NS_FAILED(rv)) {
    1.90 +      sBuffer = PR_sprintf_append(sBuffer, "XXX GetCookieString() failed!\n");
    1.91 +    }
    1.92 +    if (!*aCookie) {
    1.93 +        sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
    1.94 +    } else {
    1.95 +        sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", *aCookie);
    1.96 +    }
    1.97 +    return *aCookie != nullptr;
    1.98 +}
    1.99 +
   1.100 +// returns true if cookie(s) for the given host were found; else false.
   1.101 +// the cookie string is returned via aCookie.
   1.102 +bool
   1.103 +GetACookieNoHttp(nsICookieService *aCookieService, const char *aSpec, char **aCookie)
   1.104 +{
   1.105 +    nsCOMPtr<nsIURI> uri;
   1.106 +    NS_NewURI(getter_AddRefs(uri), aSpec);
   1.107 +
   1.108 +    sBuffer = PR_sprintf_append(sBuffer, "             \"%s\": GOT ", aSpec);
   1.109 +    nsresult rv = aCookieService->GetCookieString(uri, nullptr, aCookie);
   1.110 +    if (NS_FAILED(rv)) {
   1.111 +      sBuffer = PR_sprintf_append(sBuffer, "XXX GetCookieString() failed!\n");
   1.112 +    }
   1.113 +    if (!*aCookie) {
   1.114 +        sBuffer = PR_sprintf_append(sBuffer, "nothing\n");
   1.115 +    } else {
   1.116 +        sBuffer = PR_sprintf_append(sBuffer, "\"%s\"\n", *aCookie);
   1.117 +    }
   1.118 +    return *aCookie != nullptr;
   1.119 +}
   1.120 +
   1.121 +// some #defines for comparison rules
   1.122 +#define MUST_BE_NULL     0
   1.123 +#define MUST_EQUAL       1
   1.124 +#define MUST_CONTAIN     2
   1.125 +#define MUST_NOT_CONTAIN 3
   1.126 +#define MUST_NOT_EQUAL   4
   1.127 +
   1.128 +// a simple helper function to improve readability:
   1.129 +// takes one of the #defined rules above, and performs the appropriate test.
   1.130 +// true means the test passed; false means the test failed.
   1.131 +static inline bool
   1.132 +CheckResult(const char *aLhs, uint32_t aRule, const char *aRhs = nullptr)
   1.133 +{
   1.134 +    switch (aRule) {
   1.135 +        case MUST_BE_NULL:
   1.136 +            return !aLhs || !*aLhs;
   1.137 +
   1.138 +        case MUST_EQUAL:
   1.139 +            return !PL_strcmp(aLhs, aRhs);
   1.140 +
   1.141 +        case MUST_NOT_EQUAL:
   1.142 +            return PL_strcmp(aLhs, aRhs);
   1.143 +
   1.144 +        case MUST_CONTAIN:
   1.145 +            return PL_strstr(aLhs, aRhs) != nullptr;
   1.146 +
   1.147 +        case MUST_NOT_CONTAIN:
   1.148 +            return PL_strstr(aLhs, aRhs) == nullptr;
   1.149 +
   1.150 +        default:
   1.151 +            return false; // failure
   1.152 +    }
   1.153 +}
   1.154 +
   1.155 +// helper function that ensures the first aSize elements of aResult are
   1.156 +// true (i.e. all tests succeeded). prints the result of the tests (if any
   1.157 +// tests failed, it prints the zero-based index of each failed test).
   1.158 +bool
   1.159 +PrintResult(const bool aResult[], uint32_t aSize)
   1.160 +{
   1.161 +    bool failed = false;
   1.162 +    sBuffer = PR_sprintf_append(sBuffer, "*** tests ");
   1.163 +    for (uint32_t i = 0; i < aSize; ++i) {
   1.164 +        if (!aResult[i]) {
   1.165 +            failed = true;
   1.166 +            sBuffer = PR_sprintf_append(sBuffer, "%d ", i);
   1.167 +        }
   1.168 +    }
   1.169 +    if (failed) {
   1.170 +        sBuffer = PR_sprintf_append(sBuffer, "FAILED!\a\n");
   1.171 +    } else {
   1.172 +        sBuffer = PR_sprintf_append(sBuffer, "passed.\n");
   1.173 +    }
   1.174 +    return !failed;
   1.175 +}
   1.176 +
   1.177 +void
   1.178 +InitPrefs(nsIPrefBranch *aPrefBranch)
   1.179 +{
   1.180 +    // init some relevant prefs, so the tests don't go awry.
   1.181 +    // we use the most restrictive set of prefs we can;
   1.182 +    // however, we don't test third party blocking here.
   1.183 +    aPrefBranch->SetIntPref(kCookiesPermissions, 0); // accept all
   1.184 +    aPrefBranch->SetBoolPref(kCookiesLifetimeEnabled, true);
   1.185 +    aPrefBranch->SetIntPref(kCookiesLifetimeCurrentSession, 0);
   1.186 +    aPrefBranch->SetIntPref(kCookiesLifetimeDays, 1);
   1.187 +    aPrefBranch->SetBoolPref(kCookiesAskPermission, false);
   1.188 +    // Set the base domain limit to 50 so we have a known value.
   1.189 +    aPrefBranch->SetIntPref(kCookiesMaxPerHost, 50);
   1.190 +}
   1.191 +
   1.192 +class ScopedXPCOM
   1.193 +{
   1.194 +public:
   1.195 +  ScopedXPCOM() : rv(NS_InitXPCOM2(nullptr, nullptr, nullptr)) { }
   1.196 +  ~ScopedXPCOM()
   1.197 +  {
   1.198 +    if (NS_SUCCEEDED(rv))
   1.199 +      NS_ShutdownXPCOM(nullptr);
   1.200 +  }
   1.201 +
   1.202 +  nsresult rv;
   1.203 +};
   1.204 +
   1.205 +int
   1.206 +main(int32_t argc, char *argv[])
   1.207 +{
   1.208 +    if (test_common_init(&argc, &argv) != 0)
   1.209 +        return -1;
   1.210 +
   1.211 +    bool allTestsPassed = true;
   1.212 +
   1.213 +    ScopedXPCOM xpcom;
   1.214 +    if (NS_FAILED(xpcom.rv))
   1.215 +      return -1;
   1.216 +
   1.217 +    {
   1.218 +      nsresult rv0;
   1.219 +
   1.220 +      nsCOMPtr<nsICookieService> cookieService =
   1.221 +        do_GetService(kCookieServiceCID, &rv0);
   1.222 +      if (NS_FAILED(rv0)) return -1;
   1.223 +
   1.224 +      nsCOMPtr<nsIPrefBranch> prefBranch =
   1.225 +        do_GetService(kPrefServiceCID, &rv0);
   1.226 +      if (NS_FAILED(rv0)) return -1;
   1.227 +
   1.228 +      InitPrefs(prefBranch);
   1.229 +
   1.230 +      bool rv[20];
   1.231 +      nsCString cookie;
   1.232 +
   1.233 +      /* The basic idea behind these tests is the following:
   1.234 +       *
   1.235 +       * we set() some cookie, then try to get() it in various ways. we have
   1.236 +       * several possible tests we perform on the cookie string returned from
   1.237 +       * get():
   1.238 +       *
   1.239 +       * a) check whether the returned string is null (i.e. we got no cookies
   1.240 +       *    back). this is used e.g. to ensure a given cookie was deleted
   1.241 +       *    correctly, or to ensure a certain cookie wasn't returned to a given
   1.242 +       *    host.
   1.243 +       * b) check whether the returned string exactly matches a given string.
   1.244 +       *    this is used where we want to make sure our cookie service adheres to
   1.245 +       *    some strict spec (e.g. ordering of multiple cookies), or where we
   1.246 +       *    just know exactly what the returned string should be.
   1.247 +       * c) check whether the returned string contains/does not contain a given
   1.248 +       *    string. this is used where we don't know/don't care about the
   1.249 +       *    ordering of multiple cookies - we just want to make sure the cookie
   1.250 +       *    string contains them all, in some order.
   1.251 +       *
   1.252 +       * the results of each individual testing operation from CheckResult() is
   1.253 +       * stored in an array of bools, which is then checked against the expected
   1.254 +       * outcomes (all successes), by PrintResult(). the overall result of all
   1.255 +       * tests to date is kept in |allTestsPassed|, for convenient display at the
   1.256 +       * end.
   1.257 +       *
   1.258 +       * Interpreting the output:
   1.259 +       * each setting/getting operation will print output saying exactly what
   1.260 +       * it's doing and the outcome, respectively. this information is only
   1.261 +       * useful for debugging purposes; the actual result of the tests is
   1.262 +       * printed at the end of each block of tests. this will either be "all
   1.263 +       * tests passed" or "tests X Y Z failed", where X, Y, Z are the indexes
   1.264 +       * of rv (i.e. zero-based). at the conclusion of all tests, the overall
   1.265 +       * passed/failed result is printed.
   1.266 +       *
   1.267 +       * NOTE: this testsuite is not yet comprehensive or complete, and is
   1.268 +       * somewhat contrived - still under development, and needs improving!
   1.269 +       */
   1.270 +
   1.271 +      // *** basic tests
   1.272 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning basic tests...\n");
   1.273 +
   1.274 +      // test some basic variations of the domain & path
   1.275 +      SetACookie(cookieService, "http://www.basic.com", nullptr, "test=basic", nullptr);
   1.276 +      GetACookie(cookieService, "http://www.basic.com", nullptr, getter_Copies(cookie));
   1.277 +      rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=basic");
   1.278 +      GetACookie(cookieService, "http://www.basic.com/testPath/testfile.txt", nullptr, getter_Copies(cookie));
   1.279 +      rv[1] = CheckResult(cookie.get(), MUST_EQUAL, "test=basic");
   1.280 +      GetACookie(cookieService, "http://www.basic.com./", nullptr, getter_Copies(cookie));
   1.281 +      rv[2] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.282 +      GetACookie(cookieService, "http://www.basic.com.", nullptr, getter_Copies(cookie));
   1.283 +      rv[3] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.284 +      GetACookie(cookieService, "http://www.basic.com./testPath/testfile.txt", nullptr, getter_Copies(cookie));
   1.285 +      rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.286 +      GetACookie(cookieService, "http://www.basic2.com/", nullptr, getter_Copies(cookie));
   1.287 +      rv[5] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.288 +      SetACookie(cookieService, "http://www.basic.com", nullptr, "test=basic; max-age=-1", nullptr);
   1.289 +      GetACookie(cookieService, "http://www.basic.com/", nullptr, getter_Copies(cookie));
   1.290 +      rv[6] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.291 +
   1.292 +      allTestsPassed = PrintResult(rv, 7) && allTestsPassed;
   1.293 +
   1.294 +
   1.295 +      // *** domain tests
   1.296 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning domain tests...\n");
   1.297 +
   1.298 +      // test some variations of the domain & path, for different domains of
   1.299 +      // a domain cookie
   1.300 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=domain.com", nullptr);
   1.301 +      GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
   1.302 +      rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
   1.303 +      GetACookie(cookieService, "http://domain.com.", nullptr, getter_Copies(cookie));
   1.304 +      rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.305 +      GetACookie(cookieService, "http://www.domain.com", nullptr, getter_Copies(cookie));
   1.306 +      rv[2] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
   1.307 +      GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
   1.308 +      rv[3] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
   1.309 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=domain.com; max-age=-1", nullptr);
   1.310 +      GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
   1.311 +      rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.312 +
   1.313 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=.domain.com", nullptr);
   1.314 +      GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
   1.315 +      rv[5] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
   1.316 +      GetACookie(cookieService, "http://www.domain.com", nullptr, getter_Copies(cookie));
   1.317 +      rv[6] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
   1.318 +      GetACookie(cookieService, "http://bah.domain.com", nullptr, getter_Copies(cookie));
   1.319 +      rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "test=domain");
   1.320 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=.domain.com; max-age=-1", nullptr);
   1.321 +      GetACookie(cookieService, "http://domain.com", nullptr, getter_Copies(cookie));
   1.322 +      rv[8] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.323 +
   1.324 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=.foo.domain.com", nullptr);
   1.325 +      GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
   1.326 +      rv[9] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.327 +
   1.328 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=moose.com", nullptr);
   1.329 +      GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
   1.330 +      rv[10] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.331 +
   1.332 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=domain.com.", nullptr);
   1.333 +      GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
   1.334 +      rv[11] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.335 +
   1.336 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=..domain.com", nullptr);
   1.337 +      GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
   1.338 +      rv[12] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.339 +
   1.340 +      SetACookie(cookieService, "http://www.domain.com", nullptr, "test=domain; domain=..domain.com.", nullptr);
   1.341 +      GetACookie(cookieService, "http://foo.domain.com", nullptr, getter_Copies(cookie));
   1.342 +      rv[13] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.343 +
   1.344 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=taco; path=\"/bogus\"", nullptr);
   1.345 +      GetACookie(cookieService, "http://path.net/path/file", nullptr, getter_Copies(cookie));
   1.346 +      rv[14] = CheckResult(cookie.get(), MUST_EQUAL, "test=taco");
   1.347 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=taco; max-age=-1", nullptr);
   1.348 +      GetACookie(cookieService, "http://path.net/path/file", nullptr, getter_Copies(cookie));
   1.349 +      rv[15] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.350 +
   1.351 +      allTestsPassed = PrintResult(rv, 16) && allTestsPassed;
   1.352 +
   1.353 +
   1.354 +      // *** path tests
   1.355 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning path tests...\n");
   1.356 +
   1.357 +      // test some variations of the domain & path, for different paths of
   1.358 +      // a path cookie
   1.359 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path", nullptr);
   1.360 +      GetACookie(cookieService, "http://path.net/path", nullptr, getter_Copies(cookie));
   1.361 +      rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
   1.362 +      GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
   1.363 +      rv[1] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
   1.364 +      GetACookie(cookieService, "http://path.net/path/hithere.foo", nullptr, getter_Copies(cookie));
   1.365 +      rv[2] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
   1.366 +      GetACookie(cookieService, "http://path.net/path?hithere/foo", nullptr, getter_Copies(cookie));
   1.367 +      rv[3] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
   1.368 +      GetACookie(cookieService, "http://path.net/path2", nullptr, getter_Copies(cookie));
   1.369 +      rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.370 +      GetACookie(cookieService, "http://path.net/path2/", nullptr, getter_Copies(cookie));
   1.371 +      rv[5] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.372 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path; max-age=-1", nullptr);
   1.373 +      GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
   1.374 +      rv[6] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.375 +
   1.376 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path/", nullptr);
   1.377 +      GetACookie(cookieService, "http://path.net/path", nullptr, getter_Copies(cookie));
   1.378 +      rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
   1.379 +      GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
   1.380 +      rv[8] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
   1.381 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/path/; max-age=-1", nullptr);
   1.382 +      GetACookie(cookieService, "http://path.net/path/", nullptr, getter_Copies(cookie));
   1.383 +      rv[9] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.384 +
   1.385 +      // note that a site can set a cookie for a path it's not on.
   1.386 +      // this is an intentional deviation from spec (see comments in
   1.387 +      // nsCookieService::CheckPath()), so we test this functionality too
   1.388 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/foo/", nullptr);
   1.389 +      GetACookie(cookieService, "http://path.net/path", nullptr, getter_Copies(cookie));
   1.390 +      rv[10] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.391 +      GetACookie(cookieService, "http://path.net/foo", nullptr, getter_Copies(cookie));
   1.392 +      rv[11] = CheckResult(cookie.get(), MUST_EQUAL, "test=path");
   1.393 +      SetACookie(cookieService, "http://path.net/path/file", nullptr, "test=path; path=/foo/; max-age=-1", nullptr);
   1.394 +      GetACookie(cookieService, "http://path.net/foo/", nullptr, getter_Copies(cookie));
   1.395 +      rv[12] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.396 +
   1.397 +      // bug 373228: make sure cookies with paths longer than 1024 bytes,
   1.398 +      // and cookies with paths or names containing tabs, are rejected.
   1.399 +      // the following cookie has a path > 1024 bytes explicitly specified in the cookie
   1.400 +      SetACookie(cookieService, "http://path.net/", nullptr, "test=path; path=/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890/", nullptr);
   1.401 +      GetACookie(cookieService, "http://path.net/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", nullptr, getter_Copies(cookie));
   1.402 +      rv[13] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.403 +      // the following cookie has a path > 1024 bytes implicitly specified by the uri path
   1.404 +      SetACookie(cookieService, "http://path.net/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890/", nullptr, "test=path", nullptr);
   1.405 +      GetACookie(cookieService, "http://path.net/1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890/", nullptr, getter_Copies(cookie));
   1.406 +      rv[14] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.407 +      // the following cookie includes a tab in the path
   1.408 +      SetACookie(cookieService, "http://path.net/", nullptr, "test=path; path=/foo\tbar/", nullptr);
   1.409 +      GetACookie(cookieService, "http://path.net/foo\tbar/", nullptr, getter_Copies(cookie));
   1.410 +      rv[15] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.411 +      // the following cookie includes a tab in the name
   1.412 +      SetACookie(cookieService, "http://path.net/", nullptr, "test\ttabs=tab", nullptr);
   1.413 +      GetACookie(cookieService, "http://path.net/", nullptr, getter_Copies(cookie));
   1.414 +      rv[16] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.415 +      // the following cookie includes a tab in the value - allowed
   1.416 +      SetACookie(cookieService, "http://path.net/", nullptr, "test=tab\ttest", nullptr);
   1.417 +      GetACookie(cookieService, "http://path.net/", nullptr, getter_Copies(cookie));
   1.418 +      rv[17] = CheckResult(cookie.get(), MUST_EQUAL, "test=tab\ttest");
   1.419 +      SetACookie(cookieService, "http://path.net/", nullptr, "test=tab\ttest; max-age=-1", nullptr);
   1.420 +      GetACookie(cookieService, "http://path.net/", nullptr, getter_Copies(cookie));
   1.421 +      rv[18] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.422 +
   1.423 +      allTestsPassed = PrintResult(rv, 19) && allTestsPassed;
   1.424 +
   1.425 +
   1.426 +      // *** expiry & deletion tests
   1.427 +      // XXX add server time str parsing tests here
   1.428 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning expiry & deletion tests...\n");
   1.429 +
   1.430 +      // test some variations of the expiry time,
   1.431 +      // and test deletion of previously set cookies
   1.432 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=-1", nullptr);
   1.433 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.434 +      rv[0] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.435 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=0", nullptr);
   1.436 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.437 +      rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.438 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=bad", nullptr);
   1.439 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.440 +      rv[2] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
   1.441 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nullptr);
   1.442 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.443 +      rv[3] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.444 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=\"Thu, 10 Apr 1980 16:33:12 GMT", nullptr);
   1.445 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.446 +      rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.447 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=\"Thu, 10 Apr 1980 16:33:12 GMT\"", nullptr);
   1.448 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.449 +      rv[5] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.450 +
   1.451 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=60", nullptr);
   1.452 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.453 +      rv[6] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
   1.454 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=-20", nullptr);
   1.455 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.456 +      rv[7] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.457 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=60", nullptr);
   1.458 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.459 +      rv[8] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
   1.460 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nullptr);
   1.461 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.462 +      rv[9] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.463 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=expiry; max-age=60", nullptr);
   1.464 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "newtest=expiry; max-age=60", nullptr);
   1.465 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.466 +      rv[10] = CheckResult(cookie.get(), MUST_CONTAIN, "test=expiry");
   1.467 +      rv[11] = CheckResult(cookie.get(), MUST_CONTAIN, "newtest=expiry");
   1.468 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "test=differentvalue; max-age=0", nullptr);
   1.469 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.470 +      rv[12] = CheckResult(cookie.get(), MUST_EQUAL, "newtest=expiry");
   1.471 +      SetACookie(cookieService, "http://expireme.org/", nullptr, "newtest=evendifferentvalue; max-age=0", nullptr);
   1.472 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.473 +      rv[13] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.474 +
   1.475 +      SetACookie(cookieService, "http://foo.expireme.org/", nullptr, "test=expiry; domain=.expireme.org; max-age=60", nullptr);
   1.476 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.477 +      rv[14] = CheckResult(cookie.get(), MUST_EQUAL, "test=expiry");
   1.478 +      SetACookie(cookieService, "http://bar.expireme.org/", nullptr, "test=differentvalue; domain=.expireme.org; max-age=0", nullptr);
   1.479 +      GetACookie(cookieService, "http://expireme.org/", nullptr, getter_Copies(cookie));
   1.480 +      rv[15] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.481 +
   1.482 +      allTestsPassed = PrintResult(rv, 16) && allTestsPassed;
   1.483 +
   1.484 +
   1.485 +      // *** multiple cookie tests
   1.486 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning multiple cookie tests...\n");
   1.487 +
   1.488 +      // test the setting of multiple cookies, and test the order of precedence
   1.489 +      // (a later cookie overwriting an earlier one, in the same header string)
   1.490 +      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);
   1.491 +      GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
   1.492 +      rv[0] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test=multiple");
   1.493 +      rv[1] = CheckResult(cookie.get(), MUST_CONTAIN, "test=different");
   1.494 +      rv[2] = CheckResult(cookie.get(), MUST_CONTAIN, "test=same");
   1.495 +      rv[3] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "newtest=ciao");
   1.496 +      rv[4] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "newtest=foo");
   1.497 +      rv[5] = CheckResult(cookie.get(), MUST_CONTAIN, "newtest=reincarnated");
   1.498 +      SetACookie(cookieService, "http://multiple.cookies/", nullptr, "test=expiry; domain=.multiple.cookies; max-age=0", nullptr);
   1.499 +      GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
   1.500 +      rv[6] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test=same");
   1.501 +      SetACookie(cookieService, "http://multiple.cookies/", nullptr,  "\n test=different; max-age=0 \n", nullptr);
   1.502 +      GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
   1.503 +      rv[7] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test=different");
   1.504 +      SetACookie(cookieService, "http://multiple.cookies/", nullptr,  "newtest=dead; max-age=0", nullptr);
   1.505 +      GetACookie(cookieService, "http://multiple.cookies/", nullptr, getter_Copies(cookie));
   1.506 +      rv[8] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.507 +
   1.508 +      allTestsPassed = PrintResult(rv, 9) && allTestsPassed;
   1.509 +
   1.510 +
   1.511 +      // *** parser tests
   1.512 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning parser tests...\n");
   1.513 +
   1.514 +      // test the cookie header parser, under various circumstances.
   1.515 +      SetACookie(cookieService, "http://parser.test/", nullptr, "test=parser; domain=.parser.test; ;; ;=; ,,, ===,abc,=; abracadabra! max-age=20;=;;", nullptr);
   1.516 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.517 +      rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test=parser");
   1.518 +      SetACookie(cookieService, "http://parser.test/", nullptr, "test=parser; domain=.parser.test; max-age=0", nullptr);
   1.519 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.520 +      rv[1] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.521 +      SetACookie(cookieService, "http://parser.test/", nullptr, "test=\"fubar! = foo;bar\\\";\" parser; domain=.parser.test; max-age=6\nfive; max-age=2.63,", nullptr);
   1.522 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.523 +      rv[2] = CheckResult(cookie.get(), MUST_CONTAIN, "test=\"fubar! = foo");
   1.524 +      rv[3] = CheckResult(cookie.get(), MUST_CONTAIN, "five");
   1.525 +      SetACookie(cookieService, "http://parser.test/", nullptr, "test=kill; domain=.parser.test; max-age=0 \n five; max-age=0", nullptr);
   1.526 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.527 +      rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.528 +
   1.529 +      // test the handling of VALUE-only cookies (see bug 169091),
   1.530 +      // i.e. "six" should assume an empty NAME, which allows other VALUE-only
   1.531 +      // cookies to overwrite it
   1.532 +      SetACookie(cookieService, "http://parser.test/", nullptr, "six", nullptr);
   1.533 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.534 +      rv[5] = CheckResult(cookie.get(), MUST_EQUAL, "six");
   1.535 +      SetACookie(cookieService, "http://parser.test/", nullptr, "seven", nullptr);
   1.536 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.537 +      rv[6] = CheckResult(cookie.get(), MUST_EQUAL, "seven");
   1.538 +      SetACookie(cookieService, "http://parser.test/", nullptr, " =eight", nullptr);
   1.539 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.540 +      rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "eight");
   1.541 +      SetACookie(cookieService, "http://parser.test/", nullptr, "test=six", nullptr);
   1.542 +      GetACookie(cookieService, "http://parser.test/", nullptr, getter_Copies(cookie));
   1.543 +      rv[9] = CheckResult(cookie.get(), MUST_CONTAIN, "test=six");
   1.544 +
   1.545 +      allTestsPassed = PrintResult(rv, 10) && allTestsPassed;
   1.546 +
   1.547 +
   1.548 +      // *** path ordering tests
   1.549 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning path ordering tests...\n");
   1.550 +
   1.551 +      // test that cookies are returned in path order - longest to shortest.
   1.552 +      // if the header doesn't specify a path, it's taken from the host URI.
   1.553 +      SetACookie(cookieService, "http://multi.path.tests/", nullptr, "test1=path; path=/one/two/three", nullptr);
   1.554 +      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);
   1.555 +      SetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/", nullptr, "test6=path", nullptr);
   1.556 +      SetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/six/", nullptr, "test7=path; path=", nullptr);
   1.557 +      SetACookie(cookieService, "http://multi.path.tests/", nullptr, "test8=path; path=/", nullptr);
   1.558 +      GetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/six/", nullptr, getter_Copies(cookie));
   1.559 +      rv[0] = CheckResult(cookie.get(), MUST_EQUAL, "test7=path; test6=path; test3=path; test1=path; test5=path; test4=path; test2=path; test8=path");
   1.560 +
   1.561 +      allTestsPassed = PrintResult(rv, 1) && allTestsPassed;
   1.562 +
   1.563 +
   1.564 +      // *** httponly tests 
   1.565 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning httponly tests...\n");
   1.566 +
   1.567 +      // Since this cookie is NOT set via http, setting it fails
   1.568 +      SetACookieNoHttp(cookieService, "http://httponly.test/", "test=httponly; httponly");
   1.569 +      GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
   1.570 +      rv[0] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.571 +      // Since this cookie is set via http, it can be retrieved
   1.572 +      SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
   1.573 +      GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
   1.574 +      rv[1] = CheckResult(cookie.get(), MUST_EQUAL, "test=httponly");
   1.575 +      // ... but not by web content
   1.576 +      GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
   1.577 +      rv[2] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.578 +      // Non-Http cookies should not replace HttpOnly cookies
   1.579 +      SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
   1.580 +      SetACookieNoHttp(cookieService, "http://httponly.test/", "test=not-httponly");
   1.581 +      GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
   1.582 +      rv[3] = CheckResult(cookie.get(), MUST_EQUAL, "test=httponly");
   1.583 +      // ... and, if an HttpOnly cookie already exists, should not be set at all
   1.584 +      GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
   1.585 +      rv[4] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.586 +      // Non-Http cookies should not delete HttpOnly cookies
   1.587 +      SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
   1.588 +      SetACookieNoHttp(cookieService, "http://httponly.test/", "test=httponly; max-age=-1");
   1.589 +      GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
   1.590 +      rv[5] = CheckResult(cookie.get(), MUST_EQUAL, "test=httponly");
   1.591 +      // ... but HttpOnly cookies should
   1.592 +      SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly; max-age=-1", nullptr);
   1.593 +      GetACookie(cookieService, "http://httponly.test/", nullptr, getter_Copies(cookie));
   1.594 +      rv[6] = CheckResult(cookie.get(), MUST_BE_NULL);
   1.595 +      // Non-Httponly cookies can replace HttpOnly cookies when set over http
   1.596 +      SetACookie(cookieService, "http://httponly.test/", nullptr, "test=httponly; httponly", nullptr);
   1.597 +      SetACookie(cookieService, "http://httponly.test/", nullptr, "test=not-httponly", nullptr);
   1.598 +      GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
   1.599 +      rv[7] = CheckResult(cookie.get(), MUST_EQUAL, "test=not-httponly");
   1.600 +      // scripts should not be able to set httponly cookies by replacing an existing non-httponly cookie
   1.601 +      SetACookie(cookieService, "http://httponly.test/", nullptr, "test=not-httponly", nullptr);
   1.602 +      SetACookieNoHttp(cookieService, "http://httponly.test/", "test=httponly; httponly");
   1.603 +      GetACookieNoHttp(cookieService, "http://httponly.test/", getter_Copies(cookie));
   1.604 +      rv[8] = CheckResult(cookie.get(), MUST_EQUAL, "test=not-httponly");
   1.605 +
   1.606 +      allTestsPassed = PrintResult(rv, 9) && allTestsPassed;
   1.607 +
   1.608 +
   1.609 +      // *** nsICookieManager{2} interface tests
   1.610 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning nsICookieManager{2} interface tests...\n");
   1.611 +      nsCOMPtr<nsICookieManager> cookieMgr = do_GetService(NS_COOKIEMANAGER_CONTRACTID, &rv0);
   1.612 +      if (NS_FAILED(rv0)) return -1;
   1.613 +      nsCOMPtr<nsICookieManager2> cookieMgr2 = do_QueryInterface(cookieMgr);
   1.614 +      if (!cookieMgr2) return -1;
   1.615 +      
   1.616 +      // first, ensure a clean slate
   1.617 +      rv[0] = NS_SUCCEEDED(cookieMgr->RemoveAll());
   1.618 +      // add some cookies
   1.619 +      rv[1] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
   1.620 +                                           NS_LITERAL_CSTRING("/foo"),           // path
   1.621 +                                           NS_LITERAL_CSTRING("test1"),          // name
   1.622 +                                           NS_LITERAL_CSTRING("yes"),            // value
   1.623 +                                           false,                             // is secure
   1.624 +                                           false,                             // is httponly
   1.625 +                                           true,                              // is session
   1.626 +                                           INT64_MAX));                          // expiry time
   1.627 +      rv[2] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("cookiemgr.test"), // domain
   1.628 +                                           NS_LITERAL_CSTRING("/foo"),           // path
   1.629 +                                           NS_LITERAL_CSTRING("test2"),          // name
   1.630 +                                           NS_LITERAL_CSTRING("yes"),            // value
   1.631 +                                           false,                             // is secure
   1.632 +                                           true,                              // is httponly
   1.633 +                                           true,                              // is session
   1.634 +                                           PR_Now() / PR_USEC_PER_SEC + 2));     // expiry time
   1.635 +      rv[3] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("new.domain"),     // domain
   1.636 +                                           NS_LITERAL_CSTRING("/rabbit"),        // path
   1.637 +                                           NS_LITERAL_CSTRING("test3"),          // name
   1.638 +                                           NS_LITERAL_CSTRING("yes"),            // value
   1.639 +                                           false,                             // is secure
   1.640 +                                           false,                             // is httponly
   1.641 +                                           true,                              // is session
   1.642 +                                           INT64_MAX));                          // expiry time
   1.643 +      // confirm using enumerator
   1.644 +      nsCOMPtr<nsISimpleEnumerator> enumerator;
   1.645 +      rv[4] = NS_SUCCEEDED(cookieMgr->GetEnumerator(getter_AddRefs(enumerator)));
   1.646 +      int32_t i = 0;
   1.647 +      bool more;
   1.648 +      nsCOMPtr<nsICookie2> expiredCookie, newDomainCookie;
   1.649 +      while (NS_SUCCEEDED(enumerator->HasMoreElements(&more)) && more) {
   1.650 +        nsCOMPtr<nsISupports> cookie;
   1.651 +        if (NS_FAILED(enumerator->GetNext(getter_AddRefs(cookie)))) break;
   1.652 +        ++i;
   1.653 +        
   1.654 +        // keep tabs on the second and third cookies, so we can check them later
   1.655 +        nsCOMPtr<nsICookie2> cookie2(do_QueryInterface(cookie));
   1.656 +        if (!cookie2) break;
   1.657 +        nsAutoCString name;
   1.658 +        cookie2->GetName(name);
   1.659 +        if (name == NS_LITERAL_CSTRING("test2"))
   1.660 +          expiredCookie = cookie2;
   1.661 +        else if (name == NS_LITERAL_CSTRING("test3"))
   1.662 +          newDomainCookie = cookie2;
   1.663 +      }
   1.664 +      rv[5] = i == 3;
   1.665 +      // check the httpOnly attribute of the second cookie is honored
   1.666 +      GetACookie(cookieService, "http://cookiemgr.test/foo/", nullptr, getter_Copies(cookie));
   1.667 +      rv[6] = CheckResult(cookie.get(), MUST_CONTAIN, "test2=yes");
   1.668 +      GetACookieNoHttp(cookieService, "http://cookiemgr.test/foo/", getter_Copies(cookie));
   1.669 +      rv[7] = CheckResult(cookie.get(), MUST_NOT_CONTAIN, "test2=yes");
   1.670 +      // check CountCookiesFromHost()
   1.671 +      uint32_t hostCookies = 0;
   1.672 +      rv[8] = NS_SUCCEEDED(cookieMgr2->CountCookiesFromHost(NS_LITERAL_CSTRING("cookiemgr.test"), &hostCookies)) &&
   1.673 +              hostCookies == 2;
   1.674 +      // check CookieExists() using the third cookie
   1.675 +      bool found;
   1.676 +      rv[9] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && found;
   1.677 +      // remove the cookie, block it, and ensure it can't be added again
   1.678 +      rv[10] = NS_SUCCEEDED(cookieMgr->Remove(NS_LITERAL_CSTRING("new.domain"), // domain
   1.679 +                                              NS_LITERAL_CSTRING("test3"),      // name
   1.680 +                                              NS_LITERAL_CSTRING("/rabbit"),    // path
   1.681 +                                              true));                        // is blocked
   1.682 +      rv[11] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
   1.683 +      rv[12] = NS_SUCCEEDED(cookieMgr2->Add(NS_LITERAL_CSTRING("new.domain"),     // domain
   1.684 +                                            NS_LITERAL_CSTRING("/rabbit"),        // path
   1.685 +                                            NS_LITERAL_CSTRING("test3"),          // name
   1.686 +                                            NS_LITERAL_CSTRING("yes"),            // value
   1.687 +                                            false,                             // is secure
   1.688 +                                            false,                             // is httponly
   1.689 +                                            true,                              // is session
   1.690 +                                            INT64_MIN));                          // expiry time
   1.691 +      rv[13] = NS_SUCCEEDED(cookieMgr2->CookieExists(newDomainCookie, &found)) && !found;
   1.692 +      // sleep four seconds, to make sure the second cookie has expired
   1.693 +      PR_Sleep(4 * PR_TicksPerSecond());
   1.694 +      // check that both CountCookiesFromHost() and CookieExists() count the
   1.695 +      // expired cookie
   1.696 +      rv[14] = NS_SUCCEEDED(cookieMgr2->CountCookiesFromHost(NS_LITERAL_CSTRING("cookiemgr.test"), &hostCookies)) &&
   1.697 +              hostCookies == 2;
   1.698 +      rv[15] = NS_SUCCEEDED(cookieMgr2->CookieExists(expiredCookie, &found)) && found;
   1.699 +      // double-check RemoveAll() using the enumerator
   1.700 +      rv[16] = NS_SUCCEEDED(cookieMgr->RemoveAll());
   1.701 +      rv[17] = NS_SUCCEEDED(cookieMgr->GetEnumerator(getter_AddRefs(enumerator))) &&
   1.702 +               NS_SUCCEEDED(enumerator->HasMoreElements(&more)) &&
   1.703 +               !more;
   1.704 +
   1.705 +      allTestsPassed = PrintResult(rv, 18) && allTestsPassed;
   1.706 +
   1.707 +
   1.708 +      // *** eviction and creation ordering tests
   1.709 +      sBuffer = PR_sprintf_append(sBuffer, "*** Beginning eviction and creation ordering tests...\n");
   1.710 +
   1.711 +      // test that cookies are
   1.712 +      // a) returned by order of creation time (oldest first, newest last)
   1.713 +      // b) evicted by order of lastAccessed time, if the limit on cookies per host (50) is reached
   1.714 +      nsAutoCString name;
   1.715 +      nsAutoCString expected;
   1.716 +      for (int32_t i = 0; i < 60; ++i) {
   1.717 +        name = NS_LITERAL_CSTRING("test");
   1.718 +        name.AppendInt(i);
   1.719 +        name += NS_LITERAL_CSTRING("=creation");
   1.720 +        SetACookie(cookieService, "http://creation.ordering.tests/", nullptr, name.get(), nullptr);
   1.721 +
   1.722 +        if (i >= 10) {
   1.723 +          expected += name;
   1.724 +          if (i < 59)
   1.725 +            expected += NS_LITERAL_CSTRING("; ");
   1.726 +        }
   1.727 +      }
   1.728 +      GetACookie(cookieService, "http://creation.ordering.tests/", nullptr, getter_Copies(cookie));
   1.729 +      rv[0] = CheckResult(cookie.get(), MUST_EQUAL, expected.get());
   1.730 +
   1.731 +      allTestsPassed = PrintResult(rv, 1) && allTestsPassed;
   1.732 +
   1.733 +
   1.734 +      // XXX the following are placeholders: add these tests please!
   1.735 +      // *** "noncompliant cookie" tests
   1.736 +      // *** IP address tests
   1.737 +      // *** speed tests
   1.738 +
   1.739 +
   1.740 +      sBuffer = PR_sprintf_append(sBuffer, "\n*** Result: %s!\n\n", allTestsPassed ? "all tests passed" : "TEST(S) FAILED");
   1.741 +    }
   1.742 +
   1.743 +    if (!allTestsPassed) {
   1.744 +      // print the entire log
   1.745 +      printf("%s", sBuffer);
   1.746 +      return 1;
   1.747 +    }
   1.748 +
   1.749 +    PR_smprintf_free(sBuffer);
   1.750 +    sBuffer = nullptr;
   1.751 +
   1.752 +    return 0;
   1.753 +}

mercurial