• 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     NetRequest netrequest;
105     netrequest.ident = "testsupid";
106     netrequest.netCaps.insert(NetCap::NET_CAPABILITY_NOT_METERED);
107 
108     MessageParcel data;
109     if (!data.WriteInterfaceToken(NetSupplierCallbackStub::GetDescriptor())) {
110         return;
111     }
112     bool result = data.WriteUint32(netrequest.uid) && data.WriteUint32(netrequest.requestId) &&
113                   data.WriteUint32(netrequest.registerType) && data.WriteString(netrequest.ident);
114     if (!result) {
115         return;
116     }
117 
118     uint32_t size = static_cast<uint32_t>(netrequest.bearTypes.size());
119     if (!data.WriteUint32(size)) {
120         return;
121     }
122     for (auto netBearType : netrequest.bearTypes) {
123         if (!data.WriteInt32(netBearType)) {
124             return;
125         }
126     }
127 
128     size = static_cast<uint32_t>(netrequest.netCaps.size());
129     if (!data.WriteUint32(size)) {
130         return;
131     }
132     for (auto netCap : netrequest.netCaps) {
133         data.WriteInt32(static_cast<uint32_t>(netCap));
134     }
135 
136     MessageParcel reply;
137     MessageOption option;
138     int32_t ret = supplierCbStub_->OnRemoteRequest(
139         static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_RELEASE_NETWORK), data, reply, option);
140     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
141 }
142 
143 HWTEST_F(NetSupplierCallbackStubTest, OnAddRequest001, TestSize.Level1)
144 {
145     MessageParcel data;
146 
147     int32_t uid = 0;
148     ASSERT_TRUE(data.WriteUint32(uid));
149     int32_t requestId = 0;
150     ASSERT_TRUE(data.WriteUint32(requestId));
151     int32_t registerType = 0;
152     ASSERT_TRUE(data.WriteUint32(registerType));
153     std::string ident = "testsupid";
154     ASSERT_TRUE(data.WriteString(ident));
155     std::vector<uint32_t> netBearTypes = {NetBearType::BEARER_CELLULAR, NetBearType::BEARER_DEFAULT};
156     ASSERT_TRUE(data.WriteUInt32Vector(netBearTypes));
157     std::vector<uint32_t> netCaps = {NetCap::NET_CAPABILITY_NOT_METERED, NetCap::NET_CAPABILITY_END};
158     ASSERT_TRUE(data.WriteUInt32Vector(netCaps));
159     MessageParcel reply;
160     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
161     EXPECT_EQ(netSupplierCallbackStub->OnAddRequest(data, reply), NETMANAGER_SUCCESS);
162 }
163 
164 HWTEST_F(NetSupplierCallbackStubTest, OnAddRequest002, TestSize.Level1)
165 {
166     MessageParcel data;
167 
168     int32_t uid = 0;
169     ASSERT_TRUE(data.WriteUint32(uid));
170     int32_t requestId = 0;
171     ASSERT_TRUE(data.WriteUint32(requestId));
172     int32_t registerType = 0;
173     ASSERT_TRUE(data.WriteUint32(registerType));
174     std::string ident = "testsupid";
175     ASSERT_TRUE(data.WriteString(ident));
176     ASSERT_TRUE(data.WriteUint32(MAX_NET_BEARTYPE_NUM + 1));
177     ASSERT_TRUE(data.WriteUint32(0));
178     MessageParcel reply;
179     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
180     EXPECT_EQ(netSupplierCallbackStub->OnAddRequest(data, reply), NETMANAGER_ERR_INVALID_PARAMETER);
181 }
182 
183 HWTEST_F(NetSupplierCallbackStubTest, OnAddRequest003, TestSize.Level1)
184 {
185     MessageParcel data;
186     int32_t uid = 0;
187     ASSERT_TRUE(data.WriteUint32(uid));
188     int32_t requestId = 0;
189     ASSERT_TRUE(data.WriteUint32(requestId));
190     int32_t registerType = 0;
191     ASSERT_TRUE(data.WriteUint32(registerType));
192     std::string ident = "testsupid";
193     ASSERT_TRUE(data.WriteString(ident));
194     ASSERT_TRUE(data.WriteUint32(0));
195     ASSERT_TRUE(data.WriteUint32(MAX_NET_CAP_NUM + 1));
196     MessageParcel reply;
197     auto netSupplierCallbackStub = std::make_shared<NetSupplierCallbackStub>();
198     EXPECT_EQ(netSupplierCallbackStub->OnAddRequest(data, reply), NETMANAGER_ERR_INVALID_PARAMETER);
199 }
200 
201 } // namespace NetManagerStandard
202 } // namespace OHOS