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 #include <gtest/gtest.h>
16
17 #include "http_proxy.h"
18
19
20 namespace OHOS {
21 namespace NetManagerStandard {
22 using namespace testing::ext;
23
24 class NetProxyFromStringTest : public testing::Test {
25 public:
26 static void SetUpTestCase();
27 static void TearDownTestCase();
28 void SetUp();
29 void TearDown();
30 };
31
SetUpTestCase()32 void NetProxyFromStringTest::SetUpTestCase() {}
33
TearDownTestCase()34 void NetProxyFromStringTest::TearDownTestCase() {}
35
SetUp()36 void NetProxyFromStringTest::SetUp() {}
37
TearDown()38 void NetProxyFromStringTest::TearDown() {}
39
40 HWTEST_F(NetProxyFromStringTest, ProxyToStringAndFromString001, TestSize.Level1)
41 {
42 HttpProxy proxy {"192.168.1.1", 8080, {}};
43 auto proxyStr = proxy.ToString();
44 auto newProxyOpt = HttpProxy::FromString(proxyStr);
45 ASSERT_TRUE(newProxyOpt.has_value());
46 ASSERT_EQ(proxy, *newProxyOpt);
47 }
48
49 HWTEST_F(NetProxyFromStringTest, ProxyToStringAndFromString002, TestSize.Level1)
50 {
51 HttpProxy proxy {"192.168.33.15", 8081, {"fake.domen", "another.domen"}};
52 auto proxyStr = proxy.ToString();
53 auto newProxyOpt = HttpProxy::FromString(proxyStr);
54 ASSERT_TRUE(newProxyOpt.has_value());
55 ASSERT_EQ(*newProxyOpt, proxy);
56 }
57
58 HWTEST_F(NetProxyFromStringTest, EmptyString, TestSize.Level1)
59 {
60 auto newProxyOpt = HttpProxy::FromString("");
61 ASSERT_FALSE(newProxyOpt.has_value());
62 }
63
64 HWTEST_F(NetProxyFromStringTest, InvalidString, TestSize.Level1)
65 {
66 auto newProxyOpt = HttpProxy::FromString("not proxy");
67 ASSERT_FALSE(newProxyOpt.has_value());
68 }
69
70 HWTEST_F(NetProxyFromStringTest, WithoutPort, TestSize.Level1)
71 {
72 auto newProxyOpt = HttpProxy::FromString("fake.domen\t\t");
73 ASSERT_FALSE(newProxyOpt.has_value());
74 }
75
76 HWTEST_F(NetProxyFromStringTest, WithoutDomain, TestSize.Level1)
77 {
78 auto newProxyOpt = HttpProxy::FromString("\t0\t");
79 ASSERT_TRUE(newProxyOpt.has_value());
80 auto proxy = HttpProxy{"", 0, {}};
81 ASSERT_EQ(*newProxyOpt, proxy);
82 }
83
84 HWTEST_F(NetProxyFromStringTest, ExclusionList001, TestSize.Level1)
85 {
86 auto newProxyOpt = HttpProxy::FromString("fake.domain\t0\texclude.this");
87 ASSERT_TRUE(newProxyOpt.has_value());
88 auto proxy = HttpProxy{"fake.domain", 0, {"exclude.this"}};
89 ASSERT_EQ(*newProxyOpt, proxy);
90 }
91
92 HWTEST_F(NetProxyFromStringTest, ExclusionList002, TestSize.Level1)
93 {
94 auto newProxyOpt = HttpProxy::FromString("fake.domain\t0\texclude.this");
95 ASSERT_TRUE(newProxyOpt.has_value());
96 auto proxy = HttpProxy{"fake.domain", 0, {"exclude.this"}};
97 ASSERT_EQ(*newProxyOpt, proxy);
98 }
99
100 HWTEST_F(NetProxyFromStringTest, ExclusionList003, TestSize.Level1)
101 {
102 auto newProxyOpt = HttpProxy::FromString("fake.domain\t0\texclude.this,and.this");
103 ASSERT_TRUE(newProxyOpt.has_value());
104 auto proxy = HttpProxy{"fake.domain", 0, {"exclude.this", "and.this"}};
105 ASSERT_EQ(*newProxyOpt, proxy);
106 }
107
108 HWTEST_F(NetProxyFromStringTest, ExclusionList004, TestSize.Level1)
109 {
110 auto newProxyOpt = HttpProxy::FromString("fake.domain\t0\texclude.this,and.this,");
111 ASSERT_TRUE(newProxyOpt.has_value());
112 auto proxy = HttpProxy{"fake.domain", 0, {"exclude.this", "and.this"}};
113 ASSERT_EQ(*newProxyOpt, proxy);
114 }
115
116 HWTEST_F(NetProxyFromStringTest, PortOutOfRange001, TestSize.Level1)
117 {
118 auto newProxyOpt = HttpProxy::FromString("fake.domain\t-1\texclude.this,and.this");
119 ASSERT_FALSE(newProxyOpt.has_value());
120 }
121
122 HWTEST_F(NetProxyFromStringTest, PortOutOfRange002, TestSize.Level1)
123 {
124 auto newProxyOpt = HttpProxy::FromString("fake.domain\t70000\texclude.this,and.this");
125 ASSERT_FALSE(newProxyOpt.has_value());
126 }
127
128 HWTEST_F(NetProxyFromStringTest, PortOutOfRange003, TestSize.Level1)
129 {
130 auto newProxyOpt = HttpProxy::FromString("fake.domain\t4294967304\texclude.this,and.this");
131 ASSERT_FALSE(newProxyOpt.has_value());
132 }
133
134 HWTEST_F(NetProxyFromStringTest, PortNotInteger, TestSize.Level1)
135 {
136 auto newProxyOpt = HttpProxy::FromString("fake.domain\tportGoesHere\texclude.this,and.this");
137 ASSERT_FALSE(newProxyOpt.has_value());
138 }
139 } // namespace NetManagerStandard
140 } // namespace OHOS