1 /*
2 * Copyright (C) 2022 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 <fstream>
20 #include "gtest/gtest.h"
21 #include "cellular_call_supplement.h"
22 #include "telephony_ext_wrapper.h"
23 #include "ims_control.h"
24
25 namespace OHOS {
26 namespace Telephony {
27 using namespace testing::ext;
28 class CellularCallSupplementTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp();
33 void TearDown();
34 };
35
IsEmptyMmiResultFilteredMock(int slotId)36 bool IsEmptyMmiResultFilteredMock(int slotId)
37 {
38 return true;
39 }
40
IsEmptyMmiResultFilteredMockFalse(int slotId)41 bool IsEmptyMmiResultFilteredMockFalse(int slotId)
42 {
43 return false;
44 }
45
IsMmiCodeMock(int32_t slotId,std::string & number)46 bool IsMmiCodeMock(int32_t slotId, std::string &number)
47 {
48 return true;
49 }
50
IsMmiCodeMockFalse(int32_t slotId,std::string & number)51 bool IsMmiCodeMockFalse(int32_t slotId, std::string &number)
52 {
53 return false;
54 }
55
IsUssdReleaseMsgFilteredMock(int32_t slotId)56 bool IsUssdReleaseMsgFilteredMock(int32_t slotId)
57 {
58 return true;
59 }
60
IsUssdReleaseMsgFilteredMockFalse(int32_t slotId)61 bool IsUssdReleaseMsgFilteredMockFalse(int32_t slotId)
62 {
63 return false;
64 }
65
UnescapeHtmlCodeMock(int32_t slotId,std::string & content)66 void UnescapeHtmlCodeMock(int32_t slotId, std::string &content)
67 {}
68
RenameFile(std::string from,std::string to)69 static void RenameFile(std::string from, std::string to)
70 {
71 std::fstream fs;
72 fs.open(from.c_str());
73 if (fs.fail()) {
74 fs.close();
75 return;
76 }
77 fs.close();
78 rename(from.c_str(), to.c_str());
79 }
80
SetUpTestCase()81 void CellularCallSupplementTest::SetUpTestCase()
82 {}
83
TearDownTestCase()84 void CellularCallSupplementTest::TearDownTestCase()
85 {}
86
SetUp()87 void CellularCallSupplementTest::SetUp()
88 {}
89
TearDown()90 void CellularCallSupplementTest::TearDown()
91 {}
92
93 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_001, Function | MediumTest | Level1)
94 {
95 TELEPHONY_EXT_WRAPPER.isEmptyMmiResultFiltered_ = IsEmptyMmiResultFilteredMock;
96 UssdNoticeInfo ussdNoticeInfo;
97 ussdNoticeInfo.str = "";
98 ussdNoticeInfo.m = 1;
99 CellularCallSupplement callSup;
100 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
101 }
102
103 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_002, Function | MediumTest | Level1)
104 {
105 TELEPHONY_EXT_WRAPPER.isEmptyMmiResultFiltered_ = IsEmptyMmiResultFilteredMock;
106 UssdNoticeInfo ussdNoticeInfo;
107 ussdNoticeInfo.str = "abc";
108 ussdNoticeInfo.m = 1;
109 CellularCallSupplement callSup;
110 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
111 }
112
113 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_003, Function | MediumTest | Level1)
114 {
115 TELEPHONY_EXT_WRAPPER.isEmptyMmiResultFiltered_ = IsEmptyMmiResultFilteredMockFalse;
116 UssdNoticeInfo ussdNoticeInfo;
117 ussdNoticeInfo.str = "";
118 ussdNoticeInfo.m = 1;
119 CellularCallSupplement callSup;
120 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
121 }
122
123 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_004, Function | MediumTest | Level1)
124 {
125 TELEPHONY_EXT_WRAPPER.isEmptyMmiResultFiltered_ = nullptr;
126 UssdNoticeInfo ussdNoticeInfo;
127 ussdNoticeInfo.str = "";
128 ussdNoticeInfo.m = 1;
129 CellularCallSupplement callSup;
130 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
131 }
132
133 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_005, Function | MediumTest | Level1)
134 {
135 TELEPHONY_EXT_WRAPPER.isUssdReleaseMsgFiltered_ = IsUssdReleaseMsgFilteredMock;
136 UssdNoticeInfo ussdNoticeInfo;
137 ussdNoticeInfo.str = "abc";
138 ussdNoticeInfo.m = 2;
139 CellularCallSupplement callSup;
140 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
141 }
142
143 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_006, Function | MediumTest | Level1)
144 {
145 TELEPHONY_EXT_WRAPPER.isUssdReleaseMsgFiltered_ = IsUssdReleaseMsgFilteredMockFalse;
146 UssdNoticeInfo ussdNoticeInfo;
147 ussdNoticeInfo.str = "abc";
148 ussdNoticeInfo.m = 2;
149 CellularCallSupplement callSup;
150 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
151 }
152
153 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_007, Function | MediumTest | Level1)
154 {
155 TELEPHONY_EXT_WRAPPER.isUssdReleaseMsgFiltered_ = nullptr;
156 UssdNoticeInfo ussdNoticeInfo;
157 ussdNoticeInfo.str = "abc";
158 ussdNoticeInfo.m = 2;
159 CellularCallSupplement callSup;
160 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
161 }
162
163 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_008, Function | MediumTest | Level1)
164 {
165 TELEPHONY_EXT_WRAPPER.unescapeHtmlCode_ = UnescapeHtmlCodeMock;
166 UssdNoticeInfo ussdNoticeInfo;
167 ussdNoticeInfo.str = "&";
168 ussdNoticeInfo.m = 2;
169 CellularCallSupplement callSup;
170 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
171 }
172
173 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_009, Function | MediumTest | Level1)
174 {
175 TELEPHONY_EXT_WRAPPER.unescapeHtmlCode_ = nullptr;
176 UssdNoticeInfo ussdNoticeInfo;
177 ussdNoticeInfo.str = "&";
178 ussdNoticeInfo.m = 2;
179 CellularCallSupplement callSup;
180 EXPECT_NO_FATAL_FAILURE(callSup.EventUssdNotify(ussdNoticeInfo, 0));
181 }
182
183 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_010, Function | MediumTest | Level1)
184 {
185 TELEPHONY_EXT_WRAPPER.isMmiCode_ = IsMmiCodeMock;
186 IMSControl control;
187 std::string phoneNum = "*9";
188 CLIRMode mode = CLIRMode::DEFAULT;
189 bool isNeed = control.IsNeedExecuteMMI(0, phoneNum, mode, true);
190 EXPECT_FALSE(isNeed);
191 }
192
193 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_011, Function | MediumTest | Level1)
194 {
195 TELEPHONY_EXT_WRAPPER.isMmiCode_ = nullptr;
196 IMSControl control;
197 std::string phoneNum = "*9";
198 CLIRMode mode = CLIRMode::DEFAULT;
199 bool isNeed = control.IsNeedExecuteMMI(0, phoneNum, mode, true);
200 EXPECT_FALSE(isNeed);
201 }
202
203 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_012, Function | MediumTest | Level1)
204 {
205 TELEPHONY_EXT_WRAPPER.isMmiCode_ = IsMmiCodeMockFalse;
206 IMSControl control;
207 std::string phoneNum = "*100#";
208 CLIRMode mode = CLIRMode::DEFAULT;
209 bool isNeed = control.IsNeedExecuteMMI(0, phoneNum, mode, true);
210 EXPECT_FALSE(isNeed);
211 }
212
213 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_013, Function | MediumTest | Level1)
214 {
215 TELEPHONY_EXT_WRAPPER.InitTelephonyExtWrapper();
216 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.isEmptyMmiResultFiltered_ != nullptr);
217 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.isMmiCode_ != nullptr);
218 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.isUssdReleaseMsgFiltered_ != nullptr);
219 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.unescapeHtmlCode_ != nullptr);
220 TELEPHONY_EXT_WRAPPER.~TelephonyExtWrapper();
221 TELEPHONY_EXT_WRAPPER.InitTelephonyExtWrapper();
222 }
223
224 HWTEST_F(CellularCallSupplementTest, CellularCallSupplementTest_014, Function | MediumTest | Level1)
225 {
226 std::string filePath = "/system/lib64/libtelephony_ext_service.z.so";
227 std::string bakFilePath = "/system/lib64/libtelephony_ext_service.z.so.bak";
228 RenameFile(filePath, bakFilePath);
229 TELEPHONY_EXT_WRAPPER.InitTelephonyExtWrapper();
230 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.isEmptyMmiResultFiltered_ == nullptr);
231 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.isMmiCode_ == nullptr);
232 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.isUssdReleaseMsgFiltered_ == nullptr);
233 EXPECT_TRUE(TELEPHONY_EXT_WRAPPER.unescapeHtmlCode_ == nullptr);
234 TELEPHONY_EXT_WRAPPER.~TelephonyExtWrapper();
235 RenameFile(bakFilePath, filePath);
236 TELEPHONY_EXT_WRAPPER.InitTelephonyExtWrapper();
237 }
238
239 } // namespace Telephony
240 } // namespace OHOS