Imported Upstream version 5.0.0.42

Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-04-10 11:41:01 +00:00
parent 1190d13a04
commit 6bdd276d05
19939 changed files with 3099680 additions and 93811 deletions

View File

@@ -0,0 +1,53 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Net.WebProxy.Tests", "tests\System.Net.WebProxy.Tests.csproj", "{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}"
ProjectSection(ProjectDependencies) = postProject
{53D09AF4-0C13-4197-B8AD-9746F0374E88} = {53D09AF4-0C13-4197-B8AD-9746F0374E88}
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{350C956A-B4ED-4376-9B04-2908528D10FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Net.WebProxy", "ref\System.Net.WebProxy.csproj", "{14FAFC3A-8266-45C7-8604-8C2CB567E50D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{827B5CCD-073A-4A75-9661-73D54F0A3528}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Net.WebProxy", "src\System.Net.WebProxy.csproj", "{53D09AF4-0C13-4197-B8AD-9746F0374E88}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{096E7AD0-17A2-4EAF-9AC5-2F0FC67A4112}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
DebugNETCoreAppnet461-Windows_NTnetstandard|AnyCPU = DebugNETCoreAppnet461-Windows_NTnetstandard|AnyCPU
ReleaseNETCoreAppnet461-Windows_NTnetstandard|AnyCPU = ReleaseNETCoreAppnet461-Windows_NTnetstandard|AnyCPU
DebugNETCoreAppnetstandardnetstandard|AnyCPU = DebugNETCoreAppnetstandardnetstandard|AnyCPU
ReleaseNETCoreAppnetstandardnetstandard|AnyCPU = ReleaseNETCoreAppnetstandardnetstandard|AnyCPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.DebugNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.ActiveCfg = netstandard-Debug|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.DebugNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.Build.0 = netstandard-Debug|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.ReleaseNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.ActiveCfg = netstandard-Release|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.ReleaseNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.Build.0 = netstandard-Release|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.DebugNETCoreAppnetstandardnetstandard|AnyCPU.ActiveCfg = netstandard-Debug|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.DebugNETCoreAppnetstandardnetstandard|AnyCPU.Build.0 = netstandard-Debug|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.ReleaseNETCoreAppnetstandardnetstandard|AnyCPU.ActiveCfg = netstandard-Release|Any CPU
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}.ReleaseNETCoreAppnetstandardnetstandard|AnyCPU.Build.0 = netstandard-Release|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.DebugNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.ActiveCfg = net461-Windows_NT-Debug|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.DebugNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.Build.0 = net461-Windows_NT-Debug|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.ReleaseNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.ActiveCfg = net461-Windows_NT-Release|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.ReleaseNETCoreAppnet461-Windows_NTnetstandard|AnyCPU.Build.0 = net461-Windows_NT-Release|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.DebugNETCoreAppnetstandardnetstandard|AnyCPU.ActiveCfg = netstandard-Debug|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.DebugNETCoreAppnetstandardnetstandard|AnyCPU.Build.0 = netstandard-Debug|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.ReleaseNETCoreAppnetstandardnetstandard|AnyCPU.ActiveCfg = netstandard-Release|Any CPU
{53D09AF4-0C13-4197-B8AD-9746F0374E88}.ReleaseNETCoreAppnetstandardnetstandard|AnyCPU.Build.0 = netstandard-Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0D1E2954-A5C7-4B8C-932A-31EB4A96A726} = {0D1E2954-A5C7-4B8C-932A-31EB4A96A726}
{53D09AF4-0C13-4197-B8AD-9746F0374E88} = {53D09AF4-0C13-4197-B8AD-9746F0374E88}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\dir.props" />
<PropertyGroup>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<IsNETCoreApp>true</IsNETCoreApp>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ------------------------------------------------------------------------------
// Changes to this file must follow the http://aka.ms/api-review process.
// ------------------------------------------------------------------------------
namespace System.Net
{
public interface IWebProxyScript
{
void Close();
bool Load(System.Uri scriptLocation, string script, System.Type helperType);
string Run(string url, string host);
}
public class WebProxy : System.Net.IWebProxy, System.Runtime.Serialization.ISerializable
{
public WebProxy() { }
public WebProxy(System.Uri Address) { }
public WebProxy(System.Uri Address, bool BypassOnLocal) { }
public WebProxy(System.Uri Address, bool BypassOnLocal, string[] BypassList) { }
public WebProxy(System.Uri Address, bool BypassOnLocal, string[] BypassList, System.Net.ICredentials Credentials) { }
public WebProxy(string Host, int Port) { }
public WebProxy(string Address) { }
public WebProxy(string Address, bool BypassOnLocal) { }
public WebProxy(string Address, bool BypassOnLocal, string[] BypassList) { }
public WebProxy(string Address, bool BypassOnLocal, string[] BypassList, System.Net.ICredentials Credentials) { }
public Uri Address { get { throw null; } set { } }
public bool BypassProxyOnLocal { get { throw null; } set { } }
public string[] BypassList { get { throw null; } set { } }
public System.Collections.ArrayList BypassArrayList { get { throw null; } }
public System.Net.ICredentials Credentials { get { throw null; } set { } }
public bool UseDefaultCredentials { get { throw null; } set { } }
public Uri GetProxy(Uri destination) { throw null; }
public bool IsBypassed(Uri host) { throw null; }
protected WebProxy(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
protected virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) { }
[Obsolete("This method has been deprecated. Please use the proxy selected for you by default. http://go.microsoft.com/fwlink/?linkid=14202")]
public static WebProxy GetDefaultProxy() { throw null; }
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="System.Net.WebProxy.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Collections.NonGeneric\ref\System.Collections.NonGeneric.csproj" />
<ProjectReference Include="..\..\System.Net.Primitives\ref\System.Net.Primitives.csproj" />
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Extensions\ref\System.Runtime.Extensions.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
net461-Windows_NT;
netcoreapp;
uap;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<ProjectGuid>{53D09AF4-0C13-4197-B8AD-9746F0374E88}</ProjectGuid>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPartialFacadeAssembly Condition="'$(TargetGroup)'=='net461'">true</IsPartialFacadeAssembly>
</PropertyGroup>
<!-- Default configurations to help VS understand the options -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='net461-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='net461-Windows_NT-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netcoreapp-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='uap-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='uap-Release|AnyCPU'" />
<ItemGroup Condition="'$(IsPartialFacadeAssembly)' != 'true'">
<Compile Include="System\Net\IWebProxyScript.cs" />
<Compile Include="System\Net\WebProxy.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'net461'">
<TargetingPackReference Include="System" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Diagnostics.Tools" />
<Reference Include="System.Net.NameResolution" />
<Reference Include="System.Net.NetworkInformation" />
<Reference Include="System.Net.Primitives" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Text.RegularExpressions" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace System.Net
{
public interface IWebProxyScript
{
void Close();
bool Load(Uri scriptLocation, string script, Type helperType);
string Run(string url, string host);
}
}

View File

@@ -0,0 +1,238 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections;
using System.Globalization;
using System.Net.NetworkInformation;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
namespace System.Net
{
[Serializable]
public class WebProxy : IWebProxy, ISerializable
{
private ArrayList _bypassList;
private Regex[] _regExBypassList;
public WebProxy() : this((Uri)null, false, null, null) { }
public WebProxy(Uri Address) : this(Address, false, null, null) { }
public WebProxy(Uri Address, bool BypassOnLocal) : this(Address, BypassOnLocal, null, null) { }
public WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList) : this(Address, BypassOnLocal, BypassList, null) { }
public WebProxy(Uri Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
{
this.Address = Address;
this.Credentials = Credentials;
this.BypassProxyOnLocal = BypassOnLocal;
if (BypassList != null)
{
_bypassList = new ArrayList(BypassList);
UpdateRegExList(true);
}
}
public WebProxy(string Host, int Port)
: this(new Uri("http://" + Host + ":" + Port.ToString(CultureInfo.InvariantCulture)), false, null, null)
{
}
public WebProxy(string Address)
: this(CreateProxyUri(Address), false, null, null)
{
}
public WebProxy(string Address, bool BypassOnLocal)
: this(CreateProxyUri(Address), BypassOnLocal, null, null)
{
}
public WebProxy(string Address, bool BypassOnLocal, string[] BypassList)
: this(CreateProxyUri(Address), BypassOnLocal, BypassList, null)
{
}
public WebProxy(string Address, bool BypassOnLocal, string[] BypassList, ICredentials Credentials)
: this(CreateProxyUri(Address), BypassOnLocal, BypassList, Credentials)
{
}
public Uri Address { get; set; }
public bool BypassProxyOnLocal { get; set; }
public string[] BypassList
{
get { return _bypassList != null ? (string[])_bypassList.ToArray(typeof(string)) : Array.Empty<string>(); }
set
{
_bypassList = new ArrayList(value);
UpdateRegExList(true);
}
}
public ArrayList BypassArrayList => _bypassList ?? (_bypassList = new ArrayList());
public ICredentials Credentials { get; set; }
public bool UseDefaultCredentials
{
get { return Credentials == CredentialCache.DefaultCredentials; }
set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
}
public Uri GetProxy(Uri destination)
{
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
return IsBypassed(destination) ? destination : Address;
}
private static Uri CreateProxyUri(string address) =>
address == null ? null :
address.IndexOf("://") == -1 ? new Uri("http://" + address) :
new Uri(address);
private void UpdateRegExList(bool canThrow)
{
Regex[] regExBypassList = null;
ArrayList bypassList = _bypassList;
try
{
if (bypassList != null && bypassList.Count > 0)
{
regExBypassList = new Regex[bypassList.Count];
for (int i = 0; i < bypassList.Count; i++)
{
regExBypassList[i] = new Regex((string)bypassList[i], RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
}
}
catch
{
if (!canThrow)
{
_regExBypassList = null;
return;
}
throw;
}
// Update field here, as it could throw earlier in the loop
_regExBypassList = regExBypassList;
}
private bool IsMatchInBypassList(Uri input)
{
UpdateRegExList(false);
if (_regExBypassList != null)
{
string matchUriString = input.IsDefaultPort ?
input.Scheme + "://" + input.Host :
input.Scheme + "://" + input.Host + ":" + input.Port.ToString();
foreach (Regex r in _regExBypassList)
{
if (r.IsMatch(matchUriString))
{
return true;
}
}
}
return false;
}
private bool IsLocal(Uri host)
{
string hostString = host.Host;
IPAddress hostAddress;
if (IPAddress.TryParse(hostString, out hostAddress))
{
return IPAddress.IsLoopback(hostAddress) || IsAddressLocal(hostAddress);
}
// No dot? Local.
int dot = hostString.IndexOf('.');
if (dot == -1)
{
return true;
}
// If it matches the primary domain, it's local. (Whether or not the hostname matches.)
string local = "." + IPGlobalProperties.GetIPGlobalProperties().DomainName;
return
local.Length == (hostString.Length - dot) &&
string.Compare(local, 0, hostString, dot, local.Length, StringComparison.OrdinalIgnoreCase) == 0;
}
private static bool IsAddressLocal(IPAddress ipAddress)
{
// Perf note: The .NET Framework caches this and then uses network change notifications to track
// whether the set should be recomputed. We could consider doing the same if this is observed as
// a bottleneck, but that tracking has its own costs.
IPAddress[] localAddresses = Dns.GetHostEntryAsync(Dns.GetHostName()).GetAwaiter().GetResult().AddressList; // TODO: Use synchronous GetHostEntry when available
for (int i = 0; i < localAddresses.Length; i++)
{
if (ipAddress.Equals(localAddresses[i]))
{
return true;
}
}
return false;
}
public bool IsBypassed(Uri host)
{
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
return
Address == null ||
host.IsLoopback ||
(BypassProxyOnLocal && IsLocal(host)) ||
IsMatchInBypassList(host);
}
protected WebProxy(SerializationInfo serializationInfo, StreamingContext streamingContext)
{
BypassProxyOnLocal = serializationInfo.GetBoolean("_BypassOnLocal");
Address = (Uri)serializationInfo.GetValue("_ProxyAddress", typeof(Uri));
_bypassList = (ArrayList)serializationInfo.GetValue("_BypassList", typeof(ArrayList));
UseDefaultCredentials = serializationInfo.GetBoolean("_UseDefaultCredentials");
}
void ISerializable.GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
{
GetObjectData(serializationInfo, streamingContext);
}
protected virtual void GetObjectData(SerializationInfo serializationInfo, StreamingContext streamingContext)
{
serializationInfo.AddValue("_BypassOnLocal", BypassProxyOnLocal);
serializationInfo.AddValue("_ProxyAddress", Address);
serializationInfo.AddValue("_BypassList", _bypassList);
serializationInfo.AddValue("_UseDefaultCredentials", UseDefaultCredentials);
}
[Obsolete("This method has been deprecated. Please use the proxy selected for you by default. http://go.microsoft.com/fwlink/?linkid=14202")]
public static WebProxy GetDefaultProxy()
{
// The .NET Framework here returns a proxy that fetches IE settings and
// executes JavaScript to determine the correct proxy.
throw new PlatformNotSupportedException();
}
}
}

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netstandard;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<Project Include="System.Net.WebProxy.Tests.csproj" />
<Project Include="System.Net.WebProxy.Tests.csproj">
<OSGroup>Windows_NT</OSGroup>
<TestTFMs>net463</TestTFMs>
</Project>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.traversal.targets))\dir.traversal.targets" />
</Project>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<ProjectGuid>{0D1E2954-A5C7-4B8C-932A-31EB4A96A726}</ProjectGuid>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="WebProxyTest.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,214 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Xunit;
namespace System.Net.Tests
{
public class WebProxyTest
{
public static IEnumerable<object[]> Ctor_ExpectedPropertyValues_MemberData()
{
yield return new object[] { new WebProxy(), null, false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("http://anything"), new Uri("http://anything"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("anything", 42), new Uri("http://anything:42"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy(new Uri("http://anything")), new Uri("http://anything"), false, false, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("http://anything", true), new Uri("http://anything"), false, true, Array.Empty<string>(), null };
yield return new object[] { new WebProxy(new Uri("http://anything"), true), new Uri("http://anything"), false, true, Array.Empty<string>(), null };
yield return new object[] { new WebProxy("http://anything.com", true, new[] { ".*.com" }), new Uri("http://anything.com"), false, true, new[] { ".*.com" }, null };
yield return new object[] { new WebProxy(new Uri("http://anything.com"), true, new[] { ".*.com" }), new Uri("http://anything.com"), false, true, new[] { ".*.com" }, null };
var c = new DummyCredentials();
yield return new object[] { new WebProxy("http://anything.com", true, new[] { ".*.com" }, c), new Uri("http://anything.com"), false, true, new[] { ".*.com" }, c };
yield return new object[] { new WebProxy(new Uri("http://anything.com"), true, new[] { ".*.com" }, c), new Uri("http://anything.com"), false, true, new[] { ".*.com" }, c };
}
[Theory]
[MemberData(nameof(Ctor_ExpectedPropertyValues_MemberData))]
public static void Ctor_ExpectedPropertyValues(
WebProxy p, Uri address, bool useDefaultCredentials, bool bypassLocal, string[] bypassedAddresses, ICredentials creds)
{
Assert.Equal(address, p.Address);
Assert.Equal(useDefaultCredentials, p.UseDefaultCredentials);
Assert.Equal(bypassLocal, p.BypassProxyOnLocal);
Assert.Equal(bypassedAddresses, p.BypassList);
Assert.Equal(bypassedAddresses, (string[])p.BypassArrayList.ToArray(typeof(string)));
Assert.Equal(creds, p.Credentials);
}
[Fact]
public static void BypassList_Roundtrip()
{
var p = new WebProxy();
Assert.Empty(p.BypassList);
Assert.Empty(p.BypassArrayList);
string[] strings;
strings = new string[] { "hello", "world" };
p.BypassList = strings;
Assert.Equal(strings, p.BypassList);
Assert.Equal(strings, (string[])p.BypassArrayList.ToArray(typeof(string)));
strings = new string[] { "hello" };
p.BypassList = strings;
Assert.Equal(strings, p.BypassList);
Assert.Equal(strings, (string[])p.BypassArrayList.ToArray(typeof(string)));
}
[Fact]
public static void UseDefaultCredentials_Roundtrip()
{
var p = new WebProxy();
Assert.False(p.UseDefaultCredentials);
Assert.Null(p.Credentials);
p.UseDefaultCredentials = true;
Assert.True(p.UseDefaultCredentials);
Assert.NotNull(p.Credentials);
p.UseDefaultCredentials = false;
Assert.False(p.UseDefaultCredentials);
Assert.Null(p.Credentials);
}
[Fact]
public static void BypassProxyOnLocal_Roundtrip()
{
var p = new WebProxy();
Assert.False(p.BypassProxyOnLocal);
p.BypassProxyOnLocal = true;
Assert.True(p.BypassProxyOnLocal);
p.BypassProxyOnLocal = false;
Assert.False(p.BypassProxyOnLocal);
}
[Fact]
public static void Address_Roundtrip()
{
var p = new WebProxy();
Assert.Null(p.Address);
p.Address = new Uri("http://hello");
Assert.Equal(new Uri("http://hello"), p.Address);
p.Address = null;
Assert.Null(p.Address);
}
[Fact]
public static void InvalidArgs_Throws()
{
var p = new WebProxy();
Assert.Throws<ArgumentNullException>("destination", () => p.GetProxy(null));
Assert.Throws<ArgumentNullException>("host", () => p.IsBypassed(null));
Assert.Throws<ArgumentNullException>("c", () => p.BypassList = null);
Assert.Throws<ArgumentException>(() => p.BypassList = new string[] { "*.com" });
}
[Fact]
public static void InvalidBypassUrl_AddedDirectlyToList_SilentlyEaten()
{
var p = new WebProxy("http://bing.com");
p.BypassArrayList.Add("*.com");
p.IsBypassed(new Uri("http://microsoft.com")); // exception should be silently eaten
}
[Fact]
public static void BypassList_DoesntContainUrl_NotBypassed()
{
var p = new WebProxy("http://microsoft.com");
Assert.Equal(new Uri("http://microsoft.com"), p.GetProxy(new Uri("http://bing.com")));
}
[Fact]
public static void BypassList_ContainsUrl_IsBypassed()
{
var p = new WebProxy("http://microsoft.com", false, new[] { "hello", "bing.*", "world" });
Assert.Equal(new Uri("http://bing.com"), p.GetProxy(new Uri("http://bing.com")));
}
public static IEnumerable<object[]> BypassOnLocal_MemberData()
{
// Local
yield return new object[] { new Uri($"http://nodotinhostname"), true };
yield return new object[] { new Uri($"http://{Guid.NewGuid().ToString("N")}"), true };
foreach (IPAddress address in Dns.GetHostEntryAsync(Dns.GetHostName()).GetAwaiter().GetResult().AddressList)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
Uri uri;
try { uri = new Uri($"http://{address}"); }
catch (UriFormatException) { continue; }
yield return new object[] { uri, true };
}
}
string domain = IPGlobalProperties.GetIPGlobalProperties().DomainName;
if (!string.IsNullOrWhiteSpace(domain))
{
Uri uri = null;
try { new Uri($"http://{Guid.NewGuid().ToString("N")}.{domain}"); }
catch (UriFormatException) { }
if (uri != null)
{
yield return new object[] { uri, true };
}
}
// Non-local
yield return new object[] { new Uri($"http://{Guid.NewGuid().ToString("N")}.com"), false };
yield return new object[] { new Uri($"http://{IPAddress.None}"), false };
}
[Theory]
[MemberData(nameof(BypassOnLocal_MemberData))]
public static void BypassOnLocal_MatchesExpected(Uri destination, bool isLocal)
{
Uri proxyUri = new Uri("http://microsoft.com");
Assert.Equal(isLocal, new WebProxy(proxyUri, true).IsBypassed(destination));
Assert.False(new WebProxy(proxyUri, false).IsBypassed(destination));
Assert.Equal(isLocal ? destination : proxyUri, new WebProxy(proxyUri, true).GetProxy(destination));
Assert.Equal(proxyUri, new WebProxy(proxyUri, false).GetProxy(destination));
}
[Fact]
public static void BypassOnLocal_SpecialCases()
{
Assert.True(new WebProxy().IsBypassed(new Uri("http://anything.com")));
Assert.True(new WebProxy((string)null).IsBypassed(new Uri("http://anything.com")));
Assert.True(new WebProxy((Uri)null).IsBypassed(new Uri("http://anything.com")));
Assert.True(new WebProxy("microsoft", true).IsBypassed(new Uri($"http://{IPAddress.Loopback}")));
Assert.True(new WebProxy("microsoft", false).IsBypassed(new Uri($"http://{IPAddress.Loopback}")));
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
public static void GetDefaultProxy_NotSupported()
{
#pragma warning disable 0618 // obsolete method
Assert.Throws<PlatformNotSupportedException>(() => WebProxy.GetDefaultProxy());
#pragma warning restore 0618
}
private class DummyCredentials : ICredentials
{
public NetworkCredential GetCredential(Uri uri, string authType) => null;
}
}
}