public void TestImportExportPkcs12()
        {
            var store = new X509CertificateStore();

            store.Import(GetTestDataPath("smime.p12"), "no.secret");
            var certificate = store.Certificates.FirstOrDefault();
            var count       = store.Certificates.Count();

            Assert.AreEqual(1, count, "Unexpected number of certificates imported.");
            Assert.IsNotNull(store.GetPrivateKey(certificate), "Failed to get private key.");

            foreach (var authority in CertificateAuthorities)
            {
                store.Import(GetTestDataPath(authority));
            }

            store.Export("exported.p12", "no.secret");

            var imported = new X509CertificateStore();

            imported.Import("exported.p12", "no.secret");

            count = imported.Certificates.Count();

            Assert.AreEqual(store.Certificates.Count(), count, "Unexpected number of certificates re-imported.");
            Assert.IsNotNull(imported.GetPrivateKey(certificate), "Failed to get private key after re-importing.");
        }
        public void TestArgumentExceptions()
        {
            var store = new X509CertificateStore();

            Assert.Throws <ArgumentNullException> (() => store.Add(null));
            Assert.Throws <ArgumentNullException> (() => store.AddRange(null));
            Assert.Throws <ArgumentNullException> (() => store.Export((Stream)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Export((string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Export(Stream.Null, null));
            Assert.Throws <ArgumentNullException> (() => store.Export("fileName", null));
            Assert.Throws <ArgumentNullException> (() => store.Export((Stream)null));
            Assert.Throws <ArgumentNullException> (() => store.Export((string)null));
            Assert.Throws <ArgumentNullException> (() => store.GetPrivateKey(null));
            Assert.Throws <ArgumentNullException> (() => store.Import((Stream)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Import((string)null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Import((byte[])null, "password"));
            Assert.Throws <ArgumentNullException> (() => store.Import(Stream.Null, null));
            Assert.Throws <ArgumentNullException> (() => store.Import(GetTestDataPath("smime.p12"), null));
            Assert.Throws <ArgumentNullException> (() => store.Import(new byte[0], null));
            Assert.Throws <ArgumentNullException> (() => store.Import((Stream)null));
            Assert.Throws <ArgumentNullException> (() => store.Import((string)null));
            Assert.Throws <ArgumentNullException> (() => store.Import((byte[])null));
            Assert.Throws <ArgumentNullException> (() => store.Remove(null));
            Assert.Throws <ArgumentNullException> (() => store.RemoveRange(null));
        }
        public void TestAddRemove()
        {
            var certificates = new List <X509Certificate> ();
            var parser       = new X509CertificateParser();
            var store        = new X509CertificateStore();

            foreach (var authority in CertificateAuthorities)
            {
                var path = GetTestDataPath(authority);

                using (var stream = File.OpenRead(path)) {
                    foreach (X509Certificate certificate in parser.ReadCertificates(stream))
                    {
                        certificates.Add(certificate);
                    }
                }
            }

            foreach (var certificate in certificates)
            {
                store.Add(certificate);
            }

            var count = store.Certificates.Count();

            Assert.AreEqual(CertificateAuthorities.Length, count, "Unexpected number of certificates after Add.");

            foreach (var certificate in certificates)
            {
                var key = store.GetPrivateKey(certificate);
                Assert.IsNull(key, "GetPrivateKey");
                store.Remove(certificate);
            }

            count = store.Certificates.Count();

            Assert.AreEqual(0, count, "Unexpected number of certificates after Remove.");
        }