CryptographicAttributeObjectCollectionTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // CryptographicAttributeObjectCollectionTest.cs - NUnit tests for
  3. // System.Security.Cryptography.CryptographicAttributeObjectCollection
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Security.Cryptography;
  33. namespace MonoTests.System.Security.Cryptography {
  34. [TestFixture]
  35. public class CryptographicAttributeObjectCollectionTest {
  36. static string defaultOid = "1.2.840.113549.1.7.1";
  37. private void CommonStuff (CryptographicAttributeObjectCollection coll)
  38. {
  39. Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
  40. Assert.AreSame (coll, coll.SyncRoot, "SyncRoot");
  41. Assert.IsNotNull (coll.GetEnumerator (), "GetEnumerator");
  42. int i = coll.Count;
  43. Oid o1 = new Oid ("1.2.840.113549.1.7.3");
  44. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x05, 0x00 });
  45. Assert.AreEqual (i, coll.Add (aed), "Add(AsnEncodedData)");
  46. Assert.IsTrue ((coll[i++] is CryptographicAttributeObject), "converted");
  47. Oid o2 = new Oid ("1.2.840.113549.1.7.2");
  48. CryptographicAttributeObject cao = new CryptographicAttributeObject (o2);
  49. Assert.AreEqual (i, coll.Add (cao), "Add(CryptographicAttributeObject)");
  50. CryptographicAttributeObject[] array = new CryptographicAttributeObject [coll.Count];
  51. coll.CopyTo (array, 0);
  52. Array a = (Array) new object [coll.Count];
  53. ICollection c = (ICollection) coll;
  54. c.CopyTo (a, 0);
  55. IEnumerable e = (IEnumerable) coll;
  56. Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
  57. coll.Remove (cao);
  58. Assert.AreEqual (i, coll.Count, "Remove(CryptographicAttributeObject)");
  59. }
  60. [Test]
  61. public void Constructor_Empty ()
  62. {
  63. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection ();
  64. Assert.AreEqual (0, coll.Count, "Count");
  65. CommonStuff (coll);
  66. }
  67. [Test]
  68. public void Constructor_CryptographicAttributeObject ()
  69. {
  70. Oid o = new Oid (defaultOid);
  71. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  72. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  73. Assert.AreEqual (1, coll.Count, "Count");
  74. Assert.AreSame (cao, coll[0], "this[int]");
  75. CommonStuff (coll);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentNullException))]
  79. public void Add_AsnEncodedData_Null ()
  80. {
  81. AsnEncodedData aed = null;
  82. new CryptographicAttributeObjectCollection ().Add (aed);
  83. }
  84. [Test]
  85. [ExpectedException (typeof (ArgumentNullException))]
  86. public void Add_CryptographicAttributeObject_Null ()
  87. {
  88. CryptographicAttributeObject cao = null;
  89. new CryptographicAttributeObjectCollection ().Add (cao);
  90. }
  91. [Test]
  92. public void Add_MultipleSameOid ()
  93. {
  94. Oid o = new Oid (defaultOid);
  95. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  96. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  97. int i = 0;
  98. while (i < 10) {
  99. Assert.AreEqual (1, coll.Count, String.Format ("Count-{0}", i));
  100. Assert.AreEqual (i * 2, coll[0].Values.Count, String.Format ("Values.Count-{0}", i++));
  101. Oid o1 = new Oid (defaultOid);
  102. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte)i });
  103. coll.Add (aed);
  104. aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) i });
  105. coll.Add (aed);
  106. Oid o2 = new Oid (defaultOid);
  107. coll.Add (new CryptographicAttributeObject (o2));
  108. }
  109. }
  110. [Test]
  111. [ExpectedException (typeof (ArgumentNullException))]
  112. public void CopyTo_Null ()
  113. {
  114. new CryptographicAttributeObjectCollection ().CopyTo (null, 0);
  115. }
  116. [Test]
  117. [ExpectedException (typeof (ArgumentNullException))]
  118. public void ICollection_CopyTo_Null ()
  119. {
  120. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection ();
  121. ICollection c = (coll as ICollection);
  122. c.CopyTo (null, 0);
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentNullException))]
  126. public void Remove_Null ()
  127. {
  128. new CryptographicAttributeObjectCollection ().Remove (null);
  129. }
  130. [Test]
  131. [ExpectedException (typeof (ArgumentNullException))]
  132. public void Remove_Null_WithNullItem ()
  133. {
  134. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (null);
  135. Assert.AreEqual (1, coll.Count, "Count");
  136. Assert.IsNull (coll[0], "this[int]");
  137. coll.Remove (null);
  138. }
  139. [Test]
  140. public void Remove_MultipleSameOid_First ()
  141. {
  142. Oid o = new Oid (defaultOid);
  143. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  144. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  145. Oid o1 = new Oid (defaultOid);
  146. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  147. coll.Add (aed);
  148. aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  149. coll.Add (aed);
  150. Oid o2 = new Oid (defaultOid);
  151. coll.Add (new CryptographicAttributeObject (o2));
  152. Assert.AreEqual (1, coll.Count, "before Remove");
  153. coll.Remove (cao);
  154. Assert.AreEqual (0, coll.Count, "after Remove");
  155. }
  156. [Test]
  157. public void Remove_MultipleSameOid_Last ()
  158. {
  159. Oid o = new Oid (defaultOid);
  160. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  161. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  162. Oid o1 = new Oid (defaultOid);
  163. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  164. coll.Add (aed);
  165. aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  166. coll.Add (aed);
  167. Oid o2 = new Oid (defaultOid);
  168. CryptographicAttributeObject last = new CryptographicAttributeObject (o2);
  169. coll.Add (last);
  170. Assert.AreEqual (1, coll.Count, "before Remove");
  171. coll.Remove (last);
  172. Assert.AreEqual (1, coll.Count, "after Remove");
  173. }
  174. [Test]
  175. public void Remove_WithDifferentInstance ()
  176. {
  177. Oid o = new Oid (defaultOid);
  178. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  179. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  180. Assert.AreEqual (1, coll.Count, "before Remove");
  181. cao = new CryptographicAttributeObject (o);
  182. coll.Remove (cao);
  183. Assert.AreEqual (1, coll.Count, "after Remove");
  184. }
  185. }
  186. }