From 1fc6360cf8325be04af2c5105ff4a49b420bd19d Mon Sep 17 00:00:00 2001 From: Robert Hague Date: Sat, 21 Oct 2023 18:03:10 +0200 Subject: [PATCH] Remove duplicate GetManifestResourceStream helpers --- .../AuthenticationMethodFactory.cs | 25 ++++++------------- .../PrivateKeyAuthenticationTests.cs | 10 ++------ .../SftpTests.cs | 13 +--------- .../Renci.SshNet.IntegrationTests/TestBase.cs | 13 ++++------ ...Test_Connect_OnConnectedThrowsException.cs | 6 ++--- .../Classes/Common/HostKeyEventArgsTest.cs | 5 +--- ...st_Connect_NetConfSessionConnectFailure.cs | 6 ++--- ...tTest_Connect_SftpSessionConnectFailure.cs | 12 ++------- test/Renci.SshNet.Tests/Common/TestBase.cs | 7 ++++-- 9 files changed, 27 insertions(+), 70 deletions(-) diff --git a/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs b/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs index 638b285d..421bdc1e 100644 --- a/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs +++ b/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs @@ -10,32 +10,32 @@ 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 @@ 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 05b6c478..e17855aa 100644 --- a/test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs +++ b/test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs @@ -87,15 +87,9 @@ namespace Renci.SshNet.IntegrationTests 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 7fb18c70..8c1f9166 100644 --- a/test/Renci.SshNet.IntegrationTests/SftpTests.cs +++ b/test/Renci.SshNet.IntegrationTests/SftpTests.cs @@ -300,7 +300,7 @@ namespace Renci.SshNet.IntegrationTests 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 @@ namespace Renci.SshNet.IntegrationTests 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 511bb144..edca2d50 100644 --- a/test/Renci.SshNet.IntegrationTests/TestBase.cs +++ b/test/Renci.SshNet.IntegrationTests/TestBase.cs @@ -66,15 +66,12 @@ namespace Renci.SshNet.IntegrationTests } } - 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 806f6c30..543a5089 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 @@ namespace Renci.SshNet.Tests.Classes 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 39ff85d7..f3d0ae55 100644 --- a/test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs +++ b/test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs @@ -3,7 +3,6 @@ using Renci.SshNet.Common; using Renci.SshNet.Security; using Renci.SshNet.Tests.Common; using System.Linq; -using System.Reflection; namespace Renci.SshNet.Tests.Classes.Common { @@ -86,9 +85,7 @@ namespace Renci.SshNet.Tests.Classes.Common 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 41c285a8..1585d873 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 Moq; using Renci.SshNet.Common; using Renci.SshNet.Security; +using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes { @@ -109,9 +109,7 @@ namespace Renci.SshNet.Tests.Classes 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 92f29113..1b2a068c 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 Moq; using Renci.SshNet.Common; using Renci.SshNet.Security; +using Renci.SshNet.Tests.Common; namespace Renci.SshNet.Tests.Classes { @@ -118,16 +118,8 @@ namespace Renci.SshNet.Tests.Classes 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 5973aa4a..fb3768f2 100644 --- a/test/Renci.SshNet.Tests/Common/TestBase.cs +++ b/test/Renci.SshNet.Tests/Common/TestBase.cs @@ -49,9 +49,12 @@ namespace Renci.SshNet.Tests.Common } } - 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}'."); } } }