LCOV - code coverage report
Current view: directory - netwerk/test - TestCookie.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 413 398 96.4 %
Date: 2012-06-02 Functions: 10 10 100.0 %

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

Generated by: LCOV version 1.7