1 /* 2 * Copyright (c) 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 #include <gmock/gmock.h> 18 19 #ifdef GTEST_API_ 20 #define private public 21 #define protected public 22 #endif 23 24 #define NETMGR_DEBUG 1 25 26 #include "net_supplier_callback_proxy.h" 27 28 namespace OHOS { 29 namespace NetManagerStandard { 30 31 using namespace testing::ext; 32 using ::testing::_; 33 using ::testing::Return; 34 35 class RemoteObjectMocker : public IRemoteObject { 36 public: RemoteObjectMocker()37 RemoteObjectMocker() : IRemoteObject{u"RemoteObjectMocker"} {} ~RemoteObjectMocker()38 ~RemoteObjectMocker() {} 39 40 MOCK_METHOD(int32_t, GetObjectRefCount, (), (override)); 41 MOCK_METHOD(int, SendRequest, (uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option), 42 (override)); 43 MOCK_METHOD(bool, IsProxyObject, (), (const, override)); 44 MOCK_METHOD(bool, IsObjectDead, (), (const, override)); 45 MOCK_METHOD(std::u16string, GetInterfaceDescriptor, (), (override)); 46 MOCK_METHOD(bool, CheckObjectLegality, (), (const, override)); 47 MOCK_METHOD(bool, AddDeathRecipient, (const sptr<DeathRecipient> &recipient), (override)); 48 MOCK_METHOD(bool, RemoveDeathRecipient, (const sptr<DeathRecipient> &recipient), (override)); 49 MOCK_METHOD(bool, Marshalling, (OHOS::Parcel & parcel), (const, override)); 50 MOCK_METHOD(sptr<IRemoteBroker>, AsInterface, (), (override)); 51 MOCK_METHOD(int, Dump, (int fd, const std::vector<std::u16string> &args), (override)); 52 }; 53 54 class NetSupplierCallbackProxyTest : public testing::Test { 55 protected: 56 RemoteObjectMocker *remoteObjectMocker; 57 sptr<NetSupplierCallbackProxy> proxy; 58 SetUp()59 void SetUp() override 60 { 61 remoteObjectMocker = new RemoteObjectMocker(); 62 sptr<IRemoteObject> impl(remoteObjectMocker); 63 proxy = new (std::nothrow) NetSupplierCallbackProxy(impl); 64 } 65 TearDown()66 void TearDown() override 67 { 68 remoteObjectMocker = nullptr; 69 proxy = nullptr; 70 } 71 }; 72 73 HWTEST_F(NetSupplierCallbackProxyTest, RequestNetwork_001, TestSize.Level1) 74 { 75 // Arrange 76 std::string ident = "testIdent"; 77 std::set<NetCap> netCaps; 78 NetRequest netrequest; 79 80 EXPECT_CALL(*remoteObjectMocker, SendRequest(_, _, _, _)).WillOnce(Return(ERR_TRANSACTION_FAILED)); 81 int32_t result = proxy->RequestNetwork(ident, netCaps, netrequest); 82 ASSERT_EQ(result, ERR_TRANSACTION_FAILED); 83 } 84 85 HWTEST_F(NetSupplierCallbackProxyTest, RequestNetwork_002, TestSize.Level1) 86 { 87 // Arrange 88 std::string ident = "testIdent"; 89 std::set<NetCap> netCaps; 90 NetRequest netrequest; 91 92 EXPECT_CALL(*remoteObjectMocker, SendRequest(_, _, _, _)).WillOnce(Return(ERR_NONE)); 93 int32_t result = proxy->RequestNetwork(ident, netCaps, netrequest); 94 ASSERT_EQ(result, ERR_NONE); 95 } 96 97 HWTEST_F(NetSupplierCallbackProxyTest, RequestNetwork_003, TestSize.Level1) 98 { 99 sptr<NetSupplierCallbackProxy> proxy = new (std::nothrow) NetSupplierCallbackProxy(NULL); 100 std::string ident = "testIdent"; 101 std::set<NetCap> netCaps; 102 NetRequest netrequest; 103 104 int32_t result = proxy->RequestNetwork(ident, netCaps, netrequest); 105 ASSERT_EQ(result, NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL); 106 } 107 108 HWTEST_F(NetSupplierCallbackProxyTest, ReleaseNetwork_001, TestSize.Level1) 109 { 110 // Arrange 111 NetRequest netrequest; 112 netrequest.ident = "testIdent"; 113 114 EXPECT_CALL(*remoteObjectMocker, SendRequest(_, _, _, _)).WillOnce(Return(ERR_TRANSACTION_FAILED)); 115 int32_t result = proxy->ReleaseNetwork(netrequest); 116 ASSERT_EQ(result, ERR_TRANSACTION_FAILED); 117 } 118 119 HWTEST_F(NetSupplierCallbackProxyTest, ReleaseNetwork_002, TestSize.Level1) 120 { 121 // Arrange 122 NetRequest netrequest; 123 netrequest.ident = "testIdent"; 124 125 EXPECT_CALL(*remoteObjectMocker, SendRequest(_, _, _, _)).WillOnce(Return(ERR_NONE)); 126 int32_t result = proxy->ReleaseNetwork(netrequest); 127 ASSERT_EQ(result, ERR_NONE); 128 } 129 130 HWTEST_F(NetSupplierCallbackProxyTest, ReleaseNetwork_003, TestSize.Level1) 131 { 132 sptr<NetSupplierCallbackProxy> proxy = new (std::nothrow) NetSupplierCallbackProxy(NULL); 133 NetRequest netrequest; 134 netrequest.ident = "testIdent"; 135 136 int32_t result = proxy->ReleaseNetwork(netrequest); 137 ASSERT_EQ(result, NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL); 138 } 139 140 HWTEST_F(NetSupplierCallbackProxyTest, AddRequest_001, TestSize.Level1) 141 { 142 NetRequest netRequest; 143 netRequest.uid = 1; 144 netRequest.requestId = 1; 145 netRequest.registerType = 1; 146 netRequest.ident = "test"; 147 netRequest.bearTypes = {BEARER_WIFI, BEARER_BLUETOOTH, BEARER_ETHERNET}; 148 netRequest.netCaps = {NET_CAPABILITY_SUPL, NET_CAPABILITY_DUN, NET_CAPABILITY_IA}; 149 150 EXPECT_CALL(*remoteObjectMocker, SendRequest(_, _, _, _)).WillOnce(Return(ERR_TRANSACTION_FAILED)); 151 int32_t result = proxy->AddRequest(netRequest); 152 ASSERT_EQ(result, ERR_TRANSACTION_FAILED); 153 } 154 155 HWTEST_F(NetSupplierCallbackProxyTest, AddRequest_002, TestSize.Level1) 156 { 157 NetRequest netRequest; 158 netRequest.uid = 1; 159 netRequest.requestId = 1; 160 netRequest.registerType = 1; 161 netRequest.ident = "test"; 162 netRequest.bearTypes = {BEARER_WIFI, BEARER_BLUETOOTH, BEARER_ETHERNET}; 163 netRequest.netCaps = {NET_CAPABILITY_SUPL, NET_CAPABILITY_DUN, NET_CAPABILITY_IA}; 164 165 EXPECT_CALL(*remoteObjectMocker, SendRequest(_, _, _, _)).WillOnce(Return(ERR_NONE)); 166 int32_t result = proxy->AddRequest(netRequest); 167 ASSERT_EQ(result, ERR_NONE); 168 } 169 170 HWTEST_F(NetSupplierCallbackProxyTest, AddRequest_003, TestSize.Level1) 171 { 172 sptr<NetSupplierCallbackProxy> proxy = new (std::nothrow) NetSupplierCallbackProxy(NULL); 173 NetRequest netRequest; 174 int32_t result = proxy->AddRequest(netRequest); 175 ASSERT_EQ(result, NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL); 176 } 177 178 } // namespace NetManagerStandard 179 } // namespace OHOS