From 4c7baf25dad93936967f39b76f2fcfcb6d2623da Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 9 May 2022 14:39:19 +0200 Subject: [PATCH 1/4] [tests] Adjust a few tests to try network requests multiple times. * A few tests seem to be failing rather consistently with network errors. Rewrite these tests to try multiple urls before failing (we'll be assuming that if one of the urls succeed, the other failures were network related). * Rename the LinkAnyTest.WebClientTest to LinkAnyTest.WebClientTest_Http to follow it's original intent, and make it test http instead of https. --- .../ios/link sdk/LinkSdkRegressionTest.cs | 17 +++++- tests/linker/mac/LinkAnyTest.cs | 58 +++++++++++++------ .../System.Net.Http/NetworkResources.cs | 10 +++- 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs b/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs index 4f55aea2611a..fc1a772dbae2 100644 --- a/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs +++ b/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs @@ -671,10 +671,21 @@ public void WebClient_SSL_Leak () #if __WATCHOS__ Assert.Ignore ("WatchOS doesn't support BSD sockets, which our network stack currently requires."); #endif + var exceptions = new List (); WebClient wc = new WebClient (); - // note: needs to be executed under Instrument to verify it does not leak - string s = wc.DownloadString (NetworkResources.MicrosoftUrl); - Assert.NotNull (s); + foreach (var url in NetworkResources.HttpsUrls) { + try { + // note: needs to be executed under Instrument to verify it does not leak + string s = wc.DownloadString (url); + Assert.NotNull (s); + return; // one url succeeded, that's enough + } catch (Exception e) { + var msg = $"Url '{http}' failed: {e.ToString ()}"; + Console.WriteLine (msg); // If this keeps occurring locally for the same url, we might have to take it off the list of urls to test. + exceptions.Add (msg); + } + } + Assert.That (exceptions, Is.Empty, "At least one url should work"); } #if !__TVOS__ && !__WATCHOS__ && !__MACOS__ diff --git a/tests/linker/mac/LinkAnyTest.cs b/tests/linker/mac/LinkAnyTest.cs index f0983780985f..bfaa102ba3e8 100644 --- a/tests/linker/mac/LinkAnyTest.cs +++ b/tests/linker/mac/LinkAnyTest.cs @@ -63,22 +63,35 @@ public void GetWebPageAsyncTest () } } - [Test] - public void WebClientTest () + void WebClientTest (string[] urls) { - var wc = new WebClient (); - var data = wc.DownloadString (NetworkResources.MicrosoftUrl); + var exceptions = new List (); + foreach (var url in urls) { + try { + var wc = new WebClient (); + var data = wc.DownloadString (url); + + Assert.That (data, Is.Not.Empty, "Downloaded content"); + return; // one url succeeded, that's enough + } catch (Exception e) { + var msg = $"Url '{url}' failed: {e.ToString ()}"; + Console.WriteLine (msg); // If this keeps occurring locally for the same url, we might have to take it off the list of urls to test. + exceptions.Add (msg); + } + } + Assert.That (exceptions, Is.Empty, "At least one url should work"); + } - Assert.That (data, Is.Not.Empty, "Downloaded content"); + [Test] + public void WebClientTest_Http () + { + WebClientTest (NetworkResources.HttpUrls); } [Test] public void WebClientTest_Https () { - var wc = new WebClient (); - var data = wc.DownloadString (NetworkResources.MicrosoftUrl); - - Assert.That (data, Is.Not.Empty, "Downloaded content"); + WebClientTest (NetworkResources.HttpsUrls); } [Test] @@ -91,15 +104,26 @@ public void WebClientTest_Async () string data = null; - async Task GetWebPage (string url) - { - var wc = new WebClient (); - var task = wc.DownloadStringTaskAsync (new Uri (url)); - data = await task; + var exceptions = new List (); + foreach (var url in NetworkResources.HttpsUrls) { + try { + async Task GetWebPage (string url) + { + var wc = new WebClient (); + var task = wc.DownloadStringTaskAsync (new Uri (url)); + data = await task; + } + + GetWebPage (url).Wait (); + Assert.That (data, Is.Not.Empty, "Downloaded content"); + return; // one url succeeded, that's enough + } catch (Exception e) { + var msg = $"Url '{http}' failed: {e.ToString ()}"; + Console.WriteLine (msg); // If this keeps occurring locally for the same url, we might have to take it off the list of urls to test. + exceptions.Add (msg); + } } - - GetWebPage (NetworkResources.MicrosoftUrl).Wait (); - Assert.That (data, Is.Not.Empty, "Downloaded content"); + Assert.That (exceptions, Is.Empty, "At least one url should work"); } finally { SynchronizationContext.SetSynchronizationContext (current_sc); diff --git a/tests/monotouch-test/System.Net.Http/NetworkResources.cs b/tests/monotouch-test/System.Net.Http/NetworkResources.cs index 78f0a9dd8006..c10b7a10cc6b 100644 --- a/tests/monotouch-test/System.Net.Http/NetworkResources.cs +++ b/tests/monotouch-test/System.Net.Http/NetworkResources.cs @@ -9,12 +9,20 @@ public static class NetworkResources { public static readonly string MicrosoftUrl = "https://www.microsoft.com"; public static readonly Uri MicrosoftUri = new Uri (MicrosoftUrl); + public static readonly string MicrosoftHttpUrl = "http://www.microsoft.com"; public static readonly string XamarinUrl = "https://dotnet.microsoft.com/apps/xamarin"; + public static readonly string XamarinHttpUrl = "http://dotnet.microsoft.com/apps/xamarin"; public static readonly Uri XamarinUri = new Uri (XamarinUrl); public static readonly string StatsUrl = "https://api.imgur.com/2/stats"; - public static readonly string [] Urls = { + public static readonly string [] HttpsUrls = { MicrosoftUrl, + XamarinUrl, + }; + + public static readonly string [] HttpUrls = { + MicrosoftHttpUrl, + XamarinHttpUrl, }; // Robots urls, useful when we want to get a small file From 1f94c6cf65552f351227270efaa70d2325a814ef Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 9 May 2022 21:51:00 +0200 Subject: [PATCH 2/4] Fix build. --- tests/linker/mac/LinkAnyTest.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/linker/mac/LinkAnyTest.cs b/tests/linker/mac/LinkAnyTest.cs index bfaa102ba3e8..360fbdc5b516 100644 --- a/tests/linker/mac/LinkAnyTest.cs +++ b/tests/linker/mac/LinkAnyTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Security.Cryptography; @@ -65,7 +66,7 @@ public void GetWebPageAsyncTest () void WebClientTest (string[] urls) { - var exceptions = new List (); + var exceptions = new List (); foreach (var url in urls) { try { var wc = new WebClient (); @@ -104,7 +105,7 @@ public void WebClientTest_Async () string data = null; - var exceptions = new List (); + var exceptions = new List (); foreach (var url in NetworkResources.HttpsUrls) { try { async Task GetWebPage (string url) @@ -118,7 +119,7 @@ async Task GetWebPage (string url) Assert.That (data, Is.Not.Empty, "Downloaded content"); return; // one url succeeded, that's enough } catch (Exception e) { - var msg = $"Url '{http}' failed: {e.ToString ()}"; + var msg = $"Url '{url}' failed: {e.ToString ()}"; Console.WriteLine (msg); // If this keeps occurring locally for the same url, we might have to take it off the list of urls to test. exceptions.Add (msg); } From 62c0189eaec73a520f861cfcc980a3e82f45e2fa Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 10 May 2022 08:09:45 +0200 Subject: [PATCH 3/4] More build fixes. --- tests/linker/ios/link sdk/LinkSdkRegressionTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs b/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs index fc1a772dbae2..2a21b1b3ae15 100644 --- a/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs +++ b/tests/linker/ios/link sdk/LinkSdkRegressionTest.cs @@ -671,7 +671,7 @@ public void WebClient_SSL_Leak () #if __WATCHOS__ Assert.Ignore ("WatchOS doesn't support BSD sockets, which our network stack currently requires."); #endif - var exceptions = new List (); + var exceptions = new List (); WebClient wc = new WebClient (); foreach (var url in NetworkResources.HttpsUrls) { try { @@ -680,7 +680,7 @@ public void WebClient_SSL_Leak () Assert.NotNull (s); return; // one url succeeded, that's enough } catch (Exception e) { - var msg = $"Url '{http}' failed: {e.ToString ()}"; + var msg = $"Url '{url}' failed: {e.ToString ()}"; Console.WriteLine (msg); // If this keeps occurring locally for the same url, we might have to take it off the list of urls to test. exceptions.Add (msg); } From bf144bb4ec3329a154bac4f32a56f62d34ac1c93 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 11 May 2022 09:07:13 +0200 Subject: [PATCH 4/4] [tests] Remove unnecessary assertion from SSL_IP_5706. This is tested in plenty of other places. --- tests/linker/ios/link sdk/CryptoTest.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/linker/ios/link sdk/CryptoTest.cs b/tests/linker/ios/link sdk/CryptoTest.cs index 79d55d47c6f4..e3b969f8a86d 100644 --- a/tests/linker/ios/link sdk/CryptoTest.cs +++ b/tests/linker/ios/link sdk/CryptoTest.cs @@ -81,8 +81,6 @@ public void SSL_IP_5706 () Assert.Ignore ("WatchOS doesn't support BSD sockets, which our network stack currently requires."); #endif WebClient wc = new WebClient (); - // the certificate contains (several rules) the host name - Assert.NotNull (wc.DownloadString (NetworkResources.MicrosoftUrl)); // IP are (generally) not allowed foreach (var ip in Dns.GetHostAddresses ("www.google.com")) {