This source file includes following definitions.
- ExpectProxyServerEquals
- TEST
- TEST
- TEST
#include "net/proxy/proxy_config.h"
#include "net/proxy/proxy_config_service_common_unittest.h"
#include "net/proxy/proxy_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
void ExpectProxyServerEquals(const char* expectation,
const ProxyList& proxy_servers) {
if (expectation == NULL) {
EXPECT_TRUE(proxy_servers.IsEmpty());
} else {
EXPECT_EQ(expectation, proxy_servers.ToPacString());
}
}
TEST(ProxyConfigTest, Equals) {
ProxyConfig config1;
config1.set_auto_detect(true);
ProxyConfig config2;
config2.set_auto_detect(false);
EXPECT_FALSE(config1.Equals(config2));
EXPECT_FALSE(config2.Equals(config1));
config2.set_auto_detect(true);
EXPECT_TRUE(config1.Equals(config2));
EXPECT_TRUE(config2.Equals(config1));
config2.set_pac_url(GURL("http://wpad/wpad.dat"));
EXPECT_FALSE(config1.Equals(config2));
EXPECT_FALSE(config2.Equals(config1));
config1.set_pac_url(GURL("http://wpad/wpad.dat"));
EXPECT_TRUE(config1.Equals(config2));
EXPECT_TRUE(config2.Equals(config1));
config2.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
config2.proxy_rules().single_proxies.SetSingleProxyServer(
ProxyServer::FromURI("myproxy:80", ProxyServer::SCHEME_HTTP));
EXPECT_FALSE(config1.Equals(config2));
EXPECT_FALSE(config2.Equals(config1));
config1.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
config1.proxy_rules().single_proxies.SetSingleProxyServer(
ProxyServer::FromURI("myproxy:100", ProxyServer::SCHEME_HTTP));
EXPECT_FALSE(config1.Equals(config2));
EXPECT_FALSE(config2.Equals(config1));
config1.proxy_rules().single_proxies.SetSingleProxyServer(
ProxyServer::FromURI("myproxy", ProxyServer::SCHEME_HTTP));
EXPECT_TRUE(config1.Equals(config2));
EXPECT_TRUE(config2.Equals(config1));
config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
EXPECT_FALSE(config1.Equals(config2));
EXPECT_FALSE(config2.Equals(config1));
config1.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
EXPECT_TRUE(config1.Equals(config2));
EXPECT_TRUE(config2.Equals(config1));
config2.proxy_rules().reverse_bypass = true;
EXPECT_FALSE(config1.Equals(config2));
EXPECT_FALSE(config2.Equals(config1));
config1.proxy_rules().reverse_bypass = true;
EXPECT_TRUE(config1.Equals(config2));
EXPECT_TRUE(config2.Equals(config1));
}
TEST(ProxyConfigTest, ParseProxyRules) {
const struct {
const char* proxy_rules;
ProxyConfig::ProxyRules::Type type;
const char* single_proxy;
const char* proxy_for_http;
const char* proxy_for_https;
const char* proxy_for_ftp;
const char* fallback_proxy;
} tests[] = {
{
"myproxy:80",
ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
"PROXY myproxy:80",
NULL,
NULL,
NULL,
NULL,
},
{
"myproxy:80,https://myotherproxy",
ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
"PROXY myproxy:80;HTTPS myotherproxy:443",
NULL,
NULL,
NULL,
NULL,
},
{
"http=myproxy:80",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"PROXY myproxy:80",
NULL,
NULL,
NULL,
},
{
"ftp=ftp-proxy ; https=socks4://foopy",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
NULL,
"SOCKS foopy:1080",
"PROXY ftp-proxy:80",
NULL,
},
{
"foopy ; ftp=ftp-proxy",
ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
"PROXY foopy:80",
NULL,
NULL,
NULL,
NULL,
},
{
"ftp=ftp-proxy ; foopy",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
NULL,
NULL,
"PROXY ftp-proxy:80",
NULL,
},
{
"ftp=ftp1,ftp2,ftp3",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
NULL,
NULL,
"PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80",
NULL,
},
{
"http=http1,http2; http=http3",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"PROXY http1:80;PROXY http2:80;PROXY http3:80",
NULL,
NULL,
NULL,
},
{
"ftp=ftp1,ftp2,ftp3 ; http=http1,http2; ",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"PROXY http1:80;PROXY http2:80",
NULL,
"PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80",
NULL,
},
{
"http=https://secure_proxy; ftp=socks4://socks_proxy; https=socks://foo",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"HTTPS secure_proxy:443",
"SOCKS5 foo:1080",
"SOCKS socks_proxy:1080",
NULL,
},
{
"socks=foopy",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
NULL,
NULL,
NULL,
"SOCKS foopy:1080",
},
{
"http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"PROXY httpproxy:80",
"PROXY httpsproxy:80",
"PROXY ftpproxy:80",
"SOCKS foopy:1080",
},
{
"http=httpproxy ; https=httpsproxy ; socks=socks5://foopy ",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"PROXY httpproxy:80",
"PROXY httpsproxy:80",
NULL,
"SOCKS5 foopy:1080",
},
{
"crazy=foopy ; foo=bar ; https=myhttpsproxy",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
NULL,
"PROXY myhttpsproxy:80",
NULL,
NULL,
},
{
"http=direct://,myhttpproxy; https=direct://",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"DIRECT;PROXY myhttpproxy:80",
"DIRECT",
NULL,
NULL,
},
{
"http=myhttpproxy,direct://",
ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
NULL,
"PROXY myhttpproxy:80;DIRECT",
NULL,
NULL,
NULL,
},
};
ProxyConfig config;
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
config.proxy_rules().ParseFromString(tests[i].proxy_rules);
EXPECT_EQ(tests[i].type, config.proxy_rules().type);
ExpectProxyServerEquals(tests[i].single_proxy,
config.proxy_rules().single_proxies);
ExpectProxyServerEquals(tests[i].proxy_for_http,
config.proxy_rules().proxies_for_http);
ExpectProxyServerEquals(tests[i].proxy_for_https,
config.proxy_rules().proxies_for_https);
ExpectProxyServerEquals(tests[i].proxy_for_ftp,
config.proxy_rules().proxies_for_ftp);
ExpectProxyServerEquals(tests[i].fallback_proxy,
config.proxy_rules().fallback_proxies);
}
}
TEST(ProxyConfigTest, ProxyRulesSetBypassFlag) {
ProxyConfig::ProxyRules rules;
ProxyInfo result;
rules.ParseFromString("http=httpproxy:80");
rules.bypass_rules.AddRuleFromString(".com");
rules.Apply(GURL("http://example.com"), &result);
EXPECT_TRUE(result.is_direct_only());
EXPECT_TRUE(result.did_bypass_proxy());
rules.Apply(GURL("http://example.org"), &result);
EXPECT_FALSE(result.is_direct());
EXPECT_FALSE(result.did_bypass_proxy());
rules.reverse_bypass = true;
rules.Apply(GURL("http://example.org"), &result);
EXPECT_TRUE(result.is_direct_only());
EXPECT_TRUE(result.did_bypass_proxy());
rules.Apply(GURL("http://example.com"), &result);
EXPECT_FALSE(result.is_direct());
EXPECT_FALSE(result.did_bypass_proxy());
}
}
}