Sfoglia il codice sorgente

Added tests for GenerateRandom.
Updated project files after move and rename.
Fixed a few warning in HashAlgorithmFactory.

drieseng 9 anni fa
parent
commit
ba8a30bce1

+ 4 - 4
src/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj

@@ -47,6 +47,9 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="..\Renci.SshNet\Abstractions\CryptoAbstraction.cs">
+      <Link>Abstractions\CryptoAbstraction.cs</Link>
+    </Compile>
     <Compile Include="..\Renci.SshNet\Abstractions\DiagnosticAbstraction.cs">
       <Link>Abstractions\DiagnosticAbstraction.cs</Link>
     </Compile>
@@ -659,9 +662,6 @@
     <Compile Include="..\Renci.SshNet\Security\Cryptography\DsaKey.cs">
       <Link>Security\Cryptography\DsaKey.cs</Link>
     </Compile>
-    <Compile Include="..\Renci.SshNet\Security\Cryptography\HashAlgorithmFactory.cs">
-      <Link>Security\Cryptography\HashAlgorithmFactory.cs</Link>
-    </Compile>
     <Compile Include="..\Renci.SshNet\Security\Cryptography\HMACMD5.cs">
       <Link>Security\Cryptography\HMACMD5.cs</Link>
     </Compile>
@@ -935,7 +935,7 @@
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <ProjectExtensions>
     <VisualStudio>
-      <UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" />
+      <UserProperties ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
     </VisualStudio>
   </ProjectExtensions>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 4 - 4
src/Renci.SshNet.Silverlight5/Renci.SshNet.Silverlight5.csproj

@@ -75,6 +75,9 @@
     <Compile Include="..\Renci.SshNet.Silverlight\Session.SilverlightShared.cs">
       <Link>Session.SilverlightShared.cs</Link>
     </Compile>
+    <Compile Include="..\Renci.SshNet\Abstractions\CryptoAbstraction.cs">
+      <Link>Abstractions\CryptoAbstraction.cs</Link>
+    </Compile>
     <Compile Include="..\Renci.SshNet\Abstractions\DiagnosticAbstraction.cs">
       <Link>Abstractions\DiagnosticAbstraction.cs</Link>
     </Compile>
@@ -639,9 +642,6 @@
     <Compile Include="..\Renci.SshNet\Security\Cryptography\DsaKey.cs">
       <Link>Security\Cryptography\DsaKey.cs</Link>
     </Compile>
-    <Compile Include="..\Renci.SshNet\Security\Cryptography\HashAlgorithmFactory.cs">
-      <Link>Security\Cryptography\HashAlgorithmFactory.cs</Link>
-    </Compile>
     <Compile Include="..\Renci.SshNet\Security\Cryptography\HMACMD5.cs">
       <Link>Security\Cryptography\HMACMD5.cs</Link>
     </Compile>
@@ -910,7 +910,7 @@
       <FlavorProperties GUID="{A1591282-1198-4647-A2B1-27E5FF5F6F3B}">
         <SilverlightProjectProperties />
       </FlavorProperties>
-      <UserProperties ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
+      <UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" />
     </VisualStudio>
   </ProjectExtensions>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 10 - 1
src/Renci.SshNet.Tests.NET35/Renci.SshNet.Tests.NET35.csproj

@@ -56,6 +56,15 @@
     </CodeAnalysisDependentAssemblyPaths>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="..\..\test\Renci.SshNet.Shared.Tests\Abstractions\CryptoAbstraction_GenerateRandom.cs">
+      <Link>Classes\Abstraction\CryptoAbstraction_GenerateRandom.cs</Link>
+    </Compile>
+    <Compile Include="..\..\test\Renci.SshNet.Shared.Tests\Abstractions\DnsAbstraction_GetHostAddresses.cs">
+      <Link>Classes\Abstraction\DnsAbstraction_GetHostAddresses.cs</Link>
+    </Compile>
+    <Compile Include="..\..\test\Renci.SshNet.Shared.Tests\Abstractions\ThreadAbstraction_ExecuteThread.cs">
+      <Link>Classes\Abstraction\ThreadAbstraction_ExecuteThread.cs</Link>
+    </Compile>
     <Compile Include="..\Renci.SshNet.Tests\Classes\Channels\ChannelDirectTcpipTest.cs">
       <Link>Classes\Channels\ChannelDirectTcpipTest.cs</Link>
     </Compile>
@@ -1241,7 +1250,7 @@
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ProjectExtensions>
     <VisualStudio>
-      <UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" />
+      <UserProperties ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
     </VisualStudio>
   </ProjectExtensions>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 3 - 0
src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

@@ -60,6 +60,9 @@
     </CodeAnalysisDependentAssemblyPaths>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="..\..\test\Renci.SshNet.Shared.Tests\Abstractions\CryptoAbstraction_GenerateRandom.cs">
+      <Link>Classes\Abstractions\CryptoAbstraction_GenerateRandom.cs</Link>
+    </Compile>
     <Compile Include="..\..\test\Renci.SshNet.Shared.Tests\Abstractions\DnsAbstraction_GetHostAddresses.cs">
       <Link>Classes\Abstractions\DnsAbstraction_GetHostAddresses.cs</Link>
     </Compile>

+ 3 - 3
src/Renci.SshNet.UAP10/Renci.SshNet.UAP10.csproj

@@ -107,6 +107,9 @@
     <None Include="project.json" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="..\Renci.SshNet\Abstractions\CryptoAbstraction.cs">
+      <Link>Abstractions\CryptoAbstraction.cs</Link>
+    </Compile>
     <Compile Include="..\Renci.SshNet\Abstractions\DiagnosticAbstraction.cs">
       <Link>Abstractions\DiagnosticAbstraction.cs</Link>
     </Compile>
@@ -695,9 +698,6 @@
     <Compile Include="..\Renci.SshNet\Security\Cryptography\DsaKey.cs">
       <Link>Security\Cryptography\DsaKey.cs</Link>
     </Compile>
-    <Compile Include="..\Renci.SshNet\Security\Cryptography\HashAlgorithmFactory.cs">
-      <Link>Security\Cryptography\HashAlgorithmFactory.cs</Link>
-    </Compile>
     <Compile Include="..\Renci.SshNet\Security\Cryptography\HMACMD5.cs">
       <Link>Security\Cryptography\HMACMD5.cs</Link>
     </Compile>

+ 4 - 4
src/Renci.SshNet.WindowsPhone8/Renci.SshNet.WindowsPhone8.csproj

@@ -94,6 +94,9 @@
     <Compile Include="..\Renci.SshNet.WindowsPhone\Session.WP.cs">
       <Link>Session.WP.cs</Link>
     </Compile>
+    <Compile Include="..\Renci.SshNet\Abstractions\CryptoAbstraction.cs">
+      <Link>Abstractions\CryptoAbstraction.cs</Link>
+    </Compile>
     <Compile Include="..\Renci.SshNet\Abstractions\DiagnosticAbstraction.cs">
       <Link>Abstractions\DiagnosticAbstraction.cs</Link>
     </Compile>
@@ -655,9 +658,6 @@
     <Compile Include="..\Renci.SshNet\Security\Cryptography\DsaKey.cs">
       <Link>Security\Cryptography\DsaKey.cs</Link>
     </Compile>
-    <Compile Include="..\Renci.SshNet\Security\Cryptography\HashAlgorithmFactory.cs">
-      <Link>Security\Cryptography\HashAlgorithmFactory.cs</Link>
-    </Compile>
     <Compile Include="..\Renci.SshNet\Security\Cryptography\HMACMD5.cs">
       <Link>Security\Cryptography\HMACMD5.cs</Link>
     </Compile>
@@ -928,7 +928,7 @@
   <Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).CSharp.targets" />
   <ProjectExtensions>
     <VisualStudio>
-      <UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" />
+      <UserProperties ProjectLinkReference="2f5f8c90-0bd1-424f-997c-7bc6280919d1" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
     </VisualStudio>
   </ProjectExtensions>
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 9 - 7
src/Renci.SshNet/Abstractions/CryptoAbstraction.cs

@@ -1,39 +1,41 @@
 using System;
-using System.Security.Cryptography;
-using Renci.SshNet.Messages.Authentication;
 
 namespace Renci.SshNet.Security.Cryptography
 {
     internal static class HashAlgorithmFactory
     {
 #if FEATURE_RNG_CREATE || FEATURE_RNG_CSP
-        private static readonly RandomNumberGenerator _randomizer = HashAlgorithmFactory.CreateRandomNumberGenerator();
+        private static readonly System.Security.Cryptography.RandomNumberGenerator Randomizer = HashAlgorithmFactory.CreateRandomNumberGenerator();
 #endif
 
         /// <summary>
         /// Fills an array of bytes with a cryptographically strong random sequence of values.
         /// </summary>
         /// <param name="data">The array to fill with cryptographically strong random bytes.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="data"/> is <c>null</c>.</exception>
         /// <remarks>
         /// The length of the byte array determines how many random bytes are produced.
         /// </remarks>
         public static void GenerateRandom(byte[] data)
         {
 #if FEATURE_RNG_CREATE || FEATURE_RNG_CSP
-            _randomizer.GetBytes(data);
+            Randomizer.GetBytes(data);
 #else
+            if(data == null)
+                throw new ArgumentNullException("data");
+
             var buffer = Windows.Security.Cryptography.CryptographicBuffer.GenerateRandom((uint) data.Length);
             System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(buffer, data);
 #endif
         }
 
 #if FEATURE_RNG_CREATE || FEATURE_RNG_CSP
-        public static RandomNumberGenerator CreateRandomNumberGenerator()
+        public static System.Security.Cryptography.RandomNumberGenerator CreateRandomNumberGenerator()
         {
 #if FEATURE_RNG_CREATE
-            return RandomNumberGenerator.Create();
+            return System.Security.Cryptography.RandomNumberGenerator.Create();
 #elif FEATURE_RNG_CSP
-            return new RNGCryptoServiceProvider();
+            return new System.Security.Cryptography.RNGCryptoServiceProvider();
 #else
 #error Creation of RandomNumberGenerator is not implemented.
 #endif

+ 1 - 1
src/Renci.SshNet/Renci.SshNet.csproj

@@ -46,6 +46,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Abstractions\CryptoAbstraction.cs" />
     <Compile Include="Abstractions\DiagnosticAbstraction.cs" />
     <Compile Include="Abstractions\DnsAbstraction.cs" />
     <Compile Include="Abstractions\ReflectionAbstraction.cs" />
@@ -161,7 +162,6 @@
     <Compile Include="Messages\Transport\KeyExchangeEcdhInitMessage.cs" />
     <Compile Include="Messages\Transport\KeyExchangeEcdhReplyMessage.cs" />
     <Compile Include="Netconf\INetConfSession.cs" />
-    <Compile Include="Security\Cryptography\HashAlgorithmFactory.cs" />
     <Compile Include="Security\Cryptography\HMACMD5.cs" />
     <Compile Include="Security\Cryptography\HMACSHA1.cs" />
     <Compile Include="Security\Cryptography\HMACSHA256.cs" />

+ 53 - 0
test/Renci.SshNet.Shared.Tests/Abstractions/CryptoAbstraction_GenerateRandom.cs

@@ -0,0 +1,53 @@
+using System;
+using System.Linq;
+#if SILVERLIGHT
+using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
+#else
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+#endif
+using Renci.SshNet.Security.Cryptography;
+
+namespace Renci.SshNet.Tests.Abstractions
+{
+    [TestClass]
+    public class CryptoAbstraction_GenerateRandom
+    {
+        [TestMethod]
+        public void ShouldThrowArgumentNullExceptionWhenDataIsNull()
+        {
+            const byte[] data = null;
+
+            try
+            {
+                HashAlgorithmFactory.GenerateRandom(data); 
+                Assert.Fail();
+            }
+            catch (ArgumentNullException ex)
+            {
+                Assert.IsNull(ex.InnerException);
+                Assert.AreEqual("data", ex.ParamName);
+            }
+        }
+
+        [TestMethod]
+        public void ShouldPerformNoOpWhenDataIsZeroLength()
+        {
+            var data = new byte[0];
+
+            HashAlgorithmFactory.GenerateRandom(data);
+        }
+
+        [TestMethod]
+        public void ShouldGenerateRandomSequenceOfValues()
+        {
+            var dataLength = new Random().Next(1, 100);
+            var dataA = new byte[dataLength];
+            var dataB = new byte[dataLength];
+
+            HashAlgorithmFactory.GenerateRandom(dataA);
+            HashAlgorithmFactory.GenerateRandom(dataB);
+
+            Assert.IsFalse(dataA.SequenceEqual(dataB));
+        }
+    }
+}

+ 1 - 0
test/Renci.SshNet.Shared.Tests/Renci.SshNet.Shared.Tests.projitems

@@ -9,6 +9,7 @@
     <Import_RootNamespace>Renci.SshNet.Tests</Import_RootNamespace>
   </PropertyGroup>
   <ItemGroup>
+    <Compile Include="$(MSBuildThisFileDirectory)Abstractions\CryptoAbstraction_GenerateRandom.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Abstractions\DnsAbstraction_GetHostAddresses.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Abstractions\ThreadAbstraction_ExecuteThread.cs" />
   </ItemGroup>

+ 1 - 1
test/Renci.SshNet.WindowsPhone8.Tests/Renci.SshNet.WindowsPhone8.Tests.csproj

@@ -32,7 +32,7 @@
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
     <OutputPath>Bin\x86\Debug</OutputPath>
-    <DefineConstants>DEBUG;TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
+    <DefineConstants>TRACE;DEBUG;SILVERLIGHT</DefineConstants>
     <NoStdLib>true</NoStdLib>
     <NoConfig>true</NoConfig>
     <ErrorReport>prompt</ErrorReport>