1 /*
2 * Copyright (C) 2024-2025 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 "call_manager_info.h"
17 #include "call_manager_errors.h"
18 #include <memory>
19 #define PRIVATE public
20 #define PROTECTED public
21 #include "gtest/gtest.h"
22 #include "spam_call_adapter.h"
23 #include "callback_stub_helper.h"
24 #include "reminder_callback_stub_helper.h"
25 #include "spam_call_connection.h"
26 #include "spam_call_stub.h"
27 #include "time_wait_helper.h"
28 #include "spam_call_proxy.h"
29 #include "call_ability_connection.h"
30 #include "call_setting_ability_connection.h"
31
32 namespace OHOS {
33 namespace Telephony {
34 using namespace testing::ext;
35
36 class SpamCallTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
SetUpTestCase()43 void SpamCallTest::SetUpTestCase() {}
44
TearDownTestCase()45 void SpamCallTest::TearDownTestCase() {}
46
SetUp()47 void SpamCallTest::SetUp() {}
48
TearDown()49 void SpamCallTest::TearDown() {}
50
51 /**
52 * @tc.number Telephony_SpamCallAdapter_001
53 * @tc.name test error branch
54 * @tc.desc Function test
55 */
56 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_001, Function | MediumTest | Level1)
57 {
58 std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
59 int32_t errCode = -1;
60 std::string result = "";
61 spamCallAdapter_->GetDetectResult(errCode, result);
62 ASSERT_EQ(errCode, -1);
63 ASSERT_EQ(result, "");
64 }
65
66 /**
67 * @tc.number Telephony_SpamCallAdapter_002
68 * @tc.name test error branch
69 * @tc.desc Function test
70 */
71 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_002, Function | MediumTest | Level1)
72 {
73 std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
74 const std::string phoneNumber = "12345678900";
75 const int32_t slotId = 0;
76 ASSERT_FALSE(spamCallAdapter_->DetectSpamCall(phoneNumber, slotId));
77 }
78
79 /**
80 * @tc.number Telephony_SpamCallAdapter_003
81 * @tc.name test error branch
82 * @tc.desc Function test
83 */
84 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_003, Function | MediumTest | Level1)
85 {
86 std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
87 std::string jsonData = "{\"detectResult\":1,\"decisionReason\":1}";
88 NumberMarkInfo info;
89 bool isBlock = false;
90 int32_t blockReason = 0;
91 std::string detectDetails = "";
92 spamCallAdapter_->ParseDetectResult(jsonData, isBlock, info, blockReason, detectDetails);
93 ASSERT_TRUE(isBlock);
94 ASSERT_EQ(blockReason, 1);
95 }
96
97 /**
98 * @tc.number Telephony_CallbackStubHelper_001
99 * @tc.name test error branch
100 * @tc.desc Function test
101 */
102 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_001, Function | MediumTest | Level1)
103 {
104 std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
105
106 CallbackStubHelper callbackStubHelper(spamCallAdapter);
107 int32_t errCode = 0;
108 std::string result = "{\"detectResult\":0,\"decisionReason\":1002,\"markType\":0}";
109 ASSERT_EQ(callbackStubHelper.OnResult(errCode, result), TELEPHONY_SUCCESS);
110 ReminderCallbackStubHelper reminderCallbackStubHelper(spamCallAdapter);
111 result = "{\"reminderResult\":false,\"slotId\":0,\"reminderTime\":1736428340229,\"remindedTimes\":0}";
112 ASSERT_EQ(reminderCallbackStubHelper.OnResult(errCode, result), TELEPHONY_SUCCESS);
113 }
114
115 /**
116 * @tc.number Telephony_CallbackStubHelper_002
117 * @tc.name test error branch
118 * @tc.desc Function test
119 */
120 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_002, Function | MediumTest | Level1)
121 {
122 std::shared_ptr<SpamCallAdapter> spamCallAdapter;
123
124 CallbackStubHelper callbackStubHelper(spamCallAdapter);
125 int32_t errCode = 0;
126 std::string result;
127 ASSERT_NE(callbackStubHelper.OnResult(errCode, result), 0);
128 ReminderCallbackStubHelper reminderCallbackStubHelper(spamCallAdapter);
129 ASSERT_EQ(reminderCallbackStubHelper.OnResult(errCode, result), 0);
130 }
131
132 /**
133 * @tc.number Telephony_CallbackStubHelper_003
134 * @tc.name test error branch
135 * @tc.desc Function test
136 */
137 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_003, Function | MediumTest | Level1)
138 {
139 std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
140
141 CallbackStubHelper callbackStubHelper(spamCallAdapter);
142 MessageParcel data;
143 MessageParcel reply;
144 MessageOption option;
145 int32_t errCode = 0;
146 std::string result;
147 int32_t res = 0;
148 res = callbackStubHelper.OnRemoteRequest(errCode, data, reply, option);
149 ASSERT_NE(res, ERR_NONE);
150 }
151
152 /**
153 * @tc.number Telephony_SpamCallConnection_001
154 * @tc.name test error branch
155 * @tc.desc Function test
156 */
157 HWTEST_F(SpamCallTest, Telephony_SpamCallConnection_001, Function | MediumTest | Level1)
158 {
159 std::string phoneNumber = "123456789012";
160 int32_t slotId = 0;
161 std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
162 ASSERT_NE(spamCallAdapter, nullptr);
163 SpamCallConnection spamCallConnection(phoneNumber, slotId, spamCallAdapter);
164 std::string bundle = "111";
165 std::string ability = "222";
166 AppExecFwk::ElementName element("", bundle, ability);
167 sptr<IRemoteObject> remoteObject;
168 int resultCode = 0;
169 spamCallConnection.OnAbilityConnectDone(element, remoteObject, resultCode);
170 ASSERT_EQ(resultCode, 0);
171 spamCallConnection.OnAbilityDisconnectDone(element, resultCode);
172 ASSERT_EQ(resultCode, 0);
173 }
174
175 /**
176 * @tc.number Telephony_TimeWaitHelper_001
177 * @tc.name test error branch
178 * @tc.desc Function test
179 */
180 HWTEST_F(SpamCallTest, Telephony_TimeWaitHelper_001, Function | MediumTest | Level1)
181 {
182 TimeWaitHelper timeWaitHelper(std::chrono::milliseconds(10000));
183 timeWaitHelper.NotifyAll();
184 ASSERT_NE(timeWaitHelper.WaitForResult(), true);
185 }
186
187 /**
188 * @tc.number Telephony_SpamCallProxy_001
189 * @tc.name test error branch
190 * @tc.desc Function test
191 */
192 HWTEST_F(SpamCallTest, Telephony_SpamCallProxy_001, Function | MediumTest | Level1)
193 {
194 sptr<IRemoteObject> remoteObject;
195 SpamCallProxy spamCallProxy(remoteObject);
196 std::string phoneNumber = "123456789012";
197 int32_t slotId = 0;
198 std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
199 ASSERT_NE(spamCallProxy.DetectSpamCall(phoneNumber, slotId, spamCallAdapter), 0);
200 ASSERT_NE(spamCallProxy.RequireCallReminder(slotId, spamCallAdapter), 0);
201 }
202
203 /**
204 * @tc.number Telephony_CallAbilityConnection_001
205 * @tc.name test error branch
206 * @tc.desc Function test
207 */
208 HWTEST_F(SpamCallTest, Telephony_CallAbilityConnection_001, Function | MediumTest | Level1)
209 {
210 std::string commandStr = "11111";
211 CallAbilityConnection callAbilityConnection(commandStr);
212 std::string bundle = "111";
213 std::string ability = "222";
214 AppExecFwk::ElementName element("", bundle, ability);
215 int resultCode = 0;
216 callAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
217 ASSERT_EQ(resultCode, 0);
218 }
219
220 /**
221 * @tc.number Telephony_CallSettingAbilityConnection_001
222 * @tc.name test error branch
223 * @tc.desc Function test
224 */
225 HWTEST_F(SpamCallTest, Telephony_CallSettingAbilityConnection_001, Function | MediumTest | Level1)
226 {
227 std::string commandStr = "11111";
228 CallSettingAbilityConnection callSettingAbilityConnection(commandStr);
229 std::string bundle = "111";
230 std::string ability = "222";
231 AppExecFwk::ElementName element("", bundle, ability);
232 int resultCode = 0;
233 callSettingAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
234 ASSERT_EQ(resultCode, 0);
235 }
236
237 }
238 }
239