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 */
HWTEST_F(BranchUtilsTest, TextCoder_0001, Function | MediumTest | Level1)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 */
HWTEST_F(BranchUtilsTest, TextCoder_0002, Function | MediumTest | Level1)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 }
149
150 /**
151 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0001
152 * @tc.name Test SmsCommonUtils
153 * @tc.desc Function test
154 */
HWTEST_F(BranchUtilsTest, SmsCommonUtils_0001, Function | MediumTest | Level1)155 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0001, Function | MediumTest | Level1)
156 {
157 auto smsCommonUtils = std::make_shared<SmsCommonUtils>();
158 const unsigned char *userData = (const unsigned char *)TEXT_SMS_CONTENT.c_str();
159 const std::string str = "*#PPQQ";
160 const char *digit = (const char *)str.c_str();
161 unsigned char *packData = (unsigned char *)TEXT_SMS_CONTENT.c_str();
162 EXPECT_EQ(smsCommonUtils->Pack7bitChar(nullptr, 0, 0, nullptr, 0), 0);
163 EXPECT_EQ(smsCommonUtils->Unpack7bitChar(nullptr, 1, 1, nullptr, 1), 0);
164 EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, 0, packData, 1), 1);
165 EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, 1, packData, 1), 1);
166 EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, FILL_BITS, packData, 1), 1);
167 EXPECT_EQ(smsCommonUtils->Unpack7bitChar(userData, 1, 0, packData, 1), 0);
168 EXPECT_EQ(smsCommonUtils->Unpack7bitChar(userData, 1, FILL_BITS, packData, 1), 0);
169 EXPECT_EQ(smsCommonUtils->DigitToDtmfChar('*'), static_cast<char>(0x0B));
170 EXPECT_EQ(smsCommonUtils->DtmfCharToDigit(static_cast<char>(0x0B)), '*');
171
172 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
173 uint8_t len;
174 EXPECT_EQ(gsmUtils->DigitToBcd(digit, 1, nullptr, 0, len), 0);
175 EXPECT_EQ(gsmUtils->DigitToBcd(nullptr, 1, packData, 1, len), 0);
176 EXPECT_FALSE(gsmUtils->DigitToBcd(digit, DIGIT_LEN, packData, 1, len));
177 std::string digits;
178 EXPECT_EQ(gsmUtils->BcdToDigit(userData, 1, digits, 1), 0);
179 EXPECT_EQ(gsmUtils->BcdToDigit(nullptr, 1, digits, 1), 0);
180 }
181
182 /**
183 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0002
184 * @tc.name Test SmsCommonUtils DigitToBcd support for +*#
185 * @tc.desc Function test
186 */
HWTEST_F(BranchUtilsTest, SmsCommonUtils_0002, Function | MediumTest | Level1)187 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0002, Function | MediumTest | Level1)
188 {
189 // input data with pure number
190 const std::string packDataStr = "hello world";
191 const std::string digitTestStr = "17288424569";
192 const char *digit = (const char *)digitTestStr.c_str();
193 unsigned char *packData = (unsigned char *)packDataStr.c_str();
194 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
195 EXPECT_NE(gsmUtils, nullptr);
196 uint8_t len = static_cast<uint8_t>(DIGIT_LEN);
197 EXPECT_FALSE(gsmUtils->DigitToBcd(digit, DIGIT_LEN, packData, DIGIT_LEN, len));
198 EXPECT_FALSE(gsmUtils->DigitToBcd(digit, DIGIT_LEN, packData, DIGIT_LEN - 1, len));
199 len = 0;
200 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
201 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
202 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x71) != userDataVec.end());
203 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x82) != userDataVec.end());
204 }
205
206 /**
207 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0003
208 * @tc.name Test SmsCommonUtils DigitToBcd
209 * @tc.desc Function test
210 */
HWTEST_F(BranchUtilsTest, SmsCommonUtils_0003, Function | MediumTest | Level1)211 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0003, Function | MediumTest | Level1)
212 {
213 // input data with number and +
214 const std::string packDataStr = "hello world";
215 const std::string digitTestStr = "+17288424569";
216 const char *digit = (const char *)digitTestStr.c_str();
217 unsigned char *packData = (unsigned char *)packDataStr.c_str();
218 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
219 EXPECT_NE(gsmUtils, nullptr);
220 uint8_t len;
221 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
222 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
223 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x78) != userDataVec.end());
224 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x27) != userDataVec.end());
225 }
226
227 /**
228 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0004
229 * @tc.name Test SmsCommonUtils DigitToBcd
230 * @tc.desc Function test
231 */
HWTEST_F(BranchUtilsTest, SmsCommonUtils_0004, Function | MediumTest | Level1)232 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0004, Function | MediumTest | Level1)
233 {
234 // input data with number and #
235 const std::string packDataStr = "hello world";
236 const std::string digitTestStr = "#17288424569";
237 const char *digit = (const char *)digitTestStr.c_str();
238 unsigned char *packData = (unsigned char *)packDataStr.c_str();
239 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
240 EXPECT_NE(gsmUtils, nullptr);
241 uint8_t len;
242 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
243 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
244 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x1b) != userDataVec.end());
245 }
246
247 /**
248 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0005
249 * @tc.name Test SmsCommonUtils DigitToBcd
250 * @tc.desc Function test
251 */
HWTEST_F(BranchUtilsTest, SmsCommonUtils_0005, Function | MediumTest | Level1)252 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0005, Function | MediumTest | Level1)
253 {
254 // input data with # and *
255 const std::string packDataStr = "hello world";
256 const std::string digitTestStr = "*0#0765";
257 const char *digit = (const char *)digitTestStr.c_str();
258 unsigned char *packData = (unsigned char *)packDataStr.c_str();
259 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
260 EXPECT_NE(gsmUtils, nullptr);
261 uint8_t len;
262 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
263 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
264 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), HEX_VALUE_0B) != userDataVec.end());
265 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), HEX_VALUE_0A) != userDataVec.end());
266 }
267
268 /**
269 * @tc.number Telephony_SmsMmsGtest_SmsCommonUtils_0006
270 * @tc.name Test SmsCommonUtils DigitToBcd
271 * @tc.desc Function test
272 */
HWTEST_F(BranchUtilsTest, SmsCommonUtils_0006, Function | MediumTest | Level1)273 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0006, Function | MediumTest | Level1)
274 {
275 // input data with # and *
276 const std::string packDataStr = "hello world";
277 const std::string digitTestStr = "*21#13105550020#";
278 const char *digit = (const char *)digitTestStr.c_str();
279 unsigned char *packData = (unsigned char *)packDataStr.c_str();
280 auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
281 EXPECT_NE(gsmUtils, nullptr);
282 uint8_t len;
283 EXPECT_TRUE(gsmUtils->DigitToBcd(digit, DIGIT_LEN2, packData, DIGIT_LEN2, len));
284 std::vector<uint8_t> userDataVec(packData, packData + DIGIT_LEN2);
285 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x2a) != userDataVec.end());
286 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0xb1) != userDataVec.end());
287 EXPECT_TRUE(std::find(userDataVec.begin(), userDataVec.end(), 0x6f) != userDataVec.end());
288 }
289 } // namespace Telephony
290 } // namespace OHOS
291