Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/Standard.Licensing.Tests/KeyGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System.Collections.Generic;
using NUnit.Framework;
using Standard.Licensing.Security.Cryptography;

Expand All @@ -35,9 +34,9 @@ public class KeyGeneratorTests
[Test] // See Bug #135
public void Ensure_To_Not_Generate_Identical_Keys()
{
var passPhrase = "test";
var privKeySet = new HashSet<string>();
var pubKeySet = new HashSet<string>();
string passPhrase = "test";
HashSet<string> privKeySet = new HashSet<string>();
HashSet<string> pubKeySet = new HashSet<string>();

// add well known key
privKeySet.Add(
Expand All @@ -47,10 +46,10 @@ public void Ensure_To_Not_Generate_Identical_Keys()

for (int i = 0; i < 100; i++)
{
var keyGenerator = new KeyGenerator(256); //default key size
var pair = keyGenerator.GenerateKeyPair();
var privateKey = pair.ToEncryptedPrivateKeyString(passPhrase);
var publicKey = pair.ToPublicKeyString();
KeyGenerator keyGenerator = new KeyGenerator(256); //default key size
KeyPair? pair = keyGenerator.GenerateKeyPair();
string? privateKey = pair.ToEncryptedPrivateKeyString(passPhrase);
string? publicKey = pair.ToPublicKeyString();

Assert.That(privKeySet.Add(privateKey), Is.True);
Assert.That(pubKeySet.Add(publicKey), Is.True);
Expand Down
96 changes: 48 additions & 48 deletions src/Standard.Licensing.Tests/LicenseSignatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
using NUnit.Framework;

using Standard.Licensing.Security.Cryptography;

namespace Standard.Licensing.Tests
{
[TestFixture]
Expand All @@ -42,8 +42,8 @@ public class LicenseSignatureTests
public void Init()
{
passPhrase = Guid.NewGuid().ToString();
var keyGenerator = Security.Cryptography.KeyGenerator.Create();
var keyPair = keyGenerator.GenerateKeyPair();
KeyGenerator? keyGenerator = KeyGenerator.Create();
KeyPair? keyPair = keyGenerator.GenerateKeyPair();
privateKey = keyPair.ToEncryptedPrivateKeyString(passPhrase);
publicKey = keyPair.ToPublicKeyString();
}
Expand All @@ -58,14 +58,14 @@ private static DateTime ConvertToRfc1123(DateTime dateTime)
[Test]
public void Can_Generate_And_Validate_Signature_With_Empty_License()
{
var license = License.New()
.CreateAndSignWithPrivateKey(privateKey, passPhrase);
License? license = License.New()
.CreateAndSignWithPrivateKey(privateKey, passPhrase);

Assert.That(license, Is.Not.Null);
Assert.That(license.Signature, Is.Not.Null);

// validate xml
var xmlElement = XElement.Parse(license.ToString(), LoadOptions.None);
XElement xmlElement = XElement.Parse(license.ToString(), LoadOptions.None);
Assert.That(xmlElement.HasElements, Is.True);

// validate default values when not set
Expand All @@ -83,31 +83,31 @@ public void Can_Generate_And_Validate_Signature_With_Empty_License()
[Test]
public void Can_Generate_And_Validate_Signature_With_Standard_License()
{
var licenseId = Guid.NewGuid();
var customerName = "Max Mustermann";
var customerEmail = "[email protected]";
var expirationDate = DateTime.Now.AddYears(1);
var productFeatures = new Dictionary<string, string>
{
{"Sales Module", "yes"},
{"Purchase Module", "yes"},
{"Maximum Transactions", "10000"}
};

var license = License.New()
.WithUniqueIdentifier(licenseId)
.As(LicenseType.Standard)
.WithMaximumUtilization(10)
.WithProductFeatures(productFeatures)
.LicensedTo(customerName, customerEmail)
.ExpiresAt(expirationDate)
.CreateAndSignWithPrivateKey(privateKey, passPhrase);
Guid licenseId = Guid.NewGuid();
string customerName = "Max Mustermann";
string customerEmail = "[email protected]";
DateTime expirationDate = DateTime.Now.AddYears(1);
Dictionary<string, string> productFeatures = new Dictionary<string, string>
{
{"Sales Module", "yes"},
{"Purchase Module", "yes"},
{"Maximum Transactions", "10000"}
};

License? license = License.New()
.WithUniqueIdentifier(licenseId)
.As(LicenseType.Standard)
.WithMaximumUtilization(10)
.WithProductFeatures(productFeatures)
.LicensedTo(customerName, customerEmail)
.ExpiresAt(expirationDate)
.CreateAndSignWithPrivateKey(privateKey, passPhrase);

Assert.That(license, Is.Not.Null);
Assert.That(license.Signature, Is.Not.Null);

// validate xml
var xmlElement = XElement.Parse(license.ToString(), LoadOptions.None);
XElement xmlElement = XElement.Parse(license.ToString(), LoadOptions.None);
Assert.That(xmlElement.HasElements, Is.True);

// validate default values when not set
Expand All @@ -128,25 +128,25 @@ public void Can_Generate_And_Validate_Signature_With_Standard_License()
[Test]
public void Can_Detect_Hacked_License()
{
var licenseId = Guid.NewGuid();
var customerName = "Max Mustermann";
var customerEmail = "[email protected]";
var expirationDate = DateTime.Now.AddYears(1);
var productFeatures = new Dictionary<string, string>
{
{"Sales Module", "yes"},
{"Purchase Module", "yes"},
{"Maximum Transactions", "10000"}
};

var license = License.New()
.WithUniqueIdentifier(licenseId)
.As(LicenseType.Standard)
.WithMaximumUtilization(10)
.WithProductFeatures(productFeatures)
.LicensedTo(customerName, customerEmail)
.ExpiresAt(expirationDate)
.CreateAndSignWithPrivateKey(privateKey, passPhrase);
Guid licenseId = Guid.NewGuid();
string customerName = "Max Mustermann";
string customerEmail = "[email protected]";
DateTime expirationDate = DateTime.Now.AddYears(1);
Dictionary<string, string> productFeatures = new Dictionary<string, string>
{
{"Sales Module", "yes"},
{"Purchase Module", "yes"},
{"Maximum Transactions", "10000"}
};

License? license = License.New()
.WithUniqueIdentifier(licenseId)
.As(LicenseType.Standard)
.WithMaximumUtilization(10)
.WithProductFeatures(productFeatures)
.LicensedTo(customerName, customerEmail)
.ExpiresAt(expirationDate)
.CreateAndSignWithPrivateKey(privateKey, passPhrase);

Assert.That(license, Is.Not.Null);
Assert.That(license.Signature, Is.Not.Null);
Expand All @@ -155,15 +155,15 @@ public void Can_Detect_Hacked_License()
Assert.That(license.VerifySignature(publicKey), Is.True);

// validate xml
var xmlElement = XElement.Parse(license.ToString(), LoadOptions.None);
XElement xmlElement = XElement.Parse(license.ToString(), LoadOptions.None);
Assert.That(xmlElement.HasElements, Is.True);

// manipulate xml
Assert.That(xmlElement.Element("Quantity"), Is.Not.Null);
xmlElement.Element("Quantity").Value = "11"; // now we want to have 11 licenses

// load license from manipulated xml
var hackedLicense = License.Load(xmlElement.ToString());
License? hackedLicense = License.Load(xmlElement.ToString());

// validate default values when not set
Assert.That(hackedLicense.Id, Is.EqualTo(licenseId));
Expand Down
Loading