1 /*
2 * Copyright (c) 2023 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 "i_net_conn_callback.h"
17 #include "net_all_capabilities.h"
18 #include "net_conn_callback_stub.h"
19 #include "net_manager_constants.h"
20 #include <fcntl.h>
21 #include <gtest/gtest.h>
22 #include <iostream>
23 #include <memory>
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 using namespace testing::ext;
28 namespace {
29 constexpr uint64_t OUTOFFRANGECODE = 100;
30 constexpr int32_t TEST_NETID = 1010;
31 class TestNetConnCallbackStub : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37
38 static inline std::shared_ptr<NetConnCallbackStub> instance_ = std::make_shared<NetConnCallbackStub>();
39 };
40
SetUpTestCase()41 void TestNetConnCallbackStub::SetUpTestCase() {}
42
TearDownTestCase()43 void TestNetConnCallbackStub::TearDownTestCase() {}
44
SetUp()45 void TestNetConnCallbackStub::SetUp() {}
46
TearDown()47 void TestNetConnCallbackStub::TearDown() {}
48
49 /**
50 * @tc.name: OnRemoteRequestTest001
51 * @tc.desc: Test NetConnCallbackStub OnRemoteRequest.
52 * @tc.type: FUNC
53 */
54 HWTEST_F(TestNetConnCallbackStub, OnRemoteRequestTest001, TestSize.Level1)
55 {
56 MessageParcel data;
57 MessageParcel reply;
58 MessageOption option;
59 int32_t ret = instance_->OnRemoteRequest(OUTOFFRANGECODE, data, reply, option);
60 EXPECT_EQ(ret, NETMANAGER_ERR_DESCRIPTOR_MISMATCH);
61 }
62
63 /**
64 * @tc.name: OnRemoteRequestTest002
65 * @tc.desc: Test NetConnCallbackStub OnRemoteRequest.
66 * @tc.type: FUNC
67 */
68 HWTEST_F(TestNetConnCallbackStub, OnRemoteRequestTest002, TestSize.Level1)
69 {
70 MessageParcel data;
71 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
72 MessageParcel reply;
73 MessageOption option;
74 int32_t ret = instance_->OnRemoteRequest(OUTOFFRANGECODE, data, reply, option);
75 EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR);
76 }
77
78 /**
79 * @tc.name: OnNetAvailableTest001
80 * @tc.desc: Test NetConnCallbackStub OnNetAvailable.
81 * @tc.type: FUNC
82 */
83 HWTEST_F(TestNetConnCallbackStub, OnNetAvailableTest001, TestSize.Level1)
84 {
85 MessageParcel data;
86 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
87 MessageParcel reply;
88 MessageOption option;
89 int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_AVAILABLE),
90 data, reply, option);
91 EXPECT_EQ(ret, NETMANAGER_ERR_READ_DATA_FAIL);
92 }
93
94 /**
95 * @tc.name: OnNetAvailableTest002
96 * @tc.desc: Test NetConnCallbackStub OnNetAvailable.
97 * @tc.type: FUNC
98 */
99 HWTEST_F(TestNetConnCallbackStub, OnNetAvailableTest002, TestSize.Level1)
100 {
101 MessageParcel data;
102 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
103 data.WriteInt32(TEST_NETID);
104 MessageParcel reply;
105 MessageOption option;
106 int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_AVAILABLE),
107 data, reply, option);
108 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
109 }
110
111 /**
112 * @tc.name: OnNetCapabilitiesChangeTest001
113 * @tc.desc: Test NetConnCallbackStub OnNetCapabilitiesChange.
114 * @tc.type: FUNC
115 */
116 HWTEST_F(TestNetConnCallbackStub, OnNetCapabilitiesChangeTest001, TestSize.Level1)
117 {
118 MessageParcel data;
119 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
120 MessageParcel reply;
121 MessageOption option;
122 NetAllCapabilities netCaps;
123 netCaps.bearerTypes_.insert(NetBearType::BEARER_CELLULAR);
124 netCaps.netCaps_.insert(NetCap::NET_CAPABILITY_MMS);
125 data.WriteInt32(TEST_NETID);
126 data.WriteUint32(netCaps.linkDownBandwidthKbps_);
127 data.WriteUint32(netCaps.linkUpBandwidthKbps_);
128 data.WriteUint32(netCaps.netCaps_.size());
129 data.WriteUint32(NetCap::NET_CAPABILITY_MMS);
130 data.WriteUint32(netCaps.bearerTypes_.size());
131 data.WriteUint32(NetBearType::BEARER_CELLULAR);
132 int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_CAPABILITIES_CHANGE),
133 data, reply, option);
134 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
135 }
136
137 /**
138 * @tc.name: OnNetConnectionPropertiesChangeTest001
139 * @tc.desc: Test NetConnCallbackStub OnNetConnectionPropertiesChange.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(TestNetConnCallbackStub, OnNetConnectionPropertiesChangeTest001, TestSize.Level1)
143 {
144 MessageParcel data;
145 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
146 MessageParcel reply;
147 MessageOption option;
148 int32_t ret =instance_->OnRemoteRequest(
149 static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_CONNECTION_PROPERTIES_CHANGE), data, reply, option);
150 EXPECT_EQ(ret, NETMANAGER_ERR_READ_DATA_FAIL);
151
152 MessageParcel dataSuccess;
153 dataSuccess.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
154 dataSuccess.WriteInt32(TEST_NETID);
155 NetLinkInfo linkInfo;
156 linkInfo.ifaceName_ = "ifacename";
157 linkInfo.domain_ = "0.0.0.0";
158 linkInfo.Marshalling(dataSuccess);
159 ret = instance_->OnRemoteRequest(
160 static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_CONNECTION_PROPERTIES_CHANGE), dataSuccess, reply, option);
161 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
162 }
163
164 /**
165 * @tc.name: OnNetLostTest001
166 * @tc.desc: Test NetConnCallbackStub OnNetLost.
167 * @tc.type: FUNC
168 */
169 HWTEST_F(TestNetConnCallbackStub, OnNetLostTest001, TestSize.Level1)
170 {
171 MessageParcel data;
172 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
173 data.WriteInt32(TEST_NETID);
174 MessageParcel reply;
175 MessageOption option;
176 int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_LOST),
177 data, reply, option);
178 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
179 }
180
181 /**
182 * @tc.name: OnNetUnavailableTest001
183 * @tc.desc: Test NetConnCallbackStub OnNetUnavailable.
184 * @tc.type: FUNC
185 */
186 HWTEST_F(TestNetConnCallbackStub, OnNetUnavailableTest001, TestSize.Level1)
187 {
188 MessageParcel data;
189 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
190 MessageParcel reply;
191 MessageOption option;
192 int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_UNAVAILABLE),
193 data, reply, option);
194 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
195 }
196
197 /**
198 * @tc.name: OnNetBlockStatusChangeTest001
199 * @tc.desc: Test NetConnCallbackStub OnNetBlockStatusChange.
200 * @tc.type: FUNC
201 */
202 HWTEST_F(TestNetConnCallbackStub, OnNetBlockStatusChangeTest001, TestSize.Level1)
203 {
204 MessageParcel data;
205 data.WriteInterfaceToken(NetConnCallbackStub::GetDescriptor());
206 data.WriteInt32(TEST_NETID);
207 data.WriteBool(false);
208 MessageParcel reply;
209 MessageOption option;
210 int32_t ret = instance_->OnRemoteRequest(static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_BLOCK_STATUS_CHANGE),
211 data, reply, option);
212 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
213 }
214 } // namespace
215 } // namespace NetManagerStandard
216 } // namespace OHOS