junian / Standard.Licensing

Easy-to-use licensing library for .NET Framework, Mono, .NET Core, and MAUI / Xamarin products
https://junian.dev/Standard.Licensing/
MIT License
535 stars 120 forks source link

AssertThat throws error if Attribute or Feature deos not exist #30

Open MikeWilliams-UK opened 1 year ago

MikeWilliams-UK commented 1 year ago

In a .Net Framework (4.6.2) application.

If you have a licence which has been generated with an attribue of "Random1" and when you validate it you try to get an attribute of "Random2" you get a System.NullReferenceException when using .AssertThat(lic => lic.AdditionalAttributes.Get("Random2")

Same happens if you specify a feature which does not exist in the licence.

I would expect both of these to gracefully fail validation with a message similar to "Attribute 'Random2' not present in the licence."

SamKr commented 1 day ago

If anyone comes across this, the correct way to implement this can be found in @junian's tests projects for Standard.Licensing.

Example:

var validationFailures = license.Validate()
    .ExpirationDate(systemDateTime: DateTime.Now)
    .When(lic => lic.Type == LicenseType.Trial)
    .And()
    .Signature(EncryptedPublicKey)
    .And()
    .AssertThat(lic => lic.ProductFeatures.Contains("Sales Module"), new GeneralValidationFailure { Message = "Sales Module not licensed!" })
    .AssertValidLicense();

You can change Sales Module into anything you want, and it'll trigger the corresponding failure if it's not present in the license.