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 "call_manager_info.h"
17 #include <memory>
18 #define PRIVATE public
19 #define PROTECTED public
20 #include "gtest/gtest.h"
21 #include "spam_call_adapter.h"
22 #include "callback_stub_helper.h"
23 #include "spam_call_connection.h"
24 #include "spam_call_stub.h"
25 #include "time_wait_helper.h"
26 #include "spam_call_proxy.h"
27 #include "call_ability_connection.h"
28 #include "call_setting_ability_connection.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 using namespace testing::ext;
33 
34 class SpamCallTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 };
SetUpTestCase()41 void SpamCallTest::SetUpTestCase() {}
42 
TearDownTestCase()43 void SpamCallTest::TearDownTestCase() {}
44 
SetUp()45 void SpamCallTest::SetUp() {}
46 
TearDown()47 void SpamCallTest::TearDown() {}
48 
49 /**
50  * @tc.number   Telephony_SpamCallAdapter_001
51  * @tc.name     test error branch
52  * @tc.desc     Function test
53  */
54 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_001, Function | MediumTest | Level1)
55 {
56     std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
57     int32_t errCode = -1;
58     std::string result = "";
59     spamCallAdapter_->GetDetectResult(errCode, result);
60     ASSERT_EQ(errCode, -1);
61     ASSERT_EQ(result, "");
62 }
63 
64 /**
65  * @tc.number   Telephony_SpamCallAdapter_002
66  * @tc.name     test error branch
67  * @tc.desc     Function test
68  */
69 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_002, Function | MediumTest | Level1)
70 {
71     std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
72     const std::string phoneNumber = "12345678900";
73     const int32_t slotId = 0;
74     ASSERT_FALSE(spamCallAdapter_->DetectSpamCall(phoneNumber, slotId));
75 }
76 
77 /**
78  * @tc.number   Telephony_SpamCallAdapter_003
79  * @tc.name     test error branch
80  * @tc.desc     Function test
81  */
82 HWTEST_F(SpamCallTest, Telephony_SpamCallAdapter_003, Function | MediumTest | Level1)
83 {
84     std::shared_ptr<SpamCallAdapter> spamCallAdapter_ = std::make_shared<SpamCallAdapter>();
85     std::string jsonData = "{\"detectResult\":1,\"decisionReason\":1}";
86     NumberMarkInfo info = {
87         .markType = MarkType::MARK_TYPE_NONE,
88         .markContent = "",
89         .markCount = 0,
90         .markSource = "",
91         .isCloud = false,
92     };
93     bool isBlock = false;
94     int32_t blockReason = 0;
95     spamCallAdapter_->ParseDetectResult(jsonData, isBlock, info, blockReason);
96     ASSERT_TRUE(isBlock);
97     ASSERT_EQ(blockReason, 1);
98 }
99 
100 /**
101  * @tc.number   Telephony_CallbackStubHelper_001
102  * @tc.name     test error branch
103  * @tc.desc     Function test
104  */
105 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_001, Function | MediumTest | Level1)
106 {
107     std::shared_ptr<SpamCallAdapter> spamCallAdapter = std::make_shared<SpamCallAdapter>();
108 
109     CallbackStubHelper callbackStubHelper(spamCallAdapter);
110     int32_t errCode = 0;
111     std::string result;
112     int32_t res = 0;
113     res = callbackStubHelper.OnResult(errCode, result);
114     ASSERT_EQ(res, 0);
115 }
116 
117 /**
118  * @tc.number   Telephony_CallbackStubHelper_002
119  * @tc.name     test error branch
120  * @tc.desc     Function test
121  */
122 HWTEST_F(SpamCallTest, Telephony_CallbackStubHelper_002, Function | MediumTest | Level1)
123 {
124     std::shared_ptr<SpamCallAdapter> spamCallAdapter;
125 
126     CallbackStubHelper callbackStubHelper(spamCallAdapter);
127     int32_t errCode = 0;
128     std::string result;
129     ASSERT_NE(callbackStubHelper.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(10);
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 }
201 
202 /**
203  * @tc.number   Telephony_CallAbilityConnection_001
204  * @tc.name     test error branch
205  * @tc.desc     Function test
206  */
207 HWTEST_F(SpamCallTest, Telephony_CallAbilityConnection_001, Function | MediumTest | Level1)
208 {
209     std::string commandStr = "11111";
210     CallAbilityConnection callAbilityConnection(commandStr);
211     std::string bundle = "111";
212     std::string ability = "222";
213     AppExecFwk::ElementName element("", bundle, ability);
214     int resultCode = 0;
215     callAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
216     ASSERT_EQ(resultCode, 0);
217 }
218 
219 /**
220  * @tc.number   Telephony_CallSettingAbilityConnection_001
221  * @tc.name     test error branch
222  * @tc.desc     Function test
223  */
224 HWTEST_F(SpamCallTest, Telephony_CallSettingAbilityConnection_001, Function | MediumTest | Level1)
225 {
226     std::string commandStr = "11111";
227     CallSettingAbilityConnection callSettingAbilityConnection(commandStr);
228     std::string bundle = "111";
229     std::string ability = "222";
230     AppExecFwk::ElementName element("", bundle, ability);
231     int resultCode = 0;
232     callSettingAbilityConnection.OnAbilityDisconnectDone(element, resultCode);
233     ASSERT_EQ(resultCode, 0);
234 }
235 
236 }
237 }
238