Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 "TestHarness.h" |
michael@0 | 7 | |
michael@0 | 8 | nsresult TestGoodSurrogatePair() |
michael@0 | 9 | { |
michael@0 | 10 | // When this string is decoded, the surrogate pair is U+10302 and the rest of |
michael@0 | 11 | // the string is specified by indexes 2 onward. |
michael@0 | 12 | const char16_t goodPairData[] = { 0xD800, 0xDF02, 0x65, 0x78, 0x0 }; |
michael@0 | 13 | nsDependentString goodPair16(goodPairData); |
michael@0 | 14 | |
michael@0 | 15 | uint32_t byteCount = 0; |
michael@0 | 16 | char* goodPair8 = ToNewUTF8String(goodPair16, &byteCount); |
michael@0 | 17 | if (!goodPair8) |
michael@0 | 18 | { |
michael@0 | 19 | fail("out of memory creating goodPair8"); |
michael@0 | 20 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | if (byteCount != 6) |
michael@0 | 24 | { |
michael@0 | 25 | fail("wrong number of bytes; expected 6, got %lu", byteCount); |
michael@0 | 26 | return NS_ERROR_FAILURE; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | const unsigned char expected8[] = |
michael@0 | 30 | { 0xF0, 0x90, 0x8C, 0x82, 0x65, 0x78, 0x0 }; |
michael@0 | 31 | if (0 != memcmp(expected8, goodPair8, sizeof(expected8))) |
michael@0 | 32 | { |
michael@0 | 33 | fail("wrong translation to UTF8"); |
michael@0 | 34 | return NS_ERROR_FAILURE; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | // This takes a different code path from the above, so test it to make sure |
michael@0 | 38 | // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
michael@0 | 39 | nsDependentCString expected((const char*)expected8); |
michael@0 | 40 | if (0 != CompareUTF8toUTF16(expected, goodPair16)) |
michael@0 | 41 | { |
michael@0 | 42 | fail("bad comparison between UTF-8 and equivalent UTF-16"); |
michael@0 | 43 | return NS_ERROR_FAILURE; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | NS_Free(goodPair8); |
michael@0 | 47 | |
michael@0 | 48 | passed("TestGoodSurrogatePair"); |
michael@0 | 49 | return NS_OK; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | nsresult TestBackwardsSurrogatePair() |
michael@0 | 53 | { |
michael@0 | 54 | // When this string is decoded, the two surrogates are wrongly ordered and |
michael@0 | 55 | // must each be interpreted as U+FFFD. |
michael@0 | 56 | const char16_t backwardsPairData[] = { 0xDDDD, 0xD863, 0x65, 0x78, 0x0 }; |
michael@0 | 57 | nsDependentString backwardsPair16(backwardsPairData); |
michael@0 | 58 | |
michael@0 | 59 | uint32_t byteCount = 0; |
michael@0 | 60 | char* backwardsPair8 = ToNewUTF8String(backwardsPair16, &byteCount); |
michael@0 | 61 | if (!backwardsPair8) |
michael@0 | 62 | { |
michael@0 | 63 | fail("out of memory creating backwardsPair8"); |
michael@0 | 64 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | if (byteCount != 8) |
michael@0 | 68 | { |
michael@0 | 69 | fail("wrong number of bytes; expected 8, got %lu", byteCount); |
michael@0 | 70 | return NS_ERROR_FAILURE; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | const unsigned char expected8[] = |
michael@0 | 74 | { 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x65, 0x78, 0x0 }; |
michael@0 | 75 | if (0 != memcmp(expected8, backwardsPair8, sizeof(expected8))) |
michael@0 | 76 | { |
michael@0 | 77 | fail("wrong translation to UTF8"); |
michael@0 | 78 | return NS_ERROR_FAILURE; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | // This takes a different code path from the above, so test it to make sure |
michael@0 | 82 | // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
michael@0 | 83 | nsDependentCString expected((const char*)expected8); |
michael@0 | 84 | if (0 != CompareUTF8toUTF16(expected, backwardsPair16)) |
michael@0 | 85 | { |
michael@0 | 86 | fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); |
michael@0 | 87 | return NS_ERROR_FAILURE; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | NS_Free(backwardsPair8); |
michael@0 | 91 | |
michael@0 | 92 | passed("TestBackwardsSurrogatePair"); |
michael@0 | 93 | return NS_OK; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | nsresult TestMalformedUTF16OrphanHighSurrogate() |
michael@0 | 97 | { |
michael@0 | 98 | // When this string is decoded, the high surrogate should be replaced and the |
michael@0 | 99 | // rest of the string is specified by indexes 1 onward. |
michael@0 | 100 | const char16_t highSurrogateData[] = { 0xD863, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
michael@0 | 101 | nsDependentString highSurrogate16(highSurrogateData); |
michael@0 | 102 | |
michael@0 | 103 | uint32_t byteCount = 0; |
michael@0 | 104 | char* highSurrogate8 = ToNewUTF8String(highSurrogate16, &byteCount); |
michael@0 | 105 | if (!highSurrogate8) |
michael@0 | 106 | { |
michael@0 | 107 | fail("out of memory creating highSurrogate8"); |
michael@0 | 108 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | if (byteCount != 7) |
michael@0 | 112 | { |
michael@0 | 113 | fail("wrong number of bytes; expected 7, got %lu", byteCount); |
michael@0 | 114 | return NS_ERROR_FAILURE; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | const unsigned char expected8[] = |
michael@0 | 118 | { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
michael@0 | 119 | if (0 != memcmp(expected8, highSurrogate8, sizeof(expected8))) |
michael@0 | 120 | { |
michael@0 | 121 | fail("wrong translation to UTF8"); |
michael@0 | 122 | return NS_ERROR_FAILURE; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // This takes a different code path from the above, so test it to make sure |
michael@0 | 126 | // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
michael@0 | 127 | nsDependentCString expected((const char*)expected8); |
michael@0 | 128 | if (0 != CompareUTF8toUTF16(expected, highSurrogate16)) |
michael@0 | 129 | { |
michael@0 | 130 | fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); |
michael@0 | 131 | return NS_ERROR_FAILURE; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_Free(highSurrogate8); |
michael@0 | 135 | |
michael@0 | 136 | passed("TestMalformedUTF16OrphanHighSurrogate"); |
michael@0 | 137 | return NS_OK; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | nsresult TestMalformedUTF16OrphanLowSurrogate() |
michael@0 | 141 | { |
michael@0 | 142 | // When this string is decoded, the low surrogate should be replaced and the |
michael@0 | 143 | // rest of the string is specified by indexes 1 onward. |
michael@0 | 144 | const char16_t lowSurrogateData[] = { 0xDDDD, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
michael@0 | 145 | nsDependentString lowSurrogate16(lowSurrogateData); |
michael@0 | 146 | |
michael@0 | 147 | uint32_t byteCount = 0; |
michael@0 | 148 | char* lowSurrogate8 = ToNewUTF8String(lowSurrogate16, &byteCount); |
michael@0 | 149 | if (!lowSurrogate8) |
michael@0 | 150 | { |
michael@0 | 151 | fail("out of memory creating lowSurrogate8"); |
michael@0 | 152 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | if (byteCount != 7) |
michael@0 | 156 | { |
michael@0 | 157 | fail("wrong number of bytes; expected 7, got %lu", byteCount); |
michael@0 | 158 | return NS_ERROR_FAILURE; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | const unsigned char expected8[] = |
michael@0 | 162 | { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 }; |
michael@0 | 163 | if (0 != memcmp(expected8, lowSurrogate8, sizeof(expected8))) |
michael@0 | 164 | { |
michael@0 | 165 | fail("wrong translation to UTF8"); |
michael@0 | 166 | return NS_ERROR_FAILURE; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | // This takes a different code path from the above, so test it to make sure |
michael@0 | 170 | // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. |
michael@0 | 171 | nsDependentCString expected((const char*)expected8); |
michael@0 | 172 | if (0 != CompareUTF8toUTF16(expected, lowSurrogate16)) |
michael@0 | 173 | { |
michael@0 | 174 | fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); |
michael@0 | 175 | return NS_ERROR_FAILURE; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | NS_Free(lowSurrogate8); |
michael@0 | 179 | |
michael@0 | 180 | passed("TestMalformedUTF16OrphanLowSurrogate"); |
michael@0 | 181 | return NS_OK; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | |
michael@0 | 185 | int main(int argc, char** argv) |
michael@0 | 186 | { |
michael@0 | 187 | ScopedXPCOM xpcom("TestEncoding"); |
michael@0 | 188 | if (xpcom.failed()) |
michael@0 | 189 | return 1; |
michael@0 | 190 | |
michael@0 | 191 | int rv = 0; |
michael@0 | 192 | |
michael@0 | 193 | if (NS_FAILED(TestGoodSurrogatePair())) |
michael@0 | 194 | rv = 1; |
michael@0 | 195 | if (NS_FAILED(TestBackwardsSurrogatePair())) |
michael@0 | 196 | rv = 1; |
michael@0 | 197 | if (NS_FAILED(TestMalformedUTF16OrphanHighSurrogate())) |
michael@0 | 198 | rv = 1; |
michael@0 | 199 | if (NS_FAILED(TestMalformedUTF16OrphanLowSurrogate())) |
michael@0 | 200 | rv = 1; |
michael@0 | 201 | |
michael@0 | 202 | return rv; |
michael@0 | 203 | } |