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 <stdio.h> |
michael@0 | 7 | #include "nsXPCOM.h" |
michael@0 | 8 | #include "nsIUnicodeNormalizer.h" |
michael@0 | 9 | #include "nsStringAPI.h" |
michael@0 | 10 | #include "nsCharTraits.h" |
michael@0 | 11 | #include "nsServiceManagerUtils.h" |
michael@0 | 12 | |
michael@0 | 13 | struct testcaseLine { |
michael@0 | 14 | wchar_t* c1; |
michael@0 | 15 | wchar_t* c2; |
michael@0 | 16 | wchar_t* c3; |
michael@0 | 17 | wchar_t* c4; |
michael@0 | 18 | wchar_t* c5; |
michael@0 | 19 | char* description; |
michael@0 | 20 | }; |
michael@0 | 21 | |
michael@0 | 22 | #ifdef DEBUG_smontagu |
michael@0 | 23 | #define DEBUG_NAMED_TESTCASE(t, s) \ |
michael@0 | 24 | printf(t ": "); \ |
michael@0 | 25 | for (uint32_t i = 0; i < s.Length(); ++i) \ |
michael@0 | 26 | printf("%x ", s.CharAt(i)); \ |
michael@0 | 27 | printf("\n") |
michael@0 | 28 | #else |
michael@0 | 29 | #define DEBUG_NAMED_TESTCASE(t, s) |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #define DEBUG_TESTCASE(x) DEBUG_NAMED_TESTCASE(#x, x) |
michael@0 | 33 | |
michael@0 | 34 | #define NORMALIZE_AND_COMPARE(base, comparison, form, description) \ |
michael@0 | 35 | normalized.Truncate();\ |
michael@0 | 36 | normalizer->NormalizeUnicode##form(comparison, normalized);\ |
michael@0 | 37 | DEBUG_NAMED_TESTCASE(#form "(" #comparison ")", normalized);\ |
michael@0 | 38 | if (!base.Equals(normalized)) {\ |
michael@0 | 39 | rv = false;\ |
michael@0 | 40 | showError(description, #base " != " #form "(" #comparison ")\n");\ |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | NS_DEFINE_CID(kUnicodeNormalizerCID, NS_UNICODE_NORMALIZER_CID); |
michael@0 | 44 | |
michael@0 | 45 | nsIUnicodeNormalizer *normalizer; |
michael@0 | 46 | bool verboseMode = false; |
michael@0 | 47 | |
michael@0 | 48 | #include "NormalizationData.h" |
michael@0 | 49 | |
michael@0 | 50 | void showError(const char* description, const char* errorText) |
michael@0 | 51 | { |
michael@0 | 52 | if (verboseMode) |
michael@0 | 53 | printf("%s failed: %s", description, errorText); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | bool TestInvariants(testcaseLine* testLine) |
michael@0 | 57 | { |
michael@0 | 58 | nsAutoString c1, c2, c3, c4, c5, normalized; |
michael@0 | 59 | c1 = nsDependentString((char16_t*)testLine->c1); |
michael@0 | 60 | c2 = nsDependentString((char16_t*)testLine->c2); |
michael@0 | 61 | c3 = nsDependentString((char16_t*)testLine->c3); |
michael@0 | 62 | c4 = nsDependentString((char16_t*)testLine->c4); |
michael@0 | 63 | c5 = nsDependentString((char16_t*)testLine->c5); |
michael@0 | 64 | bool rv = true; |
michael@0 | 65 | |
michael@0 | 66 | /* |
michael@0 | 67 | 1. The following invariants must be true for all conformant implementations |
michael@0 | 68 | |
michael@0 | 69 | NFC |
michael@0 | 70 | c2 == NFC(c1) == NFC(c2) == NFC(c3) |
michael@0 | 71 | */ |
michael@0 | 72 | DEBUG_TESTCASE(c2); |
michael@0 | 73 | NORMALIZE_AND_COMPARE(c2, c1, NFC, testLine->description); |
michael@0 | 74 | NORMALIZE_AND_COMPARE(c2, c2, NFC, testLine->description); |
michael@0 | 75 | NORMALIZE_AND_COMPARE(c2, c3, NFC, testLine->description); |
michael@0 | 76 | |
michael@0 | 77 | /* |
michael@0 | 78 | c4 == NFC(c4) == NFC(c5) |
michael@0 | 79 | */ |
michael@0 | 80 | DEBUG_TESTCASE(c4); |
michael@0 | 81 | NORMALIZE_AND_COMPARE(c4, c4, NFC, testLine->description); |
michael@0 | 82 | NORMALIZE_AND_COMPARE(c4, c5, NFC, testLine->description); |
michael@0 | 83 | |
michael@0 | 84 | /* |
michael@0 | 85 | NFD |
michael@0 | 86 | c3 == NFD(c1) == NFD(c2) == NFD(c3) |
michael@0 | 87 | */ |
michael@0 | 88 | DEBUG_TESTCASE(c3); |
michael@0 | 89 | NORMALIZE_AND_COMPARE(c3, c1, NFD, testLine->description); |
michael@0 | 90 | NORMALIZE_AND_COMPARE(c3, c2, NFD, testLine->description); |
michael@0 | 91 | NORMALIZE_AND_COMPARE(c3, c3, NFD, testLine->description); |
michael@0 | 92 | /* |
michael@0 | 93 | c5 == NFD(c4) == NFD(c5) |
michael@0 | 94 | */ |
michael@0 | 95 | DEBUG_TESTCASE(c5); |
michael@0 | 96 | NORMALIZE_AND_COMPARE(c5, c4, NFD, testLine->description); |
michael@0 | 97 | NORMALIZE_AND_COMPARE(c5, c5, NFD, testLine->description); |
michael@0 | 98 | |
michael@0 | 99 | /* |
michael@0 | 100 | NFKC |
michael@0 | 101 | c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5) |
michael@0 | 102 | */ |
michael@0 | 103 | DEBUG_TESTCASE(c4); |
michael@0 | 104 | NORMALIZE_AND_COMPARE(c4, c1, NFKC, testLine->description); |
michael@0 | 105 | NORMALIZE_AND_COMPARE(c4, c2, NFKC, testLine->description); |
michael@0 | 106 | NORMALIZE_AND_COMPARE(c4, c3, NFKC, testLine->description); |
michael@0 | 107 | NORMALIZE_AND_COMPARE(c4, c4, NFKC, testLine->description); |
michael@0 | 108 | NORMALIZE_AND_COMPARE(c4, c5, NFKC, testLine->description); |
michael@0 | 109 | |
michael@0 | 110 | /* |
michael@0 | 111 | NFKD |
michael@0 | 112 | c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5) |
michael@0 | 113 | */ |
michael@0 | 114 | DEBUG_TESTCASE(c5); |
michael@0 | 115 | NORMALIZE_AND_COMPARE(c5, c1, NFKD, testLine->description); |
michael@0 | 116 | NORMALIZE_AND_COMPARE(c5, c2, NFKD, testLine->description); |
michael@0 | 117 | NORMALIZE_AND_COMPARE(c5, c3, NFKD, testLine->description); |
michael@0 | 118 | NORMALIZE_AND_COMPARE(c5, c4, NFKD, testLine->description); |
michael@0 | 119 | NORMALIZE_AND_COMPARE(c5, c5, NFKD, testLine->description); |
michael@0 | 120 | |
michael@0 | 121 | return rv; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | uint32_t UTF32CodepointFromTestcase(testcaseLine* testLine) |
michael@0 | 125 | { |
michael@0 | 126 | if (!IS_SURROGATE(testLine->c1[0])) |
michael@0 | 127 | return testLine->c1[0]; |
michael@0 | 128 | |
michael@0 | 129 | NS_ASSERTION(NS_IS_HIGH_SURROGATE(testLine->c1[0]) && |
michael@0 | 130 | NS_IS_LOW_SURROGATE(testLine->c1[1]), |
michael@0 | 131 | "Test data neither in BMP nor legal surrogate pair"); |
michael@0 | 132 | return SURROGATE_TO_UCS4(testLine->c1[0], testLine->c1[1]); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | bool TestUnspecifiedCodepoint(uint32_t codepoint) |
michael@0 | 136 | { |
michael@0 | 137 | bool rv = true; |
michael@0 | 138 | char16_t unicharArray[3]; |
michael@0 | 139 | nsAutoString X, normalized; |
michael@0 | 140 | char description[9]; |
michael@0 | 141 | |
michael@0 | 142 | if (IS_IN_BMP(codepoint)) { |
michael@0 | 143 | unicharArray[0] = codepoint; |
michael@0 | 144 | unicharArray[1] = 0; |
michael@0 | 145 | X = nsDependentString(unicharArray); |
michael@0 | 146 | } |
michael@0 | 147 | else { |
michael@0 | 148 | unicharArray[0] = H_SURROGATE(codepoint); |
michael@0 | 149 | unicharArray[1] = L_SURROGATE(codepoint); |
michael@0 | 150 | unicharArray[2] = 0; |
michael@0 | 151 | X = nsDependentString(unicharArray); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | /* |
michael@0 | 155 | 2. For every code point X assigned in this version of Unicode that is not specifically |
michael@0 | 156 | listed in Part 1, the following invariants must be true for all conformant |
michael@0 | 157 | implementations: |
michael@0 | 158 | |
michael@0 | 159 | X == NFC(X) == NFD(X) == NFKC(X) == NFKD(X) |
michael@0 | 160 | */ |
michael@0 | 161 | DEBUG_TESTCASE(X); |
michael@0 | 162 | sprintf(description, "U+%04X", codepoint); |
michael@0 | 163 | NORMALIZE_AND_COMPARE(X, X, NFC, description); |
michael@0 | 164 | NORMALIZE_AND_COMPARE(X, X, NFD, description); |
michael@0 | 165 | NORMALIZE_AND_COMPARE(X, X, NFKC, description); |
michael@0 | 166 | NORMALIZE_AND_COMPARE(X, X, NFKD, description); |
michael@0 | 167 | return rv; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | void TestPart0() |
michael@0 | 171 | { |
michael@0 | 172 | printf("Test Part0: Specific cases\n"); |
michael@0 | 173 | |
michael@0 | 174 | uint32_t i = 0; |
michael@0 | 175 | uint32_t numFailed = 0; |
michael@0 | 176 | uint32_t numPassed = 0; |
michael@0 | 177 | |
michael@0 | 178 | while (Part0TestData[i].c1[0] != 0) { |
michael@0 | 179 | if (TestInvariants(&Part0TestData[i++])) |
michael@0 | 180 | ++numPassed; |
michael@0 | 181 | else |
michael@0 | 182 | ++numFailed; |
michael@0 | 183 | } |
michael@0 | 184 | printf(" %d cases passed, %d failed\n\n", numPassed, numFailed); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | void TestPart1() |
michael@0 | 188 | { |
michael@0 | 189 | printf("Test Part1: Character by character test\n"); |
michael@0 | 190 | |
michael@0 | 191 | uint32_t i = 0; |
michael@0 | 192 | uint32_t numFailed = 0; |
michael@0 | 193 | uint32_t numPassed = 0; |
michael@0 | 194 | uint32_t codepoint; |
michael@0 | 195 | uint32_t testDataCodepoint = UTF32CodepointFromTestcase(&Part1TestData[i]); |
michael@0 | 196 | |
michael@0 | 197 | for (codepoint = 1; codepoint < 0x110000; ++codepoint) { |
michael@0 | 198 | if (testDataCodepoint == codepoint) { |
michael@0 | 199 | if (TestInvariants(&Part1TestData[i])) |
michael@0 | 200 | ++numPassed; |
michael@0 | 201 | else |
michael@0 | 202 | ++numFailed; |
michael@0 | 203 | testDataCodepoint = UTF32CodepointFromTestcase(&Part1TestData[++i]); |
michael@0 | 204 | } else { |
michael@0 | 205 | if (TestUnspecifiedCodepoint(codepoint)) |
michael@0 | 206 | ++numPassed; |
michael@0 | 207 | else |
michael@0 | 208 | ++numFailed; |
michael@0 | 209 | } |
michael@0 | 210 | } |
michael@0 | 211 | printf(" %d cases passed, %d failed\n\n", numPassed, numFailed); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | void TestPart2() |
michael@0 | 215 | { |
michael@0 | 216 | printf("Test Part2: Canonical Order Test\n"); |
michael@0 | 217 | |
michael@0 | 218 | uint32_t i = 0; |
michael@0 | 219 | uint32_t numFailed = 0; |
michael@0 | 220 | uint32_t numPassed = 0; |
michael@0 | 221 | |
michael@0 | 222 | while (Part2TestData[i].c1[0] != 0) { |
michael@0 | 223 | if (TestInvariants(&Part2TestData[i++])) |
michael@0 | 224 | ++numPassed; |
michael@0 | 225 | else |
michael@0 | 226 | ++numFailed; |
michael@0 | 227 | } |
michael@0 | 228 | printf(" %d cases passed, %d failed\n\n", numPassed, numFailed); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | void TestPart3() |
michael@0 | 232 | { |
michael@0 | 233 | printf("Test Part3: PRI #29 Test\n"); |
michael@0 | 234 | |
michael@0 | 235 | uint32_t i = 0; |
michael@0 | 236 | uint32_t numFailed = 0; |
michael@0 | 237 | uint32_t numPassed = 0; |
michael@0 | 238 | |
michael@0 | 239 | while (Part3TestData[i].c1[0] != 0) { |
michael@0 | 240 | if (TestInvariants(&Part3TestData[i++])) |
michael@0 | 241 | ++numPassed; |
michael@0 | 242 | else |
michael@0 | 243 | ++numFailed; |
michael@0 | 244 | } |
michael@0 | 245 | printf(" %d cases passed, %d failed\n\n", numPassed, numFailed); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | int main(int argc, char** argv) { |
michael@0 | 249 | if (sizeof(wchar_t) != 2) { |
michael@0 | 250 | printf("This test can only be run where sizeof(wchar_t) == 2\n"); |
michael@0 | 251 | return 1; |
michael@0 | 252 | } |
michael@0 | 253 | if (strlen(versionText) == 0) { |
michael@0 | 254 | printf("No testcases: to run the tests generate the header file using\n"); |
michael@0 | 255 | printf(" perl genNormalizationData.pl\n"); |
michael@0 | 256 | printf("in intl/unichar/tools and rebuild\n"); |
michael@0 | 257 | return 1; |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | printf("NormalizationTest: test nsIUnicodeNormalizer. UCD version: %s\n", |
michael@0 | 261 | versionText); |
michael@0 | 262 | if (argc <= 1) |
michael@0 | 263 | verboseMode = false; |
michael@0 | 264 | else if ((argc == 2) && (!strcmp(argv[1], "-v"))) |
michael@0 | 265 | verboseMode = true; |
michael@0 | 266 | else { |
michael@0 | 267 | printf(" Usage: NormalizationTest [OPTION]..\n"); |
michael@0 | 268 | printf("Options:\n"); |
michael@0 | 269 | printf(" -v Verbose mode\n"); |
michael@0 | 270 | return 1; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
michael@0 | 274 | if (NS_FAILED(rv)) { |
michael@0 | 275 | printf("NS_InitXPCOM2 failed\n"); |
michael@0 | 276 | return 1; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | normalizer = nullptr; |
michael@0 | 280 | nsresult res; |
michael@0 | 281 | res = CallGetService(kUnicodeNormalizerCID, &normalizer); |
michael@0 | 282 | |
michael@0 | 283 | if(NS_FAILED(res) || !normalizer) { |
michael@0 | 284 | printf("GetService failed\n"); |
michael@0 | 285 | return 1; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | TestPart0(); |
michael@0 | 289 | TestPart1(); |
michael@0 | 290 | TestPart2(); |
michael@0 | 291 | TestPart3(); |
michael@0 | 292 | |
michael@0 | 293 | NS_RELEASE(normalizer); |
michael@0 | 294 | |
michael@0 | 295 | printf("Test finished \n"); |
michael@0 | 296 | return 0; |
michael@0 | 297 | } |