2
0
Эх сурвалжийг харах

Add/migrate hmac+cipher integration tests (#1189)

* Add/migrate hmac+cipher integration tests

* fix integration tests

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
Rob Hague 2 жил өмнө
parent
commit
70f58b7660

+ 81 - 0
src/Renci.SshNet.IntegrationTests/CipherTests.cs

@@ -0,0 +1,81 @@
+using Renci.SshNet.IntegrationTests.Common;
+using Renci.SshNet.TestTools.OpenSSH;
+
+namespace Renci.SshNet.IntegrationTests
+{
+    [TestClass]
+    public class CipherTests : IntegrationTestBase
+    {
+        private IConnectionInfoFactory _connectionInfoFactory;
+        private RemoteSshdConfig _remoteSshdConfig;
+
+        [TestInitialize]
+        public void SetUp()
+        {
+            _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
+            _remoteSshdConfig = new RemoteSshd(new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort)).OpenConfig();
+        }
+
+        [TestCleanup]
+        public void TearDown()
+        {
+            _remoteSshdConfig?.Reset();
+        }
+
+        [TestMethod]
+        public void TripledesCbc()
+        {
+            DoTest(Cipher.TripledesCbc);
+        }
+
+        [TestMethod]
+        public void Aes128Cbc()
+        {
+            DoTest(Cipher.Aes128Cbc);
+        }
+
+        [TestMethod]
+        public void Aes192Cbc()
+        {
+            DoTest(Cipher.Aes192Cbc);
+        }
+
+        [TestMethod]
+        public void Aes256Cbc()
+        {
+            DoTest(Cipher.Aes256Cbc);
+        }
+
+        [TestMethod]
+        public void Aes128Ctr()
+        {
+            DoTest(Cipher.Aes128Ctr);
+        }
+
+        [TestMethod]
+        public void Aes192Ctr()
+        {
+            DoTest(Cipher.Aes192Ctr);
+        }
+
+        [TestMethod]
+        public void Aes256Ctr()
+        {
+            DoTest(Cipher.Aes256Ctr);
+        }
+
+        private void DoTest(Cipher cipher)
+        {
+            _remoteSshdConfig.ClearCiphers()
+                             .AddCipher(cipher)
+                             .Update()
+                             .Restart();
+
+            using (var client = new SshClient(_connectionInfoFactory.Create()))
+            {
+                client.Connect();
+                client.Disconnect();
+            }
+        }
+    }
+}

+ 1 - 0
src/Renci.SshNet.IntegrationTests/Common/RemoteSshdConfigExtensions.cs

@@ -21,6 +21,7 @@ namespace Renci.SshNet.IntegrationTests.Common
                             .ClearKeyExchangeAlgorithms()
                             .ClearHostKeyAlgorithms()
                             .ClearPublicKeyAcceptedAlgorithms()
+                            .ClearMessageAuthenticationCodeAlgorithms()
                             .WithUsePAM(true)
                             .Update()
                             .Restart();

+ 75 - 0
src/Renci.SshNet.IntegrationTests/HmacTests.cs

@@ -0,0 +1,75 @@
+using Renci.SshNet.IntegrationTests.Common;
+using Renci.SshNet.TestTools.OpenSSH;
+
+namespace Renci.SshNet.IntegrationTests
+{
+    [TestClass]
+    public class HmacTests : IntegrationTestBase
+    {
+        private IConnectionInfoFactory _connectionInfoFactory;
+        private RemoteSshdConfig _remoteSshdConfig;
+
+        [TestInitialize]
+        public void SetUp()
+        {
+            _connectionInfoFactory = new LinuxVMConnectionFactory(SshServerHostName, SshServerPort);
+            _remoteSshdConfig = new RemoteSshd(new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort)).OpenConfig();
+        }
+
+        [TestCleanup]
+        public void TearDown()
+        {
+            _remoteSshdConfig?.Reset();
+        }
+
+        [TestMethod]
+        public void HmacMd5()
+        {
+            DoTest(MessageAuthenticationCodeAlgorithm.HmacMd5);
+        }
+
+        [TestMethod]
+        public void HmacMd5_96()
+        {
+            DoTest(MessageAuthenticationCodeAlgorithm.HmacMd5_96);
+        }
+
+        [TestMethod]
+        public void HmacSha1()
+        {
+            DoTest(MessageAuthenticationCodeAlgorithm.HmacSha1);
+        }
+
+        [TestMethod]
+        public void HmacSha1_96()
+        {
+            DoTest(MessageAuthenticationCodeAlgorithm.HmacSha1_96);
+        }
+
+        [TestMethod]
+        public void HmacSha2_256()
+        {
+            DoTest(MessageAuthenticationCodeAlgorithm.HmacSha2_256);
+        }
+
+        [TestMethod]
+        public void HmacSha2_512()
+        {
+            DoTest(MessageAuthenticationCodeAlgorithm.HmacSha2_512);
+        }
+
+        private void DoTest(MessageAuthenticationCodeAlgorithm macAlgorithm)
+        {
+            _remoteSshdConfig.ClearMessageAuthenticationCodeAlgorithms()
+                             .AddMessageAuthenticationCodeAlgorithm(macAlgorithm)
+                             .Update()
+                             .Restart();
+
+            using (var client = new SshClient(_connectionInfoFactory.Create()))
+            {
+                client.Connect();
+                client.Disconnect();
+            }
+        }
+    }
+}

+ 0 - 147
src/Renci.SshNet.IntegrationTests/OldIntegrationTests/AesCipherTests.cs

@@ -1,147 +0,0 @@
-using Renci.SshNet.IntegrationTests.Common;
-using Renci.SshNet.Security.Cryptography.Ciphers;
-using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
-using Renci.SshNet.TestTools.OpenSSH;
-
-namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
-{
-    [TestClass]
-    public class AesCipherTests : IntegrationTestBase
-    {
-        private IConnectionInfoFactory _adminConnectionInfoFactory;
-        private RemoteSshdConfig _remoteSshdConfig;
-
-        [TestInitialize]
-        public void SetUp()
-        {
-            _adminConnectionInfoFactory = new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort);
-            _remoteSshdConfig = new RemoteSshd(_adminConnectionInfoFactory).OpenConfig();
-        }
-
-        [TestCleanup]
-        public void TearDown()
-        {
-            _remoteSshdConfig?.Reset();
-        }
-
-        [TestMethod]
-        [Owner("olegkap")]
-        [TestCategory("Cipher")]
-        public void Test_Cipher_AEes128CBC_Connection()
-        {
-            _remoteSshdConfig.AddCipher(Cipher.Aes128Cbc)
-                             .Update()
-                             .Restart();
-
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.Encryptions.Clear();
-            connectionInfo.Encryptions.Add("aes128-cbc", new CipherInfo(128, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), null); }));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-
-        [TestMethod]
-        [Owner("olegkap")]
-        [TestCategory("Cipher")]
-        public void Test_Cipher_Aes192CBC_Connection()
-        {
-            _remoteSshdConfig.AddCipher(Cipher.Aes192Cbc)
-                             .Update()
-                             .Restart();
-
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.Encryptions.Clear();
-            connectionInfo.Encryptions.Add("aes192-cbc", new CipherInfo(192, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), null); }));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-
-        [TestMethod]
-        [Owner("olegkap")]
-        [TestCategory("Cipher")]
-        public void Test_Cipher_Aes256CBC_Connection()
-        {
-            _remoteSshdConfig.AddCipher(Cipher.Aes256Cbc)
-                             .Update()
-                             .Restart();
-
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.Encryptions.Clear();
-            connectionInfo.Encryptions.Add("aes256-cbc", new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), null); }));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-
-        [TestMethod]
-        [Owner("olegkap")]
-        [TestCategory("Cipher")]
-        public void Test_Cipher_Aes128CTR_Connection()
-        {
-            _remoteSshdConfig.AddCipher(Cipher.Aes128Ctr)
-                             .Update()
-                             .Restart();
-
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.Encryptions.Clear();
-            connectionInfo.Encryptions.Add("aes128-ctr", new CipherInfo(128, (key, iv) => { return new AesCipher(key, new CtrCipherMode(iv), null); }));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-
-        [TestMethod]
-        [Owner("olegkap")]
-        [TestCategory("Cipher")]
-        public void Test_Cipher_Aes192CTR_Connection()
-        {
-            _remoteSshdConfig.AddCipher(Cipher.Aes192Ctr)
-                             .Update()
-                             .Restart();
-
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.Encryptions.Clear();
-            connectionInfo.Encryptions.Add("aes192-ctr", new CipherInfo(192, (key, iv) => { return new AesCipher(key, new CtrCipherMode(iv), null); }));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-
-        [TestMethod]
-        [Owner("olegkap")]
-        [TestCategory("Cipher")]
-        public void Test_Cipher_Aes256CTR_Connection()
-        {
-            _remoteSshdConfig.AddCipher(Cipher.Aes256Ctr)
-                             .Update()
-                             .Restart();
-
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.Encryptions.Clear();
-            connectionInfo.Encryptions.Add("aes256-ctr", new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CtrCipherMode(iv), null); }));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-    }
-}

+ 0 - 67
src/Renci.SshNet.IntegrationTests/OldIntegrationTests/HMacTest.cs

@@ -1,67 +0,0 @@
-using Renci.SshNet.Abstractions;
-using Renci.SshNet.IntegrationTests.Common;
-
-namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
-{
-    [TestClass]
-    public class HMacTest : IntegrationTestBase
-    {
-        private IConnectionInfoFactory _adminConnectionInfoFactory;
-        private RemoteSshdConfig _remoteSshdConfig;
-
-        [TestInitialize]
-        public void SetUp()
-        {
-            _adminConnectionInfoFactory = new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort);
-            _remoteSshdConfig = new RemoteSshd(_adminConnectionInfoFactory).OpenConfig();
-        }
-
-        [TestCleanup]
-        public void TearDown()
-        {
-            _remoteSshdConfig?.Reset();
-        }
-
-        [TestMethod]
-        public void Test_HMac_Sha1_Connection()
-        {
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.HmacAlgorithms.Clear();
-            connectionInfo.HmacAlgorithms.Add("hmac-sha1", new HashInfo(20 * 8, CryptoAbstraction.CreateHMACSHA1));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-
-        [TestMethod]
-        public void Test_HMac_Sha256_Connection()
-        {
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.HmacAlgorithms.Clear();
-            connectionInfo.HmacAlgorithms.Add("hmac-sha2-256", new HashInfo(32 * 8, CryptoAbstraction.CreateHMACSHA256));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-
-        [TestMethod]
-        public void Test_HMac_Sha2_512_Connection()
-        {
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.HmacAlgorithms.Clear();
-            connectionInfo.HmacAlgorithms.Add("hmac-sha2-512", new HashInfo(64 * 8, CryptoAbstraction.CreateHMACSHA512));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-    }
-}

+ 0 - 50
src/Renci.SshNet.IntegrationTests/OldIntegrationTests/TripleDesCipherTest.cs

@@ -1,50 +0,0 @@
-using Renci.SshNet.IntegrationTests.Common;
-using Renci.SshNet.Security.Cryptography.Ciphers;
-using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
-using Renci.SshNet.TestTools.OpenSSH;
-
-namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
-{
-    /// <summary>
-    /// Implements 3DES cipher algorithm.
-    /// </summary>
-    [TestClass]
-    public class TripleDesCipherTest : IntegrationTestBase
-    {
-        private IConnectionInfoFactory _adminConnectionInfoFactory;
-        private RemoteSshdConfig _remoteSshdConfig;
-
-        [TestInitialize]
-        public void SetUp()
-        {
-            _adminConnectionInfoFactory = new LinuxAdminConnectionFactory(SshServerHostName, SshServerPort);
-            _remoteSshdConfig = new RemoteSshd(_adminConnectionInfoFactory).OpenConfig();
-        }
-
-        [TestCleanup]
-        public void TearDown()
-        {
-            _remoteSshdConfig?.Reset();
-        }
-        
-        [TestMethod]
-        [Owner("olegkap")]
-        [TestCategory("Cipher")]
-        public void Test_Cipher_TripleDESCBC_Connection()
-        {
-            _remoteSshdConfig.AddCipher(Cipher.TripledesCbc)
-                             .Update()
-                             .Restart();
-
-            var connectionInfo = new PasswordConnectionInfo(SshServerHostName, SshServerPort, User.UserName, User.Password);
-            connectionInfo.Encryptions.Clear();
-            connectionInfo.Encryptions.Add("3des-cbc", new CipherInfo(192, (key, iv) => { return new TripleDesCipher(key, new CbcCipherMode(iv), null); }));
-
-            using (var client = new SshClient(connectionInfo))
-            {
-                client.Connect();
-                client.Disconnect();
-            }
-        }
-    }
-}

+ 1 - 1
src/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs

@@ -73,7 +73,7 @@ namespace Renci.SshNet.IntegrationTests
         private void DoTest(PublicKeyAlgorithm publicKeyAlgorithm, string keyResource)
         {
             _remoteSshdConfig.ClearPublicKeyAcceptedAlgorithms()
-                             .AddPublicKeyAcceptedAlgorithms(publicKeyAlgorithm)
+                             .AddPublicKeyAcceptedAlgorithm(publicKeyAlgorithm)
                              .Update()
                              .Restart();
 

+ 13 - 1
src/Renci.SshNet.IntegrationTests/RemoteSshd.cs

@@ -167,12 +167,24 @@ namespace Renci.SshNet.IntegrationTests
             return this;
         }
 
-        public RemoteSshdConfig AddPublicKeyAcceptedAlgorithms(PublicKeyAlgorithm publicKeyAlgorithm)
+        public RemoteSshdConfig AddPublicKeyAcceptedAlgorithm(PublicKeyAlgorithm publicKeyAlgorithm)
         {
             _config.PublicKeyAcceptedAlgorithms.Add(publicKeyAlgorithm);
             return this;
         }
 
+        public RemoteSshdConfig ClearMessageAuthenticationCodeAlgorithms()
+        {
+            _config.MessageAuthenticationCodeAlgorithms.Clear();
+            return this;
+        }
+
+        public RemoteSshdConfig AddMessageAuthenticationCodeAlgorithm(MessageAuthenticationCodeAlgorithm messageAuthenticationCodeAlgorithm)
+        {
+            _config.MessageAuthenticationCodeAlgorithms.Add(messageAuthenticationCodeAlgorithm);
+            return this;
+        }
+
         public RemoteSshdConfig ClearHostKeyAlgorithms()
         {
             _config.HostKeyAlgorithms.Clear();