2 // SecureStringTest.cs - Unit tests for System.Security.SecureString
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Security;
34 using NUnit.Framework;
36 namespace MonoTests.System.Security {
39 public class SecureStringTest {
41 private const string NotSupported = "Not supported before Windows 2000 Service Pack 3";
44 public void DefaultConstructor ()
47 SecureString ss = new SecureString ();
48 Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
49 Assert.AreEqual (0, ss.Length, "0");
51 Assert.AreEqual (1, ss.Length, "1");
53 Assert.AreEqual (0, ss.Length, "0b");
55 Assert.AreEqual (1, ss.Length, "1b");
57 Assert.AreEqual (1, ss.Length, "1c");
58 Assert.AreEqual ("System.Security.SecureString", ss.ToString (), "ToString");
60 Assert.AreEqual (0, ss.Length, "0c");
63 catch (NotSupportedException) {
64 Assert.Ignore (NotSupported);
69 public unsafe void UnsafeConstructor ()
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);
77 Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
78 Assert.AreEqual (3, ss.Length, "3");
80 Assert.AreEqual (4, ss.Length, "4");
82 Assert.AreEqual (0, ss.Length, "0b");
84 Assert.AreEqual (1, ss.Length, "1b");
86 Assert.AreEqual (1, ss.Length, "1c");
88 Assert.AreEqual (0, ss.Length, "0c");
91 catch (NotSupportedException) {
92 Assert.Ignore (NotSupported);
97 [ExpectedException (typeof (ArgumentNullException))]
98 public unsafe void UnsafeConstructor_Null ()
100 new SecureString (null, 0);
104 [ExpectedException (typeof (ArgumentOutOfRangeException))]
105 public unsafe void UnsafeConstructor_Negative ()
107 char[] data = new char[] { 'a', 'b', 'c' };
108 fixed (char* p = &data[0]) {
109 new SecureString (p, -1);
114 [ExpectedException (typeof (ArgumentOutOfRangeException))]
115 public unsafe void UnsafeConstructor_BiggerThanMax ()
117 char[] data = new char[] { 'a', 'b', 'c' };
118 fixed (char* p = &data[0]) {
119 new SecureString (p, UInt16.MaxValue + 2);
123 private SecureString max;
125 private unsafe SecureString GetMaxLength ()
128 int maxlength = UInt16.MaxValue + 1;
129 char[] data = new char[maxlength];
130 fixed (char* p = &data[0]) {
131 max = new SecureString (p, maxlength);
133 // note: don't try a loop of AppendChar with that size ;-)
139 [ExpectedException (typeof (ArgumentOutOfRangeException))]
140 public void AppendChar_BiggerThanMax ()
142 SecureString ss = GetMaxLength ();
147 [ExpectedException (typeof (ArgumentOutOfRangeException))]
148 public void InsertAt_Negative ()
150 SecureString ss = new SecureString ();
151 ss.InsertAt (-1, 'a');
155 [ExpectedException (typeof (ArgumentOutOfRangeException))]
156 public void InsertAt_BiggerThanLength ()
158 SecureString ss = new SecureString ();
159 ss.InsertAt (1, 'a');
163 [ExpectedException (typeof (ArgumentOutOfRangeException))]
164 public void SetAt_Negative ()
166 SecureString ss = new SecureString ();
171 [ExpectedException (typeof (ArgumentOutOfRangeException))]
172 public void SetAt_BiggerThanLength ()
174 SecureString ss = new SecureString ();
179 [ExpectedException (typeof (ArgumentOutOfRangeException))]
180 public void RemoveAt_Negative ()
182 SecureString ss = new SecureString ();
187 [ExpectedException (typeof (ArgumentOutOfRangeException))]
188 public void RemoveAt_BiggerThanLength ()
190 SecureString ss = new SecureString ();
195 [ExpectedException (typeof (ArgumentOutOfRangeException))]
196 public void InsertAt_BiggerThanMax ()
198 SecureString ss = GetMaxLength ();
199 ss.InsertAt (ss.Length, 'a');
202 private SecureString GetReadOnly ()
204 SecureString ss = new SecureString ();
210 public void ReadOnly ()
213 SecureString ss = GetReadOnly ();
214 Assert.IsTrue (ss.IsReadOnly (), "IsReadOnly");
215 Assert.AreEqual (0, ss.Length, "0");
218 catch (NotSupportedException) {
219 Assert.Ignore (NotSupported);
224 [ExpectedException (typeof (InvalidOperationException))]
225 public void ReadOnly_AppendChar ()
227 SecureString ss = GetReadOnly ();
232 [ExpectedException (typeof (InvalidOperationException))]
233 public void ReadOnly_Clear ()
235 SecureString ss = GetReadOnly ();
240 [ExpectedException (typeof (InvalidOperationException))]
241 public void ReadOnly_InsertAt ()
243 SecureString ss = GetReadOnly ();
244 ss.InsertAt (0, 'a');
248 [ExpectedException (typeof (InvalidOperationException))]
249 public void ReadOnly_SetAt ()
251 SecureString ss = GetReadOnly ();
256 [ExpectedException (typeof (InvalidOperationException))]
257 public void ReadOnly_RemoveAt ()
259 SecureString ss = GetReadOnly ();
263 private SecureString GetDisposed ()
265 SecureString ss = new SecureString ();
271 public void Disposed ()
274 SecureString ss = GetDisposed ();
277 catch (NotSupportedException) {
278 Assert.Ignore (NotSupported);
283 [ExpectedException (typeof (ObjectDisposedException))]
284 public void Disposed_AppendChar ()
286 SecureString ss = GetDisposed ();
291 [ExpectedException (typeof (ObjectDisposedException))]
292 public void Disposed_Clear ()
294 SecureString ss = GetDisposed ();
299 [ExpectedException (typeof (ObjectDisposedException))]
300 public void Disposed_InsertAt ()
302 SecureString ss = GetDisposed ();
303 ss.InsertAt (0, 'a');
307 [ExpectedException (typeof (ObjectDisposedException))]
308 public void Disposed_IsReadOnly ()
310 SecureString ss = GetDisposed ();
311 Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
315 [ExpectedException (typeof (ObjectDisposedException))]
316 public void Disposed_Length ()
318 SecureString ss = GetDisposed ();
319 Assert.AreEqual (0, ss.Length, "Length");
323 [ExpectedException (typeof (ObjectDisposedException))]
324 public void Disposed_SetAt ()
326 SecureString ss = GetDisposed ();
331 [ExpectedException (typeof (ObjectDisposedException))]
332 public void Disposed_RemoveAt ()
334 SecureString ss = GetDisposed ();