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