public void TestBasicFunctionality() { var dataDir = Path.Combine("..", "..", "TestData", "smime"); var chain = new X509CertificateChain(); X509Certificate cert1, cert2, cert3; cert1 = LoadCertificate(Path.Combine(dataDir, "StartComClass1PrimaryIntermediateClientCA.crt")); cert2 = LoadCertificate(Path.Combine(dataDir, "StartComCertificationAuthority.crt")); cert3 = LoadCertificate(Path.Combine(dataDir, "certificate-authority.crt")); Assert.IsFalse(chain.IsReadOnly); Assert.AreEqual(0, chain.Count, "Initial count"); chain.Add(cert3); Assert.AreEqual(1, chain.Count); Assert.AreEqual(cert3, chain[0]); chain.Insert(0, cert1); chain.Insert(1, cert2); Assert.AreEqual(3, chain.Count); Assert.AreEqual(cert1, chain[0]); Assert.AreEqual(cert2, chain[1]); Assert.AreEqual(cert3, chain[2]); Assert.IsTrue(chain.Contains(cert2), "Contains"); Assert.AreEqual(1, chain.IndexOf(cert2), "IndexOf"); var array = new X509Certificate[chain.Count]; chain.CopyTo(array, 0); chain.Clear(); Assert.AreEqual(0, chain.Count); foreach (var cert in array) { chain.Add(cert); } Assert.AreEqual(array.Length, chain.Count); Assert.IsTrue(chain.Remove(cert3)); Assert.AreEqual(2, chain.Count); Assert.AreEqual(cert1, chain[0]); Assert.AreEqual(cert2, chain[1]); chain.RemoveAt(0); Assert.AreEqual(1, chain.Count); Assert.AreEqual(cert2, chain[0]); chain[0] = cert3; Assert.AreEqual(1, chain.Count); Assert.AreEqual(cert3, chain[0]); }
public void TestBasicFunctionality() { var certs = SecureMimeTestsBase.LoadPkcs12CertificateChain(GetTestDataPath("smime.pfx"), "no.secret"); var chain = new X509CertificateChain(); Assert.IsFalse(chain.IsReadOnly); Assert.AreEqual(0, chain.Count, "Initial count"); chain.Add(certs[2]); Assert.AreEqual(1, chain.Count); Assert.AreEqual(certs[2], chain[0]); chain.Insert(0, certs[0]); chain.Insert(1, certs[1]); Assert.AreEqual(3, chain.Count); Assert.AreEqual(certs[0], chain[0]); Assert.AreEqual(certs[1], chain[1]); Assert.AreEqual(certs[2], chain[2]); Assert.IsTrue(chain.Contains(certs[1]), "Contains"); Assert.AreEqual(1, chain.IndexOf(certs[1]), "IndexOf"); var array = new X509Certificate[chain.Count]; chain.CopyTo(array, 0); chain.Clear(); Assert.AreEqual(0, chain.Count); foreach (var cert in array) { chain.Add(cert); } Assert.AreEqual(array.Length, chain.Count); Assert.IsTrue(chain.Remove(certs[2])); Assert.AreEqual(2, chain.Count); Assert.AreEqual(certs[0], chain[0]); Assert.AreEqual(certs[1], chain[1]); chain.RemoveAt(0); Assert.AreEqual(1, chain.Count); Assert.AreEqual(certs[1], chain[0]); chain[0] = certs[2]; Assert.AreEqual(1, chain.Count); Assert.AreEqual(certs[2], chain[0]); }
public void TestArgumentExceptions() { var path = Path.Combine("..", "..", "TestData", "smime", "smime.p12"); var chain = new X509CertificateChain(); CmsSigner signer; using (var stream = File.OpenRead(path)) signer = new CmsSigner(stream, "no.secret"); Assert.Throws <ArgumentNullException> (() => new X509CertificateChain(null)); Assert.Throws <ArgumentNullException> (() => chain.Add(null)); Assert.Throws <ArgumentNullException> (() => chain.AddRange(null)); Assert.Throws <ArgumentNullException> (() => chain.Contains(null)); Assert.Throws <ArgumentNullException> (() => chain.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException> (() => chain.CopyTo(new X509Certificate[0], -1)); Assert.Throws <ArgumentNullException> (() => chain.IndexOf(null)); Assert.Throws <ArgumentOutOfRangeException> (() => chain.Insert(-1, signer.Certificate)); Assert.Throws <ArgumentNullException> (() => chain.Insert(0, null)); Assert.Throws <ArgumentNullException> (() => chain[0] = null); Assert.Throws <ArgumentNullException> (() => chain.Remove(null)); Assert.Throws <ArgumentOutOfRangeException> (() => chain.RemoveAt(-1)); }