1 /*
2 * Copyright (C) 2023 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 #define private public
17 #define protected public
18
19 #include <vector>
20
21 #include "gsm_pdu_hex_value.h"
22 #include "gsm_sms_common_utils.h"
23 #include "gtest/gtest.h"
24 #include "ims_sms_client.h"
25 #include "sms_common_utils.h"
26 #include "text_coder.h"
27
28 namespace OHOS {
29 namespace Telephony {
30 using namespace testing::ext;
31
32 namespace {
33 const std::string TEXT_SMS_CONTENT = "hello world";
34 const int BUF_SIZE = 2401;
35 const int TEXT_LENGTH = 2;
36 const int FILL_BITS = 2;
37 const int DIGIT_LEN = 3;
38 const int DIGIT_LEN2 = 6;
39 const unsigned char SRC_TEXT = 2;
40 } // namespace
41
42 class BranchUtilsTest : public testing::Test {
43 public:
44 static void SetUpTestCase();
45 static void TearDownTestCase();
46 void SetUp();
47 void TearDown();
48 };
SetUpTestCase()49 void BranchUtilsTest::SetUpTestCase() {}
50
TearDownTestCase()51 void BranchUtilsTest::TearDownTestCase()
52 {
53 DelayedSingleton<ImsSmsClient>::GetInstance()->UnInit();
54 }
55
SetUp()56 void BranchUtilsTest::SetUp() {}
57
TearDown()58 void BranchUtilsTest::TearDown() {}
59
60 /**
61 * @tc.number Telephony_SmsMmsGtest_TextCoder_0001
62 * @tc.name Test TextCoder
63 * @tc.desc Function test
64 */
65 HWTEST_F(BranchUtilsTest, TextCoder_0001, Function | MediumTest | Level1)
66 {
67 DataCodingScheme DataCodingScheme;
68 MsgLangInfo msgLangInfo;
69 unsigned char encodeData[BUF_SIZE];
70 MSG_LANGUAGE_ID_T langId = 0;
71 bool unknown = false;
72 unsigned short inText = 1;
73 const uint8_t *pMsgText = (const uint8_t *)TEXT_SMS_CONTENT.c_str();
74 uint8_t *pDestText = encodeData;
75 SmsCodingNationalType codingNational = SMS_CODING_NATIONAL_TYPE_DEFAULT;
76 EXPECT_GE(TextCoder::Instance().Utf8ToGsm7bit(pDestText, BUF_SIZE, const_cast<uint8_t *>(pMsgText), 0, langId), 0);
77 EXPECT_GE(TextCoder::Instance().CdmaUtf8ToAuto(pDestText, 1, pMsgText, 1, DataCodingScheme), -1);
78 EXPECT_GE(TextCoder::Instance().GsmUtf8ToAuto(pDestText, 1, pMsgText, 1,
79 DataCodingScheme, codingNational, langId), -1);
80 EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(pDestText, 1, pMsgText, -1), -1);
81 EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(pDestText, 0, pMsgText, -1), 0);
82 EXPECT_EQ(TextCoder::Instance().CdmaUtf8ToAuto(pDestText, 1, pMsgText, 0, DataCodingScheme), 0);
83 EXPECT_EQ(TextCoder::Instance().GsmUtf8ToAuto(pDestText, 1, pMsgText, 0,
84 DataCodingScheme, codingNational, langId), 0);
85 EXPECT_EQ(TextCoder::Instance().Gsm7bitToUtf8(pDestText, 0, pMsgText, 0, msgLangInfo), 0);
86 EXPECT_GE(TextCoder::Instance().ShiftjisToUtf8(pDestText, 1, pMsgText, -1), 0);
87 EXPECT_GE(TextCoder::Instance().Ucs2ToUtf8(pDestText, 1, pMsgText, -1), 0);
88 EXPECT_GE(TextCoder::Instance().EuckrToUtf8(pDestText, 1, pMsgText, -1), 0);
89 EXPECT_FALSE(TextCoder::Instance().Ucs2ToUtf8(pDestText, 0, pMsgText, 0));
90 EXPECT_FALSE(TextCoder::Instance().EuckrToUtf8(pDestText, 0, pMsgText, 1));
91 EXPECT_FALSE(TextCoder::Instance().ShiftjisToUtf8(pDestText, 0, pMsgText, 1));
92 EXPECT_EQ(TextCoder::Instance().Ucs2ToGsm7bit(pDestText, 0, pMsgText, 0, langId), -1);
93 EXPECT_GT(TextCoder::Instance().Ucs2ToGsm7bit(pDestText, 1, pMsgText, TEXT_LENGTH, langId), 0);
94 EXPECT_EQ(TextCoder::Instance().Ucs2ToGsm7bitAuto(pDestText, 0, pMsgText, 0, unknown, codingNational), -1);
95 EXPECT_GE(TextCoder::Instance().Ucs2ToGsm7bitAuto(pDestText, 1, pMsgText, 1, unknown, codingNational), 0);
96 EXPECT_EQ(TextCoder::Instance().Ucs2ToAscii(pDestText, 0, pMsgText, 0, unknown), -1);
97 EXPECT_GE(TextCoder::Instance().Ucs2ToAscii(pDestText, 1, pMsgText, 1, unknown), 0);
98 EXPECT_EQ(TextCoder::Instance().GetLangType(pMsgText, 0), MSG_DEFAULT_CHAR);
99 EXPECT_GE(TextCoder::Instance().GetLangType(pMsgText, 0), MSG_DEFAULT_CHAR);
100 EXPECT_EQ(TextCoder::Instance().FindGsm7bitExt(pDestText, 0, inText), 0);
101 EXPECT_GE(TextCoder::Instance().FindGsm7bitExt(pDestText, 1, inText), 0);
102 EXPECT_EQ(TextCoder::Instance().FindTurkish(pDestText, 0, inText), 0);
103 EXPECT_GE(TextCoder::Instance().FindTurkish(pDestText, 1, inText), 0);
104 EXPECT_EQ(TextCoder::Instance().FindSpanish(pDestText, 0, inText), 0);
105 EXPECT_GE(TextCoder::Instance().FindSpanish(pDestText, 1, inText), 0);
106 EXPECT_EQ(TextCoder::Instance().FindPortu(pDestText, 0, inText), 0);
107 EXPECT_GE(TextCoder::Instance().FindPortu(pDestText, 1, inText), 0);
108 EXPECT_GE(TextCoder::Instance().FindReplaceChar(inText), MSG_DEFAULT_CHAR);
109 auto extMap = TextCoder::Instance().Get7BitCodingExtMap(codingNational);
110 EXPECT_GE(extMap.size(), 0);
111 }
112
113 /**
114 * @tc.number Telephony_SmsMmsGtest_TextCoder_0002
115 * @tc.name Test TextCoder
116 * @tc.desc Function test
117 */
118 HWTEST_F(BranchUtilsTest, TextCoder_0002, Function | MediumTest | Level1)
119 {
120 MsgLangInfo pLangInfo;
121 pLangInfo.bLockingShift = true;
122 pLangInfo.bSingleShift = true;
123 unsigned char encodeData[BUF_SIZE];
124 unsigned short result = 1;
125 const uint8_t *pMsgText = (const uint8_t *)TEXT_SMS_CONTENT.c_str();
126 uint8_t *pDestText = encodeData;
127 EXPECT_EQ(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 0, pMsgText, 0, pLangInfo), -1);
128 pLangInfo.lockingLang = MSG_ID_TURKISH_LANG;
129 EXPECT_GE(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 1, pMsgText, 1, pLangInfo), 0);
130 EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
131 pLangInfo.lockingLang = MSG_ID_PORTUGUESE_LANG;
132 EXPECT_GE(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 1, pMsgText, 1, pLangInfo), 0);
133 EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
134 pLangInfo.bLockingShift = false;
135 EXPECT_GE(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 1, pMsgText, 1, pLangInfo), 0);
136 EXPECT_EQ(TextCoder::Instance().EscapeTurkishLockingToUcs2(pMsgText, 0, pLangInfo, result), 0);
137 EXPECT_EQ(TextCoder::Instance().EscapeTurkishLockingToUcs2(pMsgText, 1, pLangInfo, result), 0);
138 EXPECT_EQ(TextCoder::Instance().EscapePortuLockingToUcs2(pMsgText, 0, pLangInfo, result), 0);
139 EXPECT_EQ(TextCoder::Instance().EscapePortuLockingToUcs2(pMsgText, 1, pLangInfo, result), 0);
140 EXPECT_EQ(TextCoder::Instance().EscapeGsm7bitToUcs2(pMsgText, 0, pLangInfo, result), 0);
141 EXPECT_EQ(TextCoder::Instance().EscapeGsm7bitToUcs2(pMsgText, 1, pLangInfo, result), 0);
142 pLangInfo.singleLang = MSG_ID_SPANISH_LANG;
143 EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
144 pLangInfo.singleLang = MSG_ID_RESERVED_LANG;
145 EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
146 pLangInfo.bSingleShift = false;
147 EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
148 std::string convertCharseInput = "123ascQ世界!@";
149 std::string convertCharseOUtput = "";
150 std::string convertCharseOUtputContrasts = "123ascQ荳也阜!@";
151 uint32_t valLength = convertCharseInput.length();
152 EXPECT_GE(TextCoder::Instance().GetEncodeString(convertCharseOUtput, 17, valLength, convertCharseInput), true);
153 EXPECT_GE(convertCharseOUtput, convertCharseOUtputContrasts);
154 }
155
156 /**
157 * @tc.number Telephony_SmsMmsGtest_TextCoder_0003
158 * @tc.name Test TextCoder
159 * @tc.desc Function test
160 */
161 HWTEST_F(BranchUtilsTest, TextCoder_0003, Function | MediumTest | Level1)
162 {
163 MSG_LANGUAGE_ID_T langId = 0;
164 MsgLangInfo pLangInfo;
165 pLangInfo.bLockingShift = true;
166 pLangInfo.bSingleShift = true;
167 unsigned char encodeData[BUF_SIZE];
168 unsigned short result = 1;
169 const uint8_t *pMsgText = (const uint8_t *)TEXT_SMS_CONTENT.c_str();
170 uint8_t *pDestText = encodeData;
171 EXPECT_EQ(TextCoder::Instance().Utf8ToGsm7bit(nullptr, 0, nullptr, -1, langId), 0);
172 EXPECT_EQ(TextCoder::Instance().Utf8ToGsm7bit(nullptr, 1, pMsgText, -1, langId), 0);
173 EXPECT_EQ(TextCoder::Instance().Utf8ToGsm7bit(pDestText, 0, pMsgText, 0, langId), 0);
174 EXPECT_EQ(TextCoder::Instance().Utf8ToGsm7bit(pDestText, 0, pMsgText, 1, langId), 0);
175 EXPECT_EQ(TextCoder::Instance().Gsm7bitToUcs2(nullptr, 0, nullptr, -1, pLangInfo), -1);
176 EXPECT_EQ(TextCoder::Instance().Gsm7bitToUcs2(nullptr, 1, pMsgText, -1, pLangInfo), -1);
177 EXPECT_EQ(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 0, pMsgText, 0, pLangInfo), -1);
178 EXPECT_EQ(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 0, pMsgText, 1, pLangInfo), -1);
179 EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(nullptr, 0, nullptr, -1), 0);
180 EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(nullptr, 1, pMsgText, -1), 0);
181 EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(pDestText, 0, pMsgText, 0), 0);
182 EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(pDestText, 0, pMsgText, 1), 0);
183 EXPECT_EQ(TextCoder::Instance().Get7BitCodingExtMap(SMS_CODING_NATIONAL_TYPE_DEFAULT),
184 TextCoder::Instance().gsm7bitExtMap_);
185 EXPECT_EQ(TextCoder::Instance().Get7BitCodingExtMap(SMS_CODING_NATIONAL_TYPE_TURKISH),
186 TextCoder::Instance().turkishMap_);
187 EXPECT_EQ(TextCoder::Instance().Get7BitCodingExtMap(SMS_CODING_NATIONAL_TYPE_SPANISH),
188 TextCoder::Instance().spanishMap_);
189 EXPECT_EQ(TextCoder::Instance().Get7BitCodingExtMap(SMS_CODING_NATIONAL_TYPE_PORTUGUESE),
190 TextCoder::Instance().portuMap_);
191 }
192
193 /**
194 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0001
195 * @tc.name Test SmsCommonUtils
196 * @tc.desc Function test
197 */
198 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0001, Function | MediumTest | Level1)
199 {
200 auto smsCommonUtils = std::make_shared<SmsCommonUtils>();
201 const unsigned char *userData = (const unsigned char *)TEXT_SMS_CONTENT.c_str();
202 const std::string str = "*#PPQQ";
203 const char *digit = (const char *)str.c_str();
204 unsigned char *packData = (unsigned char *)TEXT_SMS_CONTENT.c_str();
205 EXPECT_EQ(smsCommonUtils->Pack7bitChar(nullptr, 0, 0, nullptr, 0), 0);
206 EXPECT_EQ(smsCommonUtils->Unpack7bitChar(nullptr, 1, 1, nullptr, 1), 0);
207 EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, 0, packData, 1), 1);
208 EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, 1, packData, 1), 1);
209 EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, FILL_BITS, packData, 1), 1);
210 EXPECT_EQ(smsCommonUtils->Unpack7bitChar(userData, 1, 0, packData, 1), 0);
211 EXPECT_EQ(smsCommonUtils->Unpack7bitChar(userData, 1, FILL_BITS, packData, 1), 0);
212 EXPECT_EQ(smsCommonUtils->DigitToDtmfChar('*'), static_cast<char>(0x0B));
213 EXPECT_EQ(smsCommonUtils->DtmfCharToDigit(static_cast<char>(0x0B)), '*');
214
215 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
216 uint8_t len;
217 EXPECT_EQ(gsmUtils->DigitToBcd(digit, 1, nullptr, 0, len), 0);
218 EXPECT_EQ(gsmUtils->DigitToBcd(nullptr, 1, packData, 1, len), 0);
219 EXPECT_FALSE(gsmUtils->DigitToBcd(digit, DIGIT_LEN, packData, 1, len));
220 std::string digits;
221 EXPECT_EQ(gsmUtils->BcdToDigit(userData, 1, digits, 1), 0);
222 EXPECT_EQ(gsmUtils->BcdToDigit(nullptr, 1, digits, 1), 0);
223 }
224
225 /**
226 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0002
227 * @tc.name Test SmsCommonUtils DigitToBcd support for +*#
228 * @tc.desc Function test
229 */
230 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0002, Function | MediumTest | Level1)
231 {
232 // input data with pure number
233 const std::string packDataStr = "hello world";
234 const std::string digitTestStr = "17288424569";
235 const char *digit = (const char *)digitTestStr.c_str();
236 unsigned char *packData = (unsigned char *)packDataStr.c_str();
237 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
238 EXPECT_NE(gsmUtils, nullptr);
239 uint8_t len = static_cast<uint8_t>(DIGIT_LEN);
240 EXPECT_FALSE(gsmUtils->DigitToBcd(digit, DIGIT_LEN, packData, DIGIT_LEN, len));
241 EXPECT_FALSE(gsmUtils->DigitToBcd(digit, DIGIT_LEN, packData, DIGIT_LEN - 1, len));
242 len = 0;
243 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
244 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
245 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x71) != userDataVec.end());
246 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x82) != userDataVec.end());
247 }
248
249 /**
250 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0003
251 * @tc.name Test SmsCommonUtils DigitToBcd
252 * @tc.desc Function test
253 */
254 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0003, Function | MediumTest | Level1)
255 {
256 // input data with number and +
257 const std::string packDataStr = "hello world";
258 const std::string digitTestStr = "+17288424569";
259 const char *digit = (const char *)digitTestStr.c_str();
260 unsigned char *packData = (unsigned char *)packDataStr.c_str();
261 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
262 EXPECT_NE(gsmUtils, nullptr);
263 uint8_t len;
264 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
265 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
266 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x78) != userDataVec.end());
267 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x27) != userDataVec.end());
268 }
269
270 /**
271 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0004
272 * @tc.name Test SmsCommonUtils DigitToBcd
273 * @tc.desc Function test
274 */
275 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0004, Function | MediumTest | Level1)
276 {
277 // input data with number and #
278 const std::string packDataStr = "hello world";
279 const std::string digitTestStr = "#17288424569";
280 const char *digit = (const char *)digitTestStr.c_str();
281 unsigned char *packData = (unsigned char *)packDataStr.c_str();
282 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
283 EXPECT_NE(gsmUtils, nullptr);
284 uint8_t len;
285 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
286 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
287 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x1b) != userDataVec.end());
288 }
289
290 /**
291 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0005
292 * @tc.name Test SmsCommonUtils DigitToBcd
293 * @tc.desc Function test
294 */
295 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0005, Function | MediumTest | Level1)
296 {
297 // input data with # and *
298 const std::string packDataStr = "hello world";
299 const std::string digitTestStr = "*0#0765";
300 const char *digit = (const char *)digitTestStr.c_str();
301 unsigned char *packData = (unsigned char *)packDataStr.c_str();
302 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
303 EXPECT_NE(gsmUtils, nullptr);
304 uint8_t len;
305 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
306 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
307 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), HEX_VALUE_0B) != userDataVec.end());
308 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), HEX_VALUE_0A) != userDataVec.end());
309 }
310
311 /**
312 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0006
313 * @tc.name Test SmsCommonUtils DigitToBcd
314 * @tc.desc Function test
315 */
316 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0006, Function | MediumTest | Level1)
317 {
318 // input data with # and *
319 const std::string packDataStr = "hello world";
320 const std::string digitTestStr = "*21#13105550020#";
321 const char *digit = (const char *)digitTestStr.c_str();
322 unsigned char *packData = (unsigned char *)packDataStr.c_str();
323 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
324 EXPECT_NE(gsmUtils, nullptr);
325 uint8_t len;
326 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
327 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
328 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x2a) != userDataVec.end());
329 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0xb1) != userDataVec.end());
330 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x6f) != userDataVec.end());
331 }
332 } // namespace Telephony
333 } // namespace OHOS
334