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: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "TestHarness.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/Attributes.h" |
michael@0 | 9 | #include "nsIScriptableBase64Encoder.h" |
michael@0 | 10 | #include "nsIInputStream.h" |
michael@0 | 11 | #include "nsAutoPtr.h" |
michael@0 | 12 | #include "nsStringAPI.h" |
michael@0 | 13 | #include <wchar.h> |
michael@0 | 14 | |
michael@0 | 15 | struct Chunk { |
michael@0 | 16 | Chunk(uint32_t l, const char* c) |
michael@0 | 17 | : mLength(l), mData(c) |
michael@0 | 18 | {} |
michael@0 | 19 | |
michael@0 | 20 | uint32_t mLength; |
michael@0 | 21 | const char* mData; |
michael@0 | 22 | }; |
michael@0 | 23 | |
michael@0 | 24 | struct Test { |
michael@0 | 25 | Test(Chunk* c, const char* r) |
michael@0 | 26 | : mChunks(c), mResult(r) |
michael@0 | 27 | {} |
michael@0 | 28 | |
michael@0 | 29 | Chunk* mChunks; |
michael@0 | 30 | const char* mResult; |
michael@0 | 31 | }; |
michael@0 | 32 | |
michael@0 | 33 | static Chunk kTest1Chunks[] = |
michael@0 | 34 | { |
michael@0 | 35 | Chunk(9, "Hello sir"), |
michael@0 | 36 | Chunk(0, nullptr) |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | static Chunk kTest2Chunks[] = |
michael@0 | 40 | { |
michael@0 | 41 | Chunk(3, "Hel"), |
michael@0 | 42 | Chunk(3, "lo "), |
michael@0 | 43 | Chunk(3, "sir"), |
michael@0 | 44 | Chunk(0, nullptr) |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | static Chunk kTest3Chunks[] = |
michael@0 | 48 | { |
michael@0 | 49 | Chunk(1, "I"), |
michael@0 | 50 | Chunk(0, nullptr) |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | static Chunk kTest4Chunks[] = |
michael@0 | 54 | { |
michael@0 | 55 | Chunk(2, "Hi"), |
michael@0 | 56 | Chunk(0, nullptr) |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | static Chunk kTest5Chunks[] = |
michael@0 | 60 | { |
michael@0 | 61 | Chunk(1, "B"), |
michael@0 | 62 | Chunk(2, "ob"), |
michael@0 | 63 | Chunk(0, nullptr) |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | static Chunk kTest6Chunks[] = |
michael@0 | 67 | { |
michael@0 | 68 | Chunk(2, "Bo"), |
michael@0 | 69 | Chunk(1, "b"), |
michael@0 | 70 | Chunk(0, nullptr) |
michael@0 | 71 | }; |
michael@0 | 72 | |
michael@0 | 73 | static Chunk kTest7Chunks[] = |
michael@0 | 74 | { |
michael@0 | 75 | Chunk(1, "F"), // Carry over 1 |
michael@0 | 76 | Chunk(4, "iref"), // Carry over 2 |
michael@0 | 77 | Chunk(2, "ox"), // 1 |
michael@0 | 78 | Chunk(4, " is "), // 2 |
michael@0 | 79 | Chunk(2, "aw"), // 1 |
michael@0 | 80 | Chunk(4, "esom"), // 2 |
michael@0 | 81 | Chunk(2, "e!"), |
michael@0 | 82 | Chunk(0, nullptr) |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | static Chunk kTest8Chunks[] = |
michael@0 | 86 | { |
michael@0 | 87 | Chunk(5, "ALL T"), |
michael@0 | 88 | Chunk(1, "H"), |
michael@0 | 89 | Chunk(4, "ESE "), |
michael@0 | 90 | Chunk(2, "WO"), |
michael@0 | 91 | Chunk(21, "RLDS ARE YOURS EXCEPT"), |
michael@0 | 92 | Chunk(9, " EUROPA. "), |
michael@0 | 93 | Chunk(25, "ATTEMPT NO LANDING THERE."), |
michael@0 | 94 | Chunk(0, nullptr) |
michael@0 | 95 | }; |
michael@0 | 96 | |
michael@0 | 97 | static Test kTests[] = |
michael@0 | 98 | { |
michael@0 | 99 | // Test 1, test a simple round string in one chunk |
michael@0 | 100 | Test( |
michael@0 | 101 | kTest1Chunks, |
michael@0 | 102 | "SGVsbG8gc2ly" |
michael@0 | 103 | ), |
michael@0 | 104 | // Test 2, test a simple round string split into round chunks |
michael@0 | 105 | Test( |
michael@0 | 106 | kTest2Chunks, |
michael@0 | 107 | "SGVsbG8gc2ly" |
michael@0 | 108 | ), |
michael@0 | 109 | // Test 3, test a single chunk that's 2 short |
michael@0 | 110 | Test( |
michael@0 | 111 | kTest3Chunks, |
michael@0 | 112 | "SQ==" |
michael@0 | 113 | ), |
michael@0 | 114 | // Test 4, test a single chunk that's 1 short |
michael@0 | 115 | Test( |
michael@0 | 116 | kTest4Chunks, |
michael@0 | 117 | "SGk=" |
michael@0 | 118 | ), |
michael@0 | 119 | // Test 5, test a single chunk that's 2 short, followed by a chunk of 2 |
michael@0 | 120 | Test( |
michael@0 | 121 | kTest5Chunks, |
michael@0 | 122 | "Qm9i" |
michael@0 | 123 | ), |
michael@0 | 124 | // Test 6, test a single chunk that's 1 short, followed by a chunk of 1 |
michael@0 | 125 | Test( |
michael@0 | 126 | kTest6Chunks, |
michael@0 | 127 | "Qm9i" |
michael@0 | 128 | ), |
michael@0 | 129 | // Test 7, test alternating carryovers |
michael@0 | 130 | Test( |
michael@0 | 131 | kTest7Chunks, |
michael@0 | 132 | "RmlyZWZveCBpcyBhd2Vzb21lIQ==" |
michael@0 | 133 | ), |
michael@0 | 134 | // Test 8, test a longish string |
michael@0 | 135 | Test( |
michael@0 | 136 | kTest8Chunks, |
michael@0 | 137 | "QUxMIFRIRVNFIFdPUkxEUyBBUkUgWU9VUlMgRVhDRVBUIEVVUk9QQS4gQVRURU1QVCBOTyBMQU5ESU5HIFRIRVJFLg==" |
michael@0 | 138 | ), |
michael@0 | 139 | // Terminator |
michael@0 | 140 | Test( |
michael@0 | 141 | nullptr, |
michael@0 | 142 | nullptr |
michael@0 | 143 | ) |
michael@0 | 144 | }; |
michael@0 | 145 | |
michael@0 | 146 | class FakeInputStream MOZ_FINAL : public nsIInputStream |
michael@0 | 147 | { |
michael@0 | 148 | public: |
michael@0 | 149 | |
michael@0 | 150 | FakeInputStream() |
michael@0 | 151 | : mTestNumber(0), |
michael@0 | 152 | mTest(&kTests[0]), |
michael@0 | 153 | mChunk(&mTest->mChunks[0]), |
michael@0 | 154 | mClosed(false) |
michael@0 | 155 | {} |
michael@0 | 156 | |
michael@0 | 157 | NS_DECL_ISUPPORTS |
michael@0 | 158 | NS_DECL_NSIINPUTSTREAM |
michael@0 | 159 | |
michael@0 | 160 | void Reset(); |
michael@0 | 161 | bool NextTest(); |
michael@0 | 162 | bool CheckTest(nsACString& aResult); |
michael@0 | 163 | bool CheckTest(nsAString& aResult); |
michael@0 | 164 | private: |
michael@0 | 165 | uint32_t mTestNumber; |
michael@0 | 166 | const Test* mTest; |
michael@0 | 167 | const Chunk* mChunk; |
michael@0 | 168 | bool mClosed; |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | NS_IMPL_ISUPPORTS(FakeInputStream, nsIInputStream) |
michael@0 | 172 | |
michael@0 | 173 | NS_IMETHODIMP |
michael@0 | 174 | FakeInputStream::Close() |
michael@0 | 175 | { |
michael@0 | 176 | mClosed = true; |
michael@0 | 177 | return NS_OK; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | NS_IMETHODIMP |
michael@0 | 181 | FakeInputStream::Available(uint64_t* aAvailable) |
michael@0 | 182 | { |
michael@0 | 183 | *aAvailable = 0; |
michael@0 | 184 | |
michael@0 | 185 | if (mClosed) |
michael@0 | 186 | return NS_BASE_STREAM_CLOSED; |
michael@0 | 187 | |
michael@0 | 188 | const Chunk* chunk = mChunk; |
michael@0 | 189 | while (chunk->mLength) { |
michael@0 | 190 | *aAvailable += chunk->mLength; |
michael@0 | 191 | chunk++; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | return NS_OK; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | NS_IMETHODIMP |
michael@0 | 198 | FakeInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aOut) |
michael@0 | 199 | { |
michael@0 | 200 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | NS_IMETHODIMP |
michael@0 | 204 | FakeInputStream::ReadSegments(nsWriteSegmentFun aWriter, |
michael@0 | 205 | void* aClosure, |
michael@0 | 206 | uint32_t aCount, |
michael@0 | 207 | uint32_t* aRead) |
michael@0 | 208 | { |
michael@0 | 209 | *aRead = 0; |
michael@0 | 210 | |
michael@0 | 211 | if (mClosed) |
michael@0 | 212 | return NS_BASE_STREAM_CLOSED; |
michael@0 | 213 | |
michael@0 | 214 | while (mChunk->mLength) { |
michael@0 | 215 | uint32_t written = 0; |
michael@0 | 216 | |
michael@0 | 217 | nsresult rv = (*aWriter)(this, aClosure, mChunk->mData, |
michael@0 | 218 | *aRead, mChunk->mLength, &written); |
michael@0 | 219 | |
michael@0 | 220 | *aRead += written; |
michael@0 | 221 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 222 | |
michael@0 | 223 | mChunk++; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | return NS_OK; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | NS_IMETHODIMP |
michael@0 | 230 | FakeInputStream::IsNonBlocking(bool* aIsBlocking) |
michael@0 | 231 | { |
michael@0 | 232 | *aIsBlocking = false; |
michael@0 | 233 | return NS_OK; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | void |
michael@0 | 237 | FakeInputStream::Reset() |
michael@0 | 238 | { |
michael@0 | 239 | mClosed = false; |
michael@0 | 240 | mChunk = &mTest->mChunks[0]; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | bool |
michael@0 | 244 | FakeInputStream::NextTest() |
michael@0 | 245 | { |
michael@0 | 246 | mTestNumber++; |
michael@0 | 247 | mTest = &kTests[mTestNumber]; |
michael@0 | 248 | mChunk = &mTest->mChunks[0]; |
michael@0 | 249 | mClosed = false; |
michael@0 | 250 | |
michael@0 | 251 | return mTest->mChunks ? true : false; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | bool |
michael@0 | 255 | FakeInputStream::CheckTest(nsACString& aResult) |
michael@0 | 256 | { |
michael@0 | 257 | return !strcmp(aResult.BeginReading(), mTest->mResult) ? true : false; |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | #ifdef XP_WIN |
michael@0 | 261 | static inline int NS_tstrcmp(char16ptr_t x, char16ptr_t y) { |
michael@0 | 262 | return wcscmp(x, y); |
michael@0 | 263 | } |
michael@0 | 264 | #else |
michael@0 | 265 | #define NS_tstrcmp strcmp |
michael@0 | 266 | #endif |
michael@0 | 267 | |
michael@0 | 268 | bool |
michael@0 | 269 | FakeInputStream::CheckTest(nsAString& aResult) |
michael@0 | 270 | { |
michael@0 | 271 | return !NS_tstrcmp(aResult.BeginReading(), |
michael@0 | 272 | NS_ConvertASCIItoUTF16(mTest->mResult).BeginReading()) |
michael@0 | 273 | ? true : false; |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | int main(int argc, char** argv) |
michael@0 | 277 | { |
michael@0 | 278 | ScopedXPCOM xpcom("Base64"); |
michael@0 | 279 | NS_ENSURE_FALSE(xpcom.failed(), 1); |
michael@0 | 280 | |
michael@0 | 281 | nsCOMPtr<nsIScriptableBase64Encoder> encoder = |
michael@0 | 282 | do_CreateInstance("@mozilla.org/scriptablebase64encoder;1"); |
michael@0 | 283 | NS_ENSURE_TRUE(encoder, 1); |
michael@0 | 284 | |
michael@0 | 285 | nsRefPtr<FakeInputStream> stream = new FakeInputStream(); |
michael@0 | 286 | do { |
michael@0 | 287 | nsString wideString; |
michael@0 | 288 | nsCString string; |
michael@0 | 289 | |
michael@0 | 290 | nsresult rv; |
michael@0 | 291 | rv = encoder->EncodeToString(stream, 0, wideString); |
michael@0 | 292 | NS_ENSURE_SUCCESS(rv, 1); |
michael@0 | 293 | |
michael@0 | 294 | stream->Reset(); |
michael@0 | 295 | |
michael@0 | 296 | rv = encoder->EncodeToCString(stream, 0, string); |
michael@0 | 297 | NS_ENSURE_SUCCESS(rv, 1); |
michael@0 | 298 | |
michael@0 | 299 | if (!stream->CheckTest(wideString) || !stream->CheckTest(string)) |
michael@0 | 300 | fail("Failed to convert properly\n"); |
michael@0 | 301 | |
michael@0 | 302 | } while (stream->NextTest()); |
michael@0 | 303 | |
michael@0 | 304 | return 0; |
michael@0 | 305 | } |