private void TestNullConfiguration(ISecureMemoryAllocator protectedMemoryAllocator) { Debug.WriteLine("TestNullConfiguration"); using (var secret = new SecureMemorySecret(new byte[] { 0, 1 }, protectedMemoryAllocator, null)) { } }
private void TestCloseWithClosedSecretShouldNoop() { Debug.WriteLine("TestCloseWithClosedSecretShouldNoop"); byte[] secretBytes = { 0, 1 }; // TODO : Need to determine if we can stub out the protectedMemoryAllocatorMock. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Mock <MacOSProtectedMemoryAllocatorLP64> protectedMemoryAllocatorMacOSMock = new Mock <MacOSProtectedMemoryAllocatorLP64> { CallBase = true }; SecureMemorySecret secret = new SecureMemorySecret(secretBytes, protectedMemoryAllocatorMacOSMock.Object, configuration); secret.Close(); secret.Close(); protectedMemoryAllocatorMacOSMock.Verify( x => x.Free(It.IsAny <IntPtr>(), It.IsAny <ulong>()), Times.Exactly(1)); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Mock <LinuxProtectedMemoryAllocatorLP64> protectedMemoryAllocatorLinuxMock = new Mock <LinuxProtectedMemoryAllocatorLP64> { CallBase = true }; SecureMemorySecret secret = new SecureMemorySecret(secretBytes, protectedMemoryAllocatorLinuxMock.Object, configuration); secret.Close(); secret.Close(); protectedMemoryAllocatorLinuxMock.Verify( x => x.Free(It.IsAny <IntPtr>(), It.IsAny <ulong>()), Times.Exactly(1)); } }
private void TestWithSecretUtf8CharsWithClosedSecretShouldFail(ISecureMemoryAllocator protectedMemoryAllocator) { Debug.WriteLine("TestWithSecretUtf8CharsWithClosedSecretShouldFail"); char[] secretChars = { 'a', 'b' }; SecureMemorySecret secret = SecureMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration); secret.Close(); Assert.Throws <InvalidOperationException>(() => { secret.WithSecretUtf8Chars(decryptedChars => true); }); }
private void TestWithSecretBytesWithClosedSecretShouldFail(ISecureMemoryAllocator protectedMemoryAllocator) { Debug.WriteLine("TestWithSecretBytesWithClosedSecretShouldFail"); byte[] secretBytes = { 0, 1 }; SecureMemorySecret secret = new SecureMemorySecret((byte[])secretBytes.Clone(), protectedMemoryAllocator, configuration); secret.Close(); Assert.Throws <InvalidOperationException>(() => { secret.WithSecretBytes(decryptedBytes => true); }); }
private void TestConstructorWithAllocatorReturnsNullShouldFail() { Debug.WriteLine("TestConstructorWithAllocatorReturnsNullShouldFail"); secureMemoryAllocatorMock.Setup(x => x.Alloc(It.IsAny <ulong>())).Returns(IntPtr.Zero); Assert.Throws <SecureMemoryAllocationFailedException>(() => { using (var secret = new SecureMemorySecret(new byte[] { 0, 1 }, secureMemoryAllocatorMock.Object, configuration)) { } }); }
private void TestWithSecretBytesAction(ISecureMemoryAllocator protectedMemoryAllocator) { Debug.WriteLine("TestWithSecretBytesAction"); byte[] secretBytes = { 0, 1 }; using (SecureMemorySecret secret = new SecureMemorySecret((byte[])secretBytes.Clone(), protectedMemoryAllocator, configuration)) { secret.WithSecretBytes(decryptedBytes => { Assert.Equal(secretBytes, decryptedBytes); }); } }
private void TestWithSecretUtf8CharsAction(ISecureMemoryAllocator protectedMemoryAllocator) { Debug.WriteLine("TestWithSecretUtf8CharsAction"); char[] secretChars = { 'a', 'b' }; using (SecureMemorySecret secret = SecureMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration)) { secret.WithSecretUtf8Chars(decryptedChars => { Assert.Equal(secretChars, decryptedChars); }); } }
private void TestWithSecretIntPtrActionSuccess(ISecureMemoryAllocator protectedMemoryAllocator) { Debug.WriteLine("TestWithSecretIntPtrActionSuccess"); char[] secretChars = { 'a', 'b' }; using (SecureMemorySecret secret = SecureMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration)) { secret.WithSecretIntPtr((ptr, len) => { Assert.NotEqual(ptr, IntPtr.Zero); Assert.True(len == 2); }); } }
private void TestWithSecretIntPtrDisposed(ISecureMemoryAllocator protectedMemoryAllocator) { Debug.WriteLine("TestWithSecretIntPtrDisposed"); char[] secretChars = { 'a', 'b' }; SecureMemorySecret secret = SecureMemorySecret.FromCharArray(secretChars, protectedMemoryAllocator, configuration); secret.Dispose(); Assert.Throws <InvalidOperationException>(() => { secret.WithSecretIntPtr((ptr, len) => { return(true); }); }); }
private void TestAllocatorSetNoDumpFailure() { Debug.WriteLine("TestAllocatorSetNoDumpFailure"); byte[] secretBytes = { 0, 1 }; ISecureMemoryAllocator allocator = null; // TODO : Need to determine if we can stub out the protectedMemoryAllocatorMock. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Mock <MacOSProtectedMemoryAllocatorLP64> secureMemoryAllocatorMacOSMock = new Mock <MacOSProtectedMemoryAllocatorLP64> { CallBase = true }; secureMemoryAllocatorMacOSMock.Setup(x => x.SetNoDump(It.IsAny <IntPtr>(), It.IsAny <ulong>())) .Throws(new Exception()); allocator = secureMemoryAllocatorMacOSMock.Object; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Mock <LinuxOpenSSL11ProtectedMemoryAllocatorLP64> protectedMemoryAllocatorLinuxMock = new Mock <LinuxOpenSSL11ProtectedMemoryAllocatorLP64>(configuration) { CallBase = true }; protectedMemoryAllocatorLinuxMock.Setup(x => x.SetNoDump(It.IsAny <IntPtr>(), It.IsAny <ulong>())) .Throws(new Exception()); allocator = secureMemoryAllocatorMock.Object; } else { return; } Assert.Throws <SecureMemoryAllocationFailedException>(() => { using SecureMemorySecret secret = new SecureMemorySecret(secretBytes, allocator, configuration); }); }