/// <summary> /// Updates the string value from previously set SafeString. /// </summary> private void UpdateStringValue() { if (Memory.SafeString.IsNullOrEmpty(SafeString) || SafeString.IsDisposed) { String = ""; return; } unsafe { String = new string('\0', SafeString.Length); _gch = new GCHandle(); #if NETFRAMEWORK // Create a CER (Constrained Execution Region) RuntimeHelpers.PrepareConstrainedRegions(); #endif try { } finally { // Pin our string, disallowing the garbage collector from moving it around. _gch = GCHandle.Alloc(String, GCHandleType.Pinned); } // Create a CER (Constrained Execution Region) #if NETFRAMEWORK RuntimeHelpers.PrepareConstrainedRegions(); #endif var pInsecureString = (char *)_gch.AddrOfPinnedObject(); for (var charIndex = 0; charIndex < SafeString.Length; charIndex++) { pInsecureString[charIndex] = SafeString.GetAsChar(charIndex); } } }
public void IsNullOrEmpty_ForNullSafeStringObject_ReturnsTrue() { // Arrange var nullString = (ISafeString)null; // Act var isNull = SafeString.IsNullOrEmpty(nullString); // Assert Assert.That(isNull, Is.True); }
public void IsNullOrEmpty_ForNewSafeStringInstance_ReturnsTrue() { // Arrange var sut = GetSut(); // Act var isEmpty = SafeString.IsNullOrEmpty(sut); // Assert Assert.That(isEmpty, Is.True); }
public void SafeString_Returns_AppendedText() { var expected = "test"; var sut = new SafeString(); sut.Append("t"); sut.Append("es"); sut.Append('t'); using (var sm = new SafeStringToStringMarshaler(sut)) { var actual = sm.String; Assert.That(actual, Is.EqualTo(expected)); } }
public async Task IsNullOrEmpty_ForObjectHoldingSingleByte_ReturnsFalse( [Random(0, 256, 1)] int i) { // Arrange using var sut = GetSut(); var c = (char)i; await sut.AppendAsync(c); // Act var isEmpty = SafeString.IsNullOrEmpty(sut); // Assert Assert.That(isEmpty, Is.False); }
public async Task IsNullOrEmpty_ForClearedInstance_ReturnsTrue() { // Arrange var sut = GetSut(); await sut.AppendAsync("Hello world"); sut.Clear(); // Act var isNull = SafeString.IsNullOrEmpty(sut); // Assert Assert.That(isNull, Is.True); }
public async Task SafeString_TextCanBeRevealedTwice() { // Arrange const string expected = "test"; var sut = new SafeString(); await sut.AppendAsync(expected); // Act var revealed = await RevealAsync(sut); var revealedAgain = await RevealAsync(sut); // Assert Assert.AreEqual(expected, revealed); Assert.AreEqual(expected, revealedAgain);
public async Task IsNullOrEmpty_ForDisposedSafeBytesObject_ReturnsTrue( [Random(0, 256, 1)] int i) { // Arrange var sut = GetSut(); var c = (char)i; await sut.AppendAsync(c); sut.Dispose(); // Act var isNull = SafeString.IsNullOrEmpty(sut); // Assert Assert.That(isNull, Is.True); }
public async Task EqualsAsync_TwoStringsWithDifferentLength_ReturnsFalse() { // Arrange var sut1 = new SafeString(); await sut1.AppendAsync("hej"); var sut2 = new SafeString(); await sut2.AppendAsync("sp"); // Act var equality = await sut1.EqualsAsync(sut2); var equalityOpposite = await sut2.EqualsAsync(sut1); // Assert Assert.False(equality); Assert.False(equalityOpposite); }
public async Task SafeString_MultipleCharsAdded_MarshalsToExpected() { // Arrange const string expected = "test"; var sut = new SafeString(); // Act await sut.AppendAsync("t"); await sut.AppendAsync("es"); await sut.AppendAsync('t'); // Assert var actual = (await sut.RevealDecryptedStringAsync()).String; Assert.That(actual, Is.EqualTo(expected)); }
public async Task IsNullOrEmpty_ForObjectHoldingMultipleBytes_ReturnsFalse( [Random(0, 256, 1)] int i1, [Random(0, 256, 1)] int i2, [Random(0, 256, 1)] int i3) { // Arrange var sut = GetSut(); char c1 = (char)i1, c2 = (char)i2, c3 = (char)i3; await sut.AppendAsync(c1); await sut.AppendAsync(c2); await sut.AppendAsync(c3); // Act var isNull = SafeString.IsNullOrEmpty(sut); // Assert Assert.That(isNull, Is.False); }
public void String_AfterSettingSafeString_returnsStringOfSafeString() { //Arrange var sut = GetSut(); const string expected = "갿äÅ++¨¨'e"; var safeString = new SafeString(); foreach (var ch in expected.ToCharArray()) { safeString.Append(ch); } //Act sut.SafeString = safeString; var actual = sut.String; //Assert Assert.That(actual, Is.EqualTo(expected)); }
public void String_AfterDisposingTheInstance_doesNotReturnPlainText() { //Arrange var sut = GetSut(); const string plainText = "testString"; var safeString = new SafeString(); foreach (var ch in plainText.ToCharArray()) { safeString.Append(ch); } //Act sut.SafeString = safeString; sut.Dispose(); var actual = sut.String; //assert Assert.That(actual, Is.Not.EqualTo(plainText)); }
public async Task SafeString_ModifiedByAnotherThread_ModifiesAsExpected() { // Arrange var sut = new SafeString(); await sut.AppendAsync("test"); // Act var thread = new Thread(() => { sut.Remove(1); sut.Remove(1); }); thread.Start(); thread.Join(); // Assert var actual = (await sut.RevealDecryptedStringAsync()).String; Assert.That(actual, Is.EqualTo("tt")); }