Skip to content

Commit 1fc6360

Browse files
committed
Remove duplicate GetManifestResourceStream helpers
1 parent d36e7f5 commit 1fc6360

9 files changed

+27
-70
lines changed

test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,32 @@ public PasswordAuthenticationMethod CreatePowerUserPasswordAuthenticationMethod(
1010

1111
public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethod()
1212
{
13-
var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
13+
var privateKeyFile = GetPrivateKey("resources.client.id_rsa");
1414
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
1515
}
1616

1717
public PrivateKeyAuthenticationMethod CreateRegularUserMultiplePrivateKeyAuthenticationMethod()
1818
{
19-
var privateKeyFile1 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
20-
var privateKeyFile2 = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa");
19+
var privateKeyFile1 = GetPrivateKey("resources.client.id_rsa");
20+
var privateKeyFile2 = GetPrivateKey("resources.client.id_rsa");
2121
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile1, privateKeyFile2);
2222
}
2323

2424
public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithPassPhraseAuthenticationMethod()
2525
{
26-
var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", "tester");
26+
var privateKeyFile = GetPrivateKey("resources.client.id_rsa_with_pass", "tester");
2727
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
2828
}
2929

3030
public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithEmptyPassPhraseAuthenticationMethod()
3131
{
32-
var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_rsa_with_pass", null);
32+
var privateKeyFile = GetPrivateKey("resources.client.id_rsa_with_pass", null);
3333
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
3434
}
3535

3636
public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethodWithBadKey()
3737
{
38-
var privateKeyFile = GetPrivateKey("Renci.SshNet.IntegrationTests.resources.client.id_noaccess.rsa");
38+
var privateKeyFile = GetPrivateKey("resources.client.id_noaccess.rsa");
3939
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
4040
}
4141

@@ -64,21 +64,10 @@ public KeyboardInteractiveAuthenticationMethod CreateRegularUserKeyboardInteract
6464

6565
private PrivateKeyFile GetPrivateKey(string resourceName, string passPhrase = null)
6666
{
67-
using (var stream = GetResourceStream(resourceName))
67+
using (var stream = TestBase.GetData(resourceName))
6868
{
6969
return new PrivateKeyFile(stream, passPhrase);
7070
}
7171
}
72-
73-
private Stream GetResourceStream(string resourceName)
74-
{
75-
var type = GetType();
76-
var resourceStream = type.Assembly.GetManifestResourceStream(resourceName);
77-
if (resourceStream == null)
78-
{
79-
throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName));
80-
}
81-
return resourceStream;
82-
}
8372
}
8473
}

test/Renci.SshNet.IntegrationTests/PrivateKeyAuthenticationTests.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,9 @@ private void DoTest(PublicKeyAlgorithm publicKeyAlgorithm, string keyResource)
8787

8888
private PrivateKeyAuthenticationMethod CreatePrivateKeyAuthenticationMethod(string keyResource)
8989
{
90-
var privateKey = CreatePrivateKeyFromManifestResource("Renci.SshNet.IntegrationTests.resources.client." + keyResource);
91-
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKey);
92-
}
93-
94-
private PrivateKeyFile CreatePrivateKeyFromManifestResource(string resourceName)
95-
{
96-
using (var stream = GetManifestResourceStream(resourceName))
90+
using (var stream = GetData($"resources.client.{keyResource}"))
9791
{
98-
return new PrivateKeyFile(stream);
92+
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, new PrivateKeyFile(stream));
9993
}
10094
}
10195
}

test/Renci.SshNet.IntegrationTests/SftpTests.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public void Sftp_Create_FileDoesNotExist()
300300

301301
try
302302
{
303-
using (var imageStream = GetResourceStream("Renci.SshNet.IntegrationTests.resources.issue #70.png"))
303+
using (var imageStream = GetData("resources.issue #70.png"))
304304
{
305305
using (var fs = client.Create(remoteFile))
306306
{
@@ -6301,17 +6301,6 @@ private static byte[] GetBytesWithPreamble(string text, Encoding encoding)
63016301
return textBytes;
63026302
}
63036303

6304-
private static Stream GetResourceStream(string resourceName)
6305-
{
6306-
var type = typeof(SftpTests);
6307-
var resourceStream = type.Assembly.GetManifestResourceStream(resourceName);
6308-
if (resourceStream == null)
6309-
{
6310-
throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName));
6311-
}
6312-
return resourceStream;
6313-
}
6314-
63156304
private static decimal CalculateTransferSpeed(long length, long elapsedMilliseconds)
63166305
{
63176306
return (length / 1024m) / (elapsedMilliseconds / 1000m);

test/Renci.SshNet.IntegrationTests/TestBase.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,12 @@ protected static void CreateFile(string fileName, int size)
6666
}
6767
}
6868

69-
protected Stream GetManifestResourceStream(string resourceName)
69+
internal static Stream GetData(string name)
7070
{
71-
var type = GetType();
72-
var resourceStream = type.Assembly.GetManifestResourceStream(resourceName);
73-
if (resourceStream == null)
74-
{
75-
throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{type.Assembly.FullName}'.", nameof(resourceName));
76-
}
77-
return resourceStream;
71+
string resourceName = $"Renci.SshNet.IntegrationTests.{name}";
72+
73+
return typeof(TestBase).Assembly.GetManifestResourceStream(resourceName)
74+
?? throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{typeof(TestBase).Assembly.FullName}'.", nameof(resourceName));
7875
}
7976
}
8077
}

test/Renci.SshNet.Tests/Classes/BaseClientTest_Connect_OnConnectedThrowsException.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Linq;
3-
using System.Reflection;
43
using System.Threading;
54
using Microsoft.VisualStudio.TestTools.UnitTesting;
65
using Moq;
76
using Renci.SshNet.Common;
87
using Renci.SshNet.Security;
8+
using Renci.SshNet.Tests.Common;
99

1010
namespace Renci.SshNet.Tests.Classes
1111
{
@@ -136,9 +136,7 @@ public void IsConnectedShouldReturnFalse()
136136

137137
private static KeyHostAlgorithm GetKeyHostAlgorithm()
138138
{
139-
var executingAssembly = Assembly.GetExecutingAssembly();
140-
141-
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
139+
using (var s = TestBase.GetData("Key.RSA.txt"))
142140
{
143141
var privateKey = new PrivateKeyFile(s);
144142
return (KeyHostAlgorithm) privateKey.HostKeyAlgorithms.First();

test/Renci.SshNet.Tests/Classes/Common/HostKeyEventArgsTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Renci.SshNet.Security;
44
using Renci.SshNet.Tests.Common;
55
using System.Linq;
6-
using System.Reflection;
76

87
namespace Renci.SshNet.Tests.Classes.Common
98
{
@@ -86,9 +85,7 @@ public void CanTrustTest()
8685

8786
private static KeyHostAlgorithm GetKeyHostAlgorithm()
8887
{
89-
var executingAssembly = Assembly.GetExecutingAssembly();
90-
91-
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
88+
using (var s = GetData("Key.RSA.txt"))
9289
{
9390
var privateKey = new PrivateKeyFile(s);
9491
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();

test/Renci.SshNet.Tests/Classes/NetConfClientTest_Connect_NetConfSessionConnectFailure.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Linq;
3-
using System.Reflection;
43
using System.Threading;
54

65
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -9,6 +8,7 @@
98

109
using Renci.SshNet.Common;
1110
using Renci.SshNet.Security;
11+
using Renci.SshNet.Tests.Common;
1212

1313
namespace Renci.SshNet.Tests.Classes
1414
{
@@ -109,9 +109,7 @@ public void HostKeyReceivedOnSessionShouldNoLongerBeSignaledViaHostKeyReceivedOn
109109

110110
private static KeyHostAlgorithm GetKeyHostAlgorithm()
111111
{
112-
var executingAssembly = Assembly.GetExecutingAssembly();
113-
114-
using (var s = executingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt")))
112+
using (var s = TestBase.GetData("Key.RSA.txt"))
115113
{
116114
var privateKey = new PrivateKeyFile(s);
117115
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();

test/Renci.SshNet.Tests/Classes/SftpClientTest_Connect_SftpSessionConnectFailure.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Linq;
3-
using System.Reflection;
43
using System.Threading;
54

65
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -9,6 +8,7 @@
98

109
using Renci.SshNet.Common;
1110
using Renci.SshNet.Security;
11+
using Renci.SshNet.Tests.Common;
1212

1313
namespace Renci.SshNet.Tests.Classes
1414
{
@@ -118,16 +118,8 @@ public void HostKeyReceivedOnSessionShouldNoLongerBeSignaledViaHostKeyReceivedOn
118118

119119
private static KeyHostAlgorithm GetKeyHostAlgorithm()
120120
{
121-
var executingAssembly = Assembly.GetExecutingAssembly();
122-
var resourceName = string.Format("Renci.SshNet.Tests.Data.{0}", "Key.RSA.txt");
123-
124-
using (var s = executingAssembly.GetManifestResourceStream(resourceName))
121+
using (var s = TestBase.GetData("Key.RSA.txt"))
125122
{
126-
if (s is null)
127-
{
128-
throw new ArgumentException($"Resource '{resourceName}' does not exist in assembly '{executingAssembly.GetName().Name}'.");
129-
}
130-
131123
var privateKey = new PrivateKeyFile(s);
132124
return (KeyHostAlgorithm)privateKey.HostKeyAlgorithms.First();
133125
}

test/Renci.SshNet.Tests/Common/TestBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ protected void CreateTestFile(string fileName, int size)
4949
}
5050
}
5151

52-
protected static Stream GetData(string name)
52+
internal static Stream GetData(string name)
5353
{
54-
return ExecutingAssembly.GetManifestResourceStream(string.Format("Renci.SshNet.Tests.Data.{0}", name));
54+
string resourceName = $"Renci.SshNet.Tests.Data.{name}";
55+
56+
return ExecutingAssembly.GetManifestResourceStream(resourceName)
57+
?? throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{typeof(TestBase).Assembly.FullName}'.");
5558
}
5659
}
5760
}

0 commit comments

Comments
 (0)