예제 #1
0
        public void Init()
        {
            TestRuntime.AssertXcodeVersion(10, 0);
            // we want to use a single connection, since it is expensive
            connectedEvent = new AutoResetEvent(false);
            interfaces     = new List <NWInterface> ();
            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;
#if NET
                        ipOptions.SetVersion(NWIPVersion.Version4);
#else
                        ipOptions.IPSetVersion(NWIPVersion.Version4);
#endif
                    }
                    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.");
                }
        }
예제 #2
0
 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.");
         }
 }
예제 #3
0
    static void StartConnection(NWConnection connection)
    {
        connection.SetQueue(DispatchQueue.MainQueue);
        warn($"Start Connection on {connection.Handle} with {connection.Endpoint}");

        connection.SetStateChangeHandler((state, error) => {
            var remote = connection.Endpoint;
            var errno  = (SslStatus)(error != null ? error.ErrorCode : 0);
            switch (state)
            {
            case NWConnectionState.Waiting:
                warn($"Connect to {remote.Hostname} port {remote.Port} udp={useUdp} failed, is waiting");
                break;

            case NWConnectionState.Failed:
                warn($"Connect to {remote.Hostname} port {remote.Port} udp={useUdp} failed, error {errno}");
                break;

            case NWConnectionState.Ready:
                if (verbose)
                {
                    warn($"Connect to {remote.Hostname} port {remote.Port} udp={useUdp} succeeded");
                }
                break;

            case NWConnectionState.Cancelled:
                // Release reference
                connection = null;
                break;
            }
        });
        connection.Start();
    }
예제 #4
0
        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.CreateTcp())
                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) {
                        if (ipOptions != null)
                        {
#if NET
                            ipOptions.SetVersion(NWIPVersion.Version4);
#else
                            ipOptions.IPSetVersion(NWIPVersion.Version4);
#endif
                            stack.PrependApplicationProtocol(ipOptions);
                        }
                    }
                }
        }
예제 #5
0
        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));
        }
 public void Init()
 {
     TestRuntime.AssertXcodeVersion(10, 0);
     // we want to use a single connection, since it is expensive
     connectedEvent = new AutoResetEvent(false);
     host           = NetworkResources.MicrosoftUri.Host;
     interfaces     = new List <NWInterface> ();
     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.");
             using (var path = connection.CurrentPath)
             {
                 path.EnumerateInterfaces(EnumerateInterfacesHandler);
             }
         }
 }
예제 #7
0
 public void SetUp()
 {
     // connect and once the connection is done, deal with the diff tests
     connectedEvent = 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.");
         }
 }
        public void TlsDefaults()
        {
            using (var ep = NWEndpoint.Create("www.microsoft.com", "https"))
                using (var parameters = NWParameters.CreateSecureTcp())
                    using (var queue = new DispatchQueue(GetType().FullName)) {
                        var connection = new NWConnection(ep, parameters);

                        var ready = new ManualResetEvent(false);
                        connection.SetStateChangeHandler((state, error) => {
                            Console.WriteLine(state);
                            switch (state)
                            {
                            case NWConnectionState.Cancelled:
                            case NWConnectionState.Failed:
                                // We can't dispose until the connection has been closed or it failed.
                                connection.Dispose();
                                break;

                            case NWConnectionState.Invalid:
                            case NWConnectionState.Preparing:
                            case NWConnectionState.Waiting:
                                break;

                            case NWConnectionState.Ready:
                                ready.Set();
                                break;

                            default:
                                break;
                            }
                        });

                        connection.SetQueue(queue);
                        connection.Start();

                        // Wait until the connection is ready.
                        Assert.True(ready.WaitOne(TimeSpan.FromSeconds(10)), "Connection is ready");

                        using (var m = connection.GetProtocolMetadata(NWProtocolDefinition.TlsDefinition)) {
                            var s = m.TlsSecProtocolMetadata;
                            Assert.False(s.EarlyDataAccepted, "EarlyDataAccepted");
                            Assert.That(s.NegotiatedCipherSuite, Is.Not.EqualTo(SslCipherSuite.SSL_NULL_WITH_NULL_NULL), "NegotiatedCipherSuite");
                            Assert.Null(s.NegotiatedProtocol, "NegotiatedProtocol");
                            Assert.That(s.NegotiatedProtocolVersion, Is.EqualTo(SslProtocol.Tls_1_2).Or.EqualTo(SslProtocol.Tls_1_3), "NegotiatedProtocolVersion");
                            Assert.NotNull(s.PeerPublicKey, "PeerPublicKey");

                            Assert.True(SecProtocolMetadata.ChallengeParametersAreEqual(s, s), "ChallengeParametersAreEqual");
                            Assert.True(SecProtocolMetadata.PeersAreEqual(s, s), "PeersAreEqual");

                            if (TestRuntime.CheckXcodeVersion(11, 0))
                            {
                                using (var d = s.CreateSecret("Xamarin", 128)) {
                                    Assert.That(d.Size, Is.EqualTo((nuint)128), "CreateSecret-1");
                                }
                                using (var d = s.CreateSecret("Microsoft", new byte [1], 256)) {
                                    Assert.That(d.Size, Is.EqualTo((nuint)256), "CreateSecret-2");
                                }

                                Assert.That(s.NegotiatedTlsProtocolVersion, Is.EqualTo(TlsProtocolVersion.Tls12).Or.EqualTo(TlsProtocolVersion.Tls13), "NegotiatedTlsProtocolVersion");
                                // we want to test the binding/API - not the exact value which can vary depending on the negotiation between the client (OS) and server...
                                Assert.That(s.NegotiatedTlsCipherSuite, Is.Not.EqualTo(0), "NegotiatedTlsCipherSuite");
                                Assert.That(s.ServerName, Is.EqualTo("www.microsoft.com"), "ServerName");
                                // we don't have a TLS-PSK enabled server to test this
                                Assert.False(s.AccessPreSharedKeys((psk, pskId) => { }), "AccessPreSharedKeys");
                            }
                        }

                        connection.Cancel();
                    }
        }