Skip to content

Commit

Permalink
(chocolateyGH-121) specs for Md5HashProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
ferventcoder committed May 3, 2015
1 parent 31d23f2 commit e27450a
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="infrastructure\commands\CommandExecutorSpecs.cs" />
<Compile Include="infrastructure\cryptography\Md5HashProviderSpecs.cs" />
<Compile Include="infrastructure\filesystem\DotNetFileSystemSpecs.cs" />
<Compile Include="MockEventSubscriptionManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
1 change: 1 addition & 0 deletions src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="infrastructure\commands\CommandExecutorSpecs.cs" />
<Compile Include="infrastructure\commands\PowershellExecutorSpecs.cs" />
<Compile Include="infrastructure\configuration\ConfigSpecs.cs" />
<Compile Include="infrastructure\cryptography\Md5HashProviderSpecs.cs" />
<Compile Include="infrastructure\events\context\FakeEvent.cs" />
<Compile Include="infrastructure\events\context\FakeSubscriber.cs" />
<Compile Include="infrastructure\events\EventSubscriptionManagerSpecs.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IFileSystem> FileSystem = new Mock<IFileSystem>();

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<string>())).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);
}
}
}
}

0 comments on commit e27450a

Please sign in to comment.