added simplepack implementation and tests

This commit is contained in:
Kevin Froman 2020-05-27 01:40:34 -05:00
parent 3085a8e438
commit 70957e5ab2
7 changed files with 196 additions and 0 deletions

35
SimplePack/SimplePack.cs Normal file
View File

@ -0,0 +1,35 @@
using System.Collections;
using Base58Check;
namespace simplepack
{
public class SimplePack
{
private string head;
private string foot;
public SimplePack(string header, string footer){
head = header;
foot = footer;
if (header.Length == 0 | footer.Length == 0){
throw new InvalidSimplePackHeader();
}
}
public byte[] decode(string data){
if (! data.Contains(head) | ! data.Contains(foot)){ throw new InvalidSimplePackHeader();}
string base58Data = "";
for (int i = head.Length; i < data.Length - foot.Length; i++){
base58Data += data[i];
}
return Base58CheckEncoding.Decode(base58Data);
}
public string encode(byte[] data){
// OutOfMemoryException may be thrown for large payloads
return head + Base58CheckEncoding.Encode(data) + foot;
}
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Base58Check" Version="0.2.0" />
</ItemGroup>
</Project>

22
SimplePack/exception.cs Normal file
View File

@ -0,0 +1,22 @@
using System;
namespace simplepack{
public class InvalidSimplePackHeader : Exception
{
public InvalidSimplePackHeader()
{
}
public InvalidSimplePackHeader(string message)
: base(message)
{
}
public InvalidSimplePackHeader(string message, Exception inner)
: base(message, inner)
{
}
}
}

12
cli/SimplePackCLI.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
namespace cli
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

12
cli/cli.csproj Normal file
View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\SimplePack\SimplePack.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>

85
tests/testSimplePack.cs Normal file
View File

@ -0,0 +1,85 @@
using NUnit.Framework;
using System.Text;
using Base58Check;
using simplepack;
namespace tests
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void TestRequireHeader(){
string header = "";
string footer = " test-footer";
byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world");
string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer;
try{
SimplePack packer = new SimplePack(header, footer);
}
catch(InvalidSimplePackHeader){
return;
}
Assert.Fail();
}
[Test]
public void TestRequireFooter(){
string header = "test-header";
string footer = "";
byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world");
string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer;
try{
SimplePack packer = new SimplePack(header, footer);
}
catch(InvalidSimplePackHeader){
return;
}
Assert.Fail();
}
[Test]
public void TestInvalidChecksum(){
byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world");
string header = "header-part ";
string footer = " footer part.";
string encoded = header + Base58CheckEncoding.EncodePlain(expected_result) + footer;
SimplePack packer = new SimplePack(header, footer);
try{
packer.decode(encoded);
}
catch(System.FormatException){return;}
Assert.Fail(); // Did not catch no checksum, fail
}
[Test]
public void TestDecode()
{
byte[] expected_result = UTF8Encoding.UTF8.GetBytes("hello world");
string header = "header-part ";
string footer = " footer part.";
string encoded = header + Base58CheckEncoding.Encode(expected_result) + footer;
SimplePack packer = new SimplePack(header, footer);
Assert.AreEqual(packer.decode(encoded),
expected_result);
}
[Test]
public void TestEncode()
{
byte[] message = UTF8Encoding.UTF8.GetBytes("hello world");
string expected_header = "header-part ";
string expected_footer = " footer part.";
string expected_result = expected_header + Base58CheckEncoding.Encode(message) + expected_footer;
SimplePack packer = new SimplePack(expected_header, expected_footer);
string actual_result = packer.encode(message);
Assert.AreEqual(actual_result, expected_result);
}
}
}

19
tests/tests.csproj Normal file
View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SimplePack\SimplePack.csproj" />
</ItemGroup>
</Project>