Example #1
0
        public async Task WhenDisposingConnection_ThenWorkerIsStopped(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key);

                using (var connection = new SshShellConnection(
                           "testuser",
                           endpoint,
                           key,
                           SshShellConnection.DefaultTerminal,
                           SshShellConnection.DefaultTerminalSize,
                           CultureInfo.InvariantCulture,
                           _ => { },
                           exception =>
                {
                    Assert.Fail("Unexpected error");
                }))
                {
                    await connection.ConnectAsync();
                }
            }
        }
Example #2
0
        public async Task WhenSendingEchoCommand_ThenEchoIsReceived(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key);

                var receiveBuffer = new StringBuilder();

                void receiveHandler(string data)
                {
                    lock (receiveBuffer)
                    {
                        receiveBuffer.Append(data);
                    }
                }

                using (var connection = new SshShellConnection(
                           "testuser",
                           endpoint,
                           key,
                           SshShellConnection.DefaultTerminal,
                           SshShellConnection.DefaultTerminalSize,
                           CultureInfo.InvariantCulture,
                           receiveHandler,
                           exception =>
                {
                    Assert.Fail("Unexpected error");
                }))
                {
                    await connection.ConnectAsync();

                    AssertEx.ThrowsAggregateException <InvalidOperationException>(
                        () => connection.ConnectAsync().Wait());

                    await connection.SendAsync("whoami\n");

                    await connection.SendAsync("exit\n");

                    await AwaitBufferContentAsync(
                        receiveBuffer,
                        TimeSpan.FromSeconds(10),
                        "testuser");

                    StringAssert.Contains(
                        "testuser",
                        receiveBuffer.ToString());
                }
            }
        }
Example #3
0
        public async Task WhenServerRejectsLocale_ThenShellUsesDefaultLocale(
            [LinuxInstance(InitializeScript =
                               "sed -i '/AcceptEnv/d' /etc/ssh/sshd_config && systemctl restart sshd")]
            ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key);

                var receiveBuffer = new StringBuilder();

                void receiveHandler(string data)
                {
                    lock (receiveBuffer)
                    {
                        receiveBuffer.Append(data);
                    }
                }

                using (var connection = new SshShellConnection(
                           "testuser",
                           endpoint,
                           key,
                           SshShellConnection.DefaultTerminal,
                           SshShellConnection.DefaultTerminalSize,
                           new CultureInfo("en-AU"),
                           receiveHandler,
                           exception =>
                {
                    Assert.Fail("Unexpected error");
                }))
                {
                    await connection.ConnectAsync();

                    AssertEx.ThrowsAggregateException <InvalidOperationException>(
                        () => connection.ConnectAsync().Wait());

                    await connection.SendAsync("locale;sleep 1;exit\n");

                    await AwaitBufferContentAsync(
                        receiveBuffer,
                        TimeSpan.FromSeconds(10),
                        "testuser");

                    StringAssert.Contains(
                        "LC_ALL=\r\n",
                        receiveBuffer.ToString());
                }
            }
        }
        public async Task WhenLocaleIsNull_ThenShellUsesDefaultLocale(
            [LinuxInstance] ResourceTask <InstanceLocator> instanceLocatorTask)
        {
            var endpoint = new IPEndPoint(
                await InstanceUtil.PublicIpAddressForInstanceAsync(await instanceLocatorTask),
                22);

            using (var key = new RsaSshKey(new RSACng()))
            {
                await InstanceUtil.AddPublicKeyToMetadata(
                    await instanceLocatorTask,
                    "testuser",
                    key);

                var receiveBuffer = new StringBuilder();

                void receiveHandler(string data)
                {
                    lock (receiveBuffer)
                    {
                        receiveBuffer.Append(data);
                    }
                }

                using (var connection = new SshShellConnection(
                           "testuser",
                           endpoint,
                           key,
                           SshShellConnection.DefaultTerminal,
                           SshShellConnection.DefaultTerminalSize,
                           null,
                           UnexpectedAuthenticationCallback,
                           receiveHandler,
                           UnexpectedErrorCallback))
                {
                    await connection.ConnectAsync();

                    AssertEx.ThrowsAggregateException <InvalidOperationException>(
                        () => connection.ConnectAsync().Wait());

                    await connection.SendAsync("locale;sleep 1;exit\n");

                    await AwaitBufferContentAsync(
                        receiveBuffer,
                        TimeSpan.FromSeconds(10),
                        "testuser");

                    StringAssert.Contains(
                        "LC_ALL=\r\n",
                        receiveBuffer.ToString());
                }
            }
        }