1 /*
2 * Copyright (c) 2025 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 "accesstoken_kit.h"
17 #include "mock_timer.h"
18 #include "net_all_capabilities.h"
19 #include "net_connection.h"
20 #include "pac_server.h"
21 #include "token_setproc.h"
22 #include <gtest/gtest.h>
23
24 using namespace OHOS::Security::AccessToken;
25
26 PermissionDef pacPerm = {
27 .permissionName = "ohos.permission.SET_PAC_URL",
28 .bundleName = "net_client_pac_file_url_test",
29 .grantMode = 1,
30 .label = "label",
31 .labelId = 1,
32 .description = "Test web connect maneger",
33 .descriptionId = 1,
34 .availableLevel = APL_SYSTEM_BASIC,
35 };
36
37 PermissionStateFull pacState = {
38 .grantFlags = {2},
39 .grantStatus = {PermissionState::PERMISSION_GRANTED},
40 .isGeneral = true,
41 .permissionName = "ohos.permission.SET_PAC_URL",
42 .resDeviceID = {"local"},
43 };
44
45 HapPolicyParams testPolicyPrams = {
46 .apl = APL_SYSTEM_BASIC,
47 .domain = "test.domain",
48 .permList = {pacPerm},
49 .permStateList = {pacState},
50 };
51
52 HapInfoParams testInfoParms = {
53 .userID = 1,
54 .bundleName = "net_client_pac_file_url_test",
55 .instIndex = 0,
56 .appIDDesc = "test",
57 .isSystemApp = true,
58 };
Test1()59 static void Test1()
60 {
61 int32_t ret = -1;
62 {
63 std::string url = "http://127.0.0.1/index";
64 std::string host = "127.0.0.1";
65 char proxy[1024];
66 ret = OH_NetConn_FindProxyForURL(url.c_str(), nullptr, proxy);
67 EXPECT_EQ(ret, 0);
68 EXPECT_EQ(std::string(proxy), "DIRECT");
69 }
70 {
71 std::string url = "http://test.local/index";
72 char proxy[1024];
73 ret = OH_NetConn_FindProxyForURL(url.c_str(), nullptr, proxy);
74 EXPECT_EQ(ret, 0);
75 EXPECT_EQ(std::string(proxy), "DIRECT");
76 }
77 {
78 std::string url = "http://192.168.0.111/index";
79 char proxy[1024];
80 ret = OH_NetConn_FindProxyForURL(url.c_str(), nullptr, proxy);
81 EXPECT_EQ(ret, 0);
82 EXPECT_EQ(std::string(proxy), "DIRECT");
83 }
84 {
85 std::string url = "http://192.168.111.111/index";
86 char proxy[1024];
87 ret = OH_NetConn_FindProxyForURL(url.c_str(), nullptr, proxy);
88 EXPECT_EQ(ret, 0);
89 EXPECT_EQ(std::string(proxy), "DIRECT");
90 }
91 {
92 std::string url = "http://hostname/index";
93 char proxy[1024];
94 ret = OH_NetConn_FindProxyForURL(url.c_str(), nullptr, proxy);
95 EXPECT_EQ(ret, 0);
96 EXPECT_EQ(std::string(proxy), "PROXY special-proxy.com:8001");
97 }
98 }
99
TEST(MyTests,PacFileUrlClient)100 TEST(MyTests, PacFileUrlClient)
101 {
102 uint64_t currentID_ = GetSelfTokenID();
103 AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(testInfoParms, testPolicyPrams);
104 AccessTokenID accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
105 SetSelfTokenID(tokenIdEx.tokenIDEx);
106
107 StartHttpServer();
108 sleep(6);
109 std::string url = "http://localhost:8888/";
110 char pacFileUrl[1024];
111 int32_t ret = OH_NetConn_SetPacFileUrl(url.c_str());
112 EXPECT_EQ(ret, 0);
113 ret = OH_NetConn_SetProxyMode(OHOS::NetManagerStandard::ProxyModeType::PROXY_MODE_AUTO);
114 EXPECT_EQ(ret, 0);
115 {
116 std::string url = "http://127.0.0.1/index";
117 std::string host = "127.0.0.1";
118 char proxy[1024];
119 ret = OH_NetConn_FindProxyForURL(url.c_str(), nullptr, proxy);
120 EXPECT_EQ(ret, 0);
121 EXPECT_EQ(std::string(proxy), "DIRECT");
122 }
123 ret = OH_NetConn_GetPacFileUrl(pacFileUrl);
124 EXPECT_EQ(ret, 0);
125 EXPECT_EQ(std::string(pacFileUrl), url);
126 Test1();
127 AccessTokenKit::DeleteToken(accessID_);
128 SetSelfTokenID(currentID_);
129 }