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>();
Comment on lines +37 to +39

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you change all of the var lines to explicit type declarations? That is not a code cleanup, but a change in the overall standard of how the code is written within this project.


// 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();
Comment on lines +49 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C#, the ? after a type denotes a nullable value type. This means that the variable can hold either a value of that type or null. This is particularly useful for value types (like int, double, bool, etc.) which cannot naturally be null.
Here are some reasons why you might want to use a nullable type:

  1. Representing Absence of Value: It allows you to represent the absence of a value in a way that's consistent with how reference types work (where null can be used). For example, int? can represent an integer that might not have a value.
  2. Default Values: For value types, the default value is generally 0, false, or another default. A nullable type allows you to explicitly indicate that the value is not set or unknown by setting it to null.
  3. Database Interactions: When working with databases, many fields can have null values. Using nullable types ensures your C# code can accurately represent and handle these scenarios.
  4. Conditional Logic: You can easily check if a nullable type has a value using .HasValue and retrieve the value using .Value.
  5. Enhanced Readability and Maintenance: By explicitly declaring a type as nullable, it makes the code more readable and maintainable, clearly indicating that the variable is allowed to have no value.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ammarheidari yes, this is the syntax for a nullable type, but the along with the change to coding style here, you changed the nullability of the values. For instance GenerateKeyPair() does not return a nullable value, so along with teh style change, this is a behavioral change.


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();
Comment on lines +45 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again

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);
Comment on lines +61 to +62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again


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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

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 = "max@mustermann.tld";
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 = "max@mustermann.tld";
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()
Comment on lines +86 to +97

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, along with the additional white space changes

.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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

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 = "max@mustermann.tld";
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 = "max@mustermann.tld";
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)
Comment on lines +131 to +144

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same and white space changes

.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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

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());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same


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