public void TestAutoUpgrade() { var path = Path.Combine(dataDir, "smimev0.db"); const string tmp = "smimev0-tmp.db"; if (File.Exists(tmp)) { File.Delete(tmp); } File.Copy(path, tmp); using (var dbase = new SqliteCertificateDatabase(tmp, "no.secret")) { var root = chain[chain.Length - 1]; // Verify that we can select the Root Certificate bool trustedAnchor = false; foreach (var record in dbase.Find(null, true, X509CertificateRecordFields.Certificate)) { if (record.Certificate.Equals(root)) { trustedAnchor = true; break; } } Assert.IsTrue(trustedAnchor, "Did not find the MimeKit UnitTests trusted anchor"); } }
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); } } }
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)); } }
void AssertFindBy(IX509Selector selector, X509Certificate expected) { using (var dbase = new SqliteCertificateDatabase("sqlite.db", "no.secret")) { // Verify that we can select the Root Certificate bool found = false; foreach (var record in dbase.Find(selector, false, X509CertificateRecordFields.Certificate)) { if (record.Certificate.Equals(expected)) { found = true; break; } } Assert.IsTrue(found, "Did not find the expected certificate"); } }