2019-10-28 21:50:44 +03:00
|
|
|
#if !__WATCHOS__
|
|
|
|
using System;
|
|
|
|
using System.Threading;
|
2021-11-03 17:34:34 +03:00
|
|
|
|
2019-10-28 21:50:44 +03:00
|
|
|
using CoreFoundation;
|
|
|
|
using Foundation;
|
|
|
|
using Network;
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
2019-11-14 13:45:33 +03:00
|
|
|
using MonoTests.System.Net.Http;
|
|
|
|
|
2019-10-28 21:50:44 +03:00
|
|
|
|
|
|
|
namespace MonoTouchFixtures.Network {
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
[Preserve (AllMembers = true)]
|
|
|
|
public class NWConnectionTest {
|
|
|
|
|
|
|
|
AutoResetEvent connectedEvent; // used to let us know when the connection was established so that we can access the Report
|
|
|
|
string host;
|
|
|
|
NWConnection connection;
|
|
|
|
|
|
|
|
void ConnectionStateHandler (NWConnectionState state, NWError error)
|
|
|
|
{
|
2022-11-10 19:59:26 +03:00
|
|
|
switch (state) {
|
2019-10-28 21:50:44 +03:00
|
|
|
case NWConnectionState.Ready:
|
|
|
|
connectedEvent.Set ();
|
|
|
|
break;
|
|
|
|
case NWConnectionState.Invalid:
|
|
|
|
case NWConnectionState.Failed:
|
|
|
|
Assert.Inconclusive ("Network connection could not be performed.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 19:14:22 +03:00
|
|
|
[OneTimeSetUp]
|
2019-10-28 21:50:44 +03:00
|
|
|
public void Init () => TestRuntime.AssertXcodeVersion (10, 0);
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp ()
|
|
|
|
{
|
|
|
|
// connect and once the connection is done, deal with the diff tests
|
2022-11-10 19:59:26 +03:00
|
|
|
connectedEvent = new AutoResetEvent (false);
|
2019-11-14 13:45:33 +03:00
|
|
|
host = NetworkResources.MicrosoftUri.Host;
|
2019-10-28 21:50:44 +03:00
|
|
|
// 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 ())
|
2022-11-10 19:59:26 +03:00
|
|
|
using (var endpoint = NWEndpoint.Create (host, "80")) {
|
2019-10-28 21:50:44 +03:00
|
|
|
using (var protocolStack = parameters.ProtocolStack) {
|
|
|
|
var ipOptions = protocolStack.InternetProtocol;
|
2022-02-25 01:07:53 +03:00
|
|
|
#if NET
|
|
|
|
ipOptions.SetVersion (NWIPVersion.Version4);
|
|
|
|
#else
|
2019-10-28 21:50:44 +03:00
|
|
|
ipOptions.IPSetVersion (NWIPVersion.Version4);
|
2022-02-25 01:07:53 +03:00
|
|
|
#endif
|
2019-10-28 21:50:44 +03:00
|
|
|
}
|
|
|
|
connection = new NWConnection (endpoint, parameters);
|
|
|
|
connection.SetQueue (DispatchQueue.DefaultGlobalQueue); // important, else we will get blocked
|
|
|
|
connection.SetStateChangeHandler (ConnectionStateHandler);
|
2022-11-10 19:59:26 +03:00
|
|
|
connection.Start ();
|
2019-10-28 21:50:44 +03:00
|
|
|
Assert.True (connectedEvent.WaitOne (20000), "Connection timed out.");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void TearDown () => connection.Dispose ();
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestEndpointProperty () => Assert.IsNotNull (connection.Endpoint);
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestParametersProperty () => Assert.IsNotNull (connection.Parameters);
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestSetQPropertyNull () => Assert.Throws<ArgumentNullException> (() => connection.SetQueue (null));
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestCancel ()
|
|
|
|
{
|
|
|
|
// call cancel, several times, we should not crash
|
|
|
|
AutoResetEvent cancelled = new AutoResetEvent (false);
|
|
|
|
connection.SetStateChangeHandler ((s, e) => {
|
|
|
|
switch (s) {
|
|
|
|
case NWConnectionState.Cancelled:
|
|
|
|
cancelled.Set ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connection.Cancel ();
|
|
|
|
Assert.IsTrue (cancelled.WaitOne (3000), "Cancelled");
|
|
|
|
connection.Cancel ();
|
|
|
|
// lib should ignore the second call
|
|
|
|
Assert.IsFalse (cancelled.WaitOne (3000));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestForceCancel ()
|
|
|
|
{
|
|
|
|
// same as cancel, call it several times should be ok
|
|
|
|
// call cancel, several times, we should not crash
|
|
|
|
AutoResetEvent cancelled = new AutoResetEvent (false);
|
|
|
|
connection.SetStateChangeHandler ((s, e) => {
|
|
|
|
switch (s) {
|
|
|
|
case NWConnectionState.Cancelled:
|
|
|
|
cancelled.Set ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connection.ForceCancel ();
|
|
|
|
Assert.IsTrue (cancelled.WaitOne (3000), "Cancelled");
|
|
|
|
connection.ForceCancel ();
|
|
|
|
// lib should ignore the second call
|
|
|
|
Assert.IsFalse (cancelled.WaitOne (3000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 11:06:46 +03:00
|
|
|
#endif
|