Move Extensions to Renci.SshNet.Common namespace.

Moved Dispose(this Socket socket) and Dispose(this WaitHandle handle) to Extensions.cs.
This commit is contained in:
Gert Driesen
2017-09-10 20:30:25 +02:00
parent f08d7a61f1
commit 0e083cfe60
48 changed files with 110 additions and 193 deletions
@@ -1,43 +1,15 @@
using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace Renci.SshNet
namespace Renci.SshNet.Common
{
/// <summary>
/// Collection of different extension method specific for .NET 3.5
/// </summary>
internal static partial class Extensions
{
/// <summary>
/// Disposes the specified socket.
/// </summary>
/// <param name="socket">The socket.</param>
[DebuggerNonUserCode]
internal static void Dispose(this Socket socket)
{
if (socket == null)
throw new NullReferenceException();
socket.Close();
}
/// <summary>
/// Disposes the specified handle.
/// </summary>
/// <param name="handle">The handle.</param>
[DebuggerNonUserCode]
internal static void Dispose(this WaitHandle handle)
{
if (handle == null)
throw new NullReferenceException();
handle.Close();
}
/// <summary>
/// Disposes the specified algorithm.
/// </summary>
@@ -5,6 +5,9 @@ using System.Net.Sockets;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
#if !FEATURE_SOCKET_DISPOSE
using Renci.SshNet.Common;
#endif // !FEATURE_SOCKET_DISPOSE
using Renci.SshNet.Channels;
using Renci.SshNet.Messages.Connection;
using Renci.SshNet.Tests.Common;
@@ -1,8 +1,8 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes.Common
{
@@ -76,7 +76,7 @@ namespace Renci.SshNet.Tests.Classes.Common
public void ShouldReturnFalseWhenLeftIsNotEqualToRight()
{
Assert.IsFalse(Extensions.IsEqualTo(new byte[] {0x0a}, new byte[] {0x0a, 0x0d}));
Assert.IsFalse(Extensions.IsEqualTo(new byte[] { 0x0a, 0x0d }, new byte[] { 0x0a }));
Assert.IsFalse(Extensions.IsEqualTo(new byte[] {0x0a, 0x0d}, new byte[] {0x0a}));
Assert.IsFalse(Extensions.IsEqualTo(new byte[0], new byte[] { 0x0a }));
Assert.IsFalse(Extensions.IsEqualTo(new byte[] { 0x0a, 0x0d }, new byte[0]));
}
@@ -103,7 +103,7 @@ namespace Renci.SshNet.Tests.Classes.Common
{
var buffer = CreateBuffer(50000);
var left = buffer.Concat(new byte[] {0x0a});
var right = buffer.Concat(new byte[] { 0x0a });
var right = buffer.Concat(new byte[] {0x0a});
const int runs = 10000;
Performance(left, right, runs);
@@ -126,8 +126,8 @@ namespace Renci.SshNet.Tests.Classes.Common
public void Performance_LargeArray_NotEqual_SameLength()
{
var buffer = CreateBuffer(50000);
var left = buffer.Concat(new byte[] { 0x0a });
var right = buffer.Concat(new byte[] { 0x0b });
var left = buffer.Concat(new byte[] {0x0a});
var right = buffer.Concat(new byte[] {0x0b});
const int runs = 10000;
Performance(left, right, runs);
@@ -139,7 +139,7 @@ namespace Renci.SshNet.Tests.Classes.Common
public void Performance_LargeArray_Same()
{
var left = CreateBuffer(50000);
var right = left.Concat(new byte[] { 0x0a });
var right = left.Concat(new byte[] {0x0a});
const int runs = 10000;
Performance(left, right, runs);
@@ -173,7 +173,7 @@ namespace Renci.SshNet.Tests.Classes.Common
for (var i = 0; i < runs; i++)
{
var result = Enumerable.SequenceEqual(left, right);
var result = System.Linq.Enumerable.SequenceEqual(left, right);
}
GC.Collect();
@@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes.Common
{
@@ -3,6 +3,7 @@ using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes.Common
{
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes.Common
{
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes.Common
{
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
using Renci.SshNet.Tests.Properties;
using System;
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography.Ciphers;
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
using Renci.SshNet.Tests.Common;
@@ -1,5 +1,6 @@
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography.Ciphers;
using Renci.SshNet.Tests.Common;
using Renci.SshNet.Tests.Properties;
@@ -1,5 +1,6 @@
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography.Ciphers;
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers.Paddings
@@ -1,5 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
#if !FEATURE_EVENTWAITHANDLE_DISPOSE
using Renci.SshNet.Common;
#endif // !FEATURE_EVENTWAITHANDLE_DISPOSE
using Renci.SshNet.Abstractions;
using Renci.SshNet.Sftp;
using System;
@@ -22,7 +25,7 @@ namespace Renci.SshNet.Tests.Classes.Sftp
private SftpCloseAsyncResult _closeAsyncResult;
private SftpFileReader _reader;
private ObjectDisposedException _actualException;
private ManualResetEvent _disposeCompleted;
private EventWaitHandle _disposeCompleted;
[TestCleanup]
public void TearDown()
@@ -3,6 +3,7 @@ using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -3,6 +3,7 @@ using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -3,6 +3,7 @@ using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -2,6 +2,7 @@
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -3,6 +3,7 @@ using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -2,6 +2,7 @@
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -3,6 +3,7 @@ using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -3,6 +3,7 @@ using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Tests.Common;
using System;
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Tests.Common;
using System;
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Tests.Common;
using System;
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Sftp.Responses;
using Renci.SshNet.Tests.Common;
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Sftp.Responses;
using Renci.SshNet.Tests.Common;
@@ -2,6 +2,7 @@
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
namespace Renci.SshNet.Tests.Classes.Sftp
@@ -3,10 +3,12 @@ using System.Globalization;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Tests.Common;
using System.Threading;
using Renci.SshNet.Sftp.Responses;
using System.Threading;
namespace Renci.SshNet.Tests.Classes.Sftp
{
@@ -7,6 +7,7 @@ using Renci.SshNet.Sftp;
using Renci.SshNet.Tests.Common;
using System.Threading;
using Renci.SshNet.Sftp.Responses;
using Renci.SshNet.Common;
namespace Renci.SshNet.Tests.Classes.Sftp
{
@@ -4,6 +4,7 @@ using System.IO;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp;
using Renci.SshNet.Sftp.Responses;
@@ -2,6 +2,9 @@
using System.Net;
using System.Net.Sockets;
using System.Threading;
#if !FEATURE_SOCKET_DISPOSE
using Renci.SshNet.Common;
#endif // !FEATURE_SOCKET_DISPOSE
namespace Renci.SshNet.Tests.Common
{
+1
View File
@@ -1,4 +1,5 @@
using System;
using Renci.SshNet.Common;
using Renci.SshNet.Security.Cryptography;
namespace Renci.SshNet
+34 -148
View File
@@ -5,163 +5,19 @@ using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Text;
#if !FEATURE_WAITHANDLE_DISPOSE
using System.Threading;
#endif // !FEATURE_WAITHANDLE_DISPOSE
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages;
namespace Renci.SshNet
namespace Renci.SshNet.Common
{
/// <summary>
/// Collection of different extension method
/// </summary>
internal static partial class Extensions
{
private enum ShellQuoteState
{
Unquoted = 1,
SingleQuoted = 2,
Quoted = 3
}
/// <summary>
/// Quotes a <see cref="string"/> in a way to be suitable to be used with a shell.
/// </summary>
/// <param name="value">The <see cref="string"/> to quote.</param>
/// <returns>
/// A quoted <see cref="string"/>.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <c>null</c>.</exception>
/// <remarks>
/// <para>
/// If <paramref name="value"/> contains a single-quote, that character is embedded
/// in quotation marks (eg. "'"). Sequences of single-quotes are grouped in a one
/// pair of quotation marks.
/// </para>
/// <para>
/// If the <see cref="string"/> contains an exclamation mark (!), the C-Shell interprets
/// it as a meta-character for history substitution. This even works inside single-quotes
/// or quotation marks, unless escaped with a backslash (\).
/// </para>
/// <para>
/// References:
/// <list type="bullet">
/// <item>
/// <description><a href="http://pubs.opengroup.org/onlinepubs/7908799/xcu/chap2.html">Shell Command Language</a></description>
/// </item>
/// <item>
/// <description><a href="https://earthsci.stanford.edu/computing/unix/shell/specialchars.php">Unix C-Shell special characters and their uses</a></description>
/// </item>
/// <item>
/// <description><a href="https://docstore.mik.ua/orelly/unix3/upt/ch27_13.htm">Differences Between Bourne and C Shell Quoting</a></description>
/// </item>
/// </list>
/// </para>
/// </remarks>
public static string ShellQuote(this string value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
// result is at least value and leading/trailing single-quote
var sb = new StringBuilder(value.Length + 2);
var state = ShellQuoteState.Unquoted;
foreach (var c in value)
{
switch (c)
{
case '\'':
// embed a single-quote in quotes
switch (state)
{
case ShellQuoteState.Unquoted:
// Start quoted string
sb.Append('"');
break;
case ShellQuoteState.Quoted:
// Continue quoted string
break;
case ShellQuoteState.SingleQuoted:
// Close single quoted string
sb.Append('\'');
// Start quoted string
sb.Append('"');
break;
}
state = ShellQuoteState.Quoted;
break;
case '!':
// In C-Shell, an exclamatation point can only be protected from shell interpretation
// when escaped by a backslash
// Source:
// https://earthsci.stanford.edu/computing/unix/shell/specialchars.php
switch (state)
{
case ShellQuoteState.Unquoted:
sb.Append('\\');
break;
case ShellQuoteState.Quoted:
// Close quoted string
sb.Append('"');
sb.Append('\\');
break;
case ShellQuoteState.SingleQuoted:
// Close single quoted string
sb.Append('\'');
sb.Append('\\');
break;
}
state = ShellQuoteState.Unquoted;
break;
default:
switch (state)
{
case ShellQuoteState.Unquoted:
// Start single-quoted string
sb.Append('\'');
break;
case ShellQuoteState.Quoted:
// Close quoted string
sb.Append('"');
// Start single quoted string
sb.Append('\'');
break;
case ShellQuoteState.SingleQuoted:
// Continue single quoted string
break;
}
state = ShellQuoteState.SingleQuoted;
break;
}
sb.Append(c);
}
switch (state)
{
case ShellQuoteState.Unquoted:
break;
case ShellQuoteState.Quoted:
// Close quoted string
sb.Append('"');
break;
case ShellQuoteState.SingleQuoted:
/* Close single quoted string */
sb.Append('\'');
break;
}
if (sb.Length == 0)
{
sb.Append("''");
}
return sb.ToString();
}
/// <summary>
/// Determines whether the specified value is null or white space.
/// </summary>
@@ -421,5 +277,35 @@ namespace Renci.SshNet
return false;
return socket.Connected;
}
#if !FEATURE_SOCKET_DISPOSE
/// <summary>
/// Disposes the specified socket.
/// </summary>
/// <param name="socket">The socket.</param>
[DebuggerNonUserCode]
internal static void Dispose(this Socket socket)
{
if (socket == null)
throw new NullReferenceException();
socket.Close();
}
#endif // !FEATURE_SOCKET_DISPOSE
#if !FEATURE_WAITHANDLE_DISPOSE
/// <summary>
/// Disposes the specified handle.
/// </summary>
/// <param name="handle">The handle.</param>
[DebuggerNonUserCode]
internal static void Dispose(this WaitHandle handle)
{
if (handle == null)
throw new NullReferenceException();
handle.Close();
}
#endif // !FEATURE_WAITHANDLE_DISPOSE
}
}
+1
View File
@@ -1,4 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
+1
View File
@@ -1,5 +1,6 @@
using System;
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet
@@ -130,9 +129,13 @@ namespace Renci.SshNet
public KeyboardInteractiveConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword)
: base(host, port, username, proxyType, proxyHost, proxyPort, proxyUsername, proxyPassword, new KeyboardInteractiveAuthenticationMethod(username))
{
foreach (var authenticationMethod in AuthenticationMethods.OfType<KeyboardInteractiveAuthenticationMethod>())
foreach (var authenticationMethod in AuthenticationMethods)
{
authenticationMethod.AuthenticationPrompt += AuthenticationMethod_AuthenticationPrompt;
var kbdInteractive = authenticationMethod as KeyboardInteractiveAuthenticationMethod;
if (kbdInteractive != null)
{
kbdInteractive.AuthenticationPrompt += AuthenticationMethod_AuthenticationPrompt;
}
}
}
@@ -172,9 +175,13 @@ namespace Renci.SshNet
{
if (AuthenticationMethods != null)
{
foreach (var authenticationMethods in AuthenticationMethods.OfType<IDisposable>())
foreach (var authenticationMethods in AuthenticationMethods)
{
authenticationMethods.Dispose();
var disposable = authenticationMethods as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
@@ -1,4 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Authentication
{
@@ -1,4 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Transport
{
@@ -1,4 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Transport
{
@@ -1,5 +1,8 @@
using System;
using System.Threading;
#if !FEATURE_WAITHANDLE_DISPOSE
using Renci.SshNet.Common;
#endif // !FEATURE_WAITHANDLE_DISPOSE
using Renci.SshNet.Messages.Authentication;
using Renci.SshNet.Messages;
@@ -1,4 +1,6 @@
namespace Renci.SshNet.Security.Cryptography.Ciphers
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography.Ciphers
{
/// <summary>
/// Base class for cipher mode implementations
@@ -1,6 +1,7 @@
#if FEATURE_HMAC_MD5
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -1,6 +1,7 @@
#if FEATURE_HMAC_SHA1
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -1,6 +1,7 @@
#if FEATURE_HMAC_SHA256
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -1,6 +1,7 @@
#if FEATURE_HMAC_SHA384
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
@@ -1,6 +1,7 @@
#if FEATURE_HMAC_SHA512
using System.Security.Cryptography;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{