xamarin-macios/tests/monotouch-test/Network/NWEstablishmentReportTest.cs

123 строки
3.7 KiB
C#
Исходник Обычный вид История

#if !__WATCHOS__
using System;
using System.Collections.Generic;
using System.Threading;
using CoreFoundation;
using Foundation;
using Network;
using ObjCRuntime;
using Security;
using NUnit.Framework;
using MonoTests.System.Net.Http;
namespace MonoTouchFixtures.Network {
[TestFixture]
[Preserve (AllMembers = true)]
public class NWEstablishmentReportTest {
AutoResetEvent connectedEvent; // used to let us know when the connection was established so that we can access the Report
AutoResetEvent reportEvent; // used to let us know when the connection was established and we got the report
string host;
NWConnection connection;
NWEstablishmentReport report;
void ConnectionStateHandler (NWConnectionState state, NWError error)
{
switch (state){
case NWConnectionState.Ready:
connectedEvent.Set ();
break;
case NWConnectionState.Invalid:
case NWConnectionState.Failed:
Assert.Inconclusive ("Network connection could not be performed.");
break;
}
}
[TestFixtureSetUp]
public void Init ()
{
TestRuntime.AssertXcodeVersion (11, 0);
// connect so that we can later when the report and test with it
connectedEvent = new AutoResetEvent(false);
reportEvent = new AutoResetEvent (false);
host = NetworkResources.MicrosoftUri.Host;
// we create a connection which we are going to use to get the availabe
// interfaces, that way we can later test protperties of the NWParameters class.
using (var parameters = NWParameters.CreateUdp ())
using (var endpoint = NWEndpoint.Create (host, "80"))
{
using (var protocolStack = parameters.ProtocolStack) {
var ipOptions = protocolStack.InternetProtocol;
ipOptions.IPSetVersion (NWIPVersion.Version4);
}
connection = new NWConnection (endpoint, parameters);
connection.SetQueue (DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked
connection.SetStateChangeHandler (ConnectionStateHandler);
connection.Start ();
Assert.True (connectedEvent.WaitOne (20000), "Connection timed out.");
connection.GetEstablishmentReport (DispatchQueue.DefaultGlobalQueue, (r) => {
report = r;
reportEvent.Set ();
});
Assert.True (reportEvent.WaitOne (20000), "Connection timed out.");
}
}
[TestFixtureTearDown]
public void Dispose()
{
Bump to mono:2019-10 (#7192) ## Miscellaneous fixes * Fixed `/Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/builds/mono-ios-sdk-destdir/ios-sources/external/linker/src/linker/Linker.Steps/OutputStep.cs(110,15): error CS0246: The type or namespace name ‘OutputException’ could not be found (are you missing a using directive or an assembly reference?) [/Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/tools/mmp/mmp.csproj]` * Changed the name of the method that is used from linker. Because of this commit https://github.com/mono/linker/commit/6be26771b9bc6d40305a8f005d38f08dfa423252 * Added `OutputException.cs` file on `mtouch.csproj`. * Removing enter_gc_safe and exit_gc_safe because now it's already gc_safe in this part of code, after a mono change. * Added known exceptions to LLVM exception list. * Needs `ifdef` because of this https://github.com/mono/mono/pull/17260. * Bump MIN_MONO_VERSION to 6.8.0.41 and point MIN_MONO_URL to the PR. * Add ENABLE_IOS=1 and ENABLE_MAC=1. * Added switch to disable packaged mono build * [Tests] Ignore tests that fail on 32b. Ignore the test on 32b, and filled issue: https://github.com/mono/mono/issues/17752 * [Tests] Ignore a couple of tests causing OOM. Hopefully fixes https://github.com/xamarin/maccore/issues/1659 for good. * Ignore `MM0135` test on Catalina+ because it needs Xcode 9.4. * [monotouch-test] Add null checks for teardown when test didn't run because of a too early OS version. * [CFNetwork]: Http 2.0 requires OS X 10.11 or later. Check whether `_HTTPVersion2_0` is available and fallback to HTTP 1.1 otherwise. ## Bring HttpClient from CoreFX * #7346 * This bumps Mono to use https://github.com/mono/mono/pull/17645 (which is the 2019-10 backport of https://github.com/mono/mono/pull/17628). * The big user-visible change is in regards to certificate validation, everything below are just some minor adjustments to tests. ### SocketsHttpHandler CoreFX uses a completely new `HttpClientHandler` implementation called `SocketsHttpHandler`, which you can find at https://github.com/dotnet/corefx/tree/release/3.0/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler. Since this is not based on the web stack anymore, it does not use any of the related APIs such as `ServicePointManager` or `WebException`. ### Certificate Validation Changes There is a new API called `HttpClientHandler.ServerCertificateCustomValidationCallback`. - https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.servercertificatecustomvalidationcallback?view=netframework-4.8 - https://github.com/dotnet/corefx/blob/c1778515a3bee34cc09c757b5563d0af0c8b1e99/src/System.Net.Http/src/System/Net/Http/HttpClientHandler.Unix.cs#L154 - https://github.com/dotnet/corefx/blob/c1778515a3bee34cc09c757b5563d0af0c8b1e99/src/System.Net.Http/src/System/Net/Http/HttpClientHandler.Windows.cs#L383 The `ServicePointManager.ServerCertificateValidationCallback` is no longer invoked and on certificate validation failure, `AuthenticationException` (from `System.Security.Authentication`) is thrown instead of `WebException`. At the moment, the `NSUrlSessionHandler` still uses it's own validation callback and also still throws `WebException` on failure; we should probably look into making this consistent with the other handlers. ### Minor adjustments related to internal Mono APIs * `HttpContent.SerializeToStreamAsync()` is now `protected` (changed from `protected internal`). - src/Foundation/NSUrlSessionHandler.cs: changed overload accordingly. - src/System.Net.Http/CFContentStream.cs: likewise. * `HttpHeaders.GetKnownHeaderKind()` is an internal Mono API. There is a new internal API called `System.Net.Http.PlatformHelper.IsContentHeader(key)` which exists in both the old as well as the new implementation. The correct way of doing it with the CoreFX handler is `HeaderDescriptor.TryGet (key, out var descriptor) && descriptor.HeaderType == HttpHeaderType.Content` ### Minor adjustments to tests. * `HttpClientHandler.MaxRequestContentBufferSize` is now longer supported, you can set it to any non-negative value, the getter will always return 0. See https://github.com/dotnet/corefx/blob/c1778515a3bee34cc09c757b5563d0af0c8b1e99/src/System.Net.Http/src/System/Net/Http/HttpClientHandler.Core.cs#L18. - tests/linker/ios/link sdk/HttpClientHandlerTest.cs: removed assertion from test. * `HttpMessageInvoker.handler` is a `protected private` field - in the CoreFX handler, it is called `_handler` and `private`. This is accessed via reflection by some of the tests, which are now using the new name. - tests/mmptest/src/MMPTest.cs: here - tests/mtouch/MTouch.cs: here * tests/monotouch-test/System.Net.Http/MessageHandlers.cs: Adjust `RejectSslCertificatesServicePointManager` to reflect the certificate validation changes described above. - FIXME: There was an `Assert.Ignore()` related to `NSUrlSessionHandler` and macOS 10.10; I removed that to reenable the test because the description linked to an old issue in the private repo that was referenced by several "Merged" PR's, so it looked to me that this might have already been fixed - and I also didn't see why it would fail there.
2019-12-04 23:10:32 +03:00
report?.Dispose ();
connection?.Dispose ();
}
[Test]
public void TestUsedProxy ()
{
TestRuntime.IgnoreInCI ("CI bots might have proxies setup and will mean that the test will fail.");
Assert.IsFalse (report.UsedProxy, "Used proxy");
}
[Test]
public void TestProxyConfigured ()
{
TestRuntime.IgnoreInCI ("CI bots might have proxies setup and will mean that the test will fail.");
Assert.IsFalse (report.ProxyConfigured, "Proxy configured.");
}
[Test]
public void TestPreviousAttemptCount () => Assert.AreNotEqual (-1, report.PreviousAttemptCount);
[Test]
public void TestDuration () => Assert.IsTrue (report.Duration > TimeSpan.MinValue);
[Test]
public void TestConnectionSetupTime () => Assert.IsTrue (report.ConnectionSetupTime > TimeSpan.MinValue);
[Test]
public void TestEnumerateResolutions ()
{
var e = new AutoResetEvent (false);
report.EnumerateResolutions ((source, duration, count, endpoint, preferred) => {
Assert.IsTrue (duration > TimeSpan.MinValue, "Durantion");
Assert.AreNotEqual (0, count, "Count");
Assert.IsNotNull (endpoint, "endpoint");
Assert.IsNotNull (preferred, "preferred");
e.Set ();
});
e.WaitOne ();
}
[Test]
public void TestProxyEnpoint ()
{
TestRuntime.IgnoreInCI ("CI bots might have proxies setup and will mean that the test will fail.");
Assert.IsNull (report.ProxyEndpoint);
}
}
}
#endif