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 #ifdef FEATURE_NETWORK_SUPPORT
17 #include "network_ability_stub_test.h"
18
19 #include "ipc_types.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22
23 #include "common_utils.h"
24 #include "constant_definition.h"
25 #include "location_log.h"
26 #include "network_ability_proxy.h"
27 #include "network_callback_host.h"
28 #include "subability_common.h"
29 #include "locationhub_ipc_interface_code.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace Location {
36 const int UNKNOWN_CODE = -1;
37 const int ERR_CODE = 100;
38 const int DEFAULT_STATUS = 0;
SetUp()39 void NetworkAbilityStubTest::SetUp()
40 {
41 }
42
TearDown()43 void NetworkAbilityStubTest::TearDown()
44 {
45 }
46
47 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest001, TestSize.Level1)
48 {
49 GTEST_LOG_(INFO)
50 << "NetworkAbilityStubTest, NetworkAbilityStubTest001, TestSize.Level1";
51 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest001 begin");
52 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
53 EXPECT_CALL(*networkAbilityStub, SendMessage(_, _, _)).WillOnce(DoAll(Return()));
54 MessageParcel parcel;
55 parcel.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
56 MessageParcel reply;
57 MessageOption option;
58 EXPECT_EQ(ERRCODE_SUCCESS,
59 networkAbilityStub->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SEND_LOCATION_REQUEST),
60 parcel, reply, option));
61 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest001 end");
62 }
63
64 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest002, TestSize.Level1)
65 {
66 GTEST_LOG_(INFO)
67 << "NetworkAbilityStubTest, NetworkAbilityStubTest002, TestSize.Level1";
68 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest002 begin");
69 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
70 EXPECT_CALL(*networkAbilityStub, SetEnable(_)).WillOnce(DoAll(Return(ERRCODE_SUCCESS)));
71 MessageParcel parcel;
72 parcel.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
73 MessageParcel reply;
74 MessageOption option;
75 EXPECT_EQ(ERRCODE_SUCCESS,
76 networkAbilityStub->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_ENABLE),
77 parcel, reply, option));
78 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest002 end");
79 }
80
81 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest003, TestSize.Level1)
82 {
83 GTEST_LOG_(INFO)
84 << "NetworkAbilityStubTest, NetworkAbilityStubTest003, TestSize.Level1";
85 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest003 begin");
86 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
87 EXPECT_CALL(*networkAbilityStub, SendMessage(_, _, _)).WillOnce(DoAll(Return()));
88 MessageParcel parcel;
89 parcel.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
90 MessageParcel reply;
91 MessageOption option;
92 EXPECT_EQ(ERRCODE_SUCCESS,
93 networkAbilityStub->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SELF_REQUEST),
94 parcel, reply, option));
95 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest003 end");
96 }
97
98 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest004, TestSize.Level1)
99 {
100 GTEST_LOG_(INFO)
101 << "NetworkAbilityStubTest, NetworkAbilityStubTest004, TestSize.Level1";
102 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest004 begin");
103 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
104 EXPECT_CALL(*networkAbilityStub, EnableMock()).WillOnce(DoAll(Return(ERRCODE_SUCCESS)));
105 MessageParcel parcel;
106 parcel.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
107 MessageParcel reply;
108 MessageOption option;
109 EXPECT_EQ(ERRCODE_SUCCESS,
110 networkAbilityStub->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::ENABLE_LOCATION_MOCK),
111 parcel, reply, option));
112 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest004 end");
113 }
114
115 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest005, TestSize.Level1)
116 {
117 GTEST_LOG_(INFO)
118 << "NetworkAbilityStubTest, NetworkAbilityStubTest005, TestSize.Level1";
119 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest005 begin");
120 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
121 EXPECT_CALL(*networkAbilityStub, DisableMock()).WillOnce(DoAll(Return(ERRCODE_SUCCESS)));
122 MessageParcel parcel;
123 parcel.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
124 MessageParcel reply;
125 MessageOption option;
126 EXPECT_EQ(ERRCODE_SUCCESS,
127 networkAbilityStub->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::DISABLE_LOCATION_MOCK),
128 parcel, reply, option));
129 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest005 end");
130 }
131
132 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest006, TestSize.Level1)
133 {
134 GTEST_LOG_(INFO)
135 << "NetworkAbilityStubTest, NetworkAbilityStubTest006, TestSize.Level1";
136 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest006 begin");
137 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
138 EXPECT_CALL(*networkAbilityStub, SendMessage(_, _, _)).WillOnce(DoAll(Return()));
139 MessageParcel parcel;
140 parcel.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
141 MessageParcel reply;
142 MessageOption option;
143 EXPECT_EQ(ERRCODE_SUCCESS,
144 networkAbilityStub->OnRemoteRequest(static_cast<uint32_t>(NetworkInterfaceCode::SET_MOCKED_LOCATIONS),
145 parcel, reply, option));
146 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest006 end");
147 }
148
149 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest007, TestSize.Level1)
150 {
151 GTEST_LOG_(INFO)
152 << "NetworkAbilityStubTest, NetworkAbilityStubTest007, TestSize.Level1";
153 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest007 begin");
154 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
155 MessageParcel parcel;
156 parcel.WriteInterfaceToken(NetworkAbilityProxy::GetDescriptor());
157 MessageParcel reply;
158 MessageOption option;
159 EXPECT_EQ(IPC_STUB_UNKNOW_TRANS_ERR,
160 networkAbilityStub->OnRemoteRequest(UNKNOWN_CODE, parcel, reply, option));
161 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest007 end");
162 }
163
164 HWTEST_F(NetworkAbilityStubTest, NetworkAbilityStubTest008, TestSize.Level1)
165 {
166 GTEST_LOG_(INFO)
167 << "NetworkAbilityStubTest, NetworkAbilityStubTest008, TestSize.Level1";
168 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest008 begin");
169 auto networkAbilityStub = sptr<MockNetworkAbilityStub>(new (std::nothrow) MockNetworkAbilityStub());
170 MessageParcel parcel;
171 parcel.WriteInterfaceToken(u"UNKNOWN_DESCRIPTOR");
172 MessageParcel reply;
173 MessageOption option;
174 EXPECT_EQ(ERRCODE_SERVICE_UNAVAILABLE,
175 networkAbilityStub->OnRemoteRequest(UNKNOWN_CODE, parcel, reply, option));
176 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkAbilityStubTest008 end");
177 }
178
179 /*
180 * @tc.name: NetworkCallback001
181 * @tc.desc: network callback
182 * @tc.type: FUNC
183 */
184 HWTEST_F(NetworkAbilityStubTest, NetworkCallback001, TestSize.Level1)
185 {
186 GTEST_LOG_(INFO)
187 << "NetworkAbilityStubTest, NetworkCallback001, TestSize.Level1";
188 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkCallback001 begin");
189 auto networkCallbackHost =
190 sptr<NetworkCallbackHost>(new (std::nothrow) NetworkCallbackHost());
191 EXPECT_NE(nullptr, networkCallbackHost);
192 std::unique_ptr<Location> location = std::make_unique<Location>();
193 networkCallbackHost->OnLocationReport(location);
194 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkCallback001 end");
195 }
196
197 /*
198 * @tc.name: NetworkCallback002
199 * @tc.desc: network callback
200 * @tc.type: FUNC
201 */
202 HWTEST_F(NetworkAbilityStubTest, NetworkCallback002, TestSize.Level1)
203 {
204 GTEST_LOG_(INFO)
205 << "NetworkAbilityStubTest, NetworkCallback002, TestSize.Level1";
206 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkCallback002 begin");
207 auto networkCallbackHost =
208 sptr<NetworkCallbackHost>(new (std::nothrow) NetworkCallbackHost());
209 EXPECT_NE(nullptr, networkCallbackHost);
210 networkCallbackHost->OnLocatingStatusChange(DEFAULT_STATUS); // nullptr error
211 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkCallback002 end");
212 }
213
214 /*
215 * @tc.name: NetworkCallback003
216 * @tc.desc: network callback
217 * @tc.type: FUNC
218 */
219 HWTEST_F(NetworkAbilityStubTest, NetworkCallback003, TestSize.Level1)
220 {
221 GTEST_LOG_(INFO)
222 << "NetworkAbilityStubTest, NetworkCallback003, TestSize.Level1";
223 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkCallback003 begin");
224 auto networkCallbackHost =
225 sptr<NetworkCallbackHost>(new (std::nothrow) NetworkCallbackHost());
226 EXPECT_NE(nullptr, networkCallbackHost);
227 networkCallbackHost->OnErrorReport(ERR_CODE); // nullptr error
228 LBSLOGI(NETWORK, "[NetworkAbilityStubTest] NetworkCallback003 end");
229 }
230 } // namespace Location
231 } // namespace OHOS
232 #endif // FEATURE_NETWORK_SUPPORT
233