From e27450acd474fca2362d52b6ac3613aae14dbc78 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sun, 3 May 2015 16:08:28 -0500 Subject: [PATCH] (GH-121) specs for Md5HashProvider --- .../chocolatey.tests.integration.csproj | 1 + .../cryptography/Md5HashProviderSpecs.cs | 67 +++++++++++++++++++ src/chocolatey.tests/chocolatey.tests.csproj | 1 + .../cryptography/Md5HashProviderSpecs.cs | 65 ++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 src/chocolatey.tests.integration/infrastructure/cryptography/Md5HashProviderSpecs.cs create mode 100644 src/chocolatey.tests/infrastructure/cryptography/Md5HashProviderSpecs.cs diff --git a/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj b/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj index 52fd7c9a05..36d7ca3485 100644 --- a/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj +++ b/src/chocolatey.tests.integration/chocolatey.tests.integration.csproj @@ -76,6 +76,7 @@ + diff --git a/src/chocolatey.tests.integration/infrastructure/cryptography/Md5HashProviderSpecs.cs b/src/chocolatey.tests.integration/infrastructure/cryptography/Md5HashProviderSpecs.cs new file mode 100644 index 0000000000..ca63b1c570 --- /dev/null +++ b/src/chocolatey.tests.integration/infrastructure/cryptography/Md5HashProviderSpecs.cs @@ -0,0 +1,67 @@ +// Copyright © 2011 - Present RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.tests.integration.infrastructure.cryptography +{ + using System; + using System.IO; + using System.Reflection; + using System.Security.Cryptography; + using Should; + using chocolatey.infrastructure.cryptography; + using chocolatey.infrastructure.filesystem; + + public class Md5HashProviderSpecs + { + public abstract class Md5HashProviderSpecsBase : TinySpec + { + protected Md5HashProvider Provider; + protected DotNetFileSystem FileSystem; + protected string ContextDirectory; + + public override void Context() + { + FileSystem = new DotNetFileSystem(); + Provider = new Md5HashProvider(FileSystem); + ContextDirectory = FileSystem.combine_paths(FileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)), "context"); + } + } + + public class when_Md5HashProvider_provides_a_hash : Md5HashProviderSpecsBase + { + private string result; + private string filePath; + + public override void Context() + { + base.Context(); + filePath = FileSystem.combine_paths(ContextDirectory, "testing.packages.config"); + } + + public override void Because() + { + result = Provider.hash_file(filePath); + } + + [Fact] + public void should_provide_the_correct_hash_based_on_a_checksum() + { + var expected = BitConverter.ToString(MD5.Create().ComputeHash(File.ReadAllBytes(filePath))).Replace("-", string.Empty); + + result.ShouldEqual(expected); + } + } + } +} \ No newline at end of file diff --git a/src/chocolatey.tests/chocolatey.tests.csproj b/src/chocolatey.tests/chocolatey.tests.csproj index f31a59eecf..5693af63ce 100644 --- a/src/chocolatey.tests/chocolatey.tests.csproj +++ b/src/chocolatey.tests/chocolatey.tests.csproj @@ -82,6 +82,7 @@ + diff --git a/src/chocolatey.tests/infrastructure/cryptography/Md5HashProviderSpecs.cs b/src/chocolatey.tests/infrastructure/cryptography/Md5HashProviderSpecs.cs new file mode 100644 index 0000000000..560d47255f --- /dev/null +++ b/src/chocolatey.tests/infrastructure/cryptography/Md5HashProviderSpecs.cs @@ -0,0 +1,65 @@ +// Copyright © 2011 - Present RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.tests.infrastructure.cryptography +{ + using System; + using System.Security.Cryptography; + using Moq; + using Should; + using chocolatey.infrastructure.cryptography; + using chocolatey.infrastructure.filesystem; + + public class Md5HashProviderSpecs + { + public abstract class Md5HashProviderSpecsBase : TinySpec + { + protected Md5HashProvider Provider; + protected Mock FileSystem = new Mock(); + + public override void Context() + { + Provider = new Md5HashProvider(FileSystem.Object); + } + } + + public class when_Md5HashProvider_provides_a_hash : Md5HashProviderSpecsBase + { + private string result; + private string filePath = "c:\\path\\does\\not\\matter.txt"; + private readonly byte[] byteArray = new byte[] {23, 25, 27}; + + public override void Context() + { + base.Context(); + FileSystem.Setup(x => x.file_exists(It.IsAny())).Returns(true); + FileSystem.Setup(x => x.read_file_bytes(filePath)).Returns(byteArray); + } + + public override void Because() + { + result = Provider.hash_file(filePath); + } + + [Fact] + public void should_provide_the_correct_hash_based_on_a_checksum() + { + var expected = BitConverter.ToString(MD5.Create().ComputeHash(byteArray)).Replace("-", string.Empty); + + result.ShouldEqual(expected); + } + } + } +} \ No newline at end of file