Exemplo n.º 1
0
        public async Task WhenKeyUnknown_ThenErrorIsShownAndWindowIsClosed(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var instanceLocator = await instanceLocatorTask;

            using (var key = new RsaSshKey(new RSACng()))
            {
                SessionAbortedEvent deliveredEvent = null;
                this.eventService.BindHandler <SessionAbortedEvent>(e => deliveredEvent = e);

                var broker = new SshTerminalSessionBroker(
                    this.serviceProvider);
                await broker.ConnectAsync(
                    instanceLocator,
                    new IPEndPoint(await PublicAddressFromLocator(instanceLocator), 22),
                    AuthorizedKey.ForMetadata(key, "test", true, null),
                    TimeSpan.FromSeconds(10))
                .ConfigureAwait(true);

                Assert.IsNotNull(deliveredEvent, "Event fired");
                Assert.IsInstanceOf(typeof(SshNativeException), this.ExceptionShown);
                Assert.AreEqual(
                    LIBSSH2_ERROR.AUTHENTICATION_FAILED,
                    ((SshNativeException)this.ExceptionShown).ErrorCode);
            }
        }
        public async Task WhenConnected_ThenTryActivateReturnsTrue(
            [LinuxInstance] ResourceTask <InstanceLocator> testInstance,
            [Credential(Role = PredefinedRole.ComputeInstanceAdminV1)] ResourceTask <ICredential> credential)
        {
            var locator = await testInstance;

            using (var key = new RsaSshKey(new RSACng()))
                using (var gceAdapter = new ComputeEngineAdapter(
                           this.serviceProvider.GetService <IAuthorizationAdapter>()))
                    using (var keyAdapter = new AuthorizedKeyService(
                               this.serviceProvider.GetService <IAuthorizationAdapter>(),
                               new ComputeEngineAdapter(await credential),
                               new ResourceManagerAdapter(await credential),
                               new Mock <IOsLoginService>().Object))
                    {
                        var authorizedKey = await keyAdapter.AuthorizeKeyAsync(
                            locator,
                            key,
                            TimeSpan.FromMinutes(10),
                            null,
                            AuthorizeKeyMethods.InstanceMetadata,
                            CancellationToken.None)
                                            .ConfigureAwait(true);

                        var instance = await gceAdapter.GetInstanceAsync(
                            locator,
                            CancellationToken.None)
                                       .ConfigureAwait(true);

                        // Connect
                        var broker = new SshTerminalSessionBroker(this.serviceProvider);

                        ISshTerminalSession session = null;
                        await AssertRaisesEventAsync <SessionStartedEvent>(
                            async() => session = await broker.ConnectAsync(
                                locator,
                                new IPEndPoint(instance.PublicAddress(), 22),
                                authorizedKey,
                                null,
                                TimeSpan.FromSeconds(10)));

                        Assert.IsNull(this.ExceptionShown);

                        Assert.AreSame(session, SshTerminalPane.TryGetActivePane(this.mainForm));
                        Assert.AreSame(session, SshTerminalPane.TryGetExistingPane(this.mainForm, locator));
                        Assert.IsTrue(broker.IsConnected(locator));
                        Assert.IsTrue(broker.TryActivate(locator));

                        AssertRaisesEvent <SessionEndedEvent>(
                            () => session.Close());
                    }
        }
        private async Task <SshTerminalPane> ConnectSshTerminalPane(
            InstanceLocator instanceLocator,
            ICredential credential,
            CultureInfo language = null)
        {
            var authorization = new Mock <IAuthorization>();

            authorization
            .SetupGet(a => a.Email)
            .Returns("*****@*****.**");
            var authorizationAdapter = new Mock <IAuthorizationAdapter>();

            authorizationAdapter
            .Setup(a => a.Authorization)
            .Returns(authorization.Object);

            using (var keyAdapter = new AuthorizedKeyService(
                       authorizationAdapter.Object,
                       new ComputeEngineAdapter(credential),
                       new ResourceManagerAdapter(credential),
                       new Mock <IOsLoginService>().Object))
            {
                var authorizedKey = await keyAdapter.AuthorizeKeyAsync(
                    instanceLocator,
                    new RsaSshKey(new RSACng()),
                    TimeSpan.FromMinutes(10),
                    null,
                    AuthorizeKeyMethods.InstanceMetadata,
                    CancellationToken.None)
                                    .ConfigureAwait(true);

                // Connect and wait for event
                SessionStartedEvent connectedEvent = null;
                this.eventService.BindHandler <SessionStartedEvent>(e => connectedEvent = e);

                var broker = new SshTerminalSessionBroker(
                    this.serviceProvider);
                var pane = await broker.ConnectAsync(
                    instanceLocator,
                    new IPEndPoint(await PublicAddressFromLocator(instanceLocator), 22),
                    authorizedKey,
                    language,
                    TimeSpan.FromSeconds(10))
                           .ConfigureAwait(true);

                Assert.IsNotNull(connectedEvent, "ConnectionSuceededEvent event fired");
                PumpWindowMessages();

                return((SshTerminalPane)pane);
            }
        }
Exemplo n.º 4
0
        public async Task WhenWrongPort_ThenErrorIsShownAndWindowIsClosed()
        {
            using (var key = new RsaSshKey(new RSACng()))
            {
                SessionAbortedEvent deliveredEvent = null;
                this.eventService.BindHandler <SessionAbortedEvent>(e => deliveredEvent = e);

                var broker = new SshTerminalSessionBroker(
                    this.serviceProvider);
                await broker.ConnectAsync(
                    new InstanceLocator("project-1", "zone-1", "instance-1"),
                    NonSshEndpoint,
                    AuthorizedKey.ForMetadata(key, "test", true, null))
                .ConfigureAwait(true);

                Assert.IsNotNull(deliveredEvent, "Event fired");
                Assert.IsInstanceOf(typeof(SocketException), this.ExceptionShown);
                Assert.AreEqual(
                    SocketError.ConnectionRefused,
                    ((SocketException)this.ExceptionShown).SocketErrorCode);
            }
        }