Skip to content

Commit 225f61d

Browse files
committed
Implemented basic encrypt/decrypt tests.
1 parent cc3da0c commit 225f61d

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using Cantarus.Libraries.Encryption;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.IO;
5+
using System.Text;
6+
7+
namespace EncryptionTests
8+
{
9+
[TestClass]
10+
public class EncryptDecryptTests
11+
{
12+
private const int Iterations = 1000;
13+
14+
[TestMethod]
15+
public void StringEncryptDecrypt()
16+
{
17+
for (int i = 0; i < Iterations; i++)
18+
{
19+
string passPhrase = GeneratePassPhrase();
20+
string beforeString = Encoding.UTF8.GetString(GeneratePayload());
21+
22+
string encryptedString = Crypto.Encrypt(beforeString, passPhrase);
23+
24+
string afterString = Crypto.Decrypt(encryptedString, passPhrase);
25+
26+
Assert.AreEqual(beforeString, afterString);
27+
}
28+
}
29+
30+
[TestMethod]
31+
public void ByteEncryptDecrypt()
32+
{
33+
for (int i = 0; i < Iterations; i++)
34+
{
35+
string passPhrase = GeneratePassPhrase();
36+
byte[] beforeBytes = GeneratePayload();
37+
38+
byte[] encryptedBytes = Crypto.Encrypt(beforeBytes, passPhrase);
39+
40+
byte[] afterBytes = Crypto.Decrypt(encryptedBytes, passPhrase);
41+
42+
CollectionAssert.AreEqual(beforeBytes, afterBytes);
43+
}
44+
}
45+
46+
[TestMethod]
47+
public void StreamEncryptDecrypt()
48+
{
49+
for (int i = 0; i < Iterations; i++)
50+
{
51+
string passPhrase = GeneratePassPhrase();
52+
byte[] beforeBytes = GeneratePayload();
53+
byte[] afterBytes;
54+
55+
using (MemoryStream plainStream = new MemoryStream(beforeBytes))
56+
{
57+
using (Stream encryptedSteam = Crypto.Encrypt(plainStream, passPhrase))
58+
{
59+
using (MemoryStream decryptedStream = (MemoryStream)Crypto.Decrypt(encryptedSteam, passPhrase))
60+
{
61+
afterBytes = decryptedStream.ToArray();
62+
}
63+
}
64+
}
65+
66+
CollectionAssert.AreEqual(beforeBytes, afterBytes);
67+
}
68+
}
69+
70+
private string GeneratePassPhrase()
71+
{
72+
Random rand = new Random();
73+
74+
return GenerateRandomString(rand.Next(8, (256 + 1)));
75+
}
76+
77+
private byte[] GeneratePayload()
78+
{
79+
Random rand = new Random();
80+
81+
return GenerateRandomBytes(rand.Next(16, (512 + 1)));
82+
}
83+
84+
private string GenerateRandomString(int length)
85+
{
86+
byte[] bytes = new byte[length];
87+
88+
Random rand = new Random();
89+
90+
for (int i = 0; i < bytes.Length; i++)
91+
{
92+
int min;
93+
int max;
94+
95+
if (rand.Next(0, 2) > 0)
96+
{
97+
min = 65;
98+
max = 90;
99+
}
100+
else
101+
{
102+
min = 97;
103+
max = 122;
104+
}
105+
106+
int num = rand.Next(min, max);
107+
108+
bytes[i] = (byte)num;
109+
}
110+
111+
return Encoding.UTF8.GetString(bytes);
112+
}
113+
114+
private byte[] GenerateRandomBytes(int length)
115+
{
116+
Random rand = new Random();
117+
118+
byte[] bytes = new byte[length];
119+
120+
rand.NextBytes(bytes);
121+
122+
return bytes;
123+
}
124+
}
125+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{566818D4-11AE-4C96-821E-91C1C616D19B}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>EncryptionTests</RootNamespace>
11+
<AssemblyName>EncryptionTests</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
15+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
16+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
17+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
18+
<IsCodedUITest>False</IsCodedUITest>
19+
<TestProjectType>UnitTest</TestProjectType>
20+
<NuGetPackageImportStamp>
21+
</NuGetPackageImportStamp>
22+
</PropertyGroup>
23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
24+
<DebugSymbols>true</DebugSymbols>
25+
<DebugType>full</DebugType>
26+
<Optimize>false</Optimize>
27+
<OutputPath>bin\Debug\</OutputPath>
28+
<DefineConstants>DEBUG;TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
33+
<DebugType>pdbonly</DebugType>
34+
<Optimize>true</Optimize>
35+
<OutputPath>bin\Release\</OutputPath>
36+
<DefineConstants>TRACE</DefineConstants>
37+
<ErrorReport>prompt</ErrorReport>
38+
<WarningLevel>4</WarningLevel>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
42+
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
45+
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
46+
</Reference>
47+
<Reference Include="System" />
48+
<Reference Include="System.Core" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Compile Include="EncryptDecryptTests.cs" />
52+
<Compile Include="Properties\AssemblyInfo.cs" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<None Include="packages.config" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<ProjectReference Include="..\Encryption\Encryption.csproj">
59+
<Project>{ab5a3320-f260-42ee-8f19-ccf7546ca511}</Project>
60+
<Name>Encryption</Name>
61+
</ProjectReference>
62+
</ItemGroup>
63+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
64+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
65+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
66+
<PropertyGroup>
67+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
68+
</PropertyGroup>
69+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props'))" />
70+
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets'))" />
71+
</Target>
72+
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" />
73+
</Project>
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
[assembly: AssemblyTitle("EncryptionTests")]
6+
[assembly: AssemblyDescription("")]
7+
[assembly: AssemblyConfiguration("")]
8+
[assembly: AssemblyCompany("")]
9+
[assembly: AssemblyProduct("EncryptionTests")]
10+
[assembly: AssemblyCopyright("Copyright © 2019")]
11+
[assembly: AssemblyTrademark("")]
12+
[assembly: AssemblyCulture("")]
13+
14+
[assembly: ComVisible(false)]
15+
16+
[assembly: Guid("566818d4-11ae-4c96-821e-91c1c616d19b")]
17+
18+
// [assembly: AssemblyVersion("1.0.*")]
19+
[assembly: AssemblyVersion("1.0.0.0")]
20+
[assembly: AssemblyFileVersion("1.0.0.0")]

EncryptionTests/packages.config

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="MSTest.TestAdapter" version="1.2.1" targetFramework="net452" />
4+
<package id="MSTest.TestFramework" version="1.2.1" targetFramework="net452" />
5+
</packages>

PolyDeploy.sln

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Encryption", "Encryption\En
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeployClient", "DeployClient\DeployClient.csproj", "{B6122CCD-75F9-4DEF-8DAA-E11789D6D6D8}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EncryptionTests", "EncryptionTests\EncryptionTests.csproj", "{566818D4-11AE-4C96-821E-91C1C616D19B}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Clients|Any CPU = Clients|Any CPU
@@ -34,8 +36,17 @@ Global
3436
{B6122CCD-75F9-4DEF-8DAA-E11789D6D6D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
3537
{B6122CCD-75F9-4DEF-8DAA-E11789D6D6D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
3638
{B6122CCD-75F9-4DEF-8DAA-E11789D6D6D8}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{566818D4-11AE-4C96-821E-91C1C616D19B}.Clients|Any CPU.ActiveCfg = Release|Any CPU
40+
{566818D4-11AE-4C96-821E-91C1C616D19B}.Clients|Any CPU.Build.0 = Release|Any CPU
41+
{566818D4-11AE-4C96-821E-91C1C616D19B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{566818D4-11AE-4C96-821E-91C1C616D19B}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{566818D4-11AE-4C96-821E-91C1C616D19B}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{566818D4-11AE-4C96-821E-91C1C616D19B}.Release|Any CPU.Build.0 = Release|Any CPU
3745
EndGlobalSection
3846
GlobalSection(SolutionProperties) = preSolution
3947
HideSolutionNode = FALSE
4048
EndGlobalSection
49+
GlobalSection(ExtensibilityGlobals) = postSolution
50+
SolutionGuid = {95FD00DA-4F77-44B2-AE67-6929DB66F6FB}
51+
EndGlobalSection
4152
EndGlobal

0 commit comments

Comments
 (0)