Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] Adjust a few tests to try network requests multiple times. #14943

Merged
merged 7 commits into from
May 13, 2022
Merged
2 changes: 0 additions & 2 deletions tests/linker/ios/link sdk/CryptoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
17 changes: 14 additions & 3 deletions tests/linker/ios/link sdk/LinkSdkRegressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> ();
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 '{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");
}

#if !__TVOS__ && !__WATCHOS__ && !__MACOS__
Expand Down
59 changes: 42 additions & 17 deletions tests/linker/mac/LinkAnyTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
Expand Down Expand Up @@ -63,22 +64,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<string> ();
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]
Expand All @@ -91,15 +105,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<string> ();
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 '{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);
}
}

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);

Expand Down
10 changes: 9 additions & 1 deletion tests/monotouch-test/System.Net.Http/NetworkResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down