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 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 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 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());
        }