• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 <gtest/gtest.h>
17 #define private public
18 #include "net_manager_constants.h"
19 #include "net_supplier_callback_stub.h"
20 #include "net_supplier_callback_proxy.h"
21 #include "common_net_conn_callback_test.h"
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace testing;
26 using namespace testing::ext;
27 static constexpr uint32_t MAX_NET_CAP_NUM = 32;
28 static constexpr uint32_t MAX_NET_BEARTYPE_NUM = 7;
29 } // namespace
30 
31 class NetSupplierCallbackStubTest : 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<NetSupplierCallbackStubTestCb> supplierCbStub_ = nullptr;
39 };
40 
SetUpTestCase()41 void NetSupplierCallbackStubTest::SetUpTestCase()
42 {
43     supplierCbStub_ = std::make_shared<NetSupplierCallbackStubTestCb>();
44     sptr<NetSupplierCallbackBase> callback = new (std::nothrow) NetSupplierCallbackBaseTestCb();
45     supplierCbStub_->RegisterSupplierCallbackImpl(callback);
46 }
47 
TearDownTestCase()48 void NetSupplierCallbackStubTest::TearDownTestCase() {}
49 
SetUp()50 void NetSupplierCallbackStubTest::SetUp() {}
51 
TearDown()52 void NetSupplierCallbackStubTest::TearDown() {}
53 
54 HWTEST_F(NetSupplierCallbackStubTest, RequestNetwork001, TestSize.Level1)
55 {
56     std::string ident = "testsupid";
57     std::set<NetCap> netCaps;
58     netCaps.insert(NetCap::NET_CAPABILITY_NOT_METERED);
59     MessageParcel data;
60     if (!data.WriteInterfaceToken(NetSupplierCallbackStub::GetDescriptor())) {
61         return;
62     }
63     if (!data.WriteString(ident)) {
64         return;
65     }
66     uint32_t size = static_cast<uint32_t>(netCaps.size());
67     if (!data.WriteUint32(size)) {
68         return;
69     }
70     for (auto netCap : netCaps) {
71         data.WriteInt32(static_cast<uint32_t>(netCap));
72     }
73 
74     MessageParcel reply;
75     MessageOption option;
76     int32_t ret = supplierCbStub_->OnRemoteRequest(100, data, reply, option);
77     EXPECT_NE(ret, NETMANAGER_SUCCESS);
78 
79     ret = supplierCbStub_->OnRemoteRequest(
80         static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REQUEST_NETWORK), data, reply, option);
81     EXPECT_NE(ret, NETMANAGER_SUCCESS);
82 
83     MessageParcel dataOk;
84     if (!dataOk.WriteInterfaceToken(NetSupplierCallbackStub::GetDescriptor())) {
85         return;
86     }
87     if (!dataOk.WriteString(ident)) {
88         return;
89     }
90     size = static_cast<uint32_t>(netCaps.size());
91     if (!dataOk.WriteUint32(size)) {
92         return;
93     }
94     for (auto netCap : netCaps) {
95         dataOk.WriteInt32(static_cast<uint32_t>(netCap));
96     }
97     ret = supplierCbStub_->OnRemoteRequest(
98         static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REQUEST_NETWORK), dataOk, reply, option);
99     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
100 }
101 
102 HWTEST_F(NetSupplierCallbackStubTest, ReleaseNetwork001, TestSize.Level1)
103 {
104     std::string ident = "testsupid";
105     std::set<NetCap> netCaps;
106     netCaps.insert(NetCap::NET_CAPABILITY_NOT_METERED);
107     MessageParcel data;
108     if (!data.WriteInterfaceToken(NetSupplierCallbackStub::GetDescriptor())) {
109         return;
110     }
111     if (!data.WriteString(ident)) {
112         return;
113     }
114     uint32_t size = static_cast<uint32_t>(netCaps.size());
115     if (!data.WriteUint32(size)) {
116         return;
117     }
118     for (auto netCap : netCaps) {
119         data.WriteInt32(static_cast<uint32_t>(netCap));
120     }
121 
122     MessageParcel reply;
123     MessageOption option;
124     int32_t ret = supplierCbStub_->OnRemoteRequest(
125         static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_RELEASE_NETWORK), data, reply, option);
126     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
127 }
128 HWTEST_F(NetSupplierCallbackStubTest, OnAddRequest001, TestSize.Level1)
129 {
130     MessageParcel data;
131 
132     int32_t uid = 0;
133     ASSERT_TRUE(data.WriteUint32(uid));
134     int32_t requestId = 0;
135     ASSERT_TRUE(data.WriteUint32(requestId));
136     int32_t registerType = 0;
137     ASSERT_TRUE(data.WriteUint32(registerType));
138     std::string ident = "testsupid";
139     ASSERT_TRUE(data.WriteString(ident));
140     std::vector<uint32_t> netBearTypes = {NetBearType::BEARER_CELLULAR, NetBearType::BEARER_DEFAULT};
141     ASSERT_TRUE(data.WriteUInt32Vector(netBearTypes));
142     std::vector<uint32_t> netCaps = {NetCap::NET_CAPABILITY_NOT_METERED, NetCap::NET_CAPABILITY_END};
143     ASSERT_TRUE(data.WriteUInt32Vector(netCaps));
144     MessageParcel reply;
145     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
146     EXPECT_EQ(netSupplierCallbackStub->OnAddRequest(data, reply), NETMANAGER_SUCCESS);
147 }
148 
149 HWTEST_F(NetSupplierCallbackStubTest, OnAddRequest002, TestSize.Level1)
150 {
151     MessageParcel data;
152 
153     int32_t uid = 0;
154     ASSERT_TRUE(data.WriteUint32(uid));
155     int32_t requestId = 0;
156     ASSERT_TRUE(data.WriteUint32(requestId));
157     int32_t registerType = 0;
158     ASSERT_TRUE(data.WriteUint32(registerType));
159     std::string ident = "testsupid";
160     ASSERT_TRUE(data.WriteString(ident));
161     ASSERT_TRUE(data.WriteUint32(MAX_NET_BEARTYPE_NUM + 1));
162     ASSERT_TRUE(data.WriteUint32(0));
163     MessageParcel reply;
164     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
165     EXPECT_EQ(netSupplierCallbackStub->OnAddRequest(data, reply), NETMANAGER_ERR_INVALID_PARAMETER);
166 }
167 
168 HWTEST_F(NetSupplierCallbackStubTest, OnAddRequest003, TestSize.Level1)
169 {
170     MessageParcel data;
171     int32_t uid = 0;
172     ASSERT_TRUE(data.WriteUint32(uid));
173     int32_t requestId = 0;
174     ASSERT_TRUE(data.WriteUint32(requestId));
175     int32_t registerType = 0;
176     ASSERT_TRUE(data.WriteUint32(registerType));
177     std::string ident = "testsupid";
178     ASSERT_TRUE(data.WriteString(ident));
179     ASSERT_TRUE(data.WriteUint32(0));
180     ASSERT_TRUE(data.WriteUint32(MAX_NET_CAP_NUM + 1));
181     MessageParcel reply;
182     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
183     EXPECT_EQ(netSupplierCallbackStub->OnAddRequest(data, reply), NETMANAGER_ERR_INVALID_PARAMETER);
184 }
185 
186 HWTEST_F(NetSupplierCallbackStubTest, OnRemoveRequest001, TestSize.Level1)
187 {
188     MessageParcel data;
189     int32_t uid = 0;
190     ASSERT_TRUE(data.WriteUint32(uid));
191     int32_t requestId = 0;
192     ASSERT_TRUE(data.WriteUint32(requestId));
193     int32_t registerType = 0;
194     ASSERT_TRUE(data.WriteUint32(registerType));
195     std::string ident = "testsupid";
196     ASSERT_TRUE(data.WriteString(ident));
197     std::vector<uint32_t> netBearTypes = {NetBearType::BEARER_CELLULAR, NetBearType::BEARER_DEFAULT};
198     ASSERT_TRUE(data.WriteUInt32Vector(netBearTypes));
199     std::vector<uint32_t> netCaps = {NetCap::NET_CAPABILITY_NOT_METERED, NetCap::NET_CAPABILITY_END};
200     ASSERT_TRUE(data.WriteUInt32Vector(netCaps));
201     MessageParcel reply;
202     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
203     EXPECT_EQ(netSupplierCallbackStub->OnRemoveRequest(data, reply), NETMANAGER_SUCCESS);
204 }
205 
206 HWTEST_F(NetSupplierCallbackStubTest, OnRemoveRequest002, TestSize.Level1)
207 {
208     MessageParcel data;
209     int32_t uid = 0;
210     ASSERT_TRUE(data.WriteUint32(uid));
211     int32_t requestId = 0;
212     ASSERT_TRUE(data.WriteUint32(requestId));
213     int32_t registerType = 0;
214     ASSERT_TRUE(data.WriteUint32(registerType));
215     std::string ident = "testsupid";
216     ASSERT_TRUE(data.WriteString(ident));
217     ASSERT_TRUE(data.WriteUint32(MAX_NET_BEARTYPE_NUM + 1));
218     ASSERT_TRUE(data.WriteUint32(0));
219     MessageParcel reply;
220     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
221     EXPECT_EQ(netSupplierCallbackStub->OnRemoveRequest(data, reply), NETMANAGER_ERR_INVALID_PARAMETER);
222 }
223 
224 HWTEST_F(NetSupplierCallbackStubTest, OnRemoveRequest003, TestSize.Level1)
225 {
226     MessageParcel data;
227     int32_t uid = 0;
228     ASSERT_TRUE(data.WriteUint32(uid));
229     int32_t requestId = 0;
230     ASSERT_TRUE(data.WriteUint32(requestId));
231     int32_t registerType = 0;
232     ASSERT_TRUE(data.WriteUint32(registerType));
233     std::string ident = "testsupid";
234     ASSERT_TRUE(data.WriteString(ident));
235     ASSERT_TRUE(data.WriteUint32(0));
236     ASSERT_TRUE(data.WriteUint32(MAX_NET_CAP_NUM + 1));
237     MessageParcel reply;
238     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
239     EXPECT_EQ(netSupplierCallbackStub->OnRemoveRequest(data, reply), NETMANAGER_ERR_INVALID_PARAMETER);
240 }
241 
242 HWTEST_F(NetSupplierCallbackStubTest, AddRequest001, TestSize.Level1)
243 {
244     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
245     NetRequest request;
246     netSupplierCallbackStub->AddRequest(request);
247     sptr<NetSupplierCallbackBase> cb = new NetSupplierCallbackBase();
248     netSupplierCallbackStub->RegisterSupplierCallbackImpl(cb);
249     netSupplierCallbackStub->AddRequest(request);
250     EXPECT_TRUE(netSupplierCallbackStub->callback_ != nullptr);
251 }
252 
253 HWTEST_F(NetSupplierCallbackStubTest, RemoveRequest001, TestSize.Level1)
254 {
255     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
256     NetRequest request;
257     netSupplierCallbackStub->RemoveRequest(request);
258     sptr<NetSupplierCallbackBase> cb = new NetSupplierCallbackBase();
259     netSupplierCallbackStub->RegisterSupplierCallbackImpl(cb);
260     netSupplierCallbackStub->RemoveRequest(request);
261     EXPECT_TRUE(netSupplierCallbackStub->callback_ != nullptr);
262 }
263 } // namespace NetManagerStandard
264 } // namespace OHOS