Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 66 additions & 7 deletions src/Mono.Android/Test/System.Net/SslTest.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,7 +11,19 @@
namespace System.NetTests {

[TestFixture, Category ("InetAccess")]
public class SslTest {
public class SslTest
{
bool ShouldIgnoreException (WebException wex)
{
switch (wex.Status) {
case WebExceptionStatus.ConnectFailure:
case WebExceptionStatus.NameResolutionFailure:
case WebExceptionStatus.Timeout:
return true;
}

return false;
}

// https://xamarin.desk.com/agent/case/35534
[Test]
Expand DownExpand Up@@ -41,16 +53,33 @@ public void SslWithinTasksShouldWork ()
thread.Join ();

ServicePointManager.ServerCertificateValidationCallback = cb;
var wex = (exception as AggregateException)?.InnerException as WebException;
if (wex != null) {
if (ShouldIgnoreException (wex)) {
Assert.Ignore ($"Ignoring network failure: {wex}");
return;
}
throw wex;
}

if (exception != null)
throw exception;

Assert.AreEqual (TaskStatus.RanToCompletion, status);
}

[Test]
public void HttpsShouldWork ()
{
RunIgnoringWebException (DoHttpsShouldWork);
}

void DoHttpsShouldWork ()
{
// string url = "https://bugzilla.novell.com/show_bug.cgi?id=634817";
string url = "https://encrypted.google.com/";
// string url = "http://slashdot.org";
var request = (HttpWebRequest) WebRequest.Create(url);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.Method = "GET";
var response = (HttpWebResponse) request.GetResponse ();
int len = 0;
Expand All@@ -62,18 +91,48 @@ public void HttpsShouldWork ()
len += n;
}
}

Assert.IsTrue (len > 0);
}

[Test (Description="Bug https://bugzilla.xamarin.com/show_bug.cgi?id=18962")]
public void VerifyTrustedCertificates ()
{
Assert.DoesNotThrow (() => {
var tcpClient = new TcpClient ("google.com", 443);
using (var ssl = new SslStream (tcpClient.GetStream (), false)) {
ssl.AuthenticateAsClient ("google.com");
Assert.DoesNotThrow (() => RunIgnoringWebException (DoVerifyTrustedCertificates), "Certificate validation");
}

void DoVerifyTrustedCertificates ()
{
var tcpClient = new TcpClient ("google.com", 443);
using (var ssl = new SslStream (tcpClient.GetStream (), false)) {
ssl.AuthenticateAsClient ("google.com");
}
}

void RunIgnoringWebException (Action test)
{
Exception ex = null;
WebException wex = null;

try {
test ();
} catch (AggregateException e) {
ex = e;
wex = e.InnerException as WebException;
} catch (WebException e) {
wex = e;
}

if (wex != null) {
if (ShouldIgnoreException (wex)) {
Assert.Ignore ($"Ignoring network failure: {wex.Status}");
return;
}
}, "Certificate validation");
throw wex;
}

if (ex != null)
throw ex;
}
}
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -151,7 +151,7 @@ protected Task<HttpResponseMessage> ConnectIgnoreFailure (Func<Task<HttpResponse
try {
return connector ();
} catch (AggregateException ex) {
if (IgnoreIfConnectionFailed (ex.InnerException as WebException, out connectionFailed))
if (IgnoreIfConnectionFailed (ex, out connectionFailed))
return null;
throw;
}
Expand All@@ -163,24 +163,41 @@ protected void RunIgnoringNetworkIssues (Action runner, out bool connectionFaile
try {
runner ();
} catch (AggregateException ex) {
if (IgnoreIfConnectionFailed (ex.InnerException as WebException, out connectionFailed))
if (IgnoreIfConnectionFailed (ex, out connectionFailed))
return;
throw;
}
}

bool IgnoreIfConnectionFailed (AggregateException aex, out bool connectionFailed)
{
if (IgnoreIfConnectionFailed (aex.InnerException as HttpRequestException, out connectionFailed))
return true;

return IgnoreIfConnectionFailed (aex.InnerException as WebException, out connectionFailed);
}

bool IgnoreIfConnectionFailed (HttpRequestException hrex, out bool connectionFailed)
{
return IgnoreIfConnectionFailed (hrex?.InnerException as WebException, out connectionFailed);
}

bool IgnoreIfConnectionFailed (WebException wex, out bool connectionFailed)
{
connectionFailed = false;
if (wex == null)
return false;

if (wex.Status != WebExceptionStatus.ConnectFailure)
return false;
switch (wex.Status) {
case WebExceptionStatus.ConnectFailure:
case WebExceptionStatus.NameResolutionFailure:
case WebExceptionStatus.Timeout:
connectionFailed = true;
Assert.Ignore ($"Ignoring network failure: {wex}");
return true;
}

connectionFailed = true;
Assert.Ignore ($"Failed to connect to server. {wex}");
return true;
return false;
}
}

Expand Down