• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         .markType = MarkType::MARK_TYPE_NONE,
90         .markContent = "",
91         .markCount = 0,
92         .markSource = "",
93         .isCloud = false,
94     };
95     bool isBlock = false;
96     int32_t blockReason = 0;
97     spamCallAdapter_->ParseDetectResult(jsonData, isBlock, info, blockReason);
98     ASSERT_TRUE(isBlock);
99     ASSERT_EQ(blockReason, 1);
100 }
101 
102 /**
103  * @tc.number   Telephony_CallbackStubHelper_001
104  * @tc.name     test error branch
105  * @tc.desc     Function test
106  */
107 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_001, Function | MediumTest | Level1)
108 {
109     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
110 
111     CallbackStubHelper callbackStubHelper(spamCallAdapter);
112     int32_t errCode = 0;
113     std::string result = "{\"detectResult\":0,\"decisionReason\":1002,\"markType\":0}";
114     ASSERT_EQ(callbackStubHelper.OnResult(errCode, result), TELEPHONY_SUCCESS);
115     ReminderCallbackStubHelper reminderCallbackStubHelper(spamCallAdapter);
116     result = "{\"reminderResult\":false,\"slotId\":0,\"reminderTime\":1736428340229,\"remindedTimes\":0}";
117     ASSERT_EQ(reminderCallbackStubHelper.OnResult(errCode, result), TELEPHONY_SUCCESS);
118 }
119 
120 /**
121  * @tc.number   Telephony_CallbackStubHelper_002
122  * @tc.name     test error branch
123  * @tc.desc     Function test
124  */
125 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_002, Function | MediumTest | Level1)
126 {
127     std::shared_ptr<SpamCallAdapter> spamCallAdapter;
128 
129     CallbackStubHelper callbackStubHelper(spamCallAdapter);
130     int32_t errCode = 0;
131     std::string result;
132     ASSERT_NE(callbackStubHelper.OnResult(errCode, result), 0);
133     ReminderCallbackStubHelper reminderCallbackStubHelper(spamCallAdapter);
134     ASSERT_EQ(reminderCallbackStubHelper.OnResult(errCode, result), 0);
135 }
136 
137 /**
138  * @tc.number   Telephony_CallbackStubHelper_003
139  * @tc.name     test error branch
140  * @tc.desc     Function test
141  */
142 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_003, Function | MediumTest | Level1)
143 {
144     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
145 
146     CallbackStubHelper callbackStubHelper(spamCallAdapter);
147     MessageParcel data;
148     MessageParcel reply;
149     MessageOption option;
150     int32_t errCode = 0;
151     std::string result;
152     int32_t res = 0;
153     res = callbackStubHelper.OnRemoteRequest(errCode, data, reply, option);
154     ASSERT_NE(res, ERR_NONE);
155 }
156 
157 /**
158  * @tc.number   Telephony_SpamCallConnection_001
159  * @tc.name     test error branch
160  * @tc.desc     Function test
161  */
162 HWTEST_F(SpamCallTest, Telephony_SpamCallConnection_001, Function | MediumTest | Level1)
163 {
164     std::string phoneNumber = "123456789012";
165     int32_t slotId = 0;
166     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
167     ASSERT_NE(spamCallAdapter, nullptr);
168     SpamCallConnection spamCallConnection(phoneNumber, slotId, spamCallAdapter);
169     std::string bundle = "111";
170     std::string ability = "222";
171     AppExecFwk::ElementName element("", bundle, ability);
172     sptr<IRemoteObject> remoteObject;
173     int resultCode = 0;
174     spamCallConnection.OnAbilityConnectDone(element, remoteObject, resultCode);
175     ASSERT_EQ(resultCode, 0);
176     spamCallConnection.OnAbilityDisconnectDone(element, resultCode);
177     ASSERT_EQ(resultCode, 0);
178 }
179 
180 /**
181  * @tc.number   Telephony_TimeWaitHelper_001
182  * @tc.name     test error branch
183  * @tc.desc     Function test
184  */
185 HWTEST_F(SpamCallTest, Telephony_TimeWaitHelper_001, Function | MediumTest | Level1)
186 {
187     TimeWaitHelper timeWaitHelper(10);
188     timeWaitHelper.NotifyAll();
189     ASSERT_NE(timeWaitHelper.WaitForResult(), true);
190 }
191 
192 /**
193  * @tc.number   Telephony_SpamCallProxy_001
194  * @tc.name     test error branch
195  * @tc.desc     Function test
196  */
197 HWTEST_F(SpamCallTest, Telephony_SpamCallProxy_001, Function | MediumTest | Level1)
198 {
199     sptr<IRemoteObject> remoteObject;
200     SpamCallProxy spamCallProxy(remoteObject);
201     std::string phoneNumber = "123456789012";
202     int32_t slotId = 0;
203     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
204     ASSERT_NE(spamCallProxy.DetectSpamCall(phoneNumber, slotId, spamCallAdapter), 0);
205     ASSERT_NE(spamCallProxy.RequireCallReminder(slotId, spamCallAdapter), 0);
206 }
207 
208 /**
209  * @tc.number   Telephony_CallAbilityConnection_001
210  * @tc.name     test error branch
211  * @tc.desc     Function test
212  */
213 HWTEST_F(SpamCallTest, Telephony_CallAbilityConnection_001, Function | MediumTest | Level1)
214 {
215     std::string commandStr = "11111";
216     CallAbilityConnection callAbilityConnection(commandStr);
217     std::string bundle = "111";
218     std::string ability = "222";
219     AppExecFwk::ElementName element("", bundle, ability);
220     int resultCode = 0;
221     callAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
222     ASSERT_EQ(resultCode, 0);
223 }
224 
225 /**
226  * @tc.number   Telephony_CallSettingAbilityConnection_001
227  * @tc.name     test error branch
228  * @tc.desc     Function test
229  */
230 HWTEST_F(SpamCallTest, Telephony_CallSettingAbilityConnection_001, Function | MediumTest | Level1)
231 {
232     std::string commandStr = "11111";
233     CallSettingAbilityConnection callSettingAbilityConnection(commandStr);
234     std::string bundle = "111";
235     std::string ability = "222";
236     AppExecFwk::ElementName element("", bundle, ability);
237     int resultCode = 0;
238     callSettingAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
239     ASSERT_EQ(resultCode, 0);
240 }
241 
242 }
243 }
244