コード例 #1
0
 private void TestNullConfiguration(ISecureMemoryAllocator protectedMemoryAllocator)
 {
     Debug.WriteLine("TestNullConfiguration");
     using (var secret = new SecureMemorySecret(new byte[] { 0, 1 }, protectedMemoryAllocator, null))
     {
     }
 }
コード例 #2
0
        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));
            }
        }
コード例 #3
0
        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); });
        }
コード例 #4
0
        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); });
        }
コード例 #5
0
 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))
         {
         }
     });
 }
コード例 #6
0
 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);
         });
     }
 }
コード例 #7
0
 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);
         });
     }
 }
コード例 #8
0
 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);
         });
     }
 }
コード例 #9
0
        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);
                });
            });
        }
コード例 #10
0
        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);
            });
        }