forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolateyGH-121) specs for Md5HashProvider
- Loading branch information
1 parent
31d23f2
commit e27450a
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/chocolatey.tests.integration/infrastructure/cryptography/Md5HashProviderSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/chocolatey.tests/infrastructure/cryptography/Md5HashProviderSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |