dnauck / Portable.Licensing

Portable.Licensing is a cross platform software licensing framework which allows you to implement licensing into your application or library. It provides you all tools to create and validate licenses for your software.
http://dev.nauck-it.de/projects/portable-licensing
MIT License
592 stars 173 forks source link

License.Expiration - Millisecond part is ignored #19

Closed makcakaya closed 9 years ago

makcakaya commented 9 years ago

Hi Daniel;

I have problem when testing expiration date with milliseconds. I know, no one will ever need to use milliseconds to set an expiration date but the issue makes writing tests hard.

Test code is below:

[Fact]
        public void Expiration()
        {
            var passPhrase = "TopSecretPhrase";
            var keyGen = KeyGenerator.Create();
            var keyPair = keyGen.GenerateKeyPair();
            var privateKey = keyPair.ToEncryptedPrivateKeyString(passPhrase);

            var dateNoMs = DateTime.Parse("01.01.2015");
            var licenseNoMs = License.New().ExpiresAt(dateNoMs).CreateAndSignWithPrivateKey(privateKey, passPhrase);
            Assert.Equal(dateNoMs, licenseNoMs.Expiration); // Cool, no problem.

            var dateWithMs = DateTime.Now; // Has milliseconds
            var licenseWithMs = License.New().ExpiresAt(dateWithMs).CreateAndSignWithPrivateKey(privateKey, passPhrase);
            Assert.Equal(dateWithMs, licenseWithMs.Expiration); // Fails
            // Expected: 2015-05-08T17:43:51.9035841+03:00
            // Actual:   2015-05-08T17:43:51.0000000+03:00
        }

Using DateTimeStyles.RoundtripKind enum seems to solve the issue but the MSDN says it cannot be used with DateTimeStyles.AssumeUniversal.

dnauck commented 9 years ago

If you just want to test you could also use DateTime.Now.Date that does not have time information.

Is there any use case other then using DateTime.Now during Testing?

What is your real use case? Maybe we can find a nice solution for it.

makcakaya commented 9 years ago

Yeah, using DateTime.Date is a good option.

I was just surprised when I did serialization-deserialization and the Expiration's did not match.

dnauck commented 9 years ago

Can we close this issue?

makcakaya commented 9 years ago

Yes.