Example #1
0
        public void ItFindsCertByCommonName()
        {
            var commonName = "x509store.read.letsencrypt.test.natemcmaster.com";

            using var x509store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            x509store.Open(OpenFlags.ReadWrite);
            var testCert = CreateTestCert(commonName);

            x509store.Add(testCert);

            _output.WriteLine($"Adding cert {testCert.Thumbprint} to My/CurrentUser");

            try
            {
                using var certStore = new X509CertStore(NullLogger <X509CertStore> .Instance)
                      {
                          AllowInvalidCerts = true
                      };
                var foundCert = certStore.GetCertificate(commonName);
                Assert.NotNull(foundCert);
                Assert.Equal(testCert, foundCert);
            }
            finally
            {
                x509store.Remove(testCert);
            }
        }
Example #2
0
        public void ItFindsCertTheCertWithLongestLifespan()
        {
            var commonName = "x509store-ttl.letsencrypt.test.natemcmaster.com";

            using var x509store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            x509store.Open(OpenFlags.ReadWrite);
            var testCert0 = CreateTestCert(commonName, DateTimeOffset.Now.AddMinutes(2));
            var testCert1 = CreateTestCert(commonName, DateTimeOffset.Now.AddHours(1));
            var testCert2 = CreateTestCert(commonName, DateTimeOffset.Now.AddHours(2));

            x509store.Add(testCert2);
            x509store.Add(testCert1);
            x509store.Add(testCert0);
            try
            {
                using var certStore = new X509CertStore(Mock.Of <ILogger <X509CertStore> >())
                      {
                          AllowInvalidCerts = true
                      };
                var foundCert = certStore.GetCertificate(commonName);
                Assert.NotNull(foundCert);
                Assert.Equal(testCert2, foundCert);
            }
            finally
            {
                x509store.Remove(testCert0);
                x509store.Remove(testCert1);
                x509store.Remove(testCert2);
            }
        }
Example #3
0
        public void ItReturnsNullWhenCantFindCert()
        {
            var commonName = "notfound.letsencrypt.test.natemcmaster.com";

            using var certStore = new X509CertStore(Mock.Of <ILogger <X509CertStore> >())
                  {
                      AllowInvalidCerts = true
                  };
            var foundCert = certStore.GetCertificate(commonName);

            Assert.Null(foundCert);
        }