SecureStringTest.cs 8.1 KB

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