michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestHarness.h" michael@0: michael@0: nsresult TestGoodSurrogatePair() michael@0: { michael@0: // When this string is decoded, the surrogate pair is U+10302 and the rest of michael@0: // the string is specified by indexes 2 onward. michael@0: const char16_t goodPairData[] = { 0xD800, 0xDF02, 0x65, 0x78, 0x0 }; michael@0: nsDependentString goodPair16(goodPairData); michael@0: michael@0: uint32_t byteCount = 0; michael@0: char* goodPair8 = ToNewUTF8String(goodPair16, &byteCount); michael@0: if (!goodPair8) michael@0: { michael@0: fail("out of memory creating goodPair8"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (byteCount != 6) michael@0: { michael@0: fail("wrong number of bytes; expected 6, got %lu", byteCount); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: const unsigned char expected8[] = michael@0: { 0xF0, 0x90, 0x8C, 0x82, 0x65, 0x78, 0x0 }; michael@0: if (0 != memcmp(expected8, goodPair8, sizeof(expected8))) michael@0: { michael@0: fail("wrong translation to UTF8"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // This takes a different code path from the above, so test it to make sure michael@0: // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. michael@0: nsDependentCString expected((const char*)expected8); michael@0: if (0 != CompareUTF8toUTF16(expected, goodPair16)) michael@0: { michael@0: fail("bad comparison between UTF-8 and equivalent UTF-16"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_Free(goodPair8); michael@0: michael@0: passed("TestGoodSurrogatePair"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult TestBackwardsSurrogatePair() michael@0: { michael@0: // When this string is decoded, the two surrogates are wrongly ordered and michael@0: // must each be interpreted as U+FFFD. michael@0: const char16_t backwardsPairData[] = { 0xDDDD, 0xD863, 0x65, 0x78, 0x0 }; michael@0: nsDependentString backwardsPair16(backwardsPairData); michael@0: michael@0: uint32_t byteCount = 0; michael@0: char* backwardsPair8 = ToNewUTF8String(backwardsPair16, &byteCount); michael@0: if (!backwardsPair8) michael@0: { michael@0: fail("out of memory creating backwardsPair8"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (byteCount != 8) michael@0: { michael@0: fail("wrong number of bytes; expected 8, got %lu", byteCount); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: const unsigned char expected8[] = michael@0: { 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x65, 0x78, 0x0 }; michael@0: if (0 != memcmp(expected8, backwardsPair8, sizeof(expected8))) michael@0: { michael@0: fail("wrong translation to UTF8"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // This takes a different code path from the above, so test it to make sure michael@0: // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. michael@0: nsDependentCString expected((const char*)expected8); michael@0: if (0 != CompareUTF8toUTF16(expected, backwardsPair16)) michael@0: { michael@0: fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_Free(backwardsPair8); michael@0: michael@0: passed("TestBackwardsSurrogatePair"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult TestMalformedUTF16OrphanHighSurrogate() michael@0: { michael@0: // When this string is decoded, the high surrogate should be replaced and the michael@0: // rest of the string is specified by indexes 1 onward. michael@0: const char16_t highSurrogateData[] = { 0xD863, 0x74, 0x65, 0x78, 0x74, 0x0 }; michael@0: nsDependentString highSurrogate16(highSurrogateData); michael@0: michael@0: uint32_t byteCount = 0; michael@0: char* highSurrogate8 = ToNewUTF8String(highSurrogate16, &byteCount); michael@0: if (!highSurrogate8) michael@0: { michael@0: fail("out of memory creating highSurrogate8"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (byteCount != 7) michael@0: { michael@0: fail("wrong number of bytes; expected 7, got %lu", byteCount); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: const unsigned char expected8[] = michael@0: { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 }; michael@0: if (0 != memcmp(expected8, highSurrogate8, sizeof(expected8))) michael@0: { michael@0: fail("wrong translation to UTF8"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // This takes a different code path from the above, so test it to make sure michael@0: // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. michael@0: nsDependentCString expected((const char*)expected8); michael@0: if (0 != CompareUTF8toUTF16(expected, highSurrogate16)) michael@0: { michael@0: fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_Free(highSurrogate8); michael@0: michael@0: passed("TestMalformedUTF16OrphanHighSurrogate"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult TestMalformedUTF16OrphanLowSurrogate() michael@0: { michael@0: // When this string is decoded, the low surrogate should be replaced and the michael@0: // rest of the string is specified by indexes 1 onward. michael@0: const char16_t lowSurrogateData[] = { 0xDDDD, 0x74, 0x65, 0x78, 0x74, 0x0 }; michael@0: nsDependentString lowSurrogate16(lowSurrogateData); michael@0: michael@0: uint32_t byteCount = 0; michael@0: char* lowSurrogate8 = ToNewUTF8String(lowSurrogate16, &byteCount); michael@0: if (!lowSurrogate8) michael@0: { michael@0: fail("out of memory creating lowSurrogate8"); michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (byteCount != 7) michael@0: { michael@0: fail("wrong number of bytes; expected 7, got %lu", byteCount); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: const unsigned char expected8[] = michael@0: { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 }; michael@0: if (0 != memcmp(expected8, lowSurrogate8, sizeof(expected8))) michael@0: { michael@0: fail("wrong translation to UTF8"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // This takes a different code path from the above, so test it to make sure michael@0: // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. michael@0: nsDependentCString expected((const char*)expected8); michael@0: if (0 != CompareUTF8toUTF16(expected, lowSurrogate16)) michael@0: { michael@0: fail("bad comparison between UTF-8 and malformed but equivalent UTF-16"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_Free(lowSurrogate8); michael@0: michael@0: passed("TestMalformedUTF16OrphanLowSurrogate"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: ScopedXPCOM xpcom("TestEncoding"); michael@0: if (xpcom.failed()) michael@0: return 1; michael@0: michael@0: int rv = 0; michael@0: michael@0: if (NS_FAILED(TestGoodSurrogatePair())) michael@0: rv = 1; michael@0: if (NS_FAILED(TestBackwardsSurrogatePair())) michael@0: rv = 1; michael@0: if (NS_FAILED(TestMalformedUTF16OrphanHighSurrogate())) michael@0: rv = 1; michael@0: if (NS_FAILED(TestMalformedUTF16OrphanLowSurrogate())) michael@0: rv = 1; michael@0: michael@0: return rv; michael@0: }