예제 #1
0
        public async Task ExecuteCommand_SensitiveParameterNotFoundInCommandText_ExceptionThrown()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            //Act
            var exception = await Assert.ThrowsExceptionAsync <CommandSanitizationException>(async() =>
                                                                                             await client.ExecuteCommandAsync(
                                                                                                 SshRetryPolicy.AllowRetries,
                                                                                                 "some-text",
                                                                                                 new Dictionary <string, string>()
            {
                {
                    "foo", "bar"
                }
            }));

            //Assert
            Assert.IsNotNull(exception);
        }
예제 #2
0
        public async Task ExecuteCommand_SensitiveDataSensitivityAndFailedCommand_SensitiveDataNotIncludedInThrownException()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            fakeClientDecorator
            .ExecuteCommandAsync("'some-text'")
            .Returns(
                Task.FromResult(new SshCommandResult()
            {
                ExitCode = 1,
                Text     = "some-result"
            }));

            //Act
            var exception = await Assert.ThrowsExceptionAsync <SshCommandExecutionException>(async() =>
                                                                                             await client.ExecuteCommandAsync(
                                                                                                 SshRetryPolicy.ProhibitRetries,
                                                                                                 "@alias",
                                                                                                 new Dictionary <string, string>()
            {
                { "alias", "some-text" }
            }));

            //Assert
            Assert.IsNotNull(exception);

            Assert.AreEqual("@alias", exception.CommandText);
            Assert.AreEqual(1, exception.Result.ExitCode);
        }
예제 #3
0
        public async Task ExecuteCommand_SensitiveDataSensitivityAndSuccessfulCommand_HighwayTest()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            fakeClientDecorator
            .ExecuteCommandAsync("'some-text'")
            .Returns(
                Task.FromResult(new SshCommandResult()
            {
                ExitCode = 0,
                Text     = "some-result"
            }));

            //Act
            var result = await client.ExecuteCommandAsync(
                SshRetryPolicy.ProhibitRetries,
                "@alias",
                new Dictionary <string, string>()
            {
                { "alias", "some-text" }
            });

            //Assert
            Assert.AreEqual("some-result", result);
        }
예제 #4
0
        public async Task ExecuteCommand_ProhibitRetriesAndFailedCommand_ThrowsException()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            fakeClientDecorator
            .ExecuteCommandAsync("some-text")
            .Returns(
                Task.FromResult(new SshCommandResult()
            {
                ExitCode = 1
            }));

            //Act
            var exception = await Assert.ThrowsExceptionAsync <SshCommandExecutionException>(async() =>
                                                                                             await client.ExecuteCommandAsync(
                                                                                                 SshRetryPolicy.ProhibitRetries,
                                                                                                 "some-text"));

            //Assert
            Assert.IsNotNull(exception);

            await fakeClientDecorator
            .Received(1)
            .ExecuteCommandAsync(
                Arg.Any <string>());
        }
예제 #5
0
        public async Task ExecuteCommand_ProhibitRetriesAndSuccessfulCommand_ReturnsInstantly()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            fakeClientDecorator
            .ExecuteCommandAsync("some-text")
            .Returns(
                Task.FromResult(new SshCommandResult()));

            //Act
            await client.ExecuteCommandAsync(
                SshRetryPolicy.ProhibitRetries,
                "some-text");

            //Assert
            await fakeClientDecorator
            .Received(1)
            .ExecuteCommandAsync(
                Arg.Any <string>());
        }
예제 #6
0
        public async Task ExecuteCommand_AllowRetriesAndFailedCommand_RetriesAgain()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            fakeClientDecorator
            .ExecuteCommandAsync("some-text")
            .Returns(
                Task.FromResult(new SshCommandResult()
            {
                ExitCode = 1
            }),
                Task.FromResult(new SshCommandResult()));

            //Act
            await client.ExecuteCommandAsync(
                SshRetryPolicy.AllowRetries,
                "some-text");

            //Assert
            await fakeClientDecorator
            .Received(2)
            .ExecuteCommandAsync(
                Arg.Any <string>());
        }
예제 #7
0
        public async Task SanitizeBashCommand_DummyCommandGiven_InsertsEscapeCharactersEverywhere()
        {
            //Arrange
            const string input = "I'm a s@fe $tring which ends in newline\n";

            //Act
            var output = SshClient.Sanitize(input);

            //Assert
            Assert.AreEqual("'I'\\''m a s@fe $tring which ends in newline\n'", output);
        }
예제 #8
0
        public async Task Dispose_ClientDecoratorGiven_DisposesClientDecorator()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            //Act
            client.Dispose();

            //Assert
            fakeClientDecorator
            .Received(1)
            .Dispose();
        }
예제 #9
0
        public async Task Connect_SuccessfulConnection_ReturnsInstantly()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            //Act
            await client.ConnectAsync();

            //Assert
            await fakeClientDecorator
            .Received(1)
            .ConnectAsync();
        }
예제 #10
0
        public async Task Connect_FailedConnection_RetriesAgain()
        {
            //Arrange
            var fakeClientDecorator = Substitute.For <ISshClientDecorator>();
            var fakeLogger          = Substitute.For <ILogger>();

            var client = new SshClient(
                fakeClientDecorator,
                fakeLogger);

            fakeClientDecorator
            .ConnectAsync()
            .Returns(
                Task.FromException(new SshConnectionException()),
                Task.CompletedTask);

            //Act
            await client.ConnectAsync();

            //Assert
            await fakeClientDecorator
            .Received(2)
            .ConnectAsync();
        }