public void TryAddWithExistingEndpoint()
        {
            var storage = new EndpointInformationStorage();

            var endpoint   = new EndpointId("a");
            var connection = new EndpointInformation(
                new EndpointId("a"),
                new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/protocol/invalid")));

            Assert.IsTrue(storage.TryAdd(endpoint, connection));
            Assert.IsFalse(storage.CanCommunicateWithEndpoint(endpoint));
            Assert.IsTrue(storage.HasBeenContacted(endpoint));
            Assert.IsFalse(storage.IsWaitingForApproval(endpoint));

            var newConnection = new EndpointInformation(
                endpoint,
                new DiscoveryInformation(new Uri("http://other/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://other/protocol/invalid")));

            Assert.IsFalse(storage.TryAdd(endpoint, newConnection));

            EndpointInformation otherConnection;
            var result = storage.TryGetConnectionFor(endpoint, out otherConnection);

            Assert.IsTrue(result);
            Assert.AreSame(connection, otherConnection);
        }
        public void TryUpdateWithContactedEndpoint()
        {
            var endpoint   = new EndpointId("a");
            var connection = new EndpointInformation(
                new EndpointId("a"),
                new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/protocol/invalid")));

            var storage = new EndpointInformationStorage();

            Assert.IsTrue(storage.TryAdd(endpoint, connection));

            var newConnection = new EndpointInformation(
                endpoint,
                new DiscoveryInformation(new Uri("http://other/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://other/protocol/invalid")));

            Assert.IsTrue(storage.TryUpdate(newConnection));

            EndpointInformation storedConnection;

            storage.TryGetConnectionFor(endpoint, out storedConnection);
            Assert.AreSame(newConnection, storedConnection);
        }
        public void TryRemoveWithApprovedEndpoint()
        {
            var storage = new EndpointInformationStorage();

            var endpoint   = new EndpointId("a");
            var connection = new EndpointInformation(
                new EndpointId("a"),
                new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/protocol/invalid")));

            Assert.IsTrue(storage.TryAdd(endpoint, connection));
            Assert.IsFalse(storage.CanCommunicateWithEndpoint(endpoint));
            Assert.IsTrue(storage.HasBeenContacted(endpoint));
            Assert.IsFalse(storage.IsWaitingForApproval(endpoint));

            var description = new ProtocolDescription(new List <CommunicationSubject>());

            Assert.IsTrue(storage.TryStartApproval(endpoint, description));
            Assert.IsTrue(storage.TryCompleteApproval(endpoint));
            Assert.IsTrue(storage.CanCommunicateWithEndpoint(endpoint));
            Assert.IsFalse(storage.IsWaitingForApproval(endpoint));

            Assert.IsTrue(storage.TryRemoveEndpoint(endpoint));
            Assert.IsFalse(storage.CanCommunicateWithEndpoint(endpoint));
            Assert.IsFalse(storage.IsWaitingForApproval(endpoint));
            Assert.IsFalse(storage.HasBeenContacted(endpoint));
        }
        public void TryUpdateWithApprovalUnderWay()
        {
            var endpoint   = new EndpointId("a");
            var connection = new EndpointInformation(
                new EndpointId("a"),
                new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/protocol/invalid")));

            var description = new ProtocolDescription(new List <CommunicationSubject>());
            var storage     = new EndpointInformationStorage();

            Assert.IsTrue(storage.TryAdd(endpoint, connection));
            Assert.IsTrue(storage.TryStartApproval(endpoint, description));

            var newConnection = new EndpointInformation(
                endpoint,
                new DiscoveryInformation(new Uri("http://other/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://other/protocol/invalid")));

            Assert.IsTrue(storage.TryUpdate(newConnection));

            EndpointInformation storedConnection;

            storage.TryGetConnectionFor(endpoint, out storedConnection);
            Assert.AreSame(newConnection, storedConnection);
        }
        public void TryApprove()
        {
            var endpoint   = new EndpointId("a");
            var connection = new EndpointInformation(
                new EndpointId("a"),
                new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/protocol/invalid")));

            var description = new ProtocolDescription(new List <CommunicationSubject>());
            var storage     = new EndpointInformationStorage();

            var wasApproved = false;

            storage.OnEndpointConnected +=
                (s, e) =>
            {
                wasApproved = true;
                Assert.AreSame(connection.Id, e.Endpoint);
            };

            Assert.IsTrue(storage.TryAdd(endpoint, connection));
            Assert.IsFalse(storage.CanCommunicateWithEndpoint(endpoint));
            Assert.IsTrue(storage.HasBeenContacted(endpoint));
            Assert.IsFalse(storage.IsWaitingForApproval(endpoint));

            Assert.IsTrue(storage.TryStartApproval(endpoint, description));
            Assert.IsTrue(storage.TryCompleteApproval(endpoint));
            Assert.IsTrue(storage.CanCommunicateWithEndpoint(endpoint));
            Assert.IsFalse(storage.HasBeenContacted(endpoint));
            Assert.IsFalse(storage.IsWaitingForApproval(endpoint));
            Assert.IsTrue(wasApproved);
        }
        public void TryRemoveWithUnknownEndpoint()
        {
            var storage = new EndpointInformationStorage();

            var endpoint = new EndpointId("a");

            Assert.IsFalse(storage.TryRemoveEndpoint(endpoint));
        }
        public void TryApproveWithNonAddedEndpoint()
        {
            var storage = new EndpointInformationStorage();

            var endpoint = new EndpointId("a");

            Assert.IsFalse(storage.TryCompleteApproval(endpoint));
        }
        public void TryStartApproveWithNullDescription()
        {
            var storage = new EndpointInformationStorage();

            var endpoint = new EndpointId("a");

            Assert.IsFalse(storage.TryStartApproval(endpoint, null));
        }
        public void TryStartApproveWithNullEndpoint()
        {
            var storage = new EndpointInformationStorage();

            var description = new ProtocolDescription(new List <CommunicationSubject>());

            Assert.IsFalse(storage.TryStartApproval(null, description));
        }
        public void TryAddWithNullEndpoint()
        {
            var storage = new EndpointInformationStorage();

            var connection = new EndpointInformation(
                new EndpointId("a"),
                new DiscoveryInformation(new Uri("http://localhost/discovery/invalid")),
                new ProtocolInformation(
                    new Version(),
                    new Uri("http://localhost/protocol/invalid")));

            Assert.IsFalse(storage.TryAdd(null, connection));
        }
        public void HandshakeWithLocalRejection()
        {
            var id         = new EndpointId("a:10");
            var subject    = new CommunicationSubject("a");
            var connection = new EndpointInformation(
                new EndpointId("a"),
                new DiscoveryInformation(new Uri("net.tcp://localhost/discovery/invalid")),
                new ProtocolInformation(
                    new Version(1, 0),
                    new Uri("net.tcp://localhost/protocol/invalid")));

            var remoteEndpoint       = new EndpointId("b:10");
            var remoteMessageAddress = @"net.tcp://othermachine";
            var remoteDataAddress    = @"net.tcp://othermachine/data";

            var communicationDescriptions = new Mock <IStoreProtocolSubjects>();
            {
                communicationDescriptions.Setup(c => c.Subjects())
                .Returns(
                    new[]
                {
                    subject
                });
                communicationDescriptions.Setup(c => c.ToStorage())
                .Returns(
                    new ProtocolDescription(
                        new[]
                {
                    subject
                }));
            }

            var endpointApprover = new Mock <IApproveEndpointConnections>();
            {
                endpointApprover.Setup(e => e.ProtocolVersion)
                .Returns(new Version(1, 0));
                endpointApprover.Setup(e => e.IsEndpointAllowedToConnect(It.IsAny <ProtocolDescription>()))
                .Returns(false);
            }

            Func <ChannelTemplate, Uri> discoveryUri = t => new Uri("net.tcp://localhost/discovery/invalid");
            var discoveryInformation = new LocalConnectionInformation(id, discoveryUri);
            var storage = new EndpointInformationStorage();

            var endpointConnected = false;

            storage.OnEndpointConnected +=
                (s, e) =>
            {
                endpointConnected = true;
                Assert.AreEqual(remoteEndpoint, e.Endpoint);
            };

            var discovery     = new Mock <IDiscoverOtherServices>();
            var protocolLayer = new Mock <IProtocolLayer>();
            {
                protocolLayer.Setup(l => l.Id)
                .Returns(id);
                protocolLayer.Setup(l => l.LocalConnectionFor(It.IsAny <Version>(), It.IsAny <ChannelTemplate>()))
                .Returns(
                    new Tuple <EndpointId, Uri, Uri>(
                        connection.Id,
                        connection.ProtocolInformation.MessageAddress,
                        connection.ProtocolInformation.DataAddress));
                protocolLayer.Setup(l => l.SendMessageToUnregisteredEndpoint(
                                        It.IsAny <EndpointInformation>(),
                                        It.IsAny <ICommunicationMessage>(),
                                        It.IsAny <int>()))
                .Verifiable();
                protocolLayer.Setup(l => l.SendMessageToUnregisteredEndpointAndWaitForResponse(
                                        It.IsAny <EndpointInformation>(),
                                        It.IsAny <ICommunicationMessage>(),
                                        It.IsAny <int>(),
                                        It.IsAny <TimeSpan>()))
                .Returns(Task <ICommunicationMessage> .Factory.StartNew(
                             () => new SuccessMessage(remoteEndpoint, new MessageId()),
                             new CancellationTokenSource().Token,
                             TaskCreationOptions.None,
                             new CurrentThreadTaskScheduler()))
                .Verifiable();
            }

            var configuration = new Mock <IConfiguration>();
            {
                configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>()))
                .Returns(false);
            }

            var layer = new ProtocolHandshakeConductor(
                storage,
                discoveryInformation,
                new[]
            {
                discovery.Object
            },
                protocolLayer.Object,
                communicationDescriptions.Object,
                new[]
            {
                endpointApprover.Object
            },
                new[]
            {
                ChannelTemplate.NamedPipe,
                ChannelTemplate.TcpIP,
            },
                configuration.Object,
                new SystemDiagnostics((l, m) => { }, null));

            layer.ContinueHandshakeWith(
                new EndpointInformation(
                    remoteEndpoint,
                    new DiscoveryInformation(new Uri("net.tcp://localhost/discovery/invalid")),
                    new ProtocolInformation(
                        new Version(1, 0),
                        new Uri(remoteMessageAddress),
                        new Uri(remoteDataAddress))),
                new ProtocolDescription(new List <CommunicationSubject>
            {
                new CommunicationSubject("b")
            }),
                new MessageId());

            Assert.IsFalse(endpointConnected);

            Assert.IsFalse(storage.HasBeenContacted(remoteEndpoint));
            Assert.IsFalse(storage.IsWaitingForApproval(remoteEndpoint));
            Assert.IsFalse(storage.CanCommunicateWithEndpoint(remoteEndpoint));

            protocolLayer.Verify(
                l => l.SendMessageToUnregisteredEndpointAndWaitForResponse(
                    It.IsAny <EndpointInformation>(),
                    It.IsAny <ICommunicationMessage>(),
                    It.IsAny <int>(),
                    It.IsAny <TimeSpan>()),
                Times.Never());
        }
        public void TryRemoveWithNullEndpoint()
        {
            var storage = new EndpointInformationStorage();

            Assert.IsFalse(storage.TryRemoveEndpoint(null));
        }
        public void TryAddWithNullConnectionInformation()
        {
            var storage = new EndpointInformationStorage();

            Assert.IsFalse(storage.TryAdd(new EndpointId("a"), null));
        }
        public void TryUpdateWithNullInformation()
        {
            var storage = new EndpointInformationStorage();

            Assert.IsFalse(storage.TryUpdate(null));
        }
        public void TryApproveWithNullEndpoint()
        {
            var storage = new EndpointInformationStorage();

            Assert.IsFalse(storage.TryCompleteApproval(null));
        }