Skip to content

Commit

Permalink
(chocolateyGH-121) Specs and Files Service hardening
Browse files Browse the repository at this point in the history
- Rename Md5HashProvider to CryptoHashProvider and provide enumeration
   for HashAlgorithm Crytpo providers.
- Implement adapter for HashAlgorithm.
- Update specs for CryptoHashProvider
- Specs for FilesService
  • Loading branch information
ferventcoder committed May 4, 2015
1 parent a52adca commit 8b22eeb
Show file tree
Hide file tree
Showing 15 changed files with 499 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="infrastructure\commands\CommandExecutorSpecs.cs" />
<Compile Include="infrastructure\cryptography\Md5HashProviderSpecs.cs" />
<Compile Include="infrastructure\cryptography\CrytpoHashProviderSpecs.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
Expand Up @@ -23,23 +23,23 @@ namespace chocolatey.tests.integration.infrastructure.cryptography
using chocolatey.infrastructure.cryptography;
using chocolatey.infrastructure.filesystem;

public class Md5HashProviderSpecs
public class CrytpoHashProviderSpecs
{
public abstract class Md5HashProviderSpecsBase : TinySpec
public abstract class CrytpoHashProviderSpecsBase : TinySpec
{
protected Md5HashProvider Provider;
protected CrytpoHashProvider Provider;
protected DotNetFileSystem FileSystem;
protected string ContextDirectory;

public override void Context()
{
FileSystem = new DotNetFileSystem();
Provider = new Md5HashProvider(FileSystem);
Provider = new CrytpoHashProvider(FileSystem,CryptoHashProviderType.Md5);
ContextDirectory = FileSystem.combine_paths(FileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)), "context");
}
}

public class when_Md5HashProvider_provides_a_hash : Md5HashProviderSpecsBase
public class when_HashProvider_provides_a_hash : CrytpoHashProviderSpecsBase
{
private string result;
private string filePath;
Expand Down
3 changes: 2 additions & 1 deletion src/chocolatey.tests/chocolatey.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@
<Compile Include="infrastructure.app\commands\ChocolateyUpgradeCommandSpecs.cs" />
<Compile Include="infrastructure.app\configuration\ConfigurationOptionsSpec.cs" />
<Compile Include="infrastructure.app\services\AutomaticUninstallerServiceSpecs.cs" />
<Compile Include="infrastructure.app\services\FilesServiceSpecs.cs" />
<Compile Include="infrastructure\commands\ExternalCommandArgsBuilderSpecs.cs" />
<Compile Include="infrastructure\commandline\InteractivePromptSpecs.cs" />
<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\cryptography\CrytpoHashProvider.cs" />
<Compile Include="infrastructure\events\context\FakeEvent.cs" />
<Compile Include="infrastructure\events\context\FakeSubscriber.cs" />
<Compile Include="infrastructure\events\EventSubscriptionManagerSpecs.cs" />
Expand Down
248 changes: 248 additions & 0 deletions src/chocolatey.tests/infrastructure.app/services/FilesServiceSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
namespace chocolatey.tests.infrastructure.app.services
{
using System;
using System.Collections.Generic;
using System.IO;
using Moq;
using Should;
using chocolatey.infrastructure.app;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.domain;
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.cryptography;
using chocolatey.infrastructure.filesystem;
using chocolatey.infrastructure.results;
using chocolatey.infrastructure.services;

public class FilesServiceSpecs
{
public abstract class FilesServiceSpecsBase : TinySpec
{
protected FilesService Service;
protected Mock<IXmlService> XmlService = new Mock<IXmlService>();
protected Mock<IFileSystem> FileSystem = new Mock<IFileSystem>();
protected Mock<IHashProvider> HashProvider = new Mock<IHashProvider>();

public override void Context()
{
XmlService.ResetCalls();
FileSystem.ResetCalls();
HashProvider.ResetCalls();
Service = new FilesService(XmlService.Object,FileSystem.Object,HashProvider.Object);
}
}

public class when_FilesService_reads_from_files : FilesServiceSpecsBase
{
private Func<PackageFiles> because;

public override void Because()
{
because = () => Service.read_from_file("fake path");
}

[Fact]
public void should_deserialize_when_file_exists()
{
Context();
FileSystem.Setup(x => x.file_exists(It.IsAny<string>())).Returns(true);
XmlService.Setup(x => x.deserialize<PackageFiles>(It.IsAny<string>())).Returns(new PackageFiles());

because();
}

[Fact]
public void should_not_deserialize_if_file_does_not_exist()
{
Context();
FileSystem.Setup(x => x.file_exists(It.IsAny<string>())).Returns(false);

because();

XmlService.Verify(x => x.deserialize<PackageFiles>(It.IsAny<string>()),Times.Never);
}
}

public class when_FilesService_saves_files : FilesServiceSpecsBase
{
private Action because;
private PackageFiles files;

public override void Because()
{
because = () => Service.save_to_file(files , "fake path");
}

[Fact]
public void should_save_if_the_snapshot_is_not_null()
{
Context();
files = new PackageFiles();

because();

XmlService.Verify(x => x.serialize(files, It.IsAny<string>()), Times.Once());
}

[Fact]
public void should_not_do_anything_if_the_snapshot_is_null()
{
Context();
files = null;

because();

XmlService.Verify(x => x.serialize(files, It.IsAny<string>()), Times.Never);
}
}

public class when_FilesService_captures_files_and_install_directory_reports_choco_install_location : FilesServiceSpecsBase
{
private PackageFiles result;
private PackageResult packageResult;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();

public override void Context()
{
base.Context();
packageResult = new PackageResult("bob", "1.2.3", ApplicationParameters.InstallLocation);
}

public override void Because()
{
result = Service.capture_package_files(packageResult, config);
}

[Fact]
public void should_not_call_get_files()
{
FileSystem.Verify(x => x.get_files(It.IsAny<string>(), It.IsAny<string>(), SearchOption.AllDirectories), Times.Never);
}

[Fact]
public void should_return_a_warning_if_the_install_directory_matches_choco_install_location()
{
packageResult.Warning.ShouldBeTrue();
}

[Fact]
public void should_return_null()
{
result.ShouldBeNull();
}
}

public class when_FilesService_captures_files_and_install_directory_reports_packages_location : FilesServiceSpecsBase
{
private PackageFiles result;
private PackageResult packageResult;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();

public override void Context()
{
base.Context();
packageResult = new PackageResult("bob", "1.2.3", ApplicationParameters.PackagesLocation);
}

public override void Because()
{
result = Service.capture_package_files(packageResult, config);
}

[Fact]
public void should_not_call_get_files()
{
FileSystem.Verify(x => x.get_files(It.IsAny<string>(), It.IsAny<string>(), SearchOption.AllDirectories), Times.Never);
}

[Fact]
public void should_return_a_warning_if_the_install_directory_matches_choco_install_location()
{
packageResult.Warning.ShouldBeTrue();
}

[Fact]
public void should_return_null()
{
result.ShouldBeNull();
}
}

public class when_FilesService_captures_files_and_package_result_is_null : FilesServiceSpecsBase
{
private PackageFiles result;
private PackageResult packageResult;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();

public override void Context()
{
base.Context();
packageResult = null;
}

public override void Because()
{
result = Service.capture_package_files(packageResult, config);
}

[Fact]
public void should_not_call_get_files()
{
FileSystem.Verify(x => x.get_files(It.IsAny<string>(), It.IsAny<string>(), SearchOption.AllDirectories), Times.Never);
}

[Fact]
public void should_return_null()
{
result.ShouldBeNull();
}
}

public class when_FilesService_captures_files_happy_path : FilesServiceSpecsBase
{
private PackageFiles result;
private PackageResult packageResult;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private readonly string installDirectory = ApplicationParameters.PackagesLocation + "\\bob";
private readonly IList<string> files = new List<string> { "file1", "file2" };

public override void Context()
{
base.Context();
packageResult = new PackageResult("bob", "1.2.3", installDirectory);

FileSystem.Setup(x => x.get_files(ApplicationParameters.PackagesLocation + "\\bob", It.IsAny<string>(), SearchOption.AllDirectories)).Returns(files);
HashProvider.Setup(x => x.hash_file(It.IsAny<string>())).Returns("yes");
}

public override void Because()
{
result = Service.capture_package_files(packageResult, config);
}

[Fact]
public void should_return_a_PackageFiles_object()
{
result.ShouldNotBeNull();
}

[Fact]
public void should_contain_package_files()
{
result.Files.ShouldNotBeEmpty();
}

[Fact]
public void should_contain_the_correct_number_of_package_files()
{
result.Files.Count.ShouldEqual(files.Count);
}

[Fact]
public void should_call_hash_provider_for_each_file()
{
HashProvider.Verify(x => x.hash_file(It.IsAny<string>()),Times.Exactly(files.Count));
}
}
}
}
Loading

0 comments on commit 8b22eeb

Please sign in to comment.