1 /*
2  * Copyright (c) 2022 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 <iostream>
17 #include <memory>
18 
19 #include <gtest/gtest.h>
20 
21 #include "i_netsys_service.h"
22 #include "iservice_registry.h"
23 #include "notify_callback_stub.h"
24 #include "system_ability_definition.h"
25 
26 namespace OHOS::nmd {
27 using namespace testing::ext;
28 constexpr const char *IFACENAME = "wlan0";
29 bool g_flag = true;
30 sptr<OHOS::NetsysNative::INotifyCallback> nativeNotifyCallback_ = nullptr;
31 sptr<OHOS::NetsysNative::INetsysService> netsysNativeService_ = nullptr;
32 class NetlinkNativeNotifyCallBack : public OHOS::NetsysNative::NotifyCallbackStub {
33 public:
34     int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
35                                       int scope) override;
36     int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
37                                       int scope) override;
38     int32_t OnInterfaceAdded(const std::string &ifName) override;
39     int32_t OnInterfaceRemoved(const std::string &ifName) override;
40     int32_t OnInterfaceChanged(const std::string &ifName, bool up) override;
41     int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
42     int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
43                            const std::string &ifName) override;
44     int32_t OnDhcpSuccess(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult) override;
45     int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
46 };
47 
48 class NetsysWrapperTest : public testing::Test {
49 public:
SetUpTestCase()50     static void SetUpTestCase()
51     {
52         auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53         if (samgr == nullptr) {
54             return;
55         }
56         auto remote = samgr->GetSystemAbility(OHOS::COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
57         if (remote == nullptr) {
58             return;
59         }
60         nativeNotifyCallback_ = new NetlinkNativeNotifyCallBack();
61         auto proxy = iface_cast<NetsysNative::INetsysService>(remote);
62         netsysNativeService_ = proxy;
63     }
TearDownTestCase()64     static void TearDownTestCase()
65     {
66         nativeNotifyCallback_ = nullptr;
67         netsysNativeService_ = nullptr;
68     }
69 };
70 
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int flags,int scope)71 int32_t NetlinkNativeNotifyCallBack::OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName,
72                                                                int flags, int scope)
73 {
74     std::cout << " [OnInterfaceAddressUpdated] " << ifName << " Address: " << addr << std::endl;
75     EXPECT_FALSE(addr.empty());
76     EXPECT_STRCASEEQ(IFACENAME, ifName.c_str());
77     EXPECT_GE(flags, 0);
78     EXPECT_GE(scope, 0);
79     return 0;
80 }
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)81 int32_t NetlinkNativeNotifyCallBack::OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName,
82                                                                int flags, int scope)
83 {
84     std::cout << " [OnInterfaceAddressRemoved] " << ifName << " Address: " << addr << std::endl;
85     EXPECT_FALSE(addr.empty());
86     EXPECT_STRCASEEQ(IFACENAME, ifName.c_str());
87     EXPECT_GE(flags, 0);
88     EXPECT_GE(scope, 0);
89     return 0;
90 }
OnInterfaceAdded(const std::string & ifName)91 int32_t NetlinkNativeNotifyCallBack::OnInterfaceAdded(const std::string &ifName)
92 {
93     std::cout << " [OnInterfaceAdded] " << ifName << std::endl;
94     EXPECT_STRCASEEQ(IFACENAME, ifName.c_str());
95     return 0;
96 }
OnInterfaceRemoved(const std::string & ifName)97 int32_t NetlinkNativeNotifyCallBack::OnInterfaceRemoved(const std::string &ifName)
98 {
99     std::cout << " [OnInterfaceRemoved] " << ifName << std::endl;
100     EXPECT_STRCASEEQ(IFACENAME, ifName.c_str());
101     return 0;
102 }
OnInterfaceChanged(const std::string & ifName,bool up)103 int32_t NetlinkNativeNotifyCallBack::OnInterfaceChanged(const std::string &ifName, bool up)
104 {
105     std::cout << " [OnInterfaceChanged] " << ifName << " status :" << (up ? "[update]" : "[remove]") << std::endl;
106     EXPECT_STRCASEEQ(IFACENAME, ifName.c_str());
107     if (g_flag) {
108         EXPECT_TRUE(up);
109     } else {
110         EXPECT_FALSE(up);
111     }
112     return 0;
113 }
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)114 int32_t NetlinkNativeNotifyCallBack::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
115 {
116     EXPECT_STRCASEEQ(IFACENAME, ifName.c_str());
117     if (g_flag) {
118         EXPECT_TRUE(up);
119     } else {
120         EXPECT_FALSE(up);
121     }
122     return 0;
123 }
OnRouteChanged(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)124 int32_t NetlinkNativeNotifyCallBack::OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
125                                                     const std::string &ifName)
126 {
127     EXPECT_STRCASEEQ(IFACENAME, ifName.c_str());
128     EXPECT_FALSE(route.empty());
129     EXPECT_FALSE(gateway.empty());
130     if (g_flag) {
131         EXPECT_TRUE(updated);
132     } else {
133         EXPECT_FALSE(updated);
134     }
135     return 0;
136 }
137 
OnDhcpSuccess(sptr<OHOS::NetsysNative::DhcpResultParcel> & dhcpResult)138 int32_t NetlinkNativeNotifyCallBack::OnDhcpSuccess(sptr<OHOS::NetsysNative::DhcpResultParcel> &dhcpResult)
139 {
140     std::cout << " [OnDhcpSuccess] " << std::endl;
141     EXPECT_NE(dhcpResult, nullptr);
142     return 0;
143 }
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)144 int32_t NetlinkNativeNotifyCallBack::OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
145 {
146     std::cout << " [OnBandwidthReachedLimit] " << std::endl;
147     EXPECT_FALSE(limitName.empty());
148     EXPECT_FALSE(iface.empty());
149     return 0;
150 }
151 
152 HWTEST_F(NetsysWrapperTest, TestServiceGet001, TestSize.Level1)
153 {
154     EXPECT_NE(netsysNativeService_, nullptr);
155 }
156 
157 HWTEST_F(NetsysWrapperTest, RegisterCallbackTest001, TestSize.Level1)
158 {
159     auto result = netsysNativeService_->RegisterNotifyCallback(nativeNotifyCallback_);
160     EXPECT_EQ(result, 0);
161 }
162 
163 HWTEST_F(NetsysWrapperTest, UnRegisterCallbackTest001, TestSize.Level1)
164 {
165     auto result = netsysNativeService_->UnRegisterNotifyCallback(nativeNotifyCallback_);
166     EXPECT_EQ(result, 0);
167 }
168 } // namespace OHOS::nmd