Skip to content

Commit

Permalink
First passing test for the substituion sypher
Browse files Browse the repository at this point in the history
  • Loading branch information
yodax committed Sep 3, 2015
1 parent 3921e5e commit d16935e
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 43 deletions.
33 changes: 31 additions & 2 deletions Cypher.Tests/Cypher.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,26 @@
<HintPath>..\packages\FluentAssertions.4.0.0\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.core.interfaces, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.interfaces.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.util, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.util.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NUnit.VisualStudio.TestAdapter, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4cb40d35494691ac, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -50,18 +66,31 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="TechTalk.SpecFlow, Version=1.4.0.0, Culture=neutral, PublicKeyToken=0778194805d6db41, processorArchitecture=MSIL">
<HintPath>..\packages\SpecFlow.1.4.0\lib\TechTalk.SpecFlow.dll</HintPath>
<Reference Include="TechTalk.SpecFlow, Version=1.9.0.77, Culture=neutral, PublicKeyToken=0778194805d6db41, processorArchitecture=MSIL">
<HintPath>..\packages\SpecFlow.1.9.0\lib\net35\TechTalk.SpecFlow.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="EncryptPlainText.feature.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>EncryptPlainText.feature</DependentUpon>
</Compile>
<Compile Include="EncryptPlainTextSteps.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="EncryptPlainText.feature">
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>EncryptPlainText.feature.cs</LastGenOutput>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Binary file added Cypher.Tests/Cypher.Tests.v2.ncrunchproject
Binary file not shown.
46 changes: 46 additions & 0 deletions Cypher.Tests/EncryptPlainText.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Feature: Encrypt Plain Text

As a privacy nut I would like to encrypt text in the worst possible way by using a substitution cypher

The cypher will replace each letter with another letter. Each letter will be used exactly once.

My plain and text will be case insensitive and punctuation marks will not be encrypted

Scenario: A simple plain text using all letters from the alphabet
Given the plain text
"""
the quick brown fox jumps over the lazy dog
"""
When I encrypt the text using the following letter substitution
| Plain text letter | Cypher text letter |
| A | B |
| B | C |
| C | D |
| D | E |
| E | F |
| F | G |
| G | H |
| H | I |
| I | J |
| J | K |
| K | L |
| L | M |
| M | N |
| N | O |
| O | P |
| P | Q |
| Q | R |
| R | S |
| S | T |
| T | U |
| U | V |
| V | W |
| W | X |
| X | Y |
| Y | Z |
| Z | A |
Then the cypher text should be
"""
UIF RVJDL CSPXO GPY KVNQT PWFS UIF MBAZ EPH
"""

175 changes: 175 additions & 0 deletions Cypher.Tests/EncryptPlainText.feature.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions Cypher.Tests/EncryptPlainTextSteps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using TechTalk.SpecFlow;

namespace Cypher.Tests
{
[Binding]
public class EncryptPlainTextSteps
{
private string _plainText;
private string _encryptedText;

[Given(@"the plain text")]
public void GivenThePlainText(string plainText)
{
_plainText = plainText;
}

[When(@"I encrypt the text using the following letter substitution")]
public void WhenIEncryptTheTextUsingTheFollowingLetterSubstitution(Table table)
{
var cypher = new SubstitutionCypher();

foreach (var tableRow in table.Rows)
{
cypher.Substitute(tableRow[0]).For(tableRow[1]);
}

_encryptedText = cypher.Encrypt(_plainText);
}

[Then(@"the cypher text should be")]
public void ThenTheCypherTextShouldBe(string expectedCypherText)
{
_encryptedText.Should().Be(expectedCypherText);
}
}

public class SubstitutionCypher
{
private readonly List<Substitution> _substitutionList;

public SubstitutionCypher()
{
_substitutionList = new List<Substitution>();
}

public Substitution Substitute(string letterToSubstitute)
{
var substitution = new Substitution(letterToSubstitute.ToUpper());

_substitutionList.Add(substitution);

return substitution;
}

public string Encrypt(string plainText)
{
var encryptedText = "";
foreach (var letter in plainText.ToUpper())
{
var substitution = _substitutionList.SingleOrDefault(
s => s.LetterToSubstitute.Equals(letter.ToString(), StringComparison.OrdinalIgnoreCase));

if (substitution != null)
{
encryptedText += substitution.LetterToSubstituteWith;
}
else
{
encryptedText += letter;
}
}

return encryptedText;
}
}

public static class SubstitutionExtensionMethods
{
public static void For(this Substitution substitution, string letterToSubstituteWith)
{
substitution.LetterToSubstituteWith = letterToSubstituteWith.ToUpper();
}
}

public class Substitution
{
public string LetterToSubstitute { get; }
public string LetterToSubstituteWith { get; set; }

public Substitution(string letterToSubstitute)
{
LetterToSubstitute = letterToSubstitute;
}
}
}
Loading

0 comments on commit d16935e

Please sign in to comment.