SecureStringTest.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. using System;
  29. using System.Security;
  30. using System.Runtime.InteropServices;
  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. [Test]
  61. public unsafe void UnsafeConstructor ()
  62. {
  63. try {
  64. SecureString ss = null;
  65. char[] data = new char[] { 'a', 'b', 'c' };
  66. fixed (char* p = &data[0]) {
  67. ss = new SecureString (p, data.Length);
  68. }
  69. Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
  70. Assert.AreEqual (3, ss.Length, "3");
  71. ss.AppendChar ('a');
  72. Assert.AreEqual (4, ss.Length, "4");
  73. ss.Clear ();
  74. Assert.AreEqual (0, ss.Length, "0b");
  75. ss.InsertAt (0, 'b');
  76. Assert.AreEqual (1, ss.Length, "1b");
  77. ss.SetAt (0, 'c');
  78. Assert.AreEqual (1, ss.Length, "1c");
  79. ss.RemoveAt (0);
  80. Assert.AreEqual (0, ss.Length, "0c");
  81. ss.Dispose ();
  82. }
  83. catch (NotSupportedException) {
  84. Assert.Ignore (NotSupported);
  85. }
  86. }
  87. [Test]
  88. [ExpectedException (typeof (ArgumentNullException))]
  89. public unsafe void UnsafeConstructor_Null ()
  90. {
  91. new SecureString (null, 0);
  92. }
  93. [Test]
  94. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  95. public unsafe void UnsafeConstructor_Negative ()
  96. {
  97. char[] data = new char[] { 'a', 'b', 'c' };
  98. fixed (char* p = &data[0]) {
  99. new SecureString (p, -1);
  100. }
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  104. public unsafe void UnsafeConstructor_BiggerThanMax ()
  105. {
  106. char[] data = new char[] { 'a', 'b', 'c' };
  107. fixed (char* p = &data[0]) {
  108. new SecureString (p, UInt16.MaxValue + 2);
  109. }
  110. }
  111. private SecureString max;
  112. private unsafe SecureString GetMaxLength ()
  113. {
  114. if (max == null) {
  115. int maxlength = UInt16.MaxValue + 1;
  116. char[] data = new char[maxlength];
  117. fixed (char* p = &data[0]) {
  118. max = new SecureString (p, maxlength);
  119. }
  120. // note: don't try a loop of AppendChar with that size ;-)
  121. }
  122. return max;
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  126. public void AppendChar_BiggerThanMax ()
  127. {
  128. SecureString ss = GetMaxLength ();
  129. ss.AppendChar ('a');
  130. }
  131. [Test]
  132. public void Copy_Empty ()
  133. {
  134. SecureString empty = new SecureString ();
  135. Assert.AreEqual (0, empty.Length, "Empty.Length");
  136. SecureString empty_copy = empty.Copy ();
  137. Assert.AreEqual (0, empty_copy.Length, "EmptyCopy.Length");
  138. }
  139. [Test]
  140. public void Copy ()
  141. {
  142. SecureString ss = new SecureString ();
  143. ss.AppendChar ('a');
  144. Assert.AreEqual (1, ss.Length, "Length");
  145. SecureString ss2 = ss.Copy();
  146. Assert.AreEqual (1, ss2.Length, "Copy.Length");
  147. Assert.IsFalse (ss2.IsReadOnly (), "Copy.IsReadOnly");
  148. ss2.MakeReadOnly ();
  149. Assert.IsTrue (ss2.IsReadOnly (), "Copy.IsReadOnly-2");
  150. SecureString ss3 = ss2.Copy ();
  151. Assert.IsFalse (ss3.IsReadOnly (), "Copy.IsReadOnly-3");
  152. }
  153. [Test]
  154. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  155. public void InsertAt_Negative ()
  156. {
  157. SecureString ss = new SecureString ();
  158. ss.InsertAt (-1, 'a');
  159. }
  160. [Test]
  161. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  162. public void InsertAt_BiggerThanLength ()
  163. {
  164. SecureString ss = new SecureString ();
  165. ss.InsertAt (1, 'a');
  166. }
  167. [Test]
  168. public void InsertAt_UsedLikeAppendChar () // #350820
  169. {
  170. SecureString ss = new SecureString ();
  171. ss.AppendChar ('T');
  172. Assert.AreEqual (1, ss.Length, "AppendChar");
  173. ss.InsertAt (1, 'e');
  174. Assert.AreEqual (2, ss.Length, "InsertAt");
  175. }
  176. [Test]
  177. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  178. public void SetAt_Negative ()
  179. {
  180. SecureString ss = new SecureString ();
  181. ss.SetAt (-1, 'a');
  182. }
  183. [Test]
  184. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  185. public void SetAt_BiggerThanLength ()
  186. {
  187. SecureString ss = new SecureString ();
  188. ss.SetAt (1, 'a');
  189. }
  190. [Test]
  191. public void RemoveAt ()
  192. {
  193. string test_string = "test string";
  194. string expected, actual;
  195. SecureString ss = new SecureString ();
  196. foreach (char c in test_string) {
  197. ss.AppendChar (c);
  198. }
  199. ss.RemoveAt (0);
  200. expected = "est string";
  201. actual = ReadSecureString (ss);
  202. Assert.AreEqual (expected, actual, "RemoveAt begining");
  203. ss.RemoveAt (4);
  204. expected = "est tring";
  205. actual = ReadSecureString (ss);
  206. Assert.AreEqual (expected, actual, "RemoveAt middle");
  207. ss.RemoveAt (8);
  208. expected = "est trin";
  209. actual = ReadSecureString (ss);
  210. Assert.AreEqual (expected, actual, "RemoveAt end");
  211. }
  212. [Test]
  213. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  214. public void RemoveAt_Negative ()
  215. {
  216. SecureString ss = new SecureString ();
  217. ss.RemoveAt (-1);
  218. }
  219. [Test]
  220. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  221. public void RemoveAt_BiggerThanLength ()
  222. {
  223. SecureString ss = new SecureString ();
  224. ss.RemoveAt (1);
  225. }
  226. [Test]
  227. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  228. public void InsertAt_BiggerThanMax ()
  229. {
  230. SecureString ss = GetMaxLength ();
  231. ss.InsertAt (ss.Length, 'a');
  232. }
  233. private SecureString GetReadOnly ()
  234. {
  235. SecureString ss = new SecureString ();
  236. ss.MakeReadOnly ();
  237. return ss;
  238. }
  239. [Test]
  240. public void ReadOnly ()
  241. {
  242. try {
  243. SecureString ss = GetReadOnly ();
  244. Assert.IsTrue (ss.IsReadOnly (), "IsReadOnly");
  245. Assert.AreEqual (0, ss.Length, "0");
  246. ss.Dispose ();
  247. }
  248. catch (NotSupportedException) {
  249. Assert.Ignore (NotSupported);
  250. }
  251. }
  252. [Test]
  253. [ExpectedException (typeof (InvalidOperationException))]
  254. public void ReadOnly_AppendChar ()
  255. {
  256. SecureString ss = GetReadOnly ();
  257. ss.AppendChar ('a');
  258. }
  259. [Test]
  260. [ExpectedException (typeof (InvalidOperationException))]
  261. public void ReadOnly_Clear ()
  262. {
  263. SecureString ss = GetReadOnly ();
  264. ss.Clear ();
  265. }
  266. [Test]
  267. [ExpectedException (typeof (InvalidOperationException))]
  268. public void ReadOnly_InsertAt ()
  269. {
  270. SecureString ss = GetReadOnly ();
  271. ss.InsertAt (0, 'a');
  272. }
  273. [Test]
  274. [ExpectedException (typeof (InvalidOperationException))]
  275. public void ReadOnly_SetAt ()
  276. {
  277. SecureString ss = GetReadOnly ();
  278. ss.SetAt (0, 'a');
  279. }
  280. [Test]
  281. [ExpectedException (typeof (InvalidOperationException))]
  282. public void ReadOnly_RemoveAt ()
  283. {
  284. SecureString ss = GetReadOnly ();
  285. ss.RemoveAt (0);
  286. }
  287. private SecureString GetDisposed ()
  288. {
  289. SecureString ss = new SecureString ();
  290. ss.Dispose ();
  291. return ss;
  292. }
  293. [Test]
  294. public void Disposed ()
  295. {
  296. try {
  297. SecureString ss = GetDisposed ();
  298. ss.Dispose ();
  299. }
  300. catch (NotSupportedException) {
  301. Assert.Ignore (NotSupported);
  302. }
  303. }
  304. [Test]
  305. [ExpectedException (typeof (ObjectDisposedException))]
  306. public void Disposed_AppendChar ()
  307. {
  308. SecureString ss = GetDisposed ();
  309. ss.AppendChar ('a');
  310. }
  311. [Test]
  312. [ExpectedException (typeof (ObjectDisposedException))]
  313. public void Disposed_Clear ()
  314. {
  315. SecureString ss = GetDisposed ();
  316. ss.Clear ();
  317. }
  318. [Test]
  319. [ExpectedException (typeof (ObjectDisposedException))]
  320. public void Disposed_InsertAt ()
  321. {
  322. SecureString ss = GetDisposed ();
  323. ss.InsertAt (0, 'a');
  324. }
  325. [Test]
  326. [ExpectedException (typeof (ObjectDisposedException))]
  327. public void Disposed_IsReadOnly ()
  328. {
  329. SecureString ss = GetDisposed ();
  330. Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly");
  331. }
  332. [Test]
  333. [ExpectedException (typeof (ObjectDisposedException))]
  334. public void Disposed_Length ()
  335. {
  336. SecureString ss = GetDisposed ();
  337. Assert.AreEqual (0, ss.Length, "Length");
  338. }
  339. [Test]
  340. [ExpectedException (typeof (ObjectDisposedException))]
  341. public void Disposed_SetAt ()
  342. {
  343. SecureString ss = GetDisposed ();
  344. ss.SetAt (0, 'a');
  345. }
  346. [Test]
  347. [ExpectedException (typeof (ObjectDisposedException))]
  348. public void Disposed_RemoveAt ()
  349. {
  350. SecureString ss = GetDisposed ();
  351. ss.RemoveAt (0);
  352. }
  353. // helper function
  354. private static string ReadSecureString(SecureString aSecureString)
  355. {
  356. var strPtr = Marshal.SecureStringToGlobalAllocUnicode (aSecureString);
  357. var str = Marshal.PtrToStringUni(strPtr);
  358. Marshal.ZeroFreeGlobalAllocUnicode (strPtr);
  359. return str;
  360. }
  361. }
  362. }