diff --git a/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs b/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs index 638b285d8..421bdc1ec 100644 --- a/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs +++ b/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs @@ -10,32 +10,32 @@ public PasswordAuthenticationMethod CreatePowerUserPasswordAuthenticationMethod( public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethod() { - var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa"); + var privateKeyFile = GetPrivateKey("resources.client.id_rsa"); return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile); } public PrivateKeyAuthenticationMethod CreateRegularUserMultiplePrivateKeyAuthenticationMethod() { - var privateKeyFile1 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa"); - var privateKeyFile2 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa"); + var privateKeyFile1 = GetPrivateKey("resources.client.id_rsa"); + var privateKeyFile2 = GetPrivateKey("resources.client.id_rsa"); return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile1, privateKeyFile2); } public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithPassPhraseAuthenticationMethod() { - var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", "tester"); + var privateKeyFile = GetPrivateKey("resources.client.id_rsa_with_pass", "tester"); return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile); } public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithEmptyPassPhraseAuthenticationMethod() { - var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", null); + var privateKeyFile = GetPrivateKey("resources.client.id_rsa_with_pass", null); return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile); } public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethodWithBadKey() { - var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_noaccess.rsa"); + var privateKeyFile = GetPrivateKey("resources.client.id_noaccess.rsa"); return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile); } @@ -64,21 +64,10 @@ public KeyboardInteractiveAuthenticationMethod CreateRegularUserKeyboardInteract private PrivateKeyFile GetPrivateKey(string resourceName, string passPhrase = null) { - using (var stream = GetResourceStream(resourceName)) + using (var stream = TestBase.GetData(resourceName)) { return new PrivateKeyFile(stream, passPhrase); } } - - private Stream GetResourceStream(string resourceName) - { - var type = GetType(); - var resourceStream = type.Assembly.GetManifestResourceStream(resourceName); - if (resourceStream == null) - { - throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName)); - } - return resourceStream; - } } } diff --git a/test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs b/test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs index 05b6c4787..e17855aab 100644 --- a/test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs +++ b/test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs @@ -87,15 +87,9 @@ private void DoTest(PublicKeyAlgorithm publicKeyAlgorithm, string keyResource) private PrivateKeyAuthenticationMethod CreatePrivateKeyAuthenticationMethod(string keyResource) { - var privateKey = CreatePrivateKeyFromManifestResource("Renci.SshNet.IntegrationTests.resources.client." + keyResource); - return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKey); - } - - private PrivateKeyFile CreatePrivateKeyFromManifestResource(string resourceName) - { - using (var stream = GetManifestResourceStream(resourceName)) + using (var stream = GetData($"resources.client.{keyResource}")) { - return new PrivateKeyFile(stream); + return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, new PrivateKeyFile(stream)); } } } diff --git a/test/Renci.SshNet.IntegrationTests/SftpTests.cs b/test/Renci.SshNet.IntegrationTests/SftpTests.cs index 7fb18c709..8c1f91660 100644 --- a/test/Renci.SshNet.IntegrationTests/SftpTests.cs +++ b/test/Renci.SshNet.IntegrationTests/SftpTests.cs @@ -300,7 +300,7 @@ public void Sftp_Create_FileDoesNotExist() try { - using (var imageStream = GetResourceStream("Renci.SshNet.IntegrationTests.resources.issue #70.png")) + using (var imageStream = GetData("resources.issue #70.png")) { using (var fs = client.Create(remoteFile)) { @@ -6301,17 +6301,6 @@ private static byte[] GetBytesWithPreamble(string text, Encoding encoding) return textBytes; } - private static Stream GetResourceStream(string resourceName) - { - var type = typeof(SftpTests); - var resourceStream = type.Assembly.GetManifestResourceStream(resourceName); - if (resourceStream == null) - { - throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName)); - } - return resourceStream; - } - private static decimal CalculateTransferSpeed(long length, long elapsedMilliseconds) { return (length / 1024m) / (elapsedMilliseconds / 1000m); diff --git a/test/Renci.SshNet.IntegrationTests/TestBase.cs b/test/Renci.SshNet.IntegrationTests/TestBase.cs index 511bb144d..edca2d509 100644 --- a/test/Renci.SshNet.IntegrationTests/TestBase.cs +++ b/test/Renci.SshNet.IntegrationTests/TestBase.cs @@ -66,15 +66,12 @@ protected static void CreateFile(string fileName, int size) } } - protected Stream GetManifestResourceStream(string resourceName) + internal static Stream GetData(string name) { - var type = GetType(); - var resourceStream = type.Assembly.GetManifestResourceStream(resourceName); - if (resourceStream == null) - { - throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName)); - } - return resourceStream; + string resourceName = $"Renci.SshNet.IntegrationTests.{name}"; + + return typeof(TestBase).Assembly.GetManifestResourceStream(resourceName) + ?? throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{typeof(TestBase).Assembly.FullName}'.", nameof(resourceName)); } } } diff --git a/test/Renci.SshNet.Tests/Classes/BaseClientTest_Connect_OnConnectedThrowsException.cs b/test/Renci.SshNet.Tests/Classes/BaseClientTest_Connect_OnConnectedThrowsException.cs index 806f6c30a..543a5089c 100644 --- a/test/Renci.SshNet.Tests/Classes/BaseClientTest_Connect_OnConnectedThrowsException.cs +++ b/test/Renci.SshNet.Tests/Classes/BaseClientTest_Connect_OnConnectedThrowsException.cs @@ -1,11 +1,11 @@ using System; using System.Linq; -using System.Reflection; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using Renci.SshNet.Common; using Renci.SshNet.Security; +using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes { @@ -136,9 +136,7 @@ public void IsConnectedShouldReturnFalse() private static KeyHostAlgorithm GetKeyHostAlgorithm() { - var executingAssembly = Assembly.GetExecutingAssembly(); - - using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt"))) + using (var s = TestBase.GetData("Key.RSA.txt")) { var privateKey = new PrivateKeyFile(s); return (KeyHostAlgorithm) privateKey.HostKeyAlgorithms.First(); diff --git a/test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs b/test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs index 39ff85d7b..7127da8a0 100644 --- a/test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs +++ b/test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs @@ -86,9 +86,7 @@ public void CanTrustTest() private static KeyHostAlgorithm GetKeyHostAlgorithm() { - var executingAssembly = Assembly.GetExecutingAssembly(); - - using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt"))) + using (var s = GetData("Key.RSA.txt")) { var privateKey = new PrivateKeyFile(s); return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First(); diff --git a/test/Renci.SshNet.Tests/Classes/NetConfClientTest_Connect_NetConfSessionConnectFailure.cs b/test/Renci.SshNet.Tests/Classes/NetConfClientTest_Connect_NetConfSessionConnectFailure.cs index 41c285a84..1585d8734 100644 --- a/test/Renci.SshNet.Tests/Classes/NetConfClientTest_Connect_NetConfSessionConnectFailure.cs +++ b/test/Renci.SshNet.Tests/Classes/NetConfClientTest_Connect_NetConfSessionConnectFailure.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Reflection; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -9,6 +8,7 @@ using Renci.SshNet.Common; using Renci.SshNet.Security; +using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes { @@ -109,9 +109,7 @@ public void HostKeyReceivedOnSessionShouldNoLongerBeSignaledViaHostKeyReceivedOn private static KeyHostAlgorithm GetKeyHostAlgorithm() { - var executingAssembly = Assembly.GetExecutingAssembly(); - - using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt"))) + using (var s = TestBase.GetData("Key.RSA.txt")) { var privateKey = new PrivateKeyFile(s); return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First(); diff --git a/test/Renci.SshNet.Tests/Classes/SftpClientTest_Connect_SftpSessionConnectFailure.cs b/test/Renci.SshNet.Tests/Classes/SftpClientTest_Connect_SftpSessionConnectFailure.cs index 92f29113a..1b2a068c1 100644 --- a/test/Renci.SshNet.Tests/Classes/SftpClientTest_Connect_SftpSessionConnectFailure.cs +++ b/test/Renci.SshNet.Tests/Classes/SftpClientTest_Connect_SftpSessionConnectFailure.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Reflection; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -9,6 +8,7 @@ using Renci.SshNet.Common; using Renci.SshNet.Security; +using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes { @@ -118,16 +118,8 @@ public void HostKeyReceivedOnSessionShouldNoLongerBeSignaledViaHostKeyReceivedOn private static KeyHostAlgorithm GetKeyHostAlgorithm() { - var executingAssembly = Assembly.GetExecutingAssembly(); - var resourceName = string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt"); - - using (var s = executingAssembly.GetManifestResourceStream(resourceName)) + using (var s = TestBase.GetData("Key.RSA.txt")) { - if (s is null) - { - throw new ArgumentException($"Resource '{resourceName}' does not exist in assembly '{executingAssembly.GetName().Name}'."); - } - var privateKey = new PrivateKeyFile(s); return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First(); } diff --git a/test/Renci.SshNet.Tests/Common/TestBase.cs b/test/Renci.SshNet.Tests/Common/TestBase.cs index 5973aa4a3..fb3768f21 100644 --- a/test/Renci.SshNet.Tests/Common/TestBase.cs +++ b/test/Renci.SshNet.Tests/Common/TestBase.cs @@ -49,9 +49,12 @@ protected void CreateTestFile(string fileName, int size) } } - protected static Stream GetData(string name) + internal static Stream GetData(string name) { - return ExecutingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", name)); + string resourceName = $"Renci.SshNet.Tests.Data.{name}"; + + return ExecutingAssembly.GetManifestResourceStream(resourceName) + ?? throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{typeof(TestBase).Assembly.FullName}'."); } } }