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
18 #include "net_manager_constants.h"
19 #include "net_supplier_callback_stub.h"
20 #include "common_net_conn_callback_test.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace testing::ext;
26 } // namespace
27
28 class NetSupplierCallbackStubTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp();
33 void TearDown();
34
35 static inline std::shared_ptr<NetSupplierCallbackStubTestCb> supplierCbStub_ = nullptr;
36 };
37
SetUpTestCase()38 void NetSupplierCallbackStubTest::SetUpTestCase()
39 {
40 supplierCbStub_ = std::make_shared<NetSupplierCallbackStubTestCb>();
41 sptr<NetSupplierCallbackBase> callback = new (std::nothrow) NetSupplierCallbackBaseTestCb();
42 supplierCbStub_->RegisterSupplierCallbackImpl(callback);
43 }
44
TearDownTestCase()45 void NetSupplierCallbackStubTest::TearDownTestCase() {}
46
SetUp()47 void NetSupplierCallbackStubTest::SetUp() {}
48
TearDown()49 void NetSupplierCallbackStubTest::TearDown() {}
50
51 HWTEST_F(NetSupplierCallbackStubTest, RequestNetwork001, TestSize.Level1)
52 {
53 std::string ident = "testsupid";
54 std::set<NetCap> netCaps;
55 netCaps.insert(NetCap::NET_CAPABILITY_NOT_METERED);
56 MessageParcel data;
57 if (!data.WriteInterfaceToken(NetSupplierCallbackStub::GetDescriptor())) {
58 return;
59 }
60 if (!data.WriteString(ident)) {
61 return;
62 }
63 uint32_t size = static_cast<uint32_t>(netCaps.size());
64 if (!data.WriteUint32(size)) {
65 return;
66 }
67 for (auto netCap : netCaps) {
68 data.WriteInt32(static_cast<uint32_t>(netCap));
69 }
70
71 MessageParcel reply;
72 MessageOption option;
73 int32_t ret = supplierCbStub_->OnRemoteRequest(100, data, reply, option);
74 EXPECT_NE(ret, NETMANAGER_SUCCESS);
75
76 ret = supplierCbStub_->OnRemoteRequest(
77 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REQUEST_NETWORK), data, reply, option);
78 EXPECT_NE(ret, NETMANAGER_SUCCESS);
79
80 MessageParcel dataOk;
81 if (!dataOk.WriteInterfaceToken(NetSupplierCallbackStub::GetDescriptor())) {
82 return;
83 }
84 if (!dataOk.WriteString(ident)) {
85 return;
86 }
87 size = static_cast<uint32_t>(netCaps.size());
88 if (!dataOk.WriteUint32(size)) {
89 return;
90 }
91 for (auto netCap : netCaps) {
92 dataOk.WriteInt32(static_cast<uint32_t>(netCap));
93 }
94 ret = supplierCbStub_->OnRemoteRequest(
95 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REQUEST_NETWORK), dataOk, reply, option);
96 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
97 }
98
99 HWTEST_F(NetSupplierCallbackStubTest, ReleaseNetwork001, TestSize.Level1)
100 {
101 std::string ident = "testsupid";
102 std::set<NetCap> netCaps;
103 netCaps.insert(NetCap::NET_CAPABILITY_NOT_METERED);
104 MessageParcel data;
105 if (!data.WriteInterfaceToken(NetSupplierCallbackStub::GetDescriptor())) {
106 return;
107 }
108 if (!data.WriteString(ident)) {
109 return;
110 }
111 uint32_t size = static_cast<uint32_t>(netCaps.size());
112 if (!data.WriteUint32(size)) {
113 return;
114 }
115 for (auto netCap : netCaps) {
116 data.WriteInt32(static_cast<uint32_t>(netCap));
117 }
118
119 MessageParcel reply;
120 MessageOption option;
121 int32_t ret = supplierCbStub_->OnRemoteRequest(
122 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_RELEASE_NETWORK), data, reply, option);
123 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
124 }
125 } // namespace NetManagerStandard
126 } // namespace OHOS