1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/proxy_resolution/proxy_info.h"
6
7 #include "net/base/net_errors.h"
8 #include "net/base/proxy_chain.h"
9 #include "net/base/proxy_server.h"
10 #include "net/log/net_log_with_source.h"
11 #include "net/proxy_resolution/proxy_config.h"
12 #include "net/proxy_resolution/proxy_list.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16 namespace {
17
TEST(ProxyInfoTest,ProxyInfoIsDirectOnly)18 TEST(ProxyInfoTest, ProxyInfoIsDirectOnly) {
19 // Test the is_direct_only() predicate.
20 ProxyInfo info;
21
22 // An empty ProxyInfo is not considered direct.
23 EXPECT_FALSE(info.is_direct_only());
24
25 info.UseDirect();
26 EXPECT_TRUE(info.is_direct_only());
27
28 info.UsePacString("DIRECT");
29 EXPECT_TRUE(info.is_direct_only());
30
31 info.UsePacString("PROXY myproxy:80");
32 EXPECT_FALSE(info.is_direct_only());
33
34 info.UsePacString("DIRECT; PROXY myproxy:80");
35 EXPECT_TRUE(info.is_direct());
36 EXPECT_FALSE(info.is_direct_only());
37
38 info.UsePacString("PROXY myproxy:80; DIRECT");
39 EXPECT_FALSE(info.is_direct());
40 EXPECT_FALSE(info.is_direct_only());
41 EXPECT_EQ(2u, info.proxy_list().size());
42 EXPECT_EQ("PROXY myproxy:80;DIRECT", info.proxy_list().ToDebugString());
43 // After falling back to direct, we shouldn't consider it DIRECT only.
44 EXPECT_TRUE(info.Fallback(ERR_PROXY_CONNECTION_FAILED, NetLogWithSource()));
45 EXPECT_TRUE(info.is_direct());
46 EXPECT_FALSE(info.is_direct_only());
47 }
48
49 } // namespace
50
TEST(ProxyInfoTest,UseVsOverrideProxyList)51 TEST(ProxyInfoTest, UseVsOverrideProxyList) {
52 ProxyInfo info;
53 ProxyList proxy_list;
54 proxy_list.Set("http://foo.com");
55 info.OverrideProxyList(proxy_list);
56 EXPECT_EQ("PROXY foo.com:80", info.proxy_list().ToDebugString());
57 proxy_list.Set("http://bar.com");
58 info.UseProxyList(proxy_list);
59 EXPECT_EQ("PROXY bar.com:80", info.proxy_list().ToDebugString());
60 }
61
TEST(ProxyInfoTest,IsForIpProtection)62 TEST(ProxyInfoTest, IsForIpProtection) {
63 ProxyInfo info;
64
65 ProxyChain regular_proxy_chain =
66 ProxyChain::FromSchemeHostAndPort(ProxyServer::SCHEME_HTTP, "foo", 80);
67 info.UseProxyChain(regular_proxy_chain);
68 EXPECT_FALSE(info.is_for_ip_protection());
69
70 ProxyChain ip_protection_proxy_chain = ProxyChain::ForIpProtection({
71 ProxyServer::FromSchemeHostAndPort(ProxyServer::SCHEME_HTTPS, "proxy1",
72 std::nullopt),
73 ProxyServer::FromSchemeHostAndPort(ProxyServer::SCHEME_HTTPS, "proxy2",
74 std::nullopt),
75 });
76 info.UseProxyChain(ip_protection_proxy_chain);
77 EXPECT_TRUE(info.is_for_ip_protection());
78 info.UseProxyChain(regular_proxy_chain);
79 EXPECT_FALSE(info.is_for_ip_protection());
80 }
81
TEST(ProxyInfoTest,UseProxyChain)82 TEST(ProxyInfoTest, UseProxyChain) {
83 ProxyInfo info;
84 ProxyChain proxy_chain =
85 ProxyChain::FromSchemeHostAndPort(ProxyServer::SCHEME_HTTP, "foo", 80);
86 info.UseProxyChain(proxy_chain);
87 EXPECT_EQ("PROXY foo:80", info.proxy_list().ToDebugString());
88 }
89
90 } // namespace net
91