1 /*
2  * Copyright (c) 2023 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "accesstoken_kit.h"
20 
21 #define private public
22 #include "net_proxy_adapter_impl.h"
23 
24 #include "net_conn_client.h"
25 #include "token_setproc.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::NetManagerStandard;
31 using namespace OHOS::Security::AccessToken;
32 using namespace OHOS::EventFwk;
33 
34 namespace OHOS::EventFwk {
35 namespace {
36 bool g_subscribeCommonEventRet = true;
37 bool g_unSubscribeCommonEventRet = true;
38 }
SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)39 bool CommonEventManager::SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
40 {
41     return g_subscribeCommonEventRet;
42 }
43 
UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)44 bool CommonEventManager::UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
45 {
46     return g_unSubscribeCommonEventRet;
47 }
48 }
49 
50 namespace OHOS::NetManagerStandard {
51 class NetConnClientMock : public NetConnClient {
52 public:
53     MOCK_METHOD1(GetGlobalHttpProxy, int32_t(HttpProxy &));
54     MOCK_METHOD1(GetDefaultHttpProxy, int32_t(HttpProxy &));
55 };
56 NetConnClientMock *g_mock = nullptr;
GetInstance()57 NetConnClient &NetConnClient::GetInstance()
58 {
59     if (!g_mock) {
60         g_mock = new NetConnClientMock();
61     }
62     return *g_mock;
63 }
64 }
65 
66 namespace OHOS::NWeb {
67 namespace {
68 using Security::AccessToken::AccessTokenID;
69 using namespace Security::AccessToken;
70 
71 HapInfoParams testInfoParms = {
72     .userID = 1,
73     .bundleName = "web_conn_manager_test",
74     .instIndex = 0,
75     .appIDDesc = "test",
76     .isSystemApp = true,
77 };
78 
79 PermissionDef testPermDef = {
80     .permissionName = "ohos.permission.GET_NETWORK_INFO",
81     .bundleName = "web_conn_manager_test",
82     .grantMode = 1,
83     .label = "label",
84     .labelId = 1,
85     .description = "Test web connect maneger",
86     .descriptionId = 1,
87     .availableLevel = APL_SYSTEM_BASIC,
88 };
89 
90 PermissionDef testInternalPermDef = {
91     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
92     .bundleName = "web_conn_manager_test",
93     .grantMode = 1,
94     .availableLevel = APL_SYSTEM_BASIC,
95     .label = "label",
96     .labelId = 1,
97     .description = "Test web connect manager internal",
98     .descriptionId = 1,
99 };
100 
101 PermissionDef testInternetPermDef = {
102     .permissionName = "ohos.permission.INTERNET",
103     .bundleName = "web_conn_manager_test",
104     .grantMode = 1,
105     .availableLevel = APL_SYSTEM_BASIC,
106     .label = "label",
107     .labelId = 1,
108     .description = "Test web connect manager internet",
109     .descriptionId = 1,
110 };
111 
112 PermissionStateFull testState = {
113     .grantFlags = {2},
114     .grantStatus = {PermissionState::PERMISSION_GRANTED},
115     .isGeneral = true,
116     .permissionName = "ohos.permission.GET_NETWORK_INFO",
117     .resDeviceID = {"local"},
118 };
119 
120 PermissionStateFull testInternalState = {
121     .permissionName = "ohos.permission.CONNECTIVITY_INTERNAL",
122     .isGeneral = true,
123     .resDeviceID = {"local"},
124     .grantStatus = {PermissionState::PERMISSION_GRANTED},
125     .grantFlags = {2},
126 };
127 
128 PermissionStateFull testInternetState = {
129     .permissionName = "ohos.permission.INTERNET",
130     .isGeneral = true,
131     .resDeviceID = {"local"},
132     .grantStatus = {PermissionState::PERMISSION_GRANTED},
133     .grantFlags = {2},
134 };
135 
136 HapPolicyParams testPolicyPrams = {
137     .apl = APL_SYSTEM_BASIC,
138     .domain = "test.domain",
139     .permList = {testPermDef, testInternalPermDef, testInternetPermDef},
140     .permStateList = {testState, testInternalState, testInternetState},
141 };
142 } // namespace
143 
144 class NetProxyAdapterTest : public testing::Test {
145 public:
146     static void SetUpTestCase(void);
147     static void TearDownTestCase(void);
148     void SetUp();
149     void TearDown();
150 };
151 
SetUpTestCase(void)152 void NetProxyAdapterTest::SetUpTestCase(void) {}
153 
TearDownTestCase(void)154 void NetProxyAdapterTest::TearDownTestCase(void) {}
155 
SetUp(void)156 void NetProxyAdapterTest::SetUp(void) {}
157 
TearDown(void)158 void NetProxyAdapterTest::TearDown(void) {}
159 
160 class AccessToken {
161 public:
AccessToken()162     AccessToken() : currentID_(GetSelfTokenID())
163     {
164         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
165         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
166         SetSelfTokenID(tokenIdEx.tokenIDEx);
167     }
~AccessToken()168     ~AccessToken()
169     {
170         AccessTokenKit::DeleteToken(accessID_);
171         SetSelfTokenID(currentID_);
172     }
173 
174 private:
175     AccessTokenID currentID_;
176     AccessTokenID accessID_ = 0;
177 };
178 
179 class MockNetProxyEventCallbackAdapter : public NetProxyEventCallbackAdapter {
180     public:
181         MockNetProxyEventCallbackAdapter() = default;
Changed(const std::string & host,const uint16_t & port,const std::string & pacUrl,const std::vector<std::string> & exclusionList)182         void Changed(const std::string& host, const uint16_t& port, const std::string& pacUrl,
183                      const std::vector<std::string>& exclusionList) {}
184 };
185 
186 /**
187  * @tc.name: NetProxyAdapterTest_OnReceiveEvent_001.
188  * @tc.desc: IMF adapter unittest.
189  * @tc.type: FUNC.
190  * @tc.require:
191  */
192 HWTEST_F(NetProxyAdapterTest, NetProxyAdapterTest_OnReceiveEvent_001, TestSize.Level1)
193 {
194     EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
195     EventFwk::CommonEventSubscribeInfo info(skill);
196     std::shared_ptr<NetProxyEventCallbackAdapter> eventCallback =
197         std::make_shared<MockNetProxyEventCallbackAdapter>();
198     NetProxyEventSubscriber criber(info, eventCallback);
199     EXPECT_NE(criber.eventCallback_, nullptr);
200     EventFwk::CommonEventData data;
201 
202     AccessToken token;
203     HttpProxy httpProxy = {"tes.web.com", 0, {}};
204     int32_t ret = NetConnClient::GetInstance().SetGlobalHttpProxy(httpProxy);
205     EXPECT_EQ(ret, 0);
206     criber.OnReceiveEvent(data);
207     Want want;
208     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HTTP_PROXY_CHANGE);
209     data.SetWant(want);
210     EXPECT_CALL(*g_mock, GetGlobalHttpProxy(::testing::_))
211         .Times(1)
212         .WillRepeatedly(::testing::Return(NetManagerStandard::NET_CONN_SUCCESS));
213     criber.OnReceiveEvent(data);
214     EXPECT_CALL(*g_mock, GetGlobalHttpProxy(::testing::_))
215         .Times(1)
216         .WillRepeatedly(::testing::Return(NetManagerStandard::NET_CONN_ERR_INPUT_NULL_PTR));
217     criber.OnReceiveEvent(data);
218     std::string host;
219     uint16_t port;
220     std::string pacUrl;
221     std::string exclusion;
222     EXPECT_CALL(*g_mock, GetGlobalHttpProxy(::testing::_))
223         .Times(1)
224         .WillRepeatedly(::testing::Return(NetManagerStandard::NET_CONN_SUCCESS));
225     NetProxyAdapterImpl::GetInstance().GetProperty(host, port, pacUrl, exclusion);
226     EXPECT_CALL(*g_mock, GetGlobalHttpProxy(::testing::_))
227         .Times(1)
228         .WillRepeatedly(::testing::Return(NetManagerStandard::NET_CONN_ERR_INPUT_NULL_PTR));
229     NetProxyAdapterImpl::GetInstance().GetProperty(host, port, pacUrl, exclusion);
230     testing::Mock::AllowLeak(g_mock);
231 }
232 
233 /**
234  * @tc.name: NetProxyAdapterTest_RegNetProxyEvent_002.
235  * @tc.desc: IMF adapter unittest.
236  * @tc.type: FUNC.
237  * @tc.require:
238  */
239 HWTEST_F(NetProxyAdapterTest, NetProxyAdapterTest_RegNetProxyEvent_002, TestSize.Level1)
240 {
241     NetProxyAdapterImpl::GetInstance().RegNetProxyEvent(nullptr);
242     EXPECT_EQ(NetProxyAdapterImpl::GetInstance().cb_, nullptr);
243     bool result = NetProxyAdapterImpl::GetInstance().StartListen();
244     EXPECT_FALSE(result);
245     std::shared_ptr<NetProxyEventCallbackAdapter> eventCallback =
246         std::make_shared<MockNetProxyEventCallbackAdapter>();
247     NetProxyAdapterImpl::GetInstance().RegNetProxyEvent(eventCallback);
248     EXPECT_NE(NetProxyAdapterImpl::GetInstance().cb_, nullptr);
249     result = NetProxyAdapterImpl::GetInstance().StartListen();
250     EXPECT_TRUE(result);
251     NetManagerStandard::HttpProxy httpProxy;
252     httpProxy.SetHost("");
253     NetManagerStandard::NetConnClient::GetInstance().SetAppHttpProxy(httpProxy);
254     httpProxy.SetHost("NONE");
255     NetManagerStandard::NetConnClient::GetInstance().SetAppHttpProxy(httpProxy);
256     httpProxy.SetHost("test_web");
257     NetManagerStandard::NetConnClient::GetInstance().SetAppHttpProxy(httpProxy);
258 
259     EventFwk::CommonEventData data;
260     Want want;
261     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HTTP_PROXY_CHANGE);
262     data.SetWant(want);
263     EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
264     EventFwk::CommonEventSubscribeInfo info(skill);
265     NetProxyEventSubscriber criber(info, eventCallback);
266     NetManagerStandard::NetConnClient::GetInstance().appHttpProxy_.SetHost("web_test");
267     criber.OnReceiveEvent(data);
268     std::string host;
269     uint16_t port;
270     std::string pacUrl;
271     std::string exclusion;
272     NetProxyAdapterImpl::GetInstance().GetProperty(host, port, pacUrl, exclusion);
273     NetManagerStandard::NetConnClient::GetInstance().appHttpProxy_.SetHost("NONE");
274     NetProxyAdapterImpl::GetInstance().GetProperty(host, port, pacUrl, exclusion);
275     std::list<std::string> listString;
276     listString.push_back("testweb");
277     NetManagerStandard::HttpProxy proxy("test", 1, listString);
278     NetManagerStandard::NetConnClient::GetInstance().appHttpProxy_ = proxy;
279     NetProxyAdapterImpl::GetInstance().GetProperty(host, port, pacUrl, exclusion);
280 
281     g_subscribeCommonEventRet = false;
282     result = NetProxyAdapterImpl::GetInstance().StartListen();
283     EXPECT_FALSE(result);
284     NetProxyAdapterImpl::GetInstance().cb_ = nullptr;
285     NetManagerStandard::NetConnClient::GetInstance().SetAppHttpProxy(httpProxy);
286 }
287 
288 /**
289  * @tc.name: NetProxyAdapterTest_GetProperty_003.
290  * @tc.desc: IMF adapter unittest.
291  * @tc.type: FUNC.
292  * @tc.require:
293  */
294 HWTEST_F(NetProxyAdapterTest, NetProxyAdapterTest_GetProperty_003, TestSize.Level1)
295 {
296     std::string host;
297     uint16_t port;
298     std::string pacUrl;
299     std::string exclusion;
300     NetProxyAdapterImpl::GetInstance().GetProperty(host, port, pacUrl, exclusion);
301     EXPECT_NE(NetProxyAdapterImpl::GetInstance().commonEventSubscriber_, nullptr);
302     g_unSubscribeCommonEventRet = false;
303     NetProxyAdapterImpl::GetInstance().StopListen();
304     g_unSubscribeCommonEventRet = true;
305     NetProxyAdapterImpl::GetInstance().StopListen();
306     NetProxyAdapterImpl::GetInstance().commonEventSubscriber_ = nullptr;
307     NetProxyAdapterImpl::GetInstance().StopListen();
308 }
309 
310 /**
311  * @tc.name: NetProxyAdapterTest_Encode_004.
312  * @tc.desc: IMF adapter unittest.
313  * @tc.type: FUNC.
314  * @tc.require:
315  */
316 HWTEST_F(NetProxyAdapterTest, NetProxyAdapterTest_Encode_004, TestSize.Level1)
317 {
318     std::string source = "";
319     std::string  result = Base64::Encode(source);
320     EXPECT_TRUE(result.empty());
321     result = Base64::Decode(source);
322     EXPECT_TRUE(result.empty());
323     source = "++webtest";
324     Base64::Decode(source);
325     source = "webtest++++++";
326     Base64::Decode(source);
327     result = Base64::Encode(source);
328     EXPECT_FALSE(result.empty());
329 }
330 } // namespace OHOS::NWeb
331