Skip to content

Commit

Permalink
(chocolatey#18) Revamp tests
Browse files Browse the repository at this point in the history
Update tests to split out generation and validation.
Created xml files to verify aspects of generation and validation.
Updated readme with instructions to generate test license files.
  • Loading branch information
corbob authored and gep13 committed Sep 15, 2022
1 parent cee11a5 commit 923e5d5
Show file tree
Hide file tree
Showing 93 changed files with 1,670 additions and 162 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ A full build, test, and pack run can be started using the `build.ps1` PowerShell

Running `build.ps1 -Target Test` will run unit tests for the project.

### Test License files

The test license files are stored in the `test/Rhino.Licensing.Tests/LicenseFiles` directory, and automatically embedded in the Tests project.
This allows us to test the generator and validator separately.
If new license types or signing algorithms are added, new license files will need to be made.

To generate new license files, run the following PowerShell: `$env:GENERATE_TEST_LICENSES = Resolve-Path .\test\Rhino.Licensing.Tests\LicenseFiles\ ; ./build.ps1 -Target Test`.
You will then need to commit the changed files to the repository.


## Acknowledgements

Rhino Licensing is making use of the excellent log4net logging framework.
65 changes: 54 additions & 11 deletions test/Rhino.Licensing.Tests/BaseLicenseTest.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,70 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Rhino.Licensing.Tests
{
public class BaseLicenseTest
{
public readonly static string public_and_private;
public readonly static string public_only;
protected static readonly Dictionary<string, string> Resources = new Dictionary<string, string>();
protected static readonly string PublicAndPrivateKey;
protected static readonly string PublicOnlyKey;
protected static readonly Guid TestGuid = new Guid("0c6b2abc-5ba1-4f1c-af11-8c3a01147124");
protected static readonly DateTime Expiration = DateTime.MaxValue;
protected static readonly string CustomerName = "Oren Eini";

static BaseLicenseTest()
{
public_and_private = new StreamReader(typeof (Can_generate_and_validate_key)
.Assembly
.GetManifestResourceStream(
"Rhino.Licensing.Tests.public_and_private.xml"))
.ReadToEnd();
foreach (var resourceName in typeof(BaseLicenseTest).Assembly.GetManifestResourceNames().Where(name => name.EndsWith(".xml")))
{
var resourceNameSplit = resourceName.Split('.');
var resourceNameLength = resourceNameSplit.Length;
Resources.Add($"{resourceNameSplit[resourceNameLength - 3]}.{resourceNameSplit[resourceNameLength - 2]}", ReadResourceFile(resourceName));
}

public_only = new StreamReader(typeof (Can_generate_and_validate_key)
.Assembly
.GetManifestResourceStream("Rhino.Licensing.Tests.public_only.xml"))
.ReadToEnd();
PublicAndPrivateKey = Resources["SigningKeys.PublicAndPrivate"];
PublicOnlyKey = Resources["SigningKeys.PublicOnly"];
}

private static string ReadResourceFile (string resourceName)
{
var stream = typeof(LicenseGeneratorTests)
.Assembly
.GetManifestResourceStream(resourceName);

if (stream == null)
{
throw new Exception($"Something really bad happened. We couldn't load {resourceName}");
}

return new StreamReader(stream).ReadToEnd();
}

protected static string WriteLicenseFile(string resourceName)
{
var path = $"{Path.GetTempPath()}\\{resourceName}.xml";
File.WriteAllText(path, Resources[resourceName]);
return path;
}

public static IEnumerable<object[]> GetAllLicenseTypes()
{
foreach (var licenseType in Enum.GetValues(typeof(LicenseType)))
{
yield return new object[] { licenseType };
}
}

public static IEnumerable<object[]> GetAllLicenseTypesWithSigningAlgorithms()
{
foreach (var licenseType in Enum.GetValues(typeof(LicenseType)))
{
foreach (var signingAlgorithm in Enum.GetValues(typeof(SigningAlgorithm)))
{
yield return new object[] { licenseType, signingAlgorithm };
}
}
}
}
}
23 changes: 0 additions & 23 deletions test/Rhino.Licensing.Tests/Can_check_trial_date.cs

This file was deleted.

107 changes: 0 additions & 107 deletions test/Rhino.Licensing.Tests/Can_generate_and_validate_key.cs

This file was deleted.

31 changes: 31 additions & 0 deletions test/Rhino.Licensing.Tests/GenerateTestLicenses.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.IO;
using Xunit;

namespace Rhino.Licensing.Tests
{
public class GenerateTestLicenses : BaseLicenseTest
{
private static readonly LicenseGenerator Generator = new LicenseGenerator(PublicAndPrivateKey);

[GenerateTestLicensesTheory]
[MemberData(nameof(GetAllLicenseTypesWithSigningAlgorithms))]
public void GeneratesTestLicenses(LicenseType licenseType, SigningAlgorithm signingAlgorithm)
{
var path = Environment.GetEnvironmentVariable("GENERATE_TEST_LICENSES");

// Never expiring, specifically signed licenses
var key = Generator.Generate(CustomerName, TestGuid, Expiration, licenseType, signingAlgorithm);
File.WriteAllText($"{path}\\{licenseType}-{signingAlgorithm}.xml", key);

// Never expiring, default (SHA1) signed licenses
key = Generator.Generate(CustomerName, TestGuid, Expiration, licenseType);
File.WriteAllText($"{path}\\{licenseType}.xml", key);

// Expired, specifically signed licenses
key = Generator.Generate(CustomerName, TestGuid, DateTime.Parse("12-12-2013 16:47:34 -0600"), licenseType, signingAlgorithm);
File.WriteAllText($"{path}\\{licenseType}-{signingAlgorithm}-Expired.xml", key);

}
}
}
17 changes: 17 additions & 0 deletions test/Rhino.Licensing.Tests/GenerateTestLicensesFact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Xunit;

namespace Rhino.Licensing.Tests
{
public sealed class GenerateTestLicensesTheory : TheoryAttribute
{
public GenerateTestLicensesTheory()
{
if (!ShouldGenerateTestLicenses) {
Skip = "Ignore generating test licenses when we don't want them";
}
}

private static bool ShouldGenerateTestLicenses { get => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GENERATE_TEST_LICENSES")); }
}
}
18 changes: 18 additions & 0 deletions test/Rhino.Licensing.Tests/LicenseFiles/Architect-SHA1-Expired.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<license id="0c6b2abc-5ba1-4f1c-af11-8c3a01147124" expiration="2013-12-12T14:47:34.0000000" type="Architect">
<name>Oren Eini</name>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>16rcAhiW0SdSMiVLgNWYHYxpV0o=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>ZCljzRSiQ6KGKluFTVs9LLcXRS+mvQvGZ/hbR+jti6drdt1SVjns573gZux6qTj9shnJV24t/Ige+olM1cBhZiSOLncCnfglArxDyHNezZZOLeIgivoYeJWovWzh32HhdKPlDqP7m7jA16+8SOnxpoJKqcLciQ2MKIiox383RitN5fvxjFCFeQruI0RdmP38qqT20ICIuP++VRZ9oUm8uKoVAHLeKGUwTI8Ppmk+N9oVvmsx96u27G2ezYph+ApVv6iY8fC+QL+fxdB0MjMg09So6EncuJRr6mYCZWnQ/X6p+l/ZwOILTtYbm4q7EDkGDeSd+J/EILnVCK5s7/U4wQ==</SignatureValue>
</Signature>
</license>
18 changes: 18 additions & 0 deletions test/Rhino.Licensing.Tests/LicenseFiles/Architect-SHA1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<license id="0c6b2abc-5ba1-4f1c-af11-8c3a01147124" expiration="9999-12-31T23:59:59.9999999" type="Architect">
<name>Oren Eini</name>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>ouD910sKwSiCqigj5PzzCP6TbHk=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>Cty9SJRyPjYB5ttwJ5m7akpuB7cnZQ/iM2JUF20H6U+eK7DEWq/1bpTKiaJxCb6G6jHjOX0zl3QGIe3aWli50x8kpIpeoYgqYNAyap3PYRLhyBJggwpy/mmC9g36SsdL/CVclXeOEeWYbwpigi1lOpfAXu02X0551C0szprJYejDZf1MM9IzKF9xh2wEndQKA9yw7A0jW3np9+MWXif4Vgi5HHU4JuT3C3a+K7LZn0HG2um0TjDDzshtdNnkfjC2Y6xbCnh8nD0Am9E/Bm8PW5wl95NRWYLLQ2ZorhGg9owwsoFhtlZE87IX2pedzmJ0Met7QyyLwUMibVRgK4lJKg==</SignatureValue>
</Signature>
</license>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<license id="0c6b2abc-5ba1-4f1c-af11-8c3a01147124" expiration="2013-12-12T14:47:34.0000000" type="Architect">
<name>Oren Eini</name>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
<DigestValue>v08BgE8eWNm+/IIytJ80FE12pn/cBwOUqb1xLAglaK0=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>UuOOt0Yt5iOttbeGv4s2/EnCqrmKVxko6zy6HHbrcbZxBF5CzBX8C8pk6Www+NJ1uJfrj87JnMNM3cRHbN7EeejBSVecX6UTxcFlE/EMq2gHd31BASuYR3vzx8oywvyAnF/EHV8o3NiRTMYI/p33rcMZQtUkr0B3jTjgugSQqIe/MrBbZrDK9Y+1Af/z+KSurx1RuDHCzRd7SQzlliCsciDOLEMaR1esgMIMAxO0KsGeCZov/MmYcqjzVi4k0fVe6nHUhbq5HAdhy3OPIOvyXfzaS8Zxq5xglZxaH52jqyM0UUHay/qHBtlzbfX9BFV8JY2LFQatt1hlLaSdtkgjlw==</SignatureValue>
</Signature>
</license>
18 changes: 18 additions & 0 deletions test/Rhino.Licensing.Tests/LicenseFiles/Architect-SHA256.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<license id="0c6b2abc-5ba1-4f1c-af11-8c3a01147124" expiration="9999-12-31T23:59:59.9999999" type="Architect">
<name>Oren Eini</name>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
<DigestValue>Fi/Q0+qXe06ES4uyP4sJtPdM7ZguahkwDggjdZ9tncc=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>AEQOx5glJZiElwlF2K15AYyfGvg/Q3orN3Lo1HWWUky/v2oD0jmC4yUbvVKT4RI5f+QHXH8qFPbrkRfqYaG8/VHsZl3Pmvi+PeFEOaNUmWOa/YGRNWdYrdWO4KqRxxiIdnVlwPJ3nt0YXnYQFSFkuVnXuKmly4flkNlZNwQIIUpFWa1gbNAAjvMFC1n0j8BwtHrI88TfS/6HcBvLY628hygH94ldrX34FVJ7cT/NmwdY7rgkG25iE1rIWz+M9d9RxJ/LO6YS0+vmRL1UKu2SrJj/TpQ+Vc0GWLG7XGlsUPgjtZnCb00V8S7G1Wca8KbFMrCDvZb1rvQfy25AdCSLiw==</SignatureValue>
</Signature>
</license>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<license id="0c6b2abc-5ba1-4f1c-af11-8c3a01147124" expiration="2013-12-12T14:47:34.0000000" type="Architect">
<name>Oren Eini</name>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha384" />
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#sha384" />
<DigestValue>3hUeesbbvMJdV5U5ikMFinSFnY7S54OeC+w+qeo4xHc/WtjDKWj+0Uifo50ALT5Q</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>noClQysdW3qOWkCrwlysUI2iyGM0m2vN8BvVzEAmdCwB2zaZvwYWjfwsMR0tTdP3b/VnzHg74gQDXCa1VhrMA23qC8eoH8ZMcRj6+aJUMvUjR5eu662CC7fnFX8LHzyCCr1vjgn1zTa/c2pc3bd4w34H+YYqcZTZXT8/ccHUrrKqHIdPS5LeqYqBmbZKxmm+zxLdpcGqnIEXFBrKPQbDSkhfdeFJ2yLDL/qXsZM/PMMVebTEMzUDnNxC4SJNJCxYNSXrdiL6hS9Rg+GgU0ixdcuSMlv4FrCf06GTn//F+hC4i+ewGHC17F5eMtCM9KFvQP/Zx28uxhJuStYVuWJvHw==</SignatureValue>
</Signature>
</license>
Loading

0 comments on commit 923e5d5

Please sign in to comment.