2005-12-23 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security / SecureStringTest.cs
1 //
2 // SecureStringTest.cs - Unit tests for System.Security.SecureString
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 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 System;
32 using System.Security;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Security {
37
38         [TestFixture]
39         public class SecureStringTest {
40
41                 private const string NotSupported = "Not supported before Windows 2000 Service Pack 3";
42
43                 [Test]
44                 public void DefaultConstructor ()
45                 {
46                         try {
47                                 SecureString ss = new SecureString ();
48                                 Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
49                                 Assert.AreEqual (0, ss.Length, "0");
50                                 ss.AppendChar ('a');
51                                 Assert.AreEqual (1, ss.Length, "1");
52                                 ss.Clear ();
53                                 Assert.AreEqual (0, ss.Length, "0b");
54                                 ss.InsertAt (0, 'b');
55                                 Assert.AreEqual (1, ss.Length, "1b");
56                                 ss.SetAt (0, 'c');
57                                 Assert.AreEqual (1, ss.Length, "1c");
58                                 Assert.AreEqual ("System.Security.SecureString", ss.ToString (), "ToString");
59                                 ss.RemoveAt (0);
60                                 Assert.AreEqual (0, ss.Length, "0c");
61                                 ss.Dispose ();
62                         }
63                         catch (NotSupportedException) {
64                                 Assert.Ignore (NotSupported);
65                         }
66                 }
67
68                 [Test]
69                 public unsafe void UnsafeConstructor ()
70                 {
71                         try {
72                                 SecureString ss = null;
73                                 char[] data = new char[] { 'a', 'b', 'c' };
74                                 fixed (char* p = &data[0]) {
75                                         ss = new SecureString (p, data.Length);
76                                 }
77                                 Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
78                                 Assert.AreEqual (3, ss.Length, "3");
79                                 ss.AppendChar ('a');
80                                 Assert.AreEqual (4, ss.Length, "4");
81                                 ss.Clear ();
82                                 Assert.AreEqual (0, ss.Length, "0b");
83                                 ss.InsertAt (0, 'b');
84                                 Assert.AreEqual (1, ss.Length, "1b");
85                                 ss.SetAt (0, 'c');
86                                 Assert.AreEqual (1, ss.Length, "1c");
87                                 ss.RemoveAt (0);
88                                 Assert.AreEqual (0, ss.Length, "0c");
89                                 ss.Dispose ();
90                         }
91                         catch (NotSupportedException) {
92                                 Assert.Ignore (NotSupported);
93                         }
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentNullException))]
98                 public unsafe void UnsafeConstructor_Null ()
99                 {
100                         new SecureString (null, 0);
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
105                 public unsafe void UnsafeConstructor_Negative ()
106                 {
107                         char[] data = new char[] { 'a', 'b', 'c' };
108                         fixed (char* p = &data[0]) {
109                                 new SecureString (p, -1);
110                         }
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
115                 public unsafe void UnsafeConstructor_BiggerThanMax ()
116                 {
117                         char[] data = new char[] { 'a', 'b', 'c' };
118                         fixed (char* p = &data[0]) {
119                                 new SecureString (p, UInt16.MaxValue + 2);
120                         }
121                 }
122
123                 private SecureString max;
124
125                 private unsafe SecureString GetMaxLength ()
126                 {
127                         if (max == null) {
128                                 int maxlength =  UInt16.MaxValue + 1;
129                                 char[] data = new char[maxlength];
130                                 fixed (char* p = &data[0]) {
131                                         max = new SecureString (p, maxlength);
132                                 }
133                                 // note: don't try a loop of AppendChar with that size ;-)
134                         }
135                         return max;
136                 }
137
138                 [Test]
139                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
140                 public void AppendChar_BiggerThanMax ()
141                 {
142                         SecureString ss = GetMaxLength ();
143                         ss.AppendChar ('a');
144                 }
145
146                 [Test]
147                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
148                 public void InsertAt_Negative ()
149                 {
150                         SecureString ss = new SecureString ();
151                         ss.InsertAt (-1, 'a');
152                 }
153
154                 [Test]
155                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
156                 public void InsertAt_BiggerThanLength ()
157                 {
158                         SecureString ss = new SecureString ();
159                         ss.InsertAt (1, 'a');
160                 }
161
162                 [Test]
163                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
164                 public void SetAt_Negative ()
165                 {
166                         SecureString ss = new SecureString ();
167                         ss.SetAt (-1, 'a');
168                 }
169
170                 [Test]
171                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
172                 public void SetAt_BiggerThanLength ()
173                 {
174                         SecureString ss = new SecureString ();
175                         ss.SetAt (1, 'a');
176                 }
177
178                 [Test]
179                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
180                 public void RemoveAt_Negative ()
181                 {
182                         SecureString ss = new SecureString ();
183                         ss.RemoveAt (-1);
184                 }
185
186                 [Test]
187                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
188                 public void RemoveAt_BiggerThanLength ()
189                 {
190                         SecureString ss = new SecureString ();
191                         ss.RemoveAt (1);
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
196                 public void InsertAt_BiggerThanMax ()
197                 {
198                         SecureString ss = GetMaxLength ();
199                         ss.InsertAt (ss.Length, 'a');
200                 }
201
202                 private SecureString GetReadOnly ()
203                 {
204                         SecureString ss = new SecureString ();
205                         ss.MakeReadOnly ();
206                         return ss;
207                 }
208
209                 [Test]
210                 public void ReadOnly ()
211                 {
212                         try {
213                                 SecureString ss = GetReadOnly ();
214                                 Assert.IsTrue (ss.IsReadOnly (), "IsReadOnly");
215                                 Assert.AreEqual (0, ss.Length, "0");
216                                 ss.Dispose ();
217                         }
218                         catch (NotSupportedException) {
219                                 Assert.Ignore (NotSupported);
220                         }
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof (InvalidOperationException))]
225                 public void ReadOnly_AppendChar ()
226                 {
227                         SecureString ss = GetReadOnly ();
228                         ss.AppendChar ('a');
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (InvalidOperationException))]
233                 public void ReadOnly_Clear ()
234                 {
235                         SecureString ss = GetReadOnly ();
236                         ss.Clear ();
237                 }
238
239                 [Test]
240                 [ExpectedException (typeof (InvalidOperationException))]
241                 public void ReadOnly_InsertAt ()
242                 {
243                         SecureString ss = GetReadOnly ();
244                         ss.InsertAt (0, 'a');
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (InvalidOperationException))]
249                 public void ReadOnly_SetAt ()
250                 {
251                         SecureString ss = GetReadOnly ();
252                         ss.SetAt (0, 'a');
253                 }
254
255                 [Test]
256                 [ExpectedException (typeof (InvalidOperationException))]
257                 public void ReadOnly_RemoveAt ()
258                 {
259                         SecureString ss = GetReadOnly ();
260                         ss.RemoveAt (0);
261                 }
262
263                 private SecureString GetDisposed ()
264                 {
265                         SecureString ss = new SecureString ();
266                         ss.Dispose ();
267                         return ss;
268                 }
269
270                 [Test]
271                 public void Disposed ()
272                 {
273                         try {
274                                 SecureString ss = GetDisposed ();
275                                 ss.Dispose ();
276                         }
277                         catch (NotSupportedException) {
278                                 Assert.Ignore (NotSupported);
279                         }
280                 }
281
282                 [Test]
283                 [ExpectedException (typeof (ObjectDisposedException))]
284                 public void Disposed_AppendChar ()
285                 {
286                         SecureString ss = GetDisposed ();
287                         ss.AppendChar ('a');
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (ObjectDisposedException))]
292                 public void Disposed_Clear ()
293                 {
294                         SecureString ss = GetDisposed ();
295                         ss.Clear ();
296                 }
297
298                 [Test]
299                 [ExpectedException (typeof (ObjectDisposedException))]
300                 public void Disposed_InsertAt ()
301                 {
302                         SecureString ss = GetDisposed ();
303                         ss.InsertAt (0, 'a');
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (ObjectDisposedException))]
308                 public void Disposed_IsReadOnly ()
309                 {
310                         SecureString ss = GetDisposed ();
311                         Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
312                 }
313
314                 [Test]
315                 [ExpectedException (typeof (ObjectDisposedException))]
316                 public void Disposed_Length ()
317                 {
318                         SecureString ss = GetDisposed ();
319                         Assert.AreEqual (0, ss.Length, "Length");
320                 }
321
322                 [Test]
323                 [ExpectedException (typeof (ObjectDisposedException))]
324                 public void Disposed_SetAt ()
325                 {
326                         SecureString ss = GetDisposed ();
327                         ss.SetAt (0, 'a');
328                 }
329
330                 [Test]
331                 [ExpectedException (typeof (ObjectDisposedException))]
332                 public void Disposed_RemoveAt ()
333                 {
334                         SecureString ss = GetDisposed ();
335                         ss.RemoveAt (0);
336                 }
337         }
338 }
339
340 #endif