Skip to content

Commit

Permalink
Remove duplicate GetManifestResourceStream helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob-Hague committed Oct 26, 2023
1 parent f51b918 commit d73c56b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 69 deletions.
25 changes: 7 additions & 18 deletions test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
13 changes: 1 addition & 12 deletions test/Renci.SshNet.IntegrationTests/SftpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 5 additions & 8 deletions test/Renci.SshNet.IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading;

using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -9,6 +8,7 @@

using Renci.SshNet.Common;
using Renci.SshNet.Security;
using Renci.SshNet.Tests.Common;

namespace Renci.SshNet.Tests.Classes
{
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading;

using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -9,6 +8,7 @@

using Renci.SshNet.Common;
using Renci.SshNet.Security;
using Renci.SshNet.Tests.Common;

namespace Renci.SshNet.Tests.Classes
{
Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 5 additions & 2 deletions test/Renci.SshNet.Tests/Common/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}'.");
}
}
}

0 comments on commit d73c56b

Please sign in to comment.