Exemple #1
0
        public void SetPrivateKeyAllowsRepeatedInvocation()
        {
            // Arrange
            string key1     = "some-private-key";
            string sshPath  = @"x:\path\.ssh";
            var    fileBase = new Mock <FileBase>();
            int    invoked  = 0;

            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no"));
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\id_rsa", It.IsAny <string>()));
            fileBase.Setup(s => s.Exists(sshPath + @"\id_rsa.pub"))
            .Returns(() => ++ invoked == 1);

            var directory = new Mock <DirectoryBase>();

            directory.Setup(d => d.Exists(sshPath)).Returns(true);
            var fileSystem = new Mock <IFileSystem>();

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileSystem.SetupGet(f => f.Directory).Returns(directory.Object);

            var environment = new Mock <IEnvironment>();

            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act
            sshKeyManager.SetPrivateKey(key1);
            sshKeyManager.SetPrivateKey(_privateKey);

            // Assert
            fileBase.Verify(s => s.WriteAllText(sshPath + @"\id_rsa", It.IsAny <string>()), Times.Exactly(2));
            fileBase.Verify(s => s.Delete(sshPath + @"\id_rsa.pub"), Times.Exactly(1));
        }
        public void SetPrivateKeyCreatesSSHDirectoryIfItDoesNotExist()
        {
            // Arrange
            string sshPath  = @"x:\path\.ssh";
            var    fileBase = new Mock <FileBase>();

            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub")).Returns(false).Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no")).Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\id_rsa", "my super secret key")).Verifiable();

            var directory = new Mock <DirectoryBase>(MockBehavior.Strict);

            directory.Setup(d => d.Exists(sshPath)).Returns(false).Verifiable();
            directory.Setup(d => d.CreateDirectory(sshPath)).Returns(Mock.Of <DirectoryInfoBase>()).Verifiable();
            var fileSystem = new Mock <IFileSystem>(MockBehavior.Strict);

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileSystem.SetupGet(f => f.Directory).Returns(directory.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock <IEnvironment>();

            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null, deploymentSettingManager: Mock.Of <IDeploymentSettingsManager>());

            // Act
            sshKeyManager.SetPrivateKey(_privateKey);

            // Assert
            directory.Verify();
        }
Exemple #3
0
        public void SetPrivateKeySetsByPassKeyCheckAndKeyIfFile()
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            var fileBase = new Mock<FileBase>(MockBehavior.Strict);
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no")).Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\id_rsa", "my super secret key")).Verifiable();

            var directory = new Mock<DirectoryBase>(MockBehavior.Strict);
            directory.Setup(d => d.Exists(sshPath)).Returns(true).Verifiable();
            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileSystem.SetupGet(f => f.Directory).Returns(directory.Object);

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act
            sshKeyManager.SetPrivateKey("my super secret key");

            // Assert
            fileBase.Verify();
        }
Exemple #4
0
        public void SetPrivateKeySetsByPassKeyCheckAndKeyIfFile()
        {
            // Arrange
            string sshPath  = @"x:\path\.ssh";
            var    fileBase = new Mock <FileBase>();

            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no"))
            .Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\id_rsa", _privateKey))
            .Verifiable();
            fileBase.Setup(s => s.Exists(sshPath + @"\id_rsa.pub"))
            .Returns(false)
            .Verifiable();

            var directory = new Mock <DirectoryBase>();

            directory.Setup(d => d.Exists(sshPath)).Returns(true).Verifiable();
            var fileSystem = new Mock <IFileSystem>();

            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileSystem.SetupGet(f => f.Directory).Returns(directory.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock <IEnvironment>();

            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null);

            // Act
            sshKeyManager.SetPrivateKey(_privateKey);

            // Assert
            fileBase.Verify();
        }
        public void SetPrivateKeyAllowsRepeatedInvocation()
        {
            // Arrange
            string key1 = "some-private-key";
            string sshPath = @"x:\path\.ssh";
            var fileBase = new Mock<FileBase>();
            int invoked = 0;
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no"));
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\id_rsa", It.IsAny<string>()));
            fileBase.Setup(s => s.Exists(sshPath + @"\id_rsa.pub"))
                    .Returns(() => ++invoked == 1);

            var directory = new Mock<DirectoryBase>();
            directory.Setup(d => d.Exists(sshPath)).Returns(true);
            var fileSystem = new Mock<IFileSystem>();
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileSystem.SetupGet(f => f.Directory).Returns(directory.Object);

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act
            sshKeyManager.SetPrivateKey(key1);
            sshKeyManager.SetPrivateKey(_privateKey);

            // Assert
            fileBase.Verify(s => s.WriteAllText(sshPath + @"\id_rsa", It.IsAny<string>()), Times.Exactly(2));
            fileBase.Verify(s => s.Delete(sshPath + @"\id_rsa.pub"), Times.Exactly(1));
        }
Exemple #6
0
        public void SetPrivateKeyThrowsIfAPublicKeyAlreadyExistsOnFileSystem()
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            var fileBase = new Mock<FileBase>(MockBehavior.Strict);
            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub")).Returns(true).Verifiable();

            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act and Assert
            var ex = Assert.Throws<InvalidOperationException>(() => sshKeyManager.SetPrivateKey("my super secret key"));
            Assert.Equal("Cannot set key. A key pair already exists on disk. To generate a new key set 'forceCreate' to true.", ex.Message);
        }
Exemple #7
0
        public void SetPrivateKeyAllowsRepeatedInvocationIfNoPublicKeyIsPresent()
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            var fileBase = new Mock<FileBase>(MockBehavior.Strict);
            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub")).Returns(false);
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no"));
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\id_rsa", It.IsAny<string>()));

            var directory = new Mock<DirectoryBase>(MockBehavior.Strict);
            directory.Setup(d => d.Exists(sshPath)).Returns(true);
            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileSystem.SetupGet(f => f.Directory).Returns(directory.Object);

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, fileSystem.Object, traceFactory: null);

            // Act
            sshKeyManager.SetPrivateKey("key 1");
            sshKeyManager.SetPrivateKey("key 2");

            // Assert
            fileBase.Verify(s => s.WriteAllText(sshPath + @"\id_rsa", It.IsAny<string>()), Times.Exactly(2));
        }
        public void SetPrivateKeyCreatesSSHDirectoryIfItDoesNotExist()
        {
            // Arrange
            string sshPath = @"x:\path\.ssh";
            var fileBase = new Mock<FileBase>();
            fileBase.Setup(s => s.Exists(sshPath + "\\id_rsa.pub")).Returns(false).Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\config", "HOST *\r\n  StrictHostKeyChecking no")).Verifiable();
            fileBase.Setup(s => s.WriteAllText(sshPath + @"\id_rsa", "my super secret key")).Verifiable();

            var directory = new Mock<DirectoryBase>(MockBehavior.Strict);
            directory.Setup(d => d.Exists(sshPath)).Returns(false).Verifiable();
            directory.Setup(d => d.CreateDirectory(sshPath)).Returns(Mock.Of<DirectoryInfoBase>()).Verifiable();
            var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
            fileSystem.SetupGet(f => f.File).Returns(fileBase.Object);
            fileSystem.SetupGet(f => f.Directory).Returns(directory.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            var environment = new Mock<IEnvironment>();
            environment.SetupGet(e => e.SSHKeyPath).Returns(sshPath);

            var sshKeyManager = new SSHKeyManager(environment.Object, traceFactory: null);

            // Act
            sshKeyManager.SetPrivateKey(_privateKey);

            // Assert
            directory.Verify();
        }