Exemplo n.º 1
0
        public void TestArgumentExceptions()
        {
            Assert.Throws <ArgumentNullException> (() => new DefaultSecureMimeContext((string)null));
            Assert.Throws <ArgumentNullException> (() => new DefaultSecureMimeContext((IX509CertificateDatabase)null));

            Assert.Throws <ArgumentNullException> (() => new DefaultSecureMimeContext(null, "password"));
            Assert.Throws <ArgumentNullException> (() => new DefaultSecureMimeContext("fileName", null));

            Assert.Throws <ArgumentNullException> (() => new SqliteCertificateDatabase((DbConnection)null, "password"));
            Assert.Throws <ArgumentNullException> (() => new SqliteCertificateDatabase((string)null, "password"));
            Assert.Throws <ArgumentException> (() => new SqliteCertificateDatabase(string.Empty, "password"));
            Assert.Throws <ArgumentNullException> (() => new SqliteCertificateDatabase("smime.db", null));

            var database = new SqliteCertificateDatabase("smime.db", "no.secret");

            Assert.Throws <ArgumentNullException> (() => database.Add((X509CrlRecord)null));
            Assert.Throws <ArgumentNullException> (() => database.Remove((X509CrlRecord)null));
            Assert.Throws <ArgumentNullException> (() => database.Update((X509CrlRecord)null));
            Assert.Throws <ArgumentNullException> (() => database.Add((X509CertificateRecord)null));
            Assert.Throws <ArgumentNullException> (() => database.Remove((X509CertificateRecord)null));
            Assert.Throws <ArgumentNullException> (() => database.Update((X509CertificateRecord)null, X509CertificateRecordFields.Algorithms));
            Assert.Throws <ArgumentNullException> (() => database.Find((X509Crl)null, X509CrlRecordFields.IsDelta));
            Assert.Throws <ArgumentNullException> (() => database.Find((X509Name)null, X509CrlRecordFields.IsDelta).FirstOrDefault());
            Assert.Throws <ArgumentNullException> (() => database.Find((X509Certificate)null, X509CertificateRecordFields.Id));
            Assert.Throws <ArgumentNullException> (() => database.Find((MailboxAddress)null, DateTime.Now, true, X509CertificateRecordFields.PrivateKey).FirstOrDefault());

            using (var ctx = new DefaultSecureMimeContext(database)) {
                Assert.Throws <ArgumentNullException> (() => ctx.Import((Stream)null, true));
            }
        }
Exemplo n.º 2
0
        public void TestImportCertificates()
        {
            var database     = new SqliteCertificateDatabase("smime.db", "no.secret");
            var dataDir      = Path.Combine(TestHelper.ProjectDir, "TestData", "smime");
            var certificates = new List <X509Certificate> ();

            using (var ctx = new DefaultSecureMimeContext(database)) {
                foreach (var filename in CertificateAuthorities)
                {
                    var path = Path.Combine(dataDir, filename);

                    using (var stream = File.OpenRead(path)) {
                        var parser = new X509CertificateParser();

                        foreach (X509Certificate certificate in parser.ReadCertificates(stream))
                        {
                            certificates.Add(certificate);
                            ctx.Import(certificate);
                        }
                    }
                }

                // make sure each certificate is there and then delete them...
                foreach (var certificate in certificates)
                {
                    var record = database.Find(certificate, X509CertificateRecordFields.Id);

                    Assert.IsNotNull(record, "Find");

                    database.Remove(record);
                }
            }
        }