Back to index

lightning-sunbird  0.9+nobinonly
TestCookie.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Daniel Witte (dwitte@stanford.edu)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "TestCommon.h"
00040 #include "nsIServiceManager.h"
00041 #include "nsICookieService.h"
00042 #include <stdio.h>
00043 #include "plstr.h"
00044 #include "nsNetUtil.h"
00045 #include "nsNetCID.h"
00046 #include "nsXPIDLString.h"
00047 #include "nsIPrefBranch.h"
00048 #include "nsIPrefService.h"
00049 
00050 static NS_DEFINE_CID(kCookieServiceCID, NS_COOKIESERVICE_CID);
00051 static NS_DEFINE_CID(kPrefServiceCID,   NS_PREFSERVICE_CID);
00052 
00053 // various pref strings
00054 static const char kCookiesPermissions[] = "network.cookie.cookieBehavior";
00055 static const char kCookiesDisabledForMailNews[] = "network.cookie.disableCookieForMailNews";
00056 static const char kCookiesLifetimeEnabled[] = "network.cookie.lifetime.enabled";
00057 static const char kCookiesLifetimeDays[] = "network.cookie.lifetime.days";
00058 static const char kCookiesLifetimeCurrentSession[] = "network.cookie.lifetime.behavior";
00059 static const char kCookiesP3PString[] = "network.cookie.p3p";
00060 static const char kCookiesAskPermission[] = "network.cookie.warnAboutCookies";
00061 
00062 nsresult
00063 SetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSpec2, const char* aCookieString, const char *aServerTime)
00064 {
00065     nsCOMPtr<nsIURI> uri1, uri2;
00066     NS_NewURI(getter_AddRefs(uri1), aSpec1);
00067     if (aSpec2)
00068         NS_NewURI(getter_AddRefs(uri2), aSpec2);
00069 
00070     printf("    for host \"%s\": SET ", aSpec1);
00071     nsresult rv = aCookieService->SetCookieStringFromHttp(uri1, uri2, nsnull, (char *)aCookieString, aServerTime, nsnull);
00072     // the following code is useless. the cookieservice blindly returns NS_OK
00073     // from SetCookieString. we have to call GetCookie to see if the cookie was
00074     // set correctly...
00075     if (NS_FAILED(rv)) {
00076         printf("nothing\n");
00077     } else {
00078         printf("\"%s\"\n", aCookieString);
00079     }
00080     return rv;
00081 }
00082 
00083 // returns PR_TRUE if cookie(s) for the given host were found; else PR_FALSE.
00084 // the cookie string is returned via aCookie.
00085 PRBool
00086 GetACookie(nsICookieService *aCookieService, const char *aSpec1, const char *aSpec2, char **aCookie)
00087 {
00088     nsCOMPtr<nsIURI> uri1, uri2;
00089     NS_NewURI(getter_AddRefs(uri1), aSpec1);
00090     if (aSpec2)
00091         NS_NewURI(getter_AddRefs(uri2), aSpec2);
00092 
00093     printf("             \"%s\": GOT ", aSpec1);
00094     nsresult rv = aCookieService->GetCookieStringFromHttp(uri1, uri2, nsnull, aCookie);
00095     if (NS_FAILED(rv)) printf("XXX GetCookieString() failed!\n");
00096     if (!*aCookie) {
00097         printf("nothing\n");
00098     } else {
00099         printf("\"%s\"\n", *aCookie);
00100     }
00101     return *aCookie != nsnull;
00102 }
00103 
00104 // some #defines for comparison rules
00105 #define MUST_BE_NULL     0
00106 #define MUST_EQUAL       1
00107 #define MUST_CONTAIN     2
00108 #define MUST_NOT_CONTAIN 3
00109 
00110 // a simple helper function to improve readability:
00111 // takes one of the #defined rules above, and performs the appropriate test.
00112 // PR_TRUE means the test passed; PR_FALSE means the test failed.
00113 static inline PRBool
00114 CheckResult(const char *aLhs, PRUint32 aRule, const char *aRhs = nsnull)
00115 {
00116     switch (aRule) {
00117         case MUST_BE_NULL:
00118             return !aLhs;
00119 
00120         case MUST_EQUAL:
00121             return !PL_strcmp(aLhs, aRhs);
00122 
00123         case MUST_CONTAIN:
00124             return PL_strstr(aLhs, aRhs) != nsnull;
00125 
00126         case MUST_NOT_CONTAIN:
00127             return PL_strstr(aLhs, aRhs) == nsnull;
00128 
00129         default:
00130             return PR_FALSE; // failure
00131     }
00132 }
00133 
00134 // helper function that ensures the first aSize elements of aResult are
00135 // PR_TRUE (i.e. all tests succeeded). prints the result of the tests (if any
00136 // tests failed, it prints the zero-based index of each failed test).
00137 PRBool
00138 PrintResult(const PRBool aResult[], PRUint32 aSize)
00139 {
00140     PRBool failed = PR_FALSE;
00141     printf("*** tests ");
00142     for (PRUint32 i = 0; i < aSize; ++i) {
00143         if (!aResult[i]) {
00144             failed = PR_TRUE;
00145             printf("%d ", i);
00146         }
00147     }
00148     if (failed) {
00149         printf("FAILED!\a\n");
00150     } else {
00151         printf("passed.\n");
00152     }
00153     return !failed;
00154 }
00155 
00156 void
00157 InitPrefs(nsIPrefBranch *aPrefBranch)
00158 {
00159     // init some relevant prefs, so the tests don't go awry.
00160     // we use the most restrictive set of prefs we can.
00161     aPrefBranch->SetIntPref(kCookiesPermissions, 1); // 'reject foreign'
00162     aPrefBranch->SetBoolPref(kCookiesDisabledForMailNews, PR_TRUE);
00163     aPrefBranch->SetBoolPref(kCookiesLifetimeEnabled, PR_TRUE);
00164     aPrefBranch->SetIntPref(kCookiesLifetimeCurrentSession, 0);
00165     aPrefBranch->SetIntPref(kCookiesLifetimeDays, 1);
00166     aPrefBranch->SetBoolPref(kCookiesAskPermission, PR_FALSE);
00167 }
00168 
00169 int
00170 main(PRInt32 argc, char *argv[])
00171 {
00172     if (test_common_init(&argc, &argv) != 0)
00173         return -1;
00174 
00175     nsresult rv0 = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00176     if (NS_FAILED(rv0)) return -1;
00177 
00178     nsCOMPtr<nsICookieService> cookieService =
00179              do_GetService(kCookieServiceCID, &rv0);
00180          if (NS_FAILED(rv0)) return -1;
00181 
00182     nsCOMPtr<nsIPrefBranch> prefBranch =
00183              do_GetService(kPrefServiceCID, &rv0);
00184     if (NS_FAILED(rv0)) return -1;
00185 
00186     InitPrefs(prefBranch);
00187 
00188     PRBool allTestsPassed = PR_TRUE;
00189     PRBool rv[20];
00190     nsXPIDLCString cookie;
00191 
00192     // call NS_NewURI just to force chrome registrations, so all our
00193     // printf'ed messages are together.
00194     {
00195         nsCOMPtr<nsIURI> foo;
00196         NS_NewURI(getter_AddRefs(foo), "http://foo.com");
00197     }
00198     printf("\n");
00199 
00200 
00201     /* The basic idea behind these tests is the following:
00202      *
00203      * we set() some cookie, then try to get() it in various ways. we have
00204      * several possible tests we perform on the cookie string returned from
00205      * get():
00206      *
00207      * a) check whether the returned string is null (i.e. we got no cookies
00208      *    back). this is used e.g. to ensure a given cookie was deleted
00209      *    correctly, or to ensure a certain cookie wasn't returned to a given
00210      *    host.
00211      * b) check whether the returned string exactly matches a given string.
00212      *    this is used where we want to make sure our cookie service adheres to
00213      *    some strict spec (e.g. ordering of multiple cookies), or where we
00214      *    just know exactly what the returned string should be.
00215      * c) check whether the returned string contains/does not contain a given
00216      *    string. this is used where we don't know/don't care about the
00217      *    ordering of multiple cookies - we just want to make sure the cookie
00218      *    string contains them all, in some order.
00219      *
00220      * the results of each individual testing operation from CheckResult() is
00221      * stored in an array of bools, which is then checked against the expected
00222      * outcomes (all successes), by PrintResult()). the overall result of all
00223      * tests to date is kept in |allTestsPassed|, for convenient display at the
00224      * end.
00225      *
00226      * Interpreting the output:
00227      * each setting/getting operation will print output saying exactly what
00228      * it's doing and the outcome, respectively. this information is only
00229      * useful for debugging purposes; the actual result of the tests is
00230      * printed at the end of each block of tests. this will either be "all
00231      * tests passed" or "tests X Y Z failed", where X, Y, Z are the indexes
00232      * of rv (i.e. zero-based). at the conclusion of all tests, the overall
00233      * passed/failed result is printed.
00234      *
00235      * NOTE: this testsuite is not yet comprehensive or complete, and is
00236      * somewhat contrived - still under development, and needs improving!
00237      */
00238 
00239     // *** basic tests
00240     printf("*** Beginning basic tests...\n");
00241 
00242     // test some basic variations of the domain & path
00243     SetACookie(cookieService, "http://www.basic.com", nsnull, "test=basic", nsnull);
00244     GetACookie(cookieService, "http://www.basic.com", nsnull, getter_Copies(cookie));
00245     rv[0] = CheckResult(cookie, MUST_EQUAL, "test=basic");
00246     GetACookie(cookieService, "http://www.basic.com/testPath/testfile.txt", nsnull, getter_Copies(cookie));
00247     rv[1] = CheckResult(cookie, MUST_EQUAL, "test=basic");
00248     GetACookie(cookieService, "http://www.basic.com./", nsnull, getter_Copies(cookie));
00249     rv[2] = CheckResult(cookie, MUST_EQUAL, "test=basic");
00250     GetACookie(cookieService, "http://www.basic.com.", nsnull, getter_Copies(cookie));
00251     rv[3] = CheckResult(cookie, MUST_EQUAL, "test=basic");
00252     GetACookie(cookieService, "http://www.basic.com./testPath/testfile.txt", nsnull, getter_Copies(cookie));
00253     rv[4] = CheckResult(cookie, MUST_EQUAL, "test=basic");
00254     GetACookie(cookieService, "http://www.basic2.com/", nsnull, getter_Copies(cookie));
00255     rv[5] = CheckResult(cookie, MUST_BE_NULL);
00256     SetACookie(cookieService, "http://www.basic.com", nsnull, "test=basic; max-age=-1", nsnull);
00257     GetACookie(cookieService, "http://www.basic.com/", nsnull, getter_Copies(cookie));
00258     rv[6] = CheckResult(cookie, MUST_BE_NULL);
00259 
00260     allTestsPassed = PrintResult(rv, 7) && allTestsPassed;
00261 
00262 
00263     // *** domain tests
00264     printf("*** Beginning domain tests...\n");
00265 
00266     // test some variations of the domain & path, for different domains of
00267     // a domain cookie
00268     SetACookie(cookieService, "http://www.domain.com", nsnull, "test=domain; domain=domain.com", nsnull);
00269     GetACookie(cookieService, "http://domain.com", nsnull, getter_Copies(cookie));
00270     rv[0] = CheckResult(cookie, MUST_EQUAL, "test=domain");
00271     GetACookie(cookieService, "http://domain.com.", nsnull, getter_Copies(cookie));
00272     rv[1] = CheckResult(cookie, MUST_EQUAL, "test=domain");
00273     GetACookie(cookieService, "http://www.domain.com", nsnull, getter_Copies(cookie));
00274     rv[2] = CheckResult(cookie, MUST_EQUAL, "test=domain");
00275     GetACookie(cookieService, "http://foo.domain.com", nsnull, getter_Copies(cookie));
00276     rv[3] = CheckResult(cookie, MUST_EQUAL, "test=domain");
00277     SetACookie(cookieService, "http://www.domain.com", nsnull, "test=domain; domain=domain.com; max-age=-1", nsnull);
00278     GetACookie(cookieService, "http://domain.com", nsnull, getter_Copies(cookie));
00279     rv[4] = CheckResult(cookie, MUST_BE_NULL);
00280 
00281     SetACookie(cookieService, "http://www.domain.com", nsnull, "test=domain; domain=.domain.com", nsnull);
00282     GetACookie(cookieService, "http://domain.com", nsnull, getter_Copies(cookie));
00283     rv[5] = CheckResult(cookie, MUST_EQUAL, "test=domain");
00284     GetACookie(cookieService, "http://www.domain.com", nsnull, getter_Copies(cookie));
00285     rv[6] = CheckResult(cookie, MUST_EQUAL, "test=domain");
00286     GetACookie(cookieService, "http://bah.domain.com", nsnull, getter_Copies(cookie));
00287     rv[7] = CheckResult(cookie, MUST_EQUAL, "test=domain");
00288     SetACookie(cookieService, "http://www.domain.com", nsnull, "test=domain; domain=.domain.com; max-age=-1", nsnull);
00289     GetACookie(cookieService, "http://domain.com", nsnull, getter_Copies(cookie));
00290     rv[8] = CheckResult(cookie, MUST_BE_NULL);
00291 
00292     SetACookie(cookieService, "http://www.domain.com", nsnull, "test=domain; domain=.foo.domain.com", nsnull);
00293     GetACookie(cookieService, "http://foo.domain.com", nsnull, getter_Copies(cookie));
00294     rv[9] = CheckResult(cookie, MUST_BE_NULL);
00295 
00296     SetACookie(cookieService, "http://www.domain.com", nsnull, "test=domain; domain=moose.com", nsnull);
00297     GetACookie(cookieService, "http://foo.domain.com", nsnull, getter_Copies(cookie));
00298     rv[10] = CheckResult(cookie, MUST_BE_NULL);
00299 
00300     allTestsPassed = PrintResult(rv, 11) && allTestsPassed;
00301 
00302 
00303     // *** path tests
00304     printf("*** Beginning path tests...\n");
00305 
00306     // test some variations of the domain & path, for different paths of
00307     // a path cookie
00308     SetACookie(cookieService, "http://path.net/path/file", nsnull, "test=path; path=/path", nsnull);
00309     GetACookie(cookieService, "http://path.net/path", nsnull, getter_Copies(cookie));
00310     rv[0] = CheckResult(cookie, MUST_EQUAL, "test=path");
00311     GetACookie(cookieService, "http://path.net/path/", nsnull, getter_Copies(cookie));
00312     rv[1] = CheckResult(cookie, MUST_EQUAL, "test=path");
00313     GetACookie(cookieService, "http://path.net/path/hithere.foo", nsnull, getter_Copies(cookie));
00314     rv[2] = CheckResult(cookie, MUST_EQUAL, "test=path");
00315     GetACookie(cookieService, "http://path.net/path?hithere/foo", nsnull, getter_Copies(cookie));
00316     rv[3] = CheckResult(cookie, MUST_EQUAL, "test=path");
00317     GetACookie(cookieService, "http://path.net/path2", nsnull, getter_Copies(cookie));
00318     rv[4] = CheckResult(cookie, MUST_BE_NULL);
00319     GetACookie(cookieService, "http://path.net/path2/", nsnull, getter_Copies(cookie));
00320     rv[5] = CheckResult(cookie, MUST_BE_NULL);
00321     SetACookie(cookieService, "http://path.net/path/file", nsnull, "test=path; path=/path; max-age=-1", nsnull);
00322     GetACookie(cookieService, "http://path.net/path/", nsnull, getter_Copies(cookie));
00323     rv[6] = CheckResult(cookie, MUST_BE_NULL);
00324 
00325     SetACookie(cookieService, "http://path.net/path/file", nsnull, "test=path; path=/path/", nsnull);
00326     GetACookie(cookieService, "http://path.net/path", nsnull, getter_Copies(cookie));
00327     rv[7] = CheckResult(cookie, MUST_EQUAL, "test=path");
00328     GetACookie(cookieService, "http://path.net/path/", nsnull, getter_Copies(cookie));
00329     rv[8] = CheckResult(cookie, MUST_EQUAL, "test=path");
00330     SetACookie(cookieService, "http://path.net/path/file", nsnull, "test=path; path=/path/; max-age=-1", nsnull);
00331     GetACookie(cookieService, "http://path.net/path/", nsnull, getter_Copies(cookie));
00332     rv[9] = CheckResult(cookie, MUST_BE_NULL);
00333 
00334     // note that a site can set a cookie for a path it's not on.
00335     // this is an intentional deviation from spec (see comments in
00336     // nsCookieService::CheckPath()), so we test this functionality too
00337     SetACookie(cookieService, "http://path.net/path/file", nsnull, "test=path; path=/foo/", nsnull);
00338     GetACookie(cookieService, "http://path.net/path", nsnull, getter_Copies(cookie));
00339     rv[10] = CheckResult(cookie, MUST_BE_NULL);
00340     GetACookie(cookieService, "http://path.net/foo", nsnull, getter_Copies(cookie));
00341     rv[11] = CheckResult(cookie, MUST_EQUAL, "test=path");
00342     SetACookie(cookieService, "http://path.net/path/file", nsnull, "test=path; path=/foo/; max-age=-1", nsnull);
00343     GetACookie(cookieService, "http://path.net/foo/", nsnull, getter_Copies(cookie));
00344     rv[12] = CheckResult(cookie, MUST_BE_NULL);
00345 
00346     allTestsPassed = PrintResult(rv, 13) && allTestsPassed;
00347 
00348 
00349     // *** expiry & deletion tests
00350     // XXX add server time str parsing tests here
00351     printf("*** Beginning expiry & deletion tests...\n");
00352 
00353     // test some variations of the expiry time,
00354     // and test deletion of previously set cookies
00355     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=-1", nsnull);
00356     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00357     rv[0] = CheckResult(cookie, MUST_BE_NULL);
00358     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=0", nsnull);
00359     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00360     rv[1] = CheckResult(cookie, MUST_BE_NULL);
00361     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nsnull);
00362     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00363     rv[2] = CheckResult(cookie, MUST_BE_NULL);
00364 
00365     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=60", nsnull);
00366     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00367     rv[3] = CheckResult(cookie, MUST_EQUAL, "test=expiry");
00368     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=-20", nsnull);
00369     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00370     rv[4] = CheckResult(cookie, MUST_BE_NULL);
00371     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=60", nsnull);
00372     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00373     rv[5] = CheckResult(cookie, MUST_EQUAL, "test=expiry");
00374     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; expires=Thu, 10 Apr 1980 16:33:12 GMT", nsnull);
00375     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00376     rv[6] = CheckResult(cookie, MUST_BE_NULL);
00377     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=expiry; max-age=60", nsnull);
00378     SetACookie(cookieService, "http://expireme.org/", nsnull, "newtest=expiry; max-age=60", nsnull);
00379     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00380     rv[7] = CheckResult(cookie, MUST_CONTAIN, "test=expiry");
00381     rv[8] = CheckResult(cookie, MUST_CONTAIN, "newtest=expiry");
00382     SetACookie(cookieService, "http://expireme.org/", nsnull, "test=differentvalue; max-age=0", nsnull);
00383     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00384     rv[9] = CheckResult(cookie, MUST_EQUAL, "newtest=expiry");
00385     SetACookie(cookieService, "http://expireme.org/", nsnull, "newtest=evendifferentvalue; max-age=0", nsnull);
00386     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00387     rv[10] = CheckResult(cookie, MUST_BE_NULL);
00388 
00389     SetACookie(cookieService, "http://foo.expireme.org/", nsnull, "test=expiry; domain=.expireme.org; max-age=60", nsnull);
00390     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00391     rv[11] = CheckResult(cookie, MUST_EQUAL, "test=expiry");
00392     SetACookie(cookieService, "http://bar.expireme.org/", nsnull, "test=differentvalue; domain=.expireme.org; max-age=0", nsnull);
00393     GetACookie(cookieService, "http://expireme.org/", nsnull, getter_Copies(cookie));
00394     rv[12] = CheckResult(cookie, MUST_BE_NULL);
00395 
00396     allTestsPassed = PrintResult(rv, 13) && allTestsPassed;
00397 
00398 
00399     // *** multiple cookie tests
00400     printf("*** Beginning multiple cookie tests...\n");
00401 
00402     // test the setting of multiple cookies, and test the order of precedence
00403     // (a later cookie overwriting an earlier one, in the same header string)
00404     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);
00405     GetACookie(cookieService, "http://multiple.cookies/", nsnull, getter_Copies(cookie));
00406     rv[0] = CheckResult(cookie, MUST_NOT_CONTAIN, "test=multiple");
00407     rv[1] = CheckResult(cookie, MUST_CONTAIN, "test=different");
00408     rv[2] = CheckResult(cookie, MUST_CONTAIN, "test=same");
00409     rv[3] = CheckResult(cookie, MUST_NOT_CONTAIN, "newtest=ciao");
00410     rv[4] = CheckResult(cookie, MUST_NOT_CONTAIN, "newtest=foo");
00411     rv[5] = CheckResult(cookie, MUST_CONTAIN, "newtest=reincarnated") != nsnull;
00412     SetACookie(cookieService, "http://multiple.cookies/", nsnull, "test=expiry; domain=.multiple.cookies; max-age=0", nsnull);
00413     GetACookie(cookieService, "http://multiple.cookies/", nsnull, getter_Copies(cookie));
00414     rv[6] = CheckResult(cookie, MUST_NOT_CONTAIN, "test=same");
00415     SetACookie(cookieService, "http://multiple.cookies/", nsnull,  "\n test=different; max-age=0 \n", nsnull);
00416     GetACookie(cookieService, "http://multiple.cookies/", nsnull, getter_Copies(cookie));
00417     rv[7] = CheckResult(cookie, MUST_NOT_CONTAIN, "test=different");
00418     SetACookie(cookieService, "http://multiple.cookies/", nsnull,  "newtest=dead; max-age=0", nsnull);
00419     GetACookie(cookieService, "http://multiple.cookies/", nsnull, getter_Copies(cookie));
00420     rv[8] = CheckResult(cookie, MUST_BE_NULL);
00421 
00422     allTestsPassed = PrintResult(rv, 9) && allTestsPassed;
00423 
00424 
00425     // *** foreign cookie tests
00426     printf("*** Beginning foreign cookie tests...\n");
00427 
00428     // test the blocking of foreign cookies, under various circumstances.
00429     // order of URI arguments is hostURI, firstURI
00430     SetACookie(cookieService, "http://yahoo.com/", "http://yahoo.com/", "test=foreign; domain=.yahoo.com", nsnull);
00431     GetACookie(cookieService, "http://yahoo.com/", "http://yahoo.com/", getter_Copies(cookie));
00432     rv[0] = CheckResult(cookie, MUST_EQUAL, "test=foreign");
00433     SetACookie(cookieService, "http://weather.yahoo.com/", "http://yahoo.com/", "test=foreign; domain=.yahoo.com", nsnull);
00434     GetACookie(cookieService, "http://notweather.yahoo.com/", "http://sport.yahoo.com/", getter_Copies(cookie));
00435     rv[1] = CheckResult(cookie, MUST_EQUAL, "test=foreign");
00436     SetACookie(cookieService, "http://moose.yahoo.com/", "http://canada.yahoo.com/", "test=foreign; domain=.yahoo.com", nsnull);
00437     GetACookie(cookieService, "http://yahoo.com/", "http://sport.yahoo.com/", getter_Copies(cookie));
00438     rv[2] = CheckResult(cookie, MUST_BE_NULL);
00439     GetACookie(cookieService, "http://sport.yahoo.com/", "http://yahoo.com/", getter_Copies(cookie));
00440     rv[3] = CheckResult(cookie, MUST_EQUAL, "test=foreign");
00441     SetACookie(cookieService, "http://jack.yahoo.com/", "http://jill.yahoo.com/", "test=foreign; domain=.yahoo.com; max-age=0", nsnull);
00442     GetACookie(cookieService, "http://jane.yahoo.com/", "http://yahoo.com/", getter_Copies(cookie));
00443     rv[4] = CheckResult(cookie, MUST_BE_NULL);
00444 
00445     SetACookie(cookieService, "http://moose.yahoo.com/", "http://foo.moose.yahoo.com/", "test=foreign; domain=.yahoo.com", nsnull);
00446     GetACookie(cookieService, "http://yahoo.com/", "http://yahoo.com/", getter_Copies(cookie));
00447     rv[5] = CheckResult(cookie, MUST_BE_NULL);
00448     SetACookie(cookieService, "http://foo.bar.yahoo.com/", "http://yahoo.com/", "test=foreign; domain=.yahoo.com", nsnull);
00449     GetACookie(cookieService, "http://yahoo.com/", "http://yahoo.com/", getter_Copies(cookie));
00450     rv[6] = CheckResult(cookie, MUST_EQUAL, "test=foreign");
00451     SetACookie(cookieService, "http://foo.bar.yahoo.com/", "http://yahoo.com/", "test=foreign; domain=.yahoo.com; max-age=0", nsnull);
00452     GetACookie(cookieService, "http://yahoo.com/", "http://yahoo.com/", getter_Copies(cookie));
00453     rv[7] = CheckResult(cookie, MUST_BE_NULL);
00454 
00455     // test handling of IP addresses by the foreign blocking algo
00456     SetACookie(cookieService, "http://192.168.54.33/", "http://192.168.54.33/", "test=foreign; domain=192.168.54.33", nsnull);
00457     GetACookie(cookieService, "http://192.168.54.33/", "http://192.168.54.33/", getter_Copies(cookie));
00458     rv[8] = CheckResult(cookie, MUST_EQUAL, "test=foreign");
00459     GetACookie(cookieService, "http://192.168.54.33./", "http://.192.168.54.33../", getter_Copies(cookie));
00460     rv[9] = CheckResult(cookie, MUST_EQUAL, "test=foreign");
00461     GetACookie(cookieService, "http://192.168.54.33/", nsnull, getter_Copies(cookie));
00462     rv[10] = CheckResult(cookie, MUST_EQUAL, "test=foreign");
00463     GetACookie(cookieService, "http://192.168.54.33/", "http://148.168.54.33", getter_Copies(cookie));
00464     rv[11] = CheckResult(cookie, MUST_BE_NULL);
00465     SetACookie(cookieService, "http://192.168.54.33/", "http://192.168.54.33/", "test=foreign; domain=192.168.54.33; max-age=0", nsnull);
00466     GetACookie(cookieService, "http://192.168.54.33/", "http://192.168.54.33/", getter_Copies(cookie));
00467     rv[12] = CheckResult(cookie, MUST_BE_NULL);
00468     SetACookie(cookieService, "http://192.168.54.33/", "http://148.168.54.33/", "test=foreign; domain=192.168.54.33", nsnull);
00469     GetACookie(cookieService, "http://192.168.54.33/", "http://192.168.54.33/", getter_Copies(cookie));
00470     rv[13] = CheckResult(cookie, MUST_BE_NULL);
00471 
00472     allTestsPassed = PrintResult(rv, 14) && allTestsPassed;
00473 
00474 
00475     // *** parser tests
00476     printf("*** Beginning parser tests...\n");
00477 
00478     // test the cookie header parser, under various circumstances.
00479     SetACookie(cookieService, "http://parser.test/", nsnull, "test=parser; domain=.parser.test; ;; ;=; ,,, ===,abc,=; abracadabra! max-age=20;=;;", nsnull);
00480     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00481     rv[0] = CheckResult(cookie, MUST_EQUAL, "test=parser");
00482     SetACookie(cookieService, "http://parser.test/", nsnull, "test=parser; domain=.parser.test; max-age=0", nsnull);
00483     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00484     rv[1] = CheckResult(cookie, MUST_BE_NULL);
00485     SetACookie(cookieService, "http://parser.test/", nsnull, "test=\"fubar! = foo;bar\\\";\" parser; domain=.parser.test; max-age=6\nfive; max-age=2.63,", nsnull);
00486     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00487     rv[2] = CheckResult(cookie, MUST_CONTAIN, "test=\"fubar! = foo;bar\\\";\"");
00488     rv[3] = CheckResult(cookie, MUST_CONTAIN, "five");
00489     SetACookie(cookieService, "http://parser.test/", nsnull, "test=kill; domain=.parser.test; max-age=0 \n five; max-age=0", nsnull);
00490     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00491     rv[4] = CheckResult(cookie, MUST_BE_NULL);
00492 
00493     // test the handling of VALUE-only cookies (see bug 169091),
00494     // i.e. "six" should assume an empty NAME, which allows other VALUE-only
00495     // cookies to overwrite it
00496     SetACookie(cookieService, "http://parser.test/", nsnull, "six", nsnull);
00497     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00498     rv[5] = CheckResult(cookie, MUST_EQUAL, "six");
00499     SetACookie(cookieService, "http://parser.test/", nsnull, "seven", nsnull);
00500     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00501     rv[6] = CheckResult(cookie, MUST_EQUAL, "seven");
00502     SetACookie(cookieService, "http://parser.test/", nsnull, " =eight", nsnull);
00503     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00504     rv[7] = CheckResult(cookie, MUST_EQUAL, "eight");
00505     SetACookie(cookieService, "http://parser.test/", nsnull, "test=six", nsnull);
00506     GetACookie(cookieService, "http://parser.test/", nsnull, getter_Copies(cookie));
00507     rv[9] = CheckResult(cookie, MUST_CONTAIN, "test=six");
00508 
00509     allTestsPassed = PrintResult(rv, 10) && allTestsPassed;
00510 
00511 
00512     // *** mailnews tests
00513     printf("*** Beginning mailnews tests...\n");
00514 
00515     // test some mailnews cookies to ensure blockage.
00516     // we use null firstURI's deliberately, since we have hacks to deal with
00517     // this situation...
00518     SetACookie(cookieService, "mailbox://mail.co.uk/", nsnull, "test=mailnews", nsnull);
00519     GetACookie(cookieService, "mailbox://mail.co.uk/", nsnull, getter_Copies(cookie));
00520     rv[0] = CheckResult(cookie, MUST_BE_NULL);
00521     GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
00522     rv[1] = CheckResult(cookie, MUST_BE_NULL);
00523     SetACookie(cookieService, "http://mail.co.uk/", nsnull, "test=mailnews", nsnull);
00524     GetACookie(cookieService, "mailbox://mail.co.uk/", nsnull, getter_Copies(cookie));
00525     rv[2] = CheckResult(cookie, MUST_BE_NULL);
00526     GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
00527     rv[3] = CheckResult(cookie, MUST_EQUAL, "test=mailnews");
00528     SetACookie(cookieService, "http://mail.co.uk/", nsnull, "test=mailnews; max-age=0", nsnull);
00529     GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
00530     rv[4] = CheckResult(cookie, MUST_BE_NULL);
00531 
00532     // test non-null firstURI's, i) from mailnews ii) not from mailnews
00533     SetACookie(cookieService, "mailbox://mail.co.uk/", "http://mail.co.uk/", "test=mailnews", nsnull);
00534     GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
00535     rv[5] = CheckResult(cookie, MUST_BE_NULL);
00536     SetACookie(cookieService, "http://mail.co.uk/", "mailbox://mail.co.uk/", "test=mailnews", nsnull);
00537     GetACookie(cookieService, "http://mail.co.uk/", nsnull, getter_Copies(cookie));
00538     rv[6] = CheckResult(cookie, MUST_BE_NULL);
00539 
00540     allTestsPassed = PrintResult(rv, 7) && allTestsPassed;
00541 
00542 
00543     // *** path ordering tests
00544     printf("*** Beginning path ordering tests...\n");
00545 
00546     // test that cookies are returned in path order - longest to shortest.
00547     // if the header doesn't specify a path, it's taken from the host URI.
00548     SetACookie(cookieService, "http://multi.path.tests/", nsnull, "test1=path; path=/one/two/three", nsnull);
00549     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);
00550     SetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/", nsnull, "test6=path", nsnull);
00551     SetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/six/", nsnull, "test7=path; path=", nsnull);
00552     SetACookie(cookieService, "http://multi.path.tests/", nsnull, "test8=path; path=/", nsnull);
00553     GetACookie(cookieService, "http://multi.path.tests/one/two/three/four/five/six/", nsnull, getter_Copies(cookie));
00554     rv[0] = CheckResult(cookie, MUST_EQUAL, "test7=path; test6=path; test3=path; test1=path; test5=path; test4=path; test2=path; test8=path");
00555 
00556     allTestsPassed = PrintResult(rv, 1) && allTestsPassed;
00557 
00558     // XXX the following are placeholders: add these tests please!
00559     // *** "noncompliant cookie" tests
00560     // *** IP address tests
00561     // *** speed tests
00562 
00563 
00564     printf("\n*** Result: %s!\n\n", allTestsPassed ? "all tests passed" : "TEST(S) FAILED");
00565 
00566     NS_ShutdownXPCOM(nsnull);
00567     
00568     return 0;
00569 }