1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "gtest/gtest.h" 17 #include "gtest/hwext/gtest-ext.h" 18 #include "gtest/hwext/gtest-tag.h" 19 #include "unicode/unistr.h" 20 21 #include "base/utils/utf.h" 22 23 using namespace testing; 24 using namespace testing::ext; 25 26 namespace OHOS::Ace { 27 class UtfTestNg : public Test { SetUp()28 void SetUp() override 29 { 30 index = 0; 31 } 32 33 public: 34 size_t index; 35 }; 36 37 /** 38 * @tc.name:IsUTF8_001 39 * @tc.desc: Test utf IsUTF8 40 * @tc.type: FUNC 41 */ 42 HWTEST_F(UtfTestNg, IsUTF8_001, TestSize.Level1) 43 { 44 std::string str = ""; 45 EXPECT_EQ(IsUTF8(str), false); 46 std::string key = "hello"; 47 EXPECT_EQ(IsUTF8(key), true); 48 std::string twoByteStr = "é"; 49 EXPECT_TRUE(IsUTF8(twoByteStr)); 50 std::string threeByteStr = "中"; 51 EXPECT_TRUE(IsUTF8(threeByteStr)); 52 53 std::string invalidTwoByteStr = "\xC3"; 54 EXPECT_FALSE(IsUTF8(invalidTwoByteStr)); 55 std::string invalidContinuationStr = "\xE4\xB8\x41"; 56 EXPECT_FALSE(IsUTF8(invalidContinuationStr)); 57 std::string mixedStr = "Hello\xC3\x28World"; 58 EXPECT_FALSE(IsUTF8(mixedStr)); 59 } 60 61 /** 62 * @tc.name:MUtf8ToUtf16Size_001 63 * @tc.desc: Test utf MUtf8ToUtf16Size 64 * @tc.type: FUNC 65 */ 66 HWTEST_F(UtfTestNg, MUtf8ToUtf16Size_001, TestSize.Level1) 67 { 68 uint8_t utf8[3] = { 0xd8 + 1, 0xdc - 1, 0xd8 + 1 }; 69 size_t len = sizeof(utf8) / sizeof(utf8[0]); 70 EXPECT_EQ(MUtf8ToUtf16Size(utf8, len), 2); 71 const uint8_t mutf8[] = { 0xC2 }; 72 size_t mutf8Len = 1; 73 EXPECT_EQ(MUtf8ToUtf16Size(mutf8, mutf8Len), 1); 74 } 75 76 /** 77 * @tc.name:ConvertMUtf8ToUtf16Pair_001 78 * @tc.desc: Test utf ConvertMUtf8ToUtf16Pair 79 * @tc.type: FUNC 80 */ 81 HWTEST_F(UtfTestNg, ConvertMUtf8ToUtf16Pair_001, TestSize.Level1) 82 { 83 uint32_t utf8Res; 84 size_t byte; 85 uint8_t utf8[3] = { 0xd8 + 1, 0xdc - 1, 0xd8 + 1 }; 86 size_t maxBytes = sizeof(utf8) / sizeof(utf8[0]); 87 std::pair<uint32_t, size_t> result; 88 result = ConvertMUtf8ToUtf16Pair(utf8, maxBytes); 89 utf8Res = result.first; 90 byte = result.second; 91 EXPECT_EQ(utf8Res, 1627); 92 EXPECT_EQ(byte, 2); 93 } 94 95 /** 96 * @tc.name:ConvertRegionUtf8ToUtf16_001 97 * @tc.desc: Test utf ConvertRegionUtf8ToUtf16 98 * @tc.type: FUNC 99 */ 100 HWTEST_F(UtfTestNg, ConvertRegionUtf8ToUtf16_001, TestSize.Level1) 101 { 102 uint8_t mutf8In[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F}; 103 uint16_t utf16Out[5] = {0}; 104 size_t result = ConvertRegionUtf8ToUtf16(mutf8In, utf16Out, sizeof(mutf8In), sizeof(utf16Out)/sizeof(uint16_t), 0); 105 106 EXPECT_EQ(result, 5); 107 EXPECT_EQ(utf16Out[0], 0x0048); 108 EXPECT_EQ(utf16Out[1], 0x0065); 109 EXPECT_EQ(utf16Out[2], 0x006C); 110 EXPECT_EQ(utf16Out[3], 0x006C); 111 EXPECT_EQ(utf16Out[4], 0x006F); 112 } 113 114 /** 115 * @tc.name: DebuggerConvertRegionUtf16ToUtf8_001 116 * @tc.desc: Test utf DebuggerConvertRegionUtf16ToUtf8 117 * @tc.type: FUNC 118 */ 119 HWTEST_F(UtfTestNg, DebuggerConvertRegionUtf16ToUtf8_001, TestSize.Level1) 120 { 121 uint16_t utf16In[] = {0x0048, 0x0065, 0x006C, 0x006C, 0x006F}; 122 uint8_t utf8Out[10] = {0}; 123 size_t result = DebuggerConvertRegionUtf16ToUtf8(utf16In, utf8Out, 5, 10, 0); 124 125 EXPECT_EQ(result, 5); 126 EXPECT_EQ(utf8Out[0], 0x48); 127 EXPECT_EQ(utf8Out[1], 0x65); 128 EXPECT_EQ(utf8Out[2], 0x6C); 129 EXPECT_EQ(utf8Out[3], 0x6C); 130 EXPECT_EQ(utf8Out[4], 0x6F); 131 } 132 133 /** 134 * @tc.name: DebuggerConvertRegionUtf16ToUtf8_002 135 * @tc.desc: Test utf DebuggerConvertRegionUtf16ToUtf8 136 * @tc.type: FUNC 137 */ 138 HWTEST_F(UtfTestNg, DebuggerConvertRegionUtf16ToUtf8_002, TestSize.Level1) 139 { 140 uint16_t utf16In[] = {0xD83D, 0xDE00}; 141 uint8_t utf8Out[5] = {0}; 142 size_t result = DebuggerConvertRegionUtf16ToUtf8(utf16In, utf8Out, 2, 5, 0); 143 144 EXPECT_EQ(result, 4); 145 EXPECT_EQ(utf8Out[0], 0xF0); 146 EXPECT_EQ(utf8Out[1], 0x9F); 147 EXPECT_EQ(utf8Out[2], 0x98); 148 EXPECT_EQ(utf8Out[3], 0x80); 149 } 150 151 /** 152 * @tc.name: DebuggerConvertRegionUtf16ToUtf8_003 153 * @tc.desc: Test utf DebuggerConvertRegionUtf16ToUtf8 154 * @tc.type: FUNC 155 */ 156 HWTEST_F(UtfTestNg, DebuggerConvertRegionUtf16ToUtf8_003, TestSize.Level1) 157 { 158 uint16_t utf16In[] = {0x0048, 0x0065}; 159 uint8_t utf8Out[5] = {0}; 160 161 size_t nullResult1 = DebuggerConvertRegionUtf16ToUtf8(nullptr, utf8Out, 2, 5, 0); 162 EXPECT_EQ(nullResult1, 0); 163 164 size_t nullResult2 = DebuggerConvertRegionUtf16ToUtf8(utf16In, nullptr, 2, 0, 0); 165 EXPECT_EQ(nullResult2, 0); 166 } 167 168 /** 169 * @tc.name: ConvertIllegalStr_001 170 * @tc.desc: Test utf ConvertIllegalStr 171 * @tc.type: FUNC 172 */ 173 HWTEST_F(UtfTestNg, ConvertIllegalStr_001, TestSize.Level1) 174 { 175 std::string str = "Hello世界"; 176 std::string originalStr = str; 177 ConvertIllegalStr(str); 178 179 EXPECT_EQ(str, originalStr); 180 } 181 182 /** 183 * @tc.name: ConvertIllegalStr_002 184 * @tc.desc: Test utf ConvertIllegalStr 185 * @tc.type: FUNC 186 */ 187 HWTEST_F(UtfTestNg, ConvertIllegalStr_002, TestSize.Level1) 188 { 189 std::string str = "Hello\xC3\x28"; 190 ConvertIllegalStr(str); 191 192 EXPECT_TRUE(IsUTF8(str)); 193 EXPECT_NE(str, "Hello\xC3\x28"); 194 } 195 196 /** 197 * @tc.name: ConvertIllegalStr_003 198 * @tc.desc: Test utf ConvertIllegalStr 199 * @tc.type: FUNC 200 */ 201 HWTEST_F(UtfTestNg, ConvertIllegalStr_003, TestSize.Level1) 202 { 203 std::string str = ""; 204 ConvertIllegalStr(str); 205 206 EXPECT_TRUE(str.empty()); 207 } 208 209 /** 210 * @tc.name: SplitUtf16Pair_001 211 * @tc.desc: Test utf SplitUtf16Pair 212 * @tc.type: FUNC 213 */ 214 HWTEST_F(UtfTestNg, SplitUtf16Pair_001, TestSize.Level1) 215 { 216 uint32_t pair = 0xD83DDE00; 217 auto result = SplitUtf16Pair(pair); 218 219 EXPECT_EQ(result.first, 0xD83D); 220 EXPECT_EQ(result.second, 0xDE00); 221 } 222 223 /** 224 * @tc.name: SplitUtf16Pair_002 225 * @tc.desc: Test utf SplitUtf16Pair 226 * @tc.type: FUNC 227 */ 228 HWTEST_F(UtfTestNg, SplitUtf16Pair_002, TestSize.Level1) 229 { 230 uint32_t pair = 0x00410042; 231 auto result = SplitUtf16Pair(pair); 232 233 EXPECT_EQ(result.first, 0x0041); 234 EXPECT_EQ(result.second, 0x0042); 235 } 236 }