CryptographicAttributeObjectCollectionTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #if NET_2_0
  30. using NUnit.Framework;
  31. using System;
  32. using System.Collections;
  33. using System.Security.Cryptography;
  34. namespace MonoTests.System.Security.Cryptography {
  35. [TestFixture]
  36. public class CryptographicAttributeObjectCollectionTest {
  37. static string defaultOid = "1.2.840.113549.1.7.1";
  38. private void CommonStuff (CryptographicAttributeObjectCollection coll)
  39. {
  40. Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
  41. Assert.AreSame (coll, coll.SyncRoot, "SyncRoot");
  42. Assert.IsNotNull (coll.GetEnumerator (), "GetEnumerator");
  43. int i = coll.Count;
  44. Oid o1 = new Oid ("1.2.840.113549.1.7.3");
  45. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x05, 0x00 });
  46. Assert.AreEqual (i, coll.Add (aed), "Add(AsnEncodedData)");
  47. Assert.IsTrue ((coll[i++] is CryptographicAttributeObject), "converted");
  48. Oid o2 = new Oid ("1.2.840.113549.1.7.2");
  49. CryptographicAttributeObject cao = new CryptographicAttributeObject (o2);
  50. Assert.AreEqual (i, coll.Add (cao), "Add(CryptographicAttributeObject)");
  51. CryptographicAttributeObject[] array = new CryptographicAttributeObject [coll.Count];
  52. coll.CopyTo (array, 0);
  53. Array a = (Array) new object [coll.Count];
  54. ICollection c = (ICollection) coll;
  55. c.CopyTo (a, 0);
  56. IEnumerable e = (IEnumerable) coll;
  57. Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
  58. coll.Remove (cao);
  59. Assert.AreEqual (i, coll.Count, "Remove(CryptographicAttributeObject)");
  60. }
  61. [Test]
  62. public void Constructor_Empty ()
  63. {
  64. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection ();
  65. Assert.AreEqual (0, coll.Count, "Count");
  66. CommonStuff (coll);
  67. }
  68. [Test]
  69. public void Constructor_CryptographicAttributeObject ()
  70. {
  71. Oid o = new Oid (defaultOid);
  72. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  73. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  74. Assert.AreEqual (1, coll.Count, "Count");
  75. Assert.AreSame (cao, coll[0], "this[int]");
  76. CommonStuff (coll);
  77. }
  78. [Test]
  79. [ExpectedException (typeof (ArgumentNullException))]
  80. public void Add_AsnEncodedData_Null ()
  81. {
  82. AsnEncodedData aed = null;
  83. new CryptographicAttributeObjectCollection ().Add (aed);
  84. }
  85. [Test]
  86. [ExpectedException (typeof (ArgumentNullException))]
  87. public void Add_CryptographicAttributeObject_Null ()
  88. {
  89. CryptographicAttributeObject cao = null;
  90. new CryptographicAttributeObjectCollection ().Add (cao);
  91. }
  92. [Test]
  93. public void Add_MultipleSameOid ()
  94. {
  95. Oid o = new Oid (defaultOid);
  96. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  97. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  98. int i = 0;
  99. while (i < 10) {
  100. Assert.AreEqual (1, coll.Count, String.Format ("Count-{0}", i));
  101. Assert.AreEqual (i * 2, coll[0].Values.Count, String.Format ("Values.Count-{0}", i++));
  102. Oid o1 = new Oid (defaultOid);
  103. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte)i });
  104. coll.Add (aed);
  105. aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) i });
  106. coll.Add (aed);
  107. Oid o2 = new Oid (defaultOid);
  108. coll.Add (new CryptographicAttributeObject (o2));
  109. }
  110. }
  111. [Test]
  112. [ExpectedException (typeof (ArgumentNullException))]
  113. public void CopyTo_Null ()
  114. {
  115. new CryptographicAttributeObjectCollection ().CopyTo (null, 0);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentNullException))]
  119. public void ICollection_CopyTo_Null ()
  120. {
  121. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection ();
  122. ICollection c = (coll as ICollection);
  123. c.CopyTo (null, 0);
  124. }
  125. [Test]
  126. [ExpectedException (typeof (ArgumentNullException))]
  127. public void Remove_Null ()
  128. {
  129. new CryptographicAttributeObjectCollection ().Remove (null);
  130. }
  131. [Test]
  132. [ExpectedException (typeof (ArgumentNullException))]
  133. public void Remove_Null_WithNullItem ()
  134. {
  135. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (null);
  136. Assert.AreEqual (1, coll.Count, "Count");
  137. Assert.IsNull (coll[0], "this[int]");
  138. coll.Remove (null);
  139. }
  140. [Test]
  141. public void Remove_MultipleSameOid_First ()
  142. {
  143. Oid o = new Oid (defaultOid);
  144. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  145. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  146. Oid o1 = new Oid (defaultOid);
  147. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  148. coll.Add (aed);
  149. aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  150. coll.Add (aed);
  151. Oid o2 = new Oid (defaultOid);
  152. coll.Add (new CryptographicAttributeObject (o2));
  153. Assert.AreEqual (1, coll.Count, "before Remove");
  154. coll.Remove (cao);
  155. Assert.AreEqual (0, coll.Count, "after Remove");
  156. }
  157. [Test]
  158. public void Remove_MultipleSameOid_Last ()
  159. {
  160. Oid o = new Oid (defaultOid);
  161. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  162. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  163. Oid o1 = new Oid (defaultOid);
  164. AsnEncodedData aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  165. coll.Add (aed);
  166. aed = new AsnEncodedData (o1, new byte[] { 0x04, (byte) 0 });
  167. coll.Add (aed);
  168. Oid o2 = new Oid (defaultOid);
  169. CryptographicAttributeObject last = new CryptographicAttributeObject (o2);
  170. coll.Add (last);
  171. Assert.AreEqual (1, coll.Count, "before Remove");
  172. coll.Remove (last);
  173. Assert.AreEqual (1, coll.Count, "after Remove");
  174. }
  175. [Test]
  176. public void Remove_WithDifferentInstance ()
  177. {
  178. Oid o = new Oid (defaultOid);
  179. CryptographicAttributeObject cao = new CryptographicAttributeObject (o);
  180. CryptographicAttributeObjectCollection coll = new CryptographicAttributeObjectCollection (cao);
  181. Assert.AreEqual (1, coll.Count, "before Remove");
  182. cao = new CryptographicAttributeObject (o);
  183. coll.Remove (cao);
  184. Assert.AreEqual (1, coll.Count, "after Remove");
  185. }
  186. }
  187. }
  188. #endif