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 "gsm_sms_common_utils.h"
20 #include "gtest/gtest.h"
21 #include "ims_sms_client.h"
22 #include "sms_common_utils.h"
23 #include "text_coder.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 using namespace testing::ext;
28 
29 namespace {
30 const std::string TEXT_SMS_CONTENT = "hello world";
31 const int BUF_SIZE = 2401;
32 const int TEXT_LENGTH = 2;
33 const int FILL_BITS = 2;
34 const int DIGIT_LEN = 3;
35 const unsigned char SRC_TEXT = 2;
36 } // namespace
37 
38 class BranchUtilsTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp();
43     void TearDown();
44 };
SetUpTestCase()45 void BranchUtilsTest::SetUpTestCase() {}
46 
TearDownTestCase()47 void BranchUtilsTest::TearDownTestCase()
48 {
49     DelayedSingleton<ImsSmsClient>::GetInstance()->UnInit();
50 }
51 
SetUp()52 void BranchUtilsTest::SetUp() {}
53 
TearDown()54 void BranchUtilsTest::TearDown() {}
55 
56 /**
57  * @tc.number   Telephony_SmsMmsGtest_TextCoder_0001
58  * @tc.name     Test TextCoder
59  * @tc.desc     Function test
60  */
61 HWTEST_F(BranchUtilsTest, TextCoder_0001, Function | MediumTest | Level1)
62 {
63     DataCodingScheme DataCodingScheme;
64     MsgLangInfo msgLangInfo;
65     unsigned char encodeData[BUF_SIZE];
66     MSG_LANGUAGE_ID_T langId = 0;
67     bool unknown = false;
68     unsigned short inText = 1;
69     const uint8_t *pMsgText = (const uint8_t *)TEXT_SMS_CONTENT.c_str();
70     uint8_t *pDestText = encodeData;
71     SmsCodingNationalType codingNational = SMS_CODING_NATIONAL_TYPE_DEFAULT;
72     EXPECT_GE(TextCoder::Instance().Utf8ToGsm7bit(pDestText, BUF_SIZE, const_cast<uint8_t *>(pMsgText), 0, langId), 0);
73     EXPECT_GE(TextCoder::Instance().CdmaUtf8ToAuto(pDestText, 1, pMsgText, 1, DataCodingScheme), -1);
74     EXPECT_GE(TextCoder::Instance().GsmUtf8ToAuto(pDestText, 1, pMsgText, 1,
75         DataCodingScheme, codingNational, langId), -1);
76     EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(pDestText, 1, pMsgText, -1), -1);
77     EXPECT_EQ(TextCoder::Instance().Utf8ToUcs2(pDestText, 0, pMsgText, -1), 0);
78     EXPECT_EQ(TextCoder::Instance().CdmaUtf8ToAuto(pDestText, 1, pMsgText, 0, DataCodingScheme), 0);
79     EXPECT_EQ(TextCoder::Instance().GsmUtf8ToAuto(pDestText, 1, pMsgText, 0,
80         DataCodingScheme, codingNational, langId), 0);
81     EXPECT_EQ(TextCoder::Instance().Gsm7bitToUtf8(pDestText, 0, pMsgText, 0, msgLangInfo), 0);
82     EXPECT_GE(TextCoder::Instance().ShiftjisToUtf8(pDestText, 1, pMsgText, -1), 0);
83     EXPECT_GE(TextCoder::Instance().Ucs2ToUtf8(pDestText, 1, pMsgText, -1), 0);
84     EXPECT_GE(TextCoder::Instance().EuckrToUtf8(pDestText, 1, pMsgText, -1), 0);
85     EXPECT_FALSE(TextCoder::Instance().Ucs2ToUtf8(pDestText, 0, pMsgText, 0));
86     EXPECT_FALSE(TextCoder::Instance().EuckrToUtf8(pDestText, 0, pMsgText, 1));
87     EXPECT_FALSE(TextCoder::Instance().ShiftjisToUtf8(pDestText, 0, pMsgText, 1));
88     EXPECT_EQ(TextCoder::Instance().Ucs2ToGsm7bit(pDestText, 0, pMsgText, 0, langId), -1);
89     EXPECT_GT(TextCoder::Instance().Ucs2ToGsm7bit(pDestText, 1, pMsgText, TEXT_LENGTH, langId), 0);
90     EXPECT_EQ(TextCoder::Instance().Ucs2ToGsm7bitAuto(pDestText, 0, pMsgText, 0, unknown, codingNational), -1);
91     EXPECT_GE(TextCoder::Instance().Ucs2ToGsm7bitAuto(pDestText, 1, pMsgText, 1, unknown, codingNational), 0);
92     EXPECT_EQ(TextCoder::Instance().Ucs2ToAscii(pDestText, 0, pMsgText, 0, unknown), -1);
93     EXPECT_GE(TextCoder::Instance().Ucs2ToAscii(pDestText, 1, pMsgText, 1, unknown), 0);
94     EXPECT_EQ(TextCoder::Instance().GetLangType(pMsgText, 0), MSG_DEFAULT_CHAR);
95     EXPECT_GE(TextCoder::Instance().GetLangType(pMsgText, 0), MSG_DEFAULT_CHAR);
96     EXPECT_EQ(TextCoder::Instance().FindGsm7bitExt(pDestText, 0, inText), 0);
97     EXPECT_GE(TextCoder::Instance().FindGsm7bitExt(pDestText, 1, inText), 0);
98     EXPECT_EQ(TextCoder::Instance().FindTurkish(pDestText, 0, inText), 0);
99     EXPECT_GE(TextCoder::Instance().FindTurkish(pDestText, 1, inText), 0);
100     EXPECT_EQ(TextCoder::Instance().FindSpanish(pDestText, 0, inText), 0);
101     EXPECT_GE(TextCoder::Instance().FindSpanish(pDestText, 1, inText), 0);
102     EXPECT_EQ(TextCoder::Instance().FindPortu(pDestText, 0, inText), 0);
103     EXPECT_GE(TextCoder::Instance().FindPortu(pDestText, 1, inText), 0);
104     EXPECT_GE(TextCoder::Instance().FindReplaceChar(inText), MSG_DEFAULT_CHAR);
105     auto extMap = TextCoder::Instance().Get7BitCodingExtMap(codingNational);
106     EXPECT_GE(extMap.size(), 0);
107 }
108 
109 /**
110  * @tc.number   Telephony_SmsMmsGtest_TextCoder_0002
111  * @tc.name     Test TextCoder
112  * @tc.desc     Function test
113  */
114 HWTEST_F(BranchUtilsTest, TextCoder_0002, Function | MediumTest | Level1)
115 {
116     MsgLangInfo pLangInfo;
117     pLangInfo.bLockingShift = true;
118     pLangInfo.bSingleShift = true;
119     unsigned char encodeData[BUF_SIZE];
120     unsigned short result = 1;
121     const uint8_t *pMsgText = (const uint8_t *)TEXT_SMS_CONTENT.c_str();
122     uint8_t *pDestText = encodeData;
123     EXPECT_EQ(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 0, pMsgText, 0, pLangInfo), -1);
124     pLangInfo.lockingLang = MSG_ID_TURKISH_LANG;
125     EXPECT_GE(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 1, pMsgText, 1, pLangInfo), 0);
126     EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
127     pLangInfo.lockingLang = MSG_ID_PORTUGUESE_LANG;
128     EXPECT_GE(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 1, pMsgText, 1, pLangInfo), 0);
129     EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
130     pLangInfo.bLockingShift = false;
131     EXPECT_GE(TextCoder::Instance().Gsm7bitToUcs2(pDestText, 1, pMsgText, 1, pLangInfo), 0);
132     EXPECT_EQ(TextCoder::Instance().EscapeTurkishLockingToUcs2(pMsgText, 0, pLangInfo, result), 0);
133     EXPECT_EQ(TextCoder::Instance().EscapeTurkishLockingToUcs2(pMsgText, 1, pLangInfo, result), 0);
134     EXPECT_EQ(TextCoder::Instance().EscapePortuLockingToUcs2(pMsgText, 0, pLangInfo, result), 0);
135     EXPECT_EQ(TextCoder::Instance().EscapePortuLockingToUcs2(pMsgText, 1, pLangInfo, result), 0);
136     EXPECT_EQ(TextCoder::Instance().EscapeGsm7bitToUcs2(pMsgText, 0, pLangInfo, result), 0);
137     EXPECT_EQ(TextCoder::Instance().EscapeGsm7bitToUcs2(pMsgText, 1, pLangInfo, result), 0);
138     pLangInfo.singleLang = MSG_ID_SPANISH_LANG;
139     EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
140     pLangInfo.singleLang = MSG_ID_RESERVED_LANG;
141     EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
142     pLangInfo.bSingleShift = false;
143     EXPECT_GT(TextCoder::Instance().EscapeToUcs2(SRC_TEXT, pLangInfo), 0);
144 }
145 
146 /**
147  * @tc.number   Telephony_SmsMmsGtest_SmsCommonUtils_0001
148  * @tc.name     Test SmsCommonUtils
149  * @tc.desc     Function test
150  */
151 HWTEST_F(BranchUtilsTest, SmsCommonUtils_0001, Function | MediumTest | Level1)
152 {
153     auto smsCommonUtils = std::make_shared<SmsCommonUtils>();
154     const unsigned char *userData = (const unsigned char *)TEXT_SMS_CONTENT.c_str();
155     const std::string str = "*#PPQQ";
156     const char *digit = (const char *)str.c_str();
157     unsigned char *packData = (unsigned char *)TEXT_SMS_CONTENT.c_str();
158     EXPECT_EQ(smsCommonUtils->Pack7bitChar(nullptr, 0, 0, nullptr, 0), 0);
159     EXPECT_EQ(smsCommonUtils->Unpack7bitChar(nullptr, 1, 1, nullptr, 1), 0);
160     EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, 0, packData, 1), 1);
161     EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, 1, packData, 1), 1);
162     EXPECT_EQ(smsCommonUtils->Pack7bitChar(userData, 1, FILL_BITS, packData, 1), 1);
163     EXPECT_EQ(smsCommonUtils->Unpack7bitChar(userData, 1, 0, packData, 1), 0);
164     EXPECT_EQ(smsCommonUtils->Unpack7bitChar(userData, 1, FILL_BITS, packData, 1), 0);
165     EXPECT_EQ(smsCommonUtils->DigitToDtmfChar('*'), static_cast<char>(0x0B));
166     EXPECT_EQ(smsCommonUtils->DtmfCharToDigit(static_cast<char>(0x0B)), '*');
167 
168     auto gsmUtils = std::make_shared<GsmSmsCommonUtils>();
169     uint8_t len;
170     EXPECT_EQ(gsmUtils->DigitToBcd(digit, 1, nullptr, 0, len), 0);
171     EXPECT_EQ(gsmUtils->DigitToBcd(nullptr, 1, packData, 1, len), 0);
172     EXPECT_FALSE(gsmUtils->DigitToBcd(digit, DIGIT_LEN, packData, 1, len));
173     std::string digits;
174     EXPECT_EQ(gsmUtils->BcdToDigit(userData, 1, digits, 1), 0);
175     EXPECT_EQ(gsmUtils->BcdToDigit(nullptr, 1, digits, 1), 0);
176 }
177 } // namespace Telephony
178 } // namespace OHOS
179