bool test_valid() { for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { nsDependentCString str8(ValidStrings[i].m8); nsDependentString str16(ValidStrings[i].m16); if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) return false; if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) return false; nsCString tmp8("string "); AppendUTF16toUTF8(str16, tmp8); if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) return false; nsString tmp16(NS_LITERAL_STRING("string ")); AppendUTF8toUTF16(str8, tmp16); if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) return false; if (CompareUTF8toUTF16(str8, str16) != 0) return false; } return true; }
PRBool test_valid() { for (unsigned int i = 0; i < NS_ARRAY_LENGTH(ValidStrings); ++i) { nsDependentCString str8(ValidStrings[i].m8); nsDependentString str16(ValidStrings[i].m16); if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) return PR_FALSE; if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) return PR_FALSE; nsCString tmp8("string "); AppendUTF16toUTF8(str16, tmp8); if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) return PR_FALSE; nsString tmp16(NS_LITERAL_STRING("string ")); AppendUTF8toUTF16(str8, tmp16); if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) return PR_FALSE; if (CompareUTF8toUTF16(str8, str16) != 0) return PR_FALSE; } return PR_TRUE; }
TEST(UTF, Invalid8) { for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { nsDependentString str16(Invalid8Strings[i].m16); nsDependentCString str8(Invalid8Strings[i].m8); EXPECT_TRUE(NS_ConvertUTF8toUTF16(str8).Equals(str16)); nsString tmp16(NS_LITERAL_STRING("string ")); AppendUTF8toUTF16(str8, tmp16); EXPECT_TRUE(tmp16.Equals(NS_LITERAL_STRING("string ") + str16)); EXPECT_EQ(CompareUTF8toUTF16(str8, str16), 0); } }
TEST(UTF, Malformed8) { // Don't run this test in debug builds as that intentionally asserts. #ifndef DEBUG for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { nsDependentCString str8(Malformed8Strings[i]); EXPECT_TRUE(NS_ConvertUTF8toUTF16(str8).IsEmpty()); nsString tmp16(NS_LITERAL_STRING("string")); AppendUTF8toUTF16(str8, tmp16); EXPECT_TRUE(tmp16.EqualsLiteral("string")); EXPECT_NE(CompareUTF8toUTF16(str8, EmptyString()), 0); } #endif }
bool test_malformed8() { // Don't run this test in debug builds as that intentionally asserts. #ifndef DEBUG for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { nsDependentCString str8(Malformed8Strings[i]); if (!NS_ConvertUTF8toUTF16(str8).IsEmpty()) return false; nsString tmp16(NS_LITERAL_STRING("string")); AppendUTF8toUTF16(str8, tmp16); if (!tmp16.Equals(NS_LITERAL_STRING("string"))) return false; if (CompareUTF8toUTF16(str8, EmptyString()) == 0) return false; } #endif return true; }
TEST(Encoding, GoodSurrogatePair) { // When this string is decoded, the surrogate pair is U+10302 and the rest of // the string is specified by indexes 2 onward. const char16_t goodPairData[] = { 0xD800, 0xDF02, 0x65, 0x78, 0x0 }; nsDependentString goodPair16(goodPairData); uint32_t byteCount = 0; char* goodPair8 = ToNewUTF8String(goodPair16, &byteCount); EXPECT_TRUE(!!goodPair8); EXPECT_EQ(byteCount, 6u); const unsigned char expected8[] = { 0xF0, 0x90, 0x8C, 0x82, 0x65, 0x78, 0x0 }; EXPECT_EQ(0, memcmp(expected8, goodPair8, sizeof(expected8))); // This takes a different code path from the above, so test it to make sure // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. nsDependentCString expected((const char*)expected8); EXPECT_EQ(0, CompareUTF8toUTF16(expected, goodPair16)); NS_Free(goodPair8); }
TEST(Encoding, MalformedUTF16OrphanLowSurrogate) { // When this string is decoded, the low surrogate should be replaced and the // rest of the string is specified by indexes 1 onward. const char16_t lowSurrogateData[] = { 0xDDDD, 0x74, 0x65, 0x78, 0x74, 0x0 }; nsDependentString lowSurrogate16(lowSurrogateData); uint32_t byteCount = 0; char* lowSurrogate8 = ToNewUTF8String(lowSurrogate16, &byteCount); EXPECT_TRUE(!!lowSurrogate8); EXPECT_EQ(byteCount, 7u); const unsigned char expected8[] = { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 }; EXPECT_EQ(0, memcmp(expected8, lowSurrogate8, sizeof(expected8))); // This takes a different code path from the above, so test it to make sure // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. nsDependentCString expected((const char*)expected8); EXPECT_EQ(0, CompareUTF8toUTF16(expected, lowSurrogate16)); NS_Free(lowSurrogate8); }
TEST(Encoding, BackwardsSurrogatePair) { // When this string is decoded, the two surrogates are wrongly ordered and // must each be interpreted as U+FFFD. const char16_t backwardsPairData[] = { 0xDDDD, 0xD863, 0x65, 0x78, 0x0 }; nsDependentString backwardsPair16(backwardsPairData); uint32_t byteCount = 0; char* backwardsPair8 = ToNewUTF8String(backwardsPair16, &byteCount); EXPECT_TRUE(!!backwardsPair8); EXPECT_EQ(byteCount, 8u); const unsigned char expected8[] = { 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x65, 0x78, 0x0 }; EXPECT_EQ(0, memcmp(expected8, backwardsPair8, sizeof(expected8))); // This takes a different code path from the above, so test it to make sure // the UTF-16 enumeration remains in sync with the UTF-8 enumeration. nsDependentCString expected((const char*)expected8); EXPECT_EQ(0, CompareUTF8toUTF16(expected, backwardsPair16)); NS_Free(backwardsPair8); }
bool IsWidevineKeySystem(const nsAString& aKeySystem) { return !CompareUTF8toUTF16(kEMEKeySystemWidevine, aKeySystem); }
bool IsPrimetimeKeySystem(const nsAString& aKeySystem) { return !CompareUTF8toUTF16(kEMEKeySystemPrimetime, aKeySystem); }
bool IsClearkeyKeySystem(const nsAString& aKeySystem) { return !CompareUTF8toUTF16(kEMEKeySystemClearkey, aKeySystem); }