From f26842327d56fc837cf9f9d04bfd65b496412bb8 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Thu, 31 Jan 2013 21:02:00 -0600 Subject: fix System.Security.SecurString.ReadAt(int), includes NUnit test --- mcs/class/corlib/System.Security/SecureString.cs | 2 +- .../Test/System.Security/SecureStringTest.cs | 36 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'mcs') diff --git a/mcs/class/corlib/System.Security/SecureString.cs b/mcs/class/corlib/System.Security/SecureString.cs index 7b1d142ac1d..60ab898d281 100644 --- a/mcs/class/corlib/System.Security/SecureString.cs +++ b/mcs/class/corlib/System.Security/SecureString.cs @@ -210,7 +210,7 @@ namespace System.Security { try { Decrypt (); - Buffer.BlockCopy (data, index + 1, data, index, data.Length - index - 1); + Buffer.BlockCopy (data, index * 2 + 2, data, index * 2, data.Length - index * 2 - 2); Alloc (--length, true); } finally { diff --git a/mcs/class/corlib/Test/System.Security/SecureStringTest.cs b/mcs/class/corlib/Test/System.Security/SecureStringTest.cs index fb0a4a0da5e..87144d3da48 100644 --- a/mcs/class/corlib/Test/System.Security/SecureStringTest.cs +++ b/mcs/class/corlib/Test/System.Security/SecureStringTest.cs @@ -30,6 +30,7 @@ using System; using System.Security; +using System.Runtime.InteropServices; using NUnit.Framework; @@ -211,6 +212,32 @@ namespace MonoTests.System.Security { ss.SetAt (1, 'a'); } + [Test] + public void RemoveAt () + { + string test_string = "test string"; + string expected, actual; + SecureString ss = new SecureString (); + foreach (char c in test_string) { + ss.AppendChar (c); + } + + ss.RemoveAt (0); + expected = "est string"; + actual = ReadSecureString (ss); + Assert.AreEqual (expected, actual, "RemoveAt begining"); + + ss.RemoveAt (4); + expected = "est tring"; + actual = ReadSecureString (ss); + Assert.AreEqual (expected, actual, "RemoveAt middle"); + + ss.RemoveAt (8); + expected = "est trin"; + actual = ReadSecureString (ss); + Assert.AreEqual (expected, actual, "RemoveAt end"); + } + [Test] [ExpectedException (typeof (ArgumentOutOfRangeException))] public void RemoveAt_Negative () @@ -370,6 +397,15 @@ namespace MonoTests.System.Security { SecureString ss = GetDisposed (); ss.RemoveAt (0); } + + // helper function + private static string ReadSecureString(SecureString aSecureString) + { + var strPtr = Marshal.SecureStringToGlobalAllocUnicode (aSecureString); + var str = Marshal.PtrToStringUni(strPtr); + Marshal.ZeroFreeGlobalAllocUnicode (strPtr); + return str; + } } } -- cgit v1.2.3