From f281771f3efe286623f9be623922dd1cf43ef4f4 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Wed, 10 Apr 2019 20:10:37 +0200 Subject: [PATCH] [tests] More network resilience for tests Context: 608d950a703ddb527258565c2a0705aa37f71789 Context: 34dfc6d71d590183f0077b69bbd61964f8879b97 SslTests notoriously fail for reasons not related to the subject of the test, thus creating false negatives when our bots flag the related builds/PRs as red while the failure can/should be ignored. Wrap the tests in code which ignores the most common failures we can safely ignore. Additionally, slightly improve connection error handling in AndroidHttpClientHandler tests. --- src/Mono.Android/Test/System.Net/SslTest.cs | 73 +++++++++++++++++-- .../AndroidClientHandlerTests.cs | 31 ++++++-- 2 files changed, 90 insertions(+), 14 deletions(-) diff --git a/src/Mono.Android/Test/System.Net/SslTest.cs b/src/Mono.Android/Test/System.Net/SslTest.cs index e271066c950..be0842ebc57 100644 --- a/src/Mono.Android/Test/System.Net/SslTest.cs +++ b/src/Mono.Android/Test/System.Net/SslTest.cs @@ -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] @@ -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; @@ -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; } } } diff --git a/src/Mono.Android/Test/Xamarin.Android.Net/AndroidClientHandlerTests.cs b/src/Mono.Android/Test/Xamarin.Android.Net/AndroidClientHandlerTests.cs index 43d51b6f97b..c444662a85b 100644 --- a/src/Mono.Android/Test/Xamarin.Android.Net/AndroidClientHandlerTests.cs +++ b/src/Mono.Android/Test/Xamarin.Android.Net/AndroidClientHandlerTests.cs @@ -151,7 +151,7 @@ protected Task ConnectIgnoreFailure (Func