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 "net_dns_health_callback_proxy.h" 17 #include <gmock/gmock.h> 18 #include <gtest/gtest.h> 19 #include "net_manager_constants.h" 20 21 namespace OHOS { 22 namespace NetsysNative { 23 using namespace testing::ext; 24 using ::testing::_; 25 using ::testing::Return; 26 27 class RemoteObjectMocker : public IRemoteObject { 28 public: RemoteObjectMocker()29 RemoteObjectMocker() : IRemoteObject{u"RemoteObjectMocker"} {} ~RemoteObjectMocker()30 ~RemoteObjectMocker() {} 31 32 MOCK_METHOD(int32_t, GetObjectRefCount, (), (override)); 33 MOCK_METHOD(int, SendRequest, (uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option), 34 (override)); 35 MOCK_METHOD(bool, IsProxyObject, (), (const, override)); 36 MOCK_METHOD(bool, IsObjectDead, (), (const, override)); 37 MOCK_METHOD(std::u16string, GetInterfaceDescriptor, (), (override)); 38 MOCK_METHOD(bool, CheckObjectLegality, (), (const, override)); 39 MOCK_METHOD(bool, AddDeathRecipient, (const sptr<DeathRecipient> &recipient), (override)); 40 MOCK_METHOD(bool, RemoveDeathRecipient, (const sptr<DeathRecipient> &recipient), (override)); 41 MOCK_METHOD(bool, Marshalling, (OHOS::Parcel & parcel), (const, override)); 42 MOCK_METHOD(sptr<IRemoteBroker>, AsInterface, (), (override)); 43 MOCK_METHOD(int, Dump, (int fd, const std::vector<std::u16string> &args), (override)); 44 }; 45 46 class NetDnsHealthCallbackProxyTest : public testing::Test { 47 protected: 48 RemoteObjectMocker *remoteObjectMocker; 49 sptr<NetDnsHealthCallbackProxy> proxy; 50 SetUp()51 void SetUp() override 52 { 53 remoteObjectMocker = new RemoteObjectMocker(); 54 sptr<IRemoteObject> impl(remoteObjectMocker); 55 proxy = new (std::nothrow) NetDnsHealthCallbackProxy(impl); 56 } 57 TearDown()58 void TearDown() override 59 { 60 remoteObjectMocker = nullptr; 61 proxy = nullptr; 62 } 63 }; 64 65 HWTEST_F(NetDnsHealthCallbackProxyTest, NetDnsHealthCallbackProxyTest_NetDnsHealthReport_01, TestSize.Level0) 66 { 67 NetDnsHealthReport dnsResultReport; 68 int32_t ret = proxy->OnDnsHealthReport(dnsResultReport); 69 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_SUCCESS); 70 } 71 72 HWTEST_F(NetDnsHealthCallbackProxyTest, NetDnsHealthCallbackProxyTest_NetDnsHealthReport_02, TestSize.Level0) 73 { 74 NetDnsHealthReport dnsResultReport; 75 EXPECT_CALL(*remoteObjectMocker, SendRequest(_, _, _, _)).WillOnce(Return(1)); 76 int32_t ret = proxy->OnDnsHealthReport(dnsResultReport); 77 EXPECT_EQ(ret, NetManagerStandard::NETMANAGER_ERR_OPERATION_FAILED); 78 } 79 80 } // namespace NetsysNative 81 } // namespace OHOS