Files
ssh.net/test/Renci.SshNet.IntegrationTests/AuthenticationMethodFactory.cs
mus65 3b55ba3a0a Update SonarAnalyzer.CSharp (#1494)
* Update SonarAnalyzer.CSharp

* fix S3993: Custom attributes should be marked with "System.AttributeUsageAttribute"

https://rules.sonarsource.com/csharp/RSPEC-3993/

* fix S6966: Awaitable method should be used

Introduced abstractions for CancellationTokenSource.CancelAsync()
and Stream.DisposeAsync() to avoid #ifdef.

Supressed pipeStream.WriteAsync because it deadlocks the test.
I assume because PipeStream doesn't override WriteAsync.

https://rules.sonarsource.com/csharp/RSPEC-6966/

temp

* fix S3431: "[ExpectedException]" should not be used

Removed the Connect() from Multifactor_PublicKeyWithEmptyPassPhrase
because the Exception is already thrown in the factory.

https://rules.sonarsource.com/csharp/RSPEC-3431/

* fix S2325: Methods and properties that don't access instance data should be static

This one is pretty redundant with CA1822 (which is also disabled in the
tests).

It caught a few more cases in the library itself, most of which can't
be changed because they are public API.

https://rules.sonarsource.com/csharp/RSPEC-2325/

* fix S127: "for" loop stop conditions should be invariant

not sure if this one is worth having. The only cases it found
are imho legitimate or not worth fixing, so I supressed them.

https://rules.sonarsource.com/csharp/RSPEC-127/

* fix S1964: An abstract class should have both abstract and concrete methods

Suppressed for public APIs, changed ExtendedReplyInfo to interface.

https://rules.sonarsource.com/csharp/RSPEC-1694/

* Remove redundant test

this is already covered by Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsNull

* Revert "fix S2325: Methods and properties that don't access instance data should be static"

suppress it instead

This reverts commit 2020604958.

* Revert "fix S127: "for" loop stop conditions should be invariant"

suppress it instead

This reverts commit 1d8b4ac335.
2024-09-19 18:56:32 +02:00

98 lines
4.9 KiB
C#

namespace Renci.SshNet.IntegrationTests
{
public class AuthenticationMethodFactory
{
public PasswordAuthenticationMethod CreatePowerUserPasswordAuthenticationMethod()
{
var user = Users.Admin;
return new PasswordAuthenticationMethod(user.UserName, user.Password);
}
public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethod()
{
var privateKeyFile = GetPrivateKey("Data.Key.RSA.txt");
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
}
public PrivateKeyAuthenticationMethod CreateRegularUserMultiplePrivateKeyAuthenticationMethod()
{
var privateKeyFile1 = GetPrivateKey("Data.Key.RSA.txt");
var privateKeyFile2 = GetPrivateKey("Data.Key.RSA.txt");
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile1, privateKeyFile2);
}
public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyWithPassPhraseAuthenticationMethod()
{
var privateKeyFile = GetPrivateKey("Data.Key.RSA.Encrypted.Aes.256.CBC.12345.txt", "12345");
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, privateKeyFile);
}
public PrivateKeyAuthenticationMethod CreateRegularUserPrivateKeyAuthenticationMethodWithBadKey()
{
string unauthorizedKey = """
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuK3OhcrEnQbbE1+WaE57tUCcTz1yqdE2AwvMfs3of1nyfGcS
Rz9vzAFYU+3uEEApk0QOsIeWCyB2DIlPnlQHyjVWRYPqiTtQ7GmdzbF0ISa7dr23
EHJKgtJxSm3O/sb5F9JyqlxFMhKpz5NVgnN7NFcej93opHZN6h9LaP8cHgJIepWV
IkZqhcv8v6SpAgei0muoPHB+ZA6Rycnv+2//WUBzu+3AJu0PiHUkTTVC8M5svMRV
Ah8CnLsCkAAx7ld4AH7McRlFjymmkwxTSewFJYkloI/OqDOjsmuW03Gmx+eytPWa
HEPGeRhcz1kZ6eOmqrPMlTaLPV1MbFn86nauAQIDAQABAoIBAGEiWauZOMx2nKeV
8SAvl3V/5DbxVOvotAXqIMbZOl4xSw8Pj1eWEBE26+RJEpvNg5CHjUpgJhT4H978
Ibpe7DH418V8WtGPN0MBUhSsLy54lsUfh7fIxVQFp7zEAMmUkdNrxw+/tE1f75zU
G3efkb+3ysVUrFZEOz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95Lg6F58Mhd/tCFA==
-----END RSA PRIVATE KEY-----
""";
using MemoryStream memoryStream = new(Encoding.UTF8.GetBytes(unauthorizedKey));
return new PrivateKeyAuthenticationMethod(Users.Regular.UserName, new PrivateKeyFile(memoryStream));
}
public PasswordAuthenticationMethod CreateRegularUserPasswordAuthenticationMethod()
{
return new PasswordAuthenticationMethod(Users.Regular.UserName, Users.Regular.Password);
}
public PasswordAuthenticationMethod CreateRegularUserPasswordAuthenticationMethodWithBadPassword()
{
return new PasswordAuthenticationMethod(Users.Regular.UserName, "xxx");
}
public KeyboardInteractiveAuthenticationMethod CreateRegularUserKeyboardInteractiveAuthenticationMethod()
{
var keyboardInteractive = new KeyboardInteractiveAuthenticationMethod(Users.Regular.UserName);
keyboardInteractive.AuthenticationPrompt += (sender, args) =>
{
foreach (var authenticationPrompt in args.Prompts)
{
authenticationPrompt.Response = Users.Regular.Password;
}
};
return keyboardInteractive;
}
private PrivateKeyFile GetPrivateKey(string resourceName, string passPhrase = null)
{
using (var stream = TestBase.GetData(resourceName))
{
return new PrivateKeyFile(stream, passPhrase);
}
}
}
}