Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/ArrayUtils.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIAtom.h" |
michael@0 | 9 | #include "nsString.h" |
michael@0 | 10 | #include "UTFStrings.h" |
michael@0 | 11 | #include "nsIServiceManager.h" |
michael@0 | 12 | #include "nsStaticAtom.h" |
michael@0 | 13 | |
michael@0 | 14 | using namespace mozilla; |
michael@0 | 15 | |
michael@0 | 16 | namespace TestAtoms { |
michael@0 | 17 | |
michael@0 | 18 | bool |
michael@0 | 19 | test_basic() |
michael@0 | 20 | { |
michael@0 | 21 | for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { |
michael@0 | 22 | nsDependentString str16(ValidStrings[i].m16); |
michael@0 | 23 | nsDependentCString str8(ValidStrings[i].m8); |
michael@0 | 24 | |
michael@0 | 25 | nsCOMPtr<nsIAtom> atom = do_GetAtom(str16); |
michael@0 | 26 | |
michael@0 | 27 | if (!atom->Equals(str16) || !atom->EqualsUTF8(str8)) |
michael@0 | 28 | return false; |
michael@0 | 29 | |
michael@0 | 30 | nsString tmp16; |
michael@0 | 31 | nsCString tmp8; |
michael@0 | 32 | atom->ToString(tmp16); |
michael@0 | 33 | atom->ToUTF8String(tmp8); |
michael@0 | 34 | if (!str16.Equals(tmp16) || !str8.Equals(tmp8)) |
michael@0 | 35 | return false; |
michael@0 | 36 | |
michael@0 | 37 | if (!nsDependentString(atom->GetUTF16String()).Equals(str16)) |
michael@0 | 38 | return false; |
michael@0 | 39 | |
michael@0 | 40 | if (!nsAtomString(atom).Equals(str16) || |
michael@0 | 41 | !nsDependentAtomString(atom).Equals(str16) || |
michael@0 | 42 | !nsAtomCString(atom).Equals(str8)) |
michael@0 | 43 | return false; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | return true; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | bool |
michael@0 | 50 | test_16vs8() |
michael@0 | 51 | { |
michael@0 | 52 | for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { |
michael@0 | 53 | nsCOMPtr<nsIAtom> atom16 = do_GetAtom(ValidStrings[i].m16); |
michael@0 | 54 | nsCOMPtr<nsIAtom> atom8 = do_GetAtom(ValidStrings[i].m8); |
michael@0 | 55 | if (atom16 != atom8) |
michael@0 | 56 | return false; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | return true; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | bool |
michael@0 | 63 | test_buffersharing() |
michael@0 | 64 | { |
michael@0 | 65 | nsString unique; |
michael@0 | 66 | unique.AssignLiteral("this is a unique string !@#$"); |
michael@0 | 67 | |
michael@0 | 68 | nsCOMPtr<nsIAtom> atom = do_GetAtom(unique); |
michael@0 | 69 | |
michael@0 | 70 | return unique.get() == atom->GetUTF16String(); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | bool |
michael@0 | 74 | test_null() |
michael@0 | 75 | { |
michael@0 | 76 | nsAutoString str(NS_LITERAL_STRING("string with a \0 char")); |
michael@0 | 77 | nsDependentString strCut(str.get()); |
michael@0 | 78 | |
michael@0 | 79 | if (str.Equals(strCut)) |
michael@0 | 80 | return false; |
michael@0 | 81 | |
michael@0 | 82 | nsCOMPtr<nsIAtom> atomCut = do_GetAtom(strCut); |
michael@0 | 83 | nsCOMPtr<nsIAtom> atom = do_GetAtom(str); |
michael@0 | 84 | |
michael@0 | 85 | return atom->GetLength() == str.Length() && |
michael@0 | 86 | atom->Equals(str) && |
michael@0 | 87 | atom->EqualsUTF8(NS_ConvertUTF16toUTF8(str)) && |
michael@0 | 88 | atom != atomCut && |
michael@0 | 89 | atomCut->Equals(strCut); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | bool |
michael@0 | 93 | test_invalid() |
michael@0 | 94 | { |
michael@0 | 95 | for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) { |
michael@0 | 96 | nsrefcnt count = NS_GetNumberOfAtoms(); |
michael@0 | 97 | |
michael@0 | 98 | { |
michael@0 | 99 | nsCOMPtr<nsIAtom> atom16 = do_GetAtom(Invalid16Strings[i].m16); |
michael@0 | 100 | if (!atom16->Equals(nsDependentString(Invalid16Strings[i].m16))) |
michael@0 | 101 | return false; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | if (count != NS_GetNumberOfAtoms()) |
michael@0 | 105 | return false; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { |
michael@0 | 109 | nsrefcnt count = NS_GetNumberOfAtoms(); |
michael@0 | 110 | |
michael@0 | 111 | { |
michael@0 | 112 | nsCOMPtr<nsIAtom> atom8 = do_GetAtom(Invalid8Strings[i].m8); |
michael@0 | 113 | nsCOMPtr<nsIAtom> atom16 = do_GetAtom(Invalid8Strings[i].m16); |
michael@0 | 114 | if (atom16 != atom8 || |
michael@0 | 115 | !atom16->Equals(nsDependentString(Invalid8Strings[i].m16))) |
michael@0 | 116 | return false; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | if (count != NS_GetNumberOfAtoms()) |
michael@0 | 120 | return false; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | // Don't run this test in debug builds as that intentionally asserts. |
michael@0 | 124 | #ifndef DEBUG |
michael@0 | 125 | nsCOMPtr<nsIAtom> emptyAtom = do_GetAtom(""); |
michael@0 | 126 | |
michael@0 | 127 | for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { |
michael@0 | 128 | nsrefcnt count = NS_GetNumberOfAtoms(); |
michael@0 | 129 | |
michael@0 | 130 | nsCOMPtr<nsIAtom> atom8 = do_GetAtom(Malformed8Strings[i]); |
michael@0 | 131 | if (atom8 != emptyAtom || |
michael@0 | 132 | count != NS_GetNumberOfAtoms()) |
michael@0 | 133 | return false; |
michael@0 | 134 | } |
michael@0 | 135 | #endif |
michael@0 | 136 | |
michael@0 | 137 | return true; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | #define FIRST_ATOM_STR "first static atom. Hello!" |
michael@0 | 141 | #define SECOND_ATOM_STR "second static atom. @World!" |
michael@0 | 142 | #define THIRD_ATOM_STR "third static atom?!" |
michael@0 | 143 | |
michael@0 | 144 | static nsIAtom* sAtom1 = 0; |
michael@0 | 145 | static nsIAtom* sAtom2 = 0; |
michael@0 | 146 | static nsIAtom* sAtom3 = 0; |
michael@0 | 147 | NS_STATIC_ATOM_BUFFER(sAtom1_buffer, FIRST_ATOM_STR) |
michael@0 | 148 | NS_STATIC_ATOM_BUFFER(sAtom2_buffer, SECOND_ATOM_STR) |
michael@0 | 149 | NS_STATIC_ATOM_BUFFER(sAtom3_buffer, THIRD_ATOM_STR) |
michael@0 | 150 | static const nsStaticAtom sAtoms_info[] = { |
michael@0 | 151 | NS_STATIC_ATOM(sAtom1_buffer, &sAtom1), |
michael@0 | 152 | NS_STATIC_ATOM(sAtom2_buffer, &sAtom2), |
michael@0 | 153 | NS_STATIC_ATOM(sAtom3_buffer, &sAtom3), |
michael@0 | 154 | }; |
michael@0 | 155 | |
michael@0 | 156 | bool |
michael@0 | 157 | isStaticAtom(nsIAtom* atom) |
michael@0 | 158 | { |
michael@0 | 159 | // Don't use logic && in order to ensure that all addrefs/releases are always |
michael@0 | 160 | // run, even if one of the tests fail. This allows us to run this code on a |
michael@0 | 161 | // non-static atom without affecting its refcount. |
michael@0 | 162 | return (atom->AddRef() == 2) & |
michael@0 | 163 | (atom->AddRef() == 2) & |
michael@0 | 164 | (atom->AddRef() == 2) & |
michael@0 | 165 | (atom->Release() == 1) & |
michael@0 | 166 | (atom->Release() == 1) & |
michael@0 | 167 | (atom->Release() == 1); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | bool |
michael@0 | 171 | test_atomtable() |
michael@0 | 172 | { |
michael@0 | 173 | nsrefcnt count = NS_GetNumberOfAtoms(); |
michael@0 | 174 | |
michael@0 | 175 | nsCOMPtr<nsIAtom> thirdNonPerm = do_GetAtom(THIRD_ATOM_STR); |
michael@0 | 176 | |
michael@0 | 177 | if (isStaticAtom(thirdNonPerm)) |
michael@0 | 178 | return false; |
michael@0 | 179 | |
michael@0 | 180 | if (!thirdNonPerm || NS_GetNumberOfAtoms() != count + 1) |
michael@0 | 181 | return false; |
michael@0 | 182 | |
michael@0 | 183 | NS_RegisterStaticAtoms(sAtoms_info); |
michael@0 | 184 | |
michael@0 | 185 | return sAtom1 && |
michael@0 | 186 | sAtom1->Equals(NS_LITERAL_STRING(FIRST_ATOM_STR)) && |
michael@0 | 187 | isStaticAtom(sAtom1) && |
michael@0 | 188 | sAtom2 && |
michael@0 | 189 | sAtom2->Equals(NS_LITERAL_STRING(SECOND_ATOM_STR)) && |
michael@0 | 190 | isStaticAtom(sAtom2) && |
michael@0 | 191 | sAtom3 && |
michael@0 | 192 | sAtom3->Equals(NS_LITERAL_STRING(THIRD_ATOM_STR)) && |
michael@0 | 193 | isStaticAtom(sAtom3) && |
michael@0 | 194 | NS_GetNumberOfAtoms() == count + 3 && |
michael@0 | 195 | thirdNonPerm == sAtom3; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | #define FIRST_PERM_ATOM_STR "first permanent atom. Hello!" |
michael@0 | 199 | #define SECOND_PERM_ATOM_STR "second permanent atom. @World!" |
michael@0 | 200 | |
michael@0 | 201 | bool |
michael@0 | 202 | test_permanent() |
michael@0 | 203 | { |
michael@0 | 204 | nsrefcnt count = NS_GetNumberOfAtoms(); |
michael@0 | 205 | |
michael@0 | 206 | { |
michael@0 | 207 | nsCOMPtr<nsIAtom> first = do_GetAtom(FIRST_PERM_ATOM_STR); |
michael@0 | 208 | if (!first->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)) || |
michael@0 | 209 | isStaticAtom(first)) |
michael@0 | 210 | return false; |
michael@0 | 211 | |
michael@0 | 212 | nsCOMPtr<nsIAtom> first_p = |
michael@0 | 213 | NS_NewPermanentAtom(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)); |
michael@0 | 214 | if (!first_p->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR)) || |
michael@0 | 215 | !isStaticAtom(first_p) || |
michael@0 | 216 | first != first_p) |
michael@0 | 217 | return false; |
michael@0 | 218 | |
michael@0 | 219 | nsCOMPtr<nsIAtom> second_p = |
michael@0 | 220 | NS_NewPermanentAtom(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)); |
michael@0 | 221 | if (!second_p->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)) || |
michael@0 | 222 | !isStaticAtom(second_p)) |
michael@0 | 223 | return false; |
michael@0 | 224 | |
michael@0 | 225 | nsCOMPtr<nsIAtom> second = do_GetAtom(SECOND_PERM_ATOM_STR); |
michael@0 | 226 | if (!second->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR)) || |
michael@0 | 227 | !isStaticAtom(second) || |
michael@0 | 228 | second != second_p) |
michael@0 | 229 | return false; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | return NS_GetNumberOfAtoms() == count + 2; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | typedef bool (*TestFunc)(); |
michael@0 | 236 | |
michael@0 | 237 | static const struct Test |
michael@0 | 238 | { |
michael@0 | 239 | const char* name; |
michael@0 | 240 | TestFunc func; |
michael@0 | 241 | } |
michael@0 | 242 | tests[] = |
michael@0 | 243 | { |
michael@0 | 244 | { "test_basic", test_basic }, |
michael@0 | 245 | { "test_16vs8", test_16vs8 }, |
michael@0 | 246 | { "test_buffersharing", test_buffersharing }, |
michael@0 | 247 | { "test_null", test_null }, |
michael@0 | 248 | { "test_invalid", test_invalid }, |
michael@0 | 249 | // FIXME: Bug 577500 TestAtoms fails when run in dist/bin due to |
michael@0 | 250 | // static atom table already being closed. TestStaticAtoms has similar |
michael@0 | 251 | // failure. |
michael@0 | 252 | #if 0 |
michael@0 | 253 | { "test_atomtable", test_atomtable }, |
michael@0 | 254 | { "test_permanent", test_permanent }, |
michael@0 | 255 | #endif |
michael@0 | 256 | { nullptr, nullptr } |
michael@0 | 257 | }; |
michael@0 | 258 | |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | using namespace TestAtoms; |
michael@0 | 262 | |
michael@0 | 263 | int main() |
michael@0 | 264 | { |
michael@0 | 265 | { |
michael@0 | 266 | nsCOMPtr<nsIServiceManager> servMan; |
michael@0 | 267 | NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
michael@0 | 268 | |
michael@0 | 269 | for (const Test* t = tests; t->name != nullptr; ++t) |
michael@0 | 270 | { |
michael@0 | 271 | printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--"); |
michael@0 | 272 | } |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | NS_ShutdownXPCOM(nullptr); |
michael@0 | 276 | |
michael@0 | 277 | return 0; |
michael@0 | 278 | } |