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 #define private public
16 #define protected public
17
18 #include "state_registry_branch_test.h"
19
20 #include "core_service_client.h"
21 #include "sim_state_type.h"
22 #include "state_registry_test.h"
23 #include "telephony_ext_wrapper.h"
24 #include "telephony_log_wrapper.h"
25 #include "telephony_observer_client.h"
26 #include "telephony_observer_proxy.h"
27 #include "telephony_state_manager.h"
28 #include "telephony_state_registry_client.h"
29 #include "telephony_state_registry_proxy.h"
30 #include "telephony_state_registry_service.h"
31
32 namespace OHOS {
33 namespace Telephony {
34 using namespace testing::ext;
35 static constexpr int32_t DATA_STATE_CONNECTING = 1;
36 static constexpr int32_t NETWORK_TYPE_GSM = 1;
37 static constexpr int32_t DATA_FLOW_TYPE_DOWN = 1;
38 static constexpr int32_t PROFILE_STATE_DISCONNECTING = 3;
39 class StateRegistryBranchTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp();
44 void TearDown();
45 };
46
SetUpTestCase(void)47 void StateRegistryBranchTest::SetUpTestCase(void)
48 {
49 ASSERT_TRUE(CoreServiceClient::GetInstance().GetProxy() != nullptr);
50 }
51
TearDownTestCase(void)52 void StateRegistryBranchTest::TearDownTestCase(void)
53 {
54 }
55
SetUp(void)56 void StateRegistryBranchTest::SetUp(void) {}
57
TearDown(void)58 void StateRegistryBranchTest::TearDown(void) {}
59
60 class TestIRemoteObject : public IRemoteObject {
61 public:
62 uint32_t requestCode_ = -1;
63 int32_t result_ = 0;
64
65 public:
TestIRemoteObject()66 TestIRemoteObject() : IRemoteObject(u"test_remote_object") {}
67
~TestIRemoteObject()68 ~TestIRemoteObject() {}
69
70 int32_t GetObjectRefCount() override
71 {
72 return 0;
73 }
74
75 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
76 {
77 TELEPHONY_LOGI("Mock SendRequest");
78 requestCode_ = code;
79 reply.WriteInt32(result_);
80 return 0;
81 }
82
83 bool IsProxyObject() const override
84 {
85 return true;
86 }
87
88 bool CheckObjectLegality() const override
89 {
90 return true;
91 }
92
93 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override
94 {
95 return true;
96 }
97
98 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override
99 {
100 return true;
101 }
102
103 bool Marshalling(Parcel &parcel) const override
104 {
105 return true;
106 }
107
108 sptr<IRemoteBroker> AsInterface() override
109 {
110 return nullptr;
111 }
112
113 int Dump(int fd, const std::vector<std::u16string> &args) override
114 {
115 return 0;
116 }
117
GetObjectDescriptor() const118 std::u16string GetObjectDescriptor() const
119 {
120 std::u16string descriptor = std::u16string();
121 return descriptor;
122 }
123 };
124
125 /**
126 * @tc.number StateRegistry_Notify_001
127 * @tc.name Get System Services
128 * @tc.desc Function test
129 */
HWTEST_F(StateRegistryBranchTest, StateRegistry_Notify_001, Function | MediumTest | Level0)130 HWTEST_F(StateRegistryBranchTest, StateRegistry_Notify_001, Function | MediumTest | Level0)
131 {
132 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
133 ASSERT_NE(sam, nullptr);
134 sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID);
135 ASSERT_NE(obj, nullptr);
136 std::shared_ptr<OHOS::Telephony::TelephonyObserverProxy> telephonyObserverProxy =
137 std::make_shared<OHOS::Telephony::TelephonyObserverProxy>(obj);
138 CardType type = CardType::UNKNOWN_CARD;
139 SimState state = SimState::SIM_STATE_UNKNOWN;
140 LockReason reason = LockReason::SIM_NONE;
141 telephonyObserverProxy->OnSimStateUpdated(0, type, state, reason);
142 std::vector<sptr<CellInformation>> vec;
143 std::unique_ptr<GsmCellInformation> gsmCell = std::make_unique<GsmCellInformation>();
144 vec.push_back(gsmCell.release());
145 telephonyObserverProxy->OnCellInfoUpdated(0, vec);
146 std::unique_ptr<NetworkState> networkState = std::make_unique<NetworkState>();
147 telephonyObserverProxy->OnNetworkStateUpdated(0, networkState.release());
148 telephonyObserverProxy->OnCellularDataConnectStateUpdated(0, DATA_STATE_CONNECTING, NETWORK_TYPE_GSM);
149 telephonyObserverProxy->OnCellularDataFlowUpdated(0, DATA_FLOW_TYPE_DOWN);
150 telephonyObserverProxy->OnCfuIndicatorUpdated(0, true);
151 telephonyObserverProxy->OnVoiceMailMsgIndicatorUpdated(0, true);
152 ASSERT_NE(telephonyObserverProxy, nullptr);
153 }
154
155 /**
156 * @tc.number TelephonyStateRegistryService_OnStart_001
157 * @tc.name Get System Services
158 * @tc.desc Function test
159 */
HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_OnStart_001, Function | MediumTest | Level0)160 HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_OnStart_001, Function | MediumTest | Level0)
161 {
162 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
163 ASSERT_NE(service, nullptr);
164 service->state_ = ServiceRunningState::STATE_RUNNING;
165 service->OnStart();
166 ASSERT_EQ(static_cast<int32_t>(service->state_), static_cast<int32_t>(ServiceRunningState::STATE_RUNNING));
167 }
168
169 /**
170 * @tc.number TelephonyStateRegistryService_OnStart_002
171 * @tc.name Get System Services
172 * @tc.desc Function test
173 */
HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_OnStart_002, Function | MediumTest | Level0)174 HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_OnStart_002, Function | MediumTest | Level0)
175 {
176 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
177 ASSERT_NE(service, nullptr);
178 service->OnStart();
179 service->OnStop();
180 ASSERT_EQ(static_cast<int32_t>(service->state_), static_cast<int32_t>(ServiceRunningState::STATE_STOPPED));
181 }
182
183 /**
184 * @tc.number TelephonyStateRegistryService_UpdateIccAccount_001
185 * @tc.name Get System Services
186 * @tc.desc Function test
187 */
HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_UpdateIccAccount_001, Function | MediumTest | Level0)188 HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_UpdateIccAccount_001, Function | MediumTest | Level0)
189 {
190 AccessToken token;
191 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
192 ASSERT_NE(service, nullptr);
193 TelephonyStateRegistryRecord record;
194 service->stateRecords_.push_back(record);
195 service->stateRecords_[0].mask_ = TelephonyObserverBroker::OBSERVER_MASK_ICC_ACCOUNT;
196 service->stateRecords_[0].telephonyObserver_ = nullptr;
197 auto result = service->UpdateIccAccount();
198 ASSERT_EQ(result, TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST);
199 }
200
201 /**
202 * @tc.number TelephonyStateRegistryService_UpdateIccAccount_002
203 * @tc.name Get System Services
204 * @tc.desc Function test
205 */
HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_UpdateIccAccount_002, Function | MediumTest | Level0)206 HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_UpdateIccAccount_002, Function | MediumTest | Level0)
207 {
208 AccessToken token;
209 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
210 ASSERT_NE(service, nullptr);
211 TelephonyStateRegistryRecord record;
212 service->stateRecords_.push_back(record);
213 service->stateRecords_[0].mask_ = TelephonyObserverBroker::OBSERVER_MASK_ICC_ACCOUNT;
214 service->stateRecords_[0].telephonyObserver_ = std::make_unique<TelephonyObserver>().release();
215 auto result = service->UpdateIccAccount();
216 ASSERT_EQ(result, TELEPHONY_SUCCESS);
217 }
218
219 /**
220 * @tc.number TelephonyStateRegistryService_UpdateIccAccount_003
221 * @tc.name Get System Services
222 * @tc.desc Function test
223 */
HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_UpdateIccAccount_003, Function | MediumTest | Level0)224 HWTEST_F(StateRegistryBranchTest, TelephonyStateRegistryService_UpdateIccAccount_003, Function | MediumTest | Level0)
225 {
226 AccessToken token;
227 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
228 ASSERT_NE(service, nullptr);
229 TelephonyStateRegistryRecord record;
230 service->stateRecords_.push_back(record);
231 service->stateRecords_[0].mask_ = TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE;
232 auto result = service->UpdateIccAccount();
233 ASSERT_EQ(result, TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST);
234 }
235
236 /**
237 * @tc.number TelephonyStateRegistryService_CheckCallerIsSystemApp_001
238 * @tc.name Get System Services
239 * @tc.desc Function test
240 */
HWTEST_F(StateRegistryBranchTest, Service_CheckCallerIsSystemApp_001, Function | MediumTest | Level0)241 HWTEST_F(StateRegistryBranchTest, Service_CheckCallerIsSystemApp_001, Function | MediumTest | Level0)
242 {
243 SecurityToken token;
244 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
245 ASSERT_NE(service, nullptr);
246 bool result = service->CheckCallerIsSystemApp(TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO);
247 ASSERT_EQ(result, false);
248 result = service->CheckCallerIsSystemApp(TelephonyObserverBroker::OBSERVER_MASK_CFU_INDICATOR);
249 ASSERT_EQ(result, false);
250 result = service->CheckCallerIsSystemApp(TelephonyObserverBroker::OBSERVER_MASK_VOICE_MAIL_MSG_INDICATOR);
251 ASSERT_EQ(result, false);
252 pid_t pid = -1;
253 auto ret = service->UnregisterStateChange(0, TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO, 0, pid);
254 ASSERT_EQ(ret, TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API);
255 }
256
257 /**
258 * @tc.number TelephonyStateRegistryService_UnregisterStateChange_001
259 * @tc.name Get System Services
260 * @tc.desc Function test
261 */
HWTEST_F(StateRegistryBranchTest, Service_UnregisterStateChange_001, Function | MediumTest | Level0)262 HWTEST_F(StateRegistryBranchTest, Service_UnregisterStateChange_001, Function | MediumTest | Level0)
263 {
264 AccessToken token;
265 auto service = DelayedSingleton<TelephonyStateRegistryService>::GetInstance();
266 ASSERT_NE(service, nullptr);
267 TelephonyStateRegistryRecord record;
268 service->stateRecords_.push_back(record);
269 service->stateRecords_[0].slotId_ = 1;
270 pid_t pid = -1;
271 auto result = service->UnregisterStateChange(0, TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO, 0, pid);
272 ASSERT_EQ(result, TELEPHONY_STATE_UNREGISTRY_DATA_NOT_EXIST);
273 service->stateRecords_[0].slotId_ = 0;
274 service->stateRecords_[0].mask_ = TelephonyObserverBroker::OBSERVER_MASK_DATA_FLOW;
275 result = service->UnregisterStateChange(0, TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO, 0, pid);
276 ASSERT_EQ(result, TELEPHONY_STATE_UNREGISTRY_DATA_NOT_EXIST);
277 service->stateRecords_[0].mask_ = TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO;
278 service->stateRecords_[0].tokenId_ = 1234;
279 result = service->UnregisterStateChange(0, TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO, 0, pid);
280 ASSERT_EQ(result, TELEPHONY_STATE_UNREGISTRY_DATA_NOT_EXIST);
281 service->stateRecords_[0].pid_ = 1;
282 result = service->UnregisterStateChange(0, TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO, 1234, pid);
283 ASSERT_EQ(result, TELEPHONY_STATE_UNREGISTRY_DATA_NOT_EXIST);
284 }
285
286 /**
287 * @tc.number TelephonyStateRegistryService_UnregisterStateChange_001
288 * @tc.name Get System Services
289 * @tc.desc Function test
290 */
HWTEST_F(StateRegistryBranchTest, UpdateCellularDataConnectState_001, Function | MediumTest | Level0)291 HWTEST_F(StateRegistryBranchTest, UpdateCellularDataConnectState_001, Function | MediumTest | Level0)
292 {
293 sptr<TestIRemoteObject> remote = new (std::nothrow) TestIRemoteObject();
294 std::shared_ptr<TelephonyStateRegistryProxy> proxy = std::make_shared<TelephonyStateRegistryProxy>(remote);
295 auto result = proxy->UpdateCellularDataConnectState(0, PROFILE_STATE_DISCONNECTING, 0);
296 ASSERT_EQ(result, TELEPHONY_SUCCESS);
297 result = proxy->UpdateCellularDataFlow(0, 0);
298 ASSERT_EQ(result, TELEPHONY_SUCCESS);
299 int32_t callState = 16;
300 std::string phoneNumber("137xxxxxxxx");
301 std::u16string number = Str8ToStr16(phoneNumber);
302 result = proxy->UpdateCallState(callState, number);
303 ASSERT_EQ(result, TELEPHONY_SUCCESS);
304 result = proxy->UpdateCallStateForSlotId(0, callState, number);
305 ASSERT_EQ(result, TELEPHONY_SUCCESS);
306 std::vector<sptr<SignalInformation>> vec;
307 std::unique_ptr<SignalInformation> signal = std::make_unique<GsmSignalInformation>();
308 ASSERT_TRUE(signal != nullptr);
309 vec.push_back(signal.release());
310 result = proxy->UpdateSignalInfo(0, vec);
311 ASSERT_EQ(result, TELEPHONY_SUCCESS);
312 std::vector<sptr<CellInformation>> cellInfoList;
313 sptr<CellInformation> gsmCellInformation = new GsmCellInformation();
314 cellInfoList.push_back(gsmCellInformation);
315 result = proxy->UpdateCellInfo(0, cellInfoList);
316 ASSERT_EQ(result, TELEPHONY_SUCCESS);
317 sptr<NetworkState> networkState = std::make_unique<NetworkState>().release();
318 result = proxy->UpdateNetworkState(0, networkState);
319 ASSERT_EQ(result, TELEPHONY_SUCCESS);
320 CardType type = CardType::UNKNOWN_CARD;
321 SimState state = SimState::SIM_STATE_UNKNOWN;
322 LockReason reason = LockReason::SIM_NONE;
323 result = proxy->UpdateSimState(0, type, state, reason);
324 ASSERT_EQ(result, TELEPHONY_SUCCESS);
325 result = proxy->UnregisterStateChange(0, TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO);
326 ASSERT_EQ(result, TELEPHONY_SUCCESS);
327 result = proxy->UpdateCfuIndicator(0, true);
328 ASSERT_EQ(result, TELEPHONY_SUCCESS);
329 result = proxy->UpdateVoiceMailMsgIndicator(0, true);
330 ASSERT_EQ(result, TELEPHONY_SUCCESS);
331 result = proxy->UpdateIccAccount();
332 ASSERT_EQ(result, TELEPHONY_SUCCESS);
333 }
334 } // namespace Telephony
335 } // namespace OHOS