コード例 #1
0
        /// <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);
                }
            }
        }
コード例 #2
0
        public void IsNullOrEmpty_ForNullSafeStringObject_ReturnsTrue()
        {
            // Arrange
            var nullString = (ISafeString)null;

            // Act
            var isNull = SafeString.IsNullOrEmpty(nullString);

            // Assert
            Assert.That(isNull, Is.True);
        }
コード例 #3
0
        public void IsNullOrEmpty_ForNewSafeStringInstance_ReturnsTrue()
        {
            // Arrange
            var sut = GetSut();

            // Act
            var isEmpty = SafeString.IsNullOrEmpty(sut);

            // Assert
            Assert.That(isEmpty, Is.True);
        }
コード例 #4
0
        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));
            }
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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));
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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));
        }
コード例 #13
0
        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));
        }
コード例 #14
0
        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"));
        }