Merge pull request #649 from DavidS/feature/implement-additional-reference-path
[mono.git] / mcs / class / System / Test / System.Security.Cryptography.X509Certificates / X509ExtensionCollectionTest.cs
1 //
2 // X509ExtensionCollectionTest.cs - NUnit tests for X509ExtensionCollection
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using NUnit.Framework;
32
33 using System;
34 using System.Collections;
35 using System.Security.Cryptography;
36 using System.Security.Cryptography.X509Certificates;
37 using System.Text;
38
39 namespace MonoTests.System.Security.Cryptography.X509Certificates {
40
41         [TestFixture]
42         public class X509ExtensionCollectionTest {
43
44                 private X509ExtensionCollection empty;
45                 private X509Extension extn_empty;
46
47                 [TestFixtureSetUp]
48                 public void FixtureSetUp ()
49                 {
50                         empty = new X509ExtensionCollection ();
51                         extn_empty = new X509Extension ("1.2", new byte[] { 0x05, 0x00 }, false);
52                 }
53
54                 [Test]
55                 public void Defaults ()
56                 {
57                         Assert.AreEqual (0, empty.Count, "Count");
58                         Assert.IsFalse (empty.IsSynchronized, "IsSynchronized");
59                         Assert.IsTrue (Object.ReferenceEquals (empty, empty.SyncRoot), "SyncRoot");
60                         Assert.AreEqual (typeof (X509ExtensionEnumerator), empty.GetEnumerator ().GetType (), "GetEnumerator");
61                         // IEnumerable
62                         IEnumerable e = (empty as IEnumerable);
63                         Assert.AreEqual (typeof (X509ExtensionEnumerator), e.GetEnumerator ().GetType (), "IEnumerable.GetEnumerator");
64                 }
65
66                 [Test]
67                 [ExpectedException (typeof (InvalidOperationException))]
68                 public void Indexer_Int_Negative ()
69                 {
70                         Assert.IsNotNull (empty [-1]);
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
75                 public void Indexer_Int_OutOfRange ()
76                 {
77                         Assert.IsNotNull (empty [0]);
78                 }
79
80                 [Test]
81                 public void Indexer_Int ()
82                 {
83                         X509ExtensionCollection c = new X509ExtensionCollection ();
84                         c.Add (extn_empty);
85                         Assert.AreEqual (extn_empty, c[0], "0");
86                 }
87
88                 [Test]
89                 [ExpectedException (typeof (ArgumentNullException))]
90                 public void Indexer_String_Null ()
91                 {
92                         Assert.IsNotNull (empty [null]);
93                 }
94
95                 [Test]
96                 public void Indexer_String ()
97                 {
98                         X509ExtensionCollection c = new X509ExtensionCollection ();
99                         c.Add (extn_empty);
100                         Assert.IsNull (c[String.Empty]);
101                         Assert.IsNull (c ["1.2.3"]);
102                         Assert.AreEqual (extn_empty, c["1.2"], "0");
103                 }
104
105                 [Test]
106                 [ExpectedException (typeof (ArgumentNullException))]
107                 public void Add_Null ()
108                 {
109                         empty.Add (null);
110                 }
111
112                 [Test]
113                 public void Add ()
114                 {
115                         X509ExtensionCollection c = new X509ExtensionCollection ();
116                         Assert.AreEqual (0, c.Count, "Count-0");
117                         c.Add (extn_empty);
118                         Assert.AreEqual (1, c.Count, "Count-1");
119                 }
120
121                 [Test]
122                 [ExpectedException (typeof (ArgumentNullException))]
123                 public void CopyTo_Null ()
124                 {
125                         empty.CopyTo (null, 0);
126                 }
127
128                 [Test]
129                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
130                 public void CopyTo_Negative ()
131                 {
132                         X509Extension[] array = new X509Extension[1];
133                         empty.CopyTo (array, -1);
134                 }
135
136                 [Test]
137                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
138                 public void CopyTo_EmptyArray ()
139                 {
140                         X509Extension[] array = new X509Extension[0];
141                         empty.CopyTo (array, 0);
142                 }
143
144                 [Test]
145                 public void CopyTo_EmptyCollection ()
146                 {
147                         X509Extension[] array = new X509Extension[1];
148                         empty.CopyTo (array, 0);
149                 }
150
151                 [Test]
152                 public void CopyTo ()
153                 {
154                         X509ExtensionCollection c = new X509ExtensionCollection ();
155                         c.Add (extn_empty);
156                         X509Extension[] array = new X509Extension[1];
157                         c.CopyTo (array, 0);
158                         Assert.AreEqual (extn_empty, array[0], "0");
159                 }
160
161                 [Test]
162                 [ExpectedException (typeof (ArgumentNullException))]
163                 public void ICollection_CopyTo_Null ()
164                 {
165                         (empty as ICollection).CopyTo (null, 0);
166                 }
167
168                 [Test]
169                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
170                 public void ICollection_CopyTo_Negative ()
171                 {
172                         X509Extension[] array = new X509Extension[1];
173                         (empty as ICollection).CopyTo (array, -1);
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
178                 public void ICollection_CopyTo_EmptyArray ()
179                 {
180                         X509Extension[] array = new X509Extension[0];
181                         (empty as ICollection).CopyTo (array, 0);
182                 }
183
184                 [Test]
185                 public void ICollection_CopyTo_EmptyCollection ()
186                 {
187                         X509Extension[] array = new X509Extension[1];
188                         (empty as ICollection).CopyTo (array, 0);
189                 }
190
191                 [Test]
192                 public void ICollection_CopyTo ()
193                 {
194                         X509ExtensionCollection c = new X509ExtensionCollection ();
195                         c.Add (extn_empty);
196                         X509Extension[] array = new X509Extension[1];
197                         (c as ICollection).CopyTo (array, 0);
198                         Assert.AreEqual (extn_empty, array[0], "0");
199                 }
200         }
201 }
202
203 #endif