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 #define private public
18 #define protected public
19 #include "cb_start_ability.h"
20 #include "os_account.h"
21 #include "ios_account.h"
22 #include "gsm_sms_param_codec.h"
23 #include "gsm_pdu_hex_value.h"
24 #include "gsm_sms_common_utils.h"
25 #include "gsm_sms_message.h"
26 #include "gsm_sms_receive_handler.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 using namespace testing::ext;
31 
32 static constexpr uint8_t MAX_GSM_7BIT_DATA_LEN = 160;
33 
34 class SmsGsmTest : public testing::Test {
35 public:
SetUpTestCase()36     static void SetUpTestCase()
37     {}
TearDownTestCase()38     static void TearDownTestCase()
39     {}
SetUp()40     void SetUp()
41     {}
TearDown()42     void TearDown()
43     {}
44 };
45 
46 /**
47  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_001
48  * @tc.name     SmsGsmTest_001
49  * @tc.desc     Function test
50  */
HWTEST_F(SmsGsmTest, SmsGsmTest_001, Function | MediumTest | Level1)51 HWTEST_F(SmsGsmTest, SmsGsmTest_001, Function | MediumTest | Level1)
52 {
53     AAFwk::Want want;
54     DelayedSingleton<CbStartAbility>::GetInstance()->StartAbility(want);
55     EXPECT_EQ(want.GetModuleName(), "entry");
56 }
57 
58 /**
59  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_002
60  * @tc.name     SmsGsmTest_002
61  * @tc.desc     Function test
62  */
HWTEST_F(SmsGsmTest, SmsGsmTest_002, Function | MediumTest | Level1)63 HWTEST_F(SmsGsmTest, SmsGsmTest_002, Function | MediumTest | Level1)
64 {
65     std::shared_ptr<GsmSmsParamCodec> codec = std::make_shared<GsmSmsParamCodec>();
66 
67     string strBuff;
68     int32_t setType = 0;
69     int32_t indType = 0;
70     strBuff += HEX_VALUE_0D;
71     SmsReadBuffer smsReadBuff(strBuff);
72     EXPECT_FALSE(codec->CheckVoicemail(smsReadBuff, &setType, &indType));
73 }
74 
75 /**
76  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_003
77  * @tc.name     SmsGsmTest_003
78  * @tc.desc     Function test
79  */
HWTEST_F(SmsGsmTest, SmsGsmTest_003, Function | MediumTest | Level1)80 HWTEST_F(SmsGsmTest, SmsGsmTest_003, Function | MediumTest | Level1)
81 {
82     std::shared_ptr<GsmSmsParamCodec> codec = std::make_shared<GsmSmsParamCodec>();
83 
84     string strBuff;
85     int32_t setType = 0;
86     int32_t indType = 0;
87     strBuff += HEX_VALUE_04;
88     SmsReadBuffer smsReadBuff(strBuff);
89     EXPECT_FALSE(codec->CheckVoicemail(smsReadBuff, &setType, &indType));
90 }
91 
92 /**
93  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_004
94  * @tc.name     SmsGsmTest_004
95  * @tc.desc     Function test
96  */
HWTEST_F(SmsGsmTest, SmsGsmTest_004, Function | MediumTest | Level1)97 HWTEST_F(SmsGsmTest, SmsGsmTest_004, Function | MediumTest | Level1)
98 {
99     std::shared_ptr<GsmSmsParamCodec> codec = std::make_shared<GsmSmsParamCodec>();
100 
101     string strBuff;
102     int32_t setType = 0;
103     int32_t indType = 0;
104     strBuff += HEX_VALUE_04;
105     strBuff += HEX_VALUE_04;
106     SmsReadBuffer smsReadBuff(strBuff);
107     EXPECT_FALSE(codec->CheckVoicemail(smsReadBuff, &setType, &indType));
108 }
109 
110 /**
111  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_005
112  * @tc.name     SmsGsmTest_005
113  * @tc.desc     Function test
114  */
HWTEST_F(SmsGsmTest, SmsGsmTest_005, Function | MediumTest | Level1)115 HWTEST_F(SmsGsmTest, SmsGsmTest_005, Function | MediumTest | Level1)
116 {
117     std::shared_ptr<GsmSmsParamCodec> codec = std::make_shared<GsmSmsParamCodec>();
118 
119     string strBuff;
120     int32_t setType = 0;
121     int32_t indType = 0;
122     strBuff += HEX_VALUE_04;
123     strBuff += HEX_VALUE_D0;
124     SmsReadBuffer smsReadBuff(strBuff);
125     EXPECT_FALSE(codec->CheckVoicemail(smsReadBuff, &setType, &indType));
126 }
127 
128 
129 /**
130  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_006
131  * @tc.name     SmsGsmTest_006
132  * @tc.desc     Function test
133  */
HWTEST_F(SmsGsmTest, SmsGsmTest_006, Function | MediumTest | Level1)134 HWTEST_F(SmsGsmTest, SmsGsmTest_006, Function | MediumTest | Level1)
135 {
136     std::shared_ptr<GsmSmsParamCodec> codec = std::make_shared<GsmSmsParamCodec>();
137     string strBuff;
138     int32_t setType = 0;
139     int32_t indType = 0;
140     strBuff += HEX_VALUE_04;
141     strBuff += HEX_VALUE_D0;
142     strBuff += HEX_VALUE_D0;
143     SmsReadBuffer smsReadBuff(strBuff);
144     EXPECT_FALSE(codec->CheckVoicemail(smsReadBuff, &setType, &indType));
145 }
146 
147 /**
148  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_007
149  * @tc.name     SmsGsmTest_007
150  * @tc.desc     Function test
151  */
HWTEST_F(SmsGsmTest, SmsGsmTest_007, Function | MediumTest | Level1)152 HWTEST_F(SmsGsmTest, SmsGsmTest_007, Function | MediumTest | Level1)
153 {
154     std::shared_ptr<GsmSmsParamCodec> codec = std::make_shared<GsmSmsParamCodec>();
155 
156     string strBuff;
157     int32_t setType = 0;
158     int32_t indType = 0;
159     strBuff += HEX_VALUE_04;
160     strBuff += HEX_VALUE_D0;
161     strBuff += HEX_VALUE_11;
162     SmsReadBuffer smsReadBuff(strBuff);
163     EXPECT_FALSE(codec->CheckVoicemail(smsReadBuff, &setType, &indType));
164 }
165 
166 /**
167  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_008
168  * @tc.name     SmsGsmTest_008
169  * @tc.desc     Function test
170  */
HWTEST_F(SmsGsmTest, SmsGsmTest_008, Function | MediumTest | Level1)171 HWTEST_F(SmsGsmTest, SmsGsmTest_008, Function | MediumTest | Level1)
172 {
173     std::shared_ptr<GsmSmsParamCodec> codec = std::make_shared<GsmSmsParamCodec>();
174 
175     string strBuff;
176     int32_t setType = 0;
177     int32_t indType = 0;
178     strBuff += HEX_VALUE_04;
179     strBuff += HEX_VALUE_D0;
180     strBuff += HEX_VALUE_10;
181     strBuff += HEX_VALUE_10;
182     SmsReadBuffer smsReadBuff(strBuff);
183     EXPECT_TRUE(codec->CheckVoicemail(smsReadBuff, &setType, &indType));
184 }
185 
186 /**
187  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_009
188  * @tc.name     SmsGsmTest_009
189  * @tc.desc     Function test
190  */
HWTEST_F(SmsGsmTest, SmsGsmTest_009, Function | MediumTest | Level1)191 HWTEST_F(SmsGsmTest, SmsGsmTest_009, Function | MediumTest | Level1)
192 {
193     std::shared_ptr<GsmSmsCommonUtils> utils = std::make_shared<GsmSmsCommonUtils>();
194     SmsWriteBuffer buffer;
195     EXPECT_FALSE(utils->Pack7bitChar(buffer, nullptr, 0, 0));
196 }
197 
198 /**
199  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_010
200  * @tc.name     SmsGsmTest_010
201  * @tc.desc     Function test
202  */
HWTEST_F(SmsGsmTest, SmsGsmTest_010, Function | MediumTest | Level1)203 HWTEST_F(SmsGsmTest, SmsGsmTest_010, Function | MediumTest | Level1)
204 {
205     std::shared_ptr<GsmSmsCommonUtils> utils = std::make_shared<GsmSmsCommonUtils>();
206     std::vector<uint8_t> vectData;
207     uint8_t fillBits = 1;
208 
209     SmsWriteBuffer buffer;
210     EXPECT_FALSE(utils->Pack7bitChar(buffer, static_cast<const uint8_t*>(&vectData[0]), 0, fillBits));
211 }
212 
213 /**
214  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_011
215  * @tc.name     SmsGsmTest_011
216  * @tc.desc     Function test
217  */
HWTEST_F(SmsGsmTest, SmsGsmTest_011, Function | MediumTest | Level1)218 HWTEST_F(SmsGsmTest, SmsGsmTest_011, Function | MediumTest | Level1)
219 {
220     std::shared_ptr<GsmSmsCommonUtils> utils = std::make_shared<GsmSmsCommonUtils>();
221     std::vector<uint8_t> vectData;
222     uint8_t fillBits = 1;
223     vectData.push_back(1);
224     vectData.push_back(2);
225     vectData.push_back(3);
226     vectData.push_back(4);
227     SmsWriteBuffer buffer;
228     EXPECT_TRUE(utils->Pack7bitChar(buffer, static_cast<const uint8_t*>(&vectData[0]), vectData.size(), fillBits));
229 
230     vectData.clear();
231     vectData.assign(MAX_GSM_7BIT_DATA_LEN + 1, 1); // first branch
232     EXPECT_FALSE(utils->Pack7bitChar(buffer, static_cast<const uint8_t*>(&vectData[0]), vectData.size(), fillBits));
233     vectData.clear();
234     EXPECT_TRUE(utils->Pack7bitChar(buffer, static_cast<const uint8_t*>(&vectData[0]), vectData.size(), fillBits));
235 }
236 
237 /**
238  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_012
239  * @tc.name     SmsGsmTest_012
240  * @tc.desc     Function test
241  */
HWTEST_F(SmsGsmTest, SmsGsmTest_012, Function | MediumTest | Level1)242 HWTEST_F(SmsGsmTest, SmsGsmTest_012, Function | MediumTest | Level1)
243 {
244     std::shared_ptr<GsmSmsCommonUtils> utils = std::make_shared<GsmSmsCommonUtils>();
245     std::string strBuffer;
246     uint8_t fillBits = 0;
247     uint8_t dstIdx = 0;
248 
249     SmsReadBuffer buffer(strBuffer);
250     EXPECT_FALSE(utils->Unpack7bitChar(buffer, strBuffer.size(), fillBits,  nullptr, 0, dstIdx));
251     fillBits = 8;
252     EXPECT_FALSE(utils->Unpack7bitChar(buffer, strBuffer.size(), fillBits,  nullptr, 0, dstIdx));
253 }
254 
255 /**
256  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_013
257  * @tc.name     SmsGsmTest_013
258  * @tc.desc     Function test
259  */
HWTEST_F(SmsGsmTest, SmsGsmTest_013, Function | MediumTest | Level1)260 HWTEST_F(SmsGsmTest, SmsGsmTest_013, Function | MediumTest | Level1)
261 {
262     std::shared_ptr<GsmSmsCommonUtils> utils = std::make_shared<GsmSmsCommonUtils>();
263     std::string strBuffer;
264     std::vector<uint8_t> vectData;
265     uint8_t fillBits = 1;
266     uint8_t dstIdx = 0;
267 
268     SmsReadBuffer buffer(strBuffer);
269     EXPECT_FALSE(utils->Unpack7bitChar(buffer, strBuffer.size(), fillBits,
270         static_cast<uint8_t*>(&vectData[0]), vectData.size(), dstIdx));
271 }
272 
273 /**
274  * @tc.number   Telephony_SmsMmsGtest_SmsGsmTest_014
275  * @tc.name     SmsGsmTest_014
276  * @tc.desc     Function test Unpack7bitCharForMiddlePart
277  */
HWTEST_F(SmsGsmTest, SmsGsmTest_014, Function | MediumTest | Level1)278 HWTEST_F(SmsGsmTest, SmsGsmTest_014, Function | MediumTest | Level1)
279 {
280     std::shared_ptr<GsmSmsCommonUtils> utils = std::make_shared<GsmSmsCommonUtils>();
281     std::string dataStr("1141515");
282     std::vector<uint8_t> vectData;
283 
284     const uint8_t *buffer = reinterpret_cast<const uint8_t *>(dataStr.c_str());
285     EXPECT_FALSE(utils->Unpack7bitCharForMiddlePart(nullptr, 0, static_cast<uint8_t*>(&vectData[0])));
286     EXPECT_FALSE(utils->Unpack7bitCharForMiddlePart(buffer, dataStr.size(), nullptr));
287     EXPECT_FALSE(utils->Unpack7bitCharForMiddlePart(buffer, 0, static_cast<uint8_t*>(&vectData[0])));
288 }
289 }
290 }