public static void CopyExceptions()
        {
            CmsRecipient a0 = s_cr0;
            CmsRecipient a1 = s_cr1;
            CmsRecipient a2 = s_cr2;

            CmsRecipientCollection c = new CmsRecipientCollection();

            c.Add(a0);
            c.Add(a1);
            c.Add(a2);

            CmsRecipient[] a = new CmsRecipient[3];
            Assert.Throws <ArgumentNullException>(() => c.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, 3));
            AssertExtensions.Throws <ArgumentException>(null, () => c.CopyTo(a, 1));

            ICollection ic = c;

            Assert.Throws <ArgumentNullException>(() => ic.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, 3));
            AssertExtensions.Throws <ArgumentException>(null, () => ic.CopyTo(a, 1));
            AssertExtensions.Throws <ArgumentException>(null, () => ic.CopyTo(new CmsRecipient[2, 2], 1));
            Assert.Throws <InvalidCastException>(() => ic.CopyTo(new int[10], 1));

            if (PlatformDetection.IsNonZeroLowerBoundArraySupported)
            {
                // Array has non-zero lower bound
                Array array = Array.CreateInstance(typeof(object), new int[] { 10 }, new int[] { 10 });
                Assert.Throws <IndexOutOfRangeException>(() => ic.CopyTo(array, 0));
            }
        }
        public static void CopyExceptions()
        {
            CmsRecipient a0 = s_cr0;
            CmsRecipient a1 = s_cr1;
            CmsRecipient a2 = s_cr2;

            CmsRecipientCollection c = new CmsRecipientCollection();

            c.Add(a0);
            c.Add(a1);
            c.Add(a2);

            CmsRecipient[] a = new CmsRecipient[3];
            Assert.Throws <ArgumentNullException>(() => c.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => c.CopyTo(a, 3));
            Assert.Throws <ArgumentException>(() => c.CopyTo(a, 1));

            ICollection ic = c;

            Assert.Throws <ArgumentNullException>(() => ic.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => ic.CopyTo(a, 3));
            Assert.Throws <ArgumentException>(() => ic.CopyTo(a, 1));
            Assert.Throws <ArgumentException>(() => ic.CopyTo(new CmsRecipient[2, 2], 1));
            Assert.Throws <InvalidCastException>(() => ic.CopyTo(new int[10], 1));
        }
        public void TestArgumentExceptions()
        {
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((X509Certificate2)null));
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((X509Certificate)null));
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((Stream)null));
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((string)null));

            var recipients = new CmsRecipientCollection();

            Assert.AreEqual(0, recipients.Count);
            Assert.IsFalse(recipients.IsReadOnly);
            Assert.Throws <ArgumentNullException> (() => recipients.Add(null));
            Assert.Throws <ArgumentNullException> (() => recipients.Contains(null));
            Assert.Throws <ArgumentNullException> (() => recipients.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => recipients.CopyTo(new CmsRecipient[1], -1));
            Assert.Throws <ArgumentOutOfRangeException> (() => recipients.CopyTo(new CmsRecipient[1], 2));
            Assert.Throws <ArgumentNullException> (() => recipients.Remove(null));
        }
        private static void AssertEquals(CmsRecipientCollection c, IList <CmsRecipient> expected)
        {
            Assert.Equal(expected.Count, c.Count);

            for (int i = 0; i < c.Count; i++)
            {
                Assert.Equal(expected[i], c[i]);
            }

            int index = 0;

            foreach (CmsRecipient a in c)
            {
                Assert.Equal(expected[index++], a);
            }
            Assert.Equal(c.Count, index);

            ValidateEnumerator(c.GetEnumerator(), expected);
            ValidateEnumerator(((ICollection)c).GetEnumerator(), expected);

            {
                CmsRecipient[] dumped = new CmsRecipient[c.Count + 3];
                c.CopyTo(dumped, 2);
                Assert.Null(dumped[0]);
                Assert.Null(dumped[1]);
                Assert.Null(dumped[dumped.Length - 1]);
                Assert.Equal <CmsRecipient>(expected, dumped.Skip(2).Take(c.Count));
            }

            {
                CmsRecipient[] dumped = new CmsRecipient[c.Count + 3];
                ((ICollection)c).CopyTo(dumped, 2);
                Assert.Null(dumped[0]);
                Assert.Null(dumped[1]);
                Assert.Null(dumped[dumped.Length - 1]);
                Assert.Equal <CmsRecipient>(expected, dumped.Skip(2).Take(c.Count));
            }
        }
        public void TestCollectionAddRemove()
        {
            var path       = Path.Combine("..", "..", "TestData", "smime", "certificate-authority.crt");
            var recipients = new CmsRecipientCollection();
            var recipient  = new CmsRecipient(path);
            var array      = new CmsRecipient[1];

            Assert.IsFalse(recipients.Contains(recipient), "Contains: False");
            Assert.IsFalse(recipients.Remove(recipient), "Remove: False");

            recipients.Add(recipient);

            Assert.AreEqual(1, recipients.Count, "Count");
            Assert.IsTrue(recipients.Contains(recipient), "Contains: True");

            recipients.CopyTo(array, 0);
            Assert.AreEqual(recipient, array[0], "CopyTo");

            Assert.IsTrue(recipients.Remove(recipient), "Remove: True");

            Assert.AreEqual(0, recipients.Count, "Count");

            recipients.Clear();
        }