diff --git a/tests/monotouch-test/Network/NWParametersTest.cs b/tests/monotouch-test/Network/NWParametersTest.cs index d5777e6df183..c973bd30f1f0 100644 --- a/tests/monotouch-test/Network/NWParametersTest.cs +++ b/tests/monotouch-test/Network/NWParametersTest.cs @@ -29,52 +29,72 @@ public class NWParametersTest { bool secureConnectionWasSet = false; bool protocolConfigured = false; List interfaces = new List (); + string host; + NWConnection connection; - [SetUp] - public void SetUp () + + [TestFixtureSetUp] + public void Init () { TestRuntime.AssertXcodeVersion (10, 0); - secureEvent = new AutoResetEvent(false); - configureEvent = new AutoResetEvent(false); + // we want to use a single connection, since it is expensive connectedEvent = new AutoResetEvent(false); - secureConnectionWasSet = false; - protocolConfigured = false; + host = "www.google.com"; interfaces = new List (); - // 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 ("wwww.google.com", "80")) - using (var connection = new NWConnection (endpoint, parameters)) { + using (var endpoint = NWEndpoint.Create (host, "80")) { + connection = new NWConnection (endpoint, parameters); + connection.SetQueue (DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked connection.SetStateChangeHandler (ConnectionStateHandler); - connection.SetQueue (DispatchQueue.MainQueue); - connection.Start (); - connectedEvent.WaitOne (500); + connection.Start (); + Assert.True (connectedEvent.WaitOne (20000), "Connection timed out."); using (var path = connection.CurrentPath) { path.EnumerateInterfaces (EnumerateInterfacesHandler); } - } + } + } + + [TestFixtureTearDown] + public void Dispose() + { + connection?.Cancel (); } - [TearDown] - public void TearDown () + [SetUp] + public void SetUp () { - secureEvent = null; + secureEvent = new AutoResetEvent(false); + configureEvent = new AutoResetEvent(false); secureConnectionWasSet = false; protocolConfigured = false; - foreach (var i in interfaces) { - i.Dispose (); - } } - void ConnectionStateHandler (NWConnectionState state, NWError error) - { - var errno = (SslStatus)(error != null ? error.ErrorCode : 0); - switch (state){ - case NWConnectionState.Ready: - connectedEvent.Set (); + void ConnectionStateHandler (NWConnectionState state, NWError error) + { + switch (state){ + case NWConnectionState.Ready: + connectedEvent.Set (); + break; + case NWConnectionState.Cancelled: + connection?.Dispose (); + connection = null; + foreach (var i in interfaces) + i.Dispose (); break; - } + case NWConnectionState.Invalid: + case NWConnectionState.Failed: + Assert.Inconclusive ("Network connection could not be performed."); + break; + } + } + + [TearDown] + public void TearDown () + { + secureEvent = null; + secureConnectionWasSet = false; + protocolConfigured = false; } void EnumerateInterfacesHandler (NWInterface nwInterface) diff --git a/tests/monotouch-test/Network/NWProtocolStackTest.cs b/tests/monotouch-test/Network/NWProtocolStackTest.cs new file mode 100644 index 000000000000..0dbec72be16a --- /dev/null +++ b/tests/monotouch-test/Network/NWProtocolStackTest.cs @@ -0,0 +1,144 @@ +#if !__WATCHOS__ +using System; +using System.Collections.Generic; +using System.Threading; +#if XAMCORE_2_0 +using CoreFoundation; +using Foundation; +using Network; +using ObjCRuntime; +using Security; +#else +using MonoTouch.CoreFoundation; +using MonoTouch.Foundation; +using MonoTouch.Network; +using MonoTouch.Security; +#endif + +using NUnit.Framework; + +namespace MonoTouchFixtures.Network { + + [TestFixture] + [Preserve (AllMembers = true)] + public class NWProtocolStackTest { + + AutoResetEvent connectedEvent; // used to let us know when the connection was established so that we can access the NWPath + string host; + NWConnection connection; + NWProtocolStack stack; + List options; + + [TestFixtureSetUp] + public void Init () + { + TestRuntime.AssertXcodeVersion (10, 0); + // we want to use a single connection, since it is expensive + connectedEvent = new AutoResetEvent(false); + host = "www.google.com"; + using (var parameters = NWParameters.CreateUdp ()) + using (var endpoint = NWEndpoint.Create (host, "80")) { + 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."); + stack = parameters.ProtocolStack; + using (var ipOptions = stack.InternetProtocol) { + ipOptions.IPSetVersion (NWIPVersion.Version4); + stack.PrependApplicationProtocol (ipOptions); + } + } + } + + [TestFixtureTearDown] + public void Dispose() + { + connection?.Cancel (); + } + + [SetUp] + public void SetUp () + { + options = new List (); + } + + void ConnectionStateHandler (NWConnectionState state, NWError error) + { + switch (state){ + case NWConnectionState.Ready: + connectedEvent.Set (); + break; + case NWConnectionState.Cancelled: + connection?.Dispose (); + connection = null; + stack?.Dispose (); + stack = null; + foreach (var o in options) + o.Dispose (); + break; + case NWConnectionState.Invalid: + case NWConnectionState.Failed: + Assert.Inconclusive ("Network connection could not be performed."); + break; + } + } + + [Test] + public void PrependApplicationProtocolNullOptionsTest () + { + // not need to test the method with a valid argument since it is part of the setup. + Assert.Throws (() => stack.PrependApplicationProtocol (null)); + } + + // handler to iterate over the app protocols + public void InterateProtocolsHandler (NWProtocolOptions o) + { + options.Add (o); + } + + [Test] + public void ClearApplicationProtocolsTest () + { + // test the clean and the iterate + stack.IterateProtocols (InterateProtocolsHandler); + Assert.That (options.Count, Is.GreaterThan (0), "options.Count"); + // remove present ones + foreach (var o in options) + o.Dispose (); + options = new List (); + stack.ClearApplicationProtocols (); + stack.IterateProtocols (InterateProtocolsHandler); + Assert.AreEqual (0, options.Count, "Cleared options"); + } + + [Test] + public void TransportProtocolPropertyTest () + { + using (var options = stack.TransportProtocol) + { + Assert.IsNotNull (options, "Transport protocol should not be null."); + } + using (var options = NWProtocolOptions.CreateUdp ()) + { + stack.TransportProtocol = options; + using (var copyOptions = stack.TransportProtocol) + { + copyOptions.IPSetUseMinimumMtu (true); // should not crash + } + } + } + + [Test] + public void InternetProtocolTest () + { + using (var o = stack.InternetProtocol) + { + o.IPSetUseMinimumMtu (true); // should not crash + } + } + + } + } + + #endif \ No newline at end of file