| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using Renci.SshNet.IntegrationTests.Common;
- using Renci.SshNet.TestTools.OpenSSH;
- namespace Renci.SshNet.IntegrationTests
- {
- [TestClass]
- public class PrivateKeyAuthenticationTests : TestBase
- {
- 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 SshDss()
- {
- DoTest(PublicKeyAlgorithm.SshDss, "id_dsa");
- }
- [TestMethod]
- public void SshRsa()
- {
- DoTest(PublicKeyAlgorithm.SshRsa, "id_rsa");
- }
- [TestMethod]
- public void SshRsaSha256()
- {
- DoTest(PublicKeyAlgorithm.RsaSha2256, "id_rsa");
- }
- [TestMethod]
- public void SshRsaSha512()
- {
- DoTest(PublicKeyAlgorithm.RsaSha2512, "id_rsa");
- }
- [TestMethod]
- public void Ecdsa256()
- {
- DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp256, "key_ecdsa_256_openssh");
- }
- [TestMethod]
- public void Ecdsa384()
- {
- DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp384, "key_ecdsa_384_openssh");
- }
- [TestMethod]
- public void Ecdsa521()
- {
- DoTest(PublicKeyAlgorithm.EcdsaSha2Nistp521, "key_ecdsa_521_openssh");
- }
- [TestMethod]
- public void Ed25519()
- {
- DoTest(PublicKeyAlgorithm.SshEd25519, "key_ed25519_openssh");
- }
- private void DoTest(PublicKeyAlgorithm publicKeyAlgorithm, string keyResource)
- {
- _remoteSshdConfig.ClearPublicKeyAcceptedAlgorithms()
- .AddPublicKeyAcceptedAlgorithm(publicKeyAlgorithm)
- .Update()
- .Restart();
- var connectionInfo = _connectionInfoFactory.Create(CreatePrivateKeyAuthenticationMethod(keyResource));
- using (var client = new SshClient(connectionInfo))
- {
- client.Connect();
- }
- }
- private PrivateKeyAuthenticationMethod CreatePrivateKeyAuthenticationMethod(string keyResource)
- {
- using (var stream = GetData($"resources.client.{keyResource}"))
- {
- return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, new PrivateKeyFile(stream));
- }
- }
- }
- }
|