-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
base: master
Are you sure you want to change the base?
Improvement #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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( | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
Assert.That(privKeySet.Add(privateKey), Is.True); | ||
Assert.That(pubKeySet.Add(publicKey), Is.True); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again |
||
privateKey = keyPair.ToEncryptedPrivateKeyString(passPhrase); | ||
publicKey = keyPair.ToPublicKeyString(); | ||
} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
There was a problem hiding this comment.
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.