private void EndToEndTest() { Trace.Listeners.Clear(); var consoleListener = new ConsoleTraceListener(); Trace.Listeners.Add(consoleListener); var configuration = new ConfigurationBuilder() .AddInMemoryCollection() .Build(); try { Debug.WriteLine("SampleTest.EndToEndTest"); using (ISecretFactory secretFactory = new SecureMemorySecretFactory(configuration)) { var secretBytes = new byte[] { 0, 1, 2, 3 }; using (var secret = secretFactory.CreateSecret(secretBytes.Clone() as byte[])) { secret.WithSecretBytes(decryptedBytes => Assert.Equal(secretBytes, decryptedBytes)); } } Debug.WriteLine("SampleTest.EndToEndTest finish"); } catch (Exception e) { Debug.WriteLine("SampleTest.EndToEndTest exception: " + e.Message); } }
private void TestCreateSecretCharArray() { Debug.WriteLine("SecureMemorySecretFactoryTest.TestCreateSecretCharArray"); using (var factory = new SecureMemorySecretFactory(configuration)) { using Secret secret = factory.CreateSecret(new[] { 'a', 'b' }); Assert.Equal(typeof(SecureMemorySecret), secret.GetType()); } }
private void TestDefaultMlockConfigurationForMac() { Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); ISecureMemoryAllocator allocator = SecureMemorySecretFactory.ConfigureForMacOS64(configuration); Assert.IsType <MacOSProtectedMemoryAllocatorLP64>(allocator); Assert.IsNotType <MacOSSecureMemoryAllocatorLP64>(allocator); }
private void TestDoubleDispose() { var factory = new SecureMemorySecretFactory(configuration); factory.Dispose(); Assert.Throws <Exception>(() => { factory.Dispose(); }); }
private void TestMlockConfigurationSettingForLinuxWithInvalidValueThrowsException() { Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); IConfigurationRoot configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string> { { "mlock", "no" } }).Build(); Assert.Throws <ConfigurationErrorsException>(() => SecureMemorySecretFactory.ConfigureForLinux64(configuration)); }
private void TestMmapConfiguration() { var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string> { { "secureHeapEngine", "mmap" } }).Build(); Debug.WriteLine("SecureMemorySecretFactoryTest.TestMmapConfiguration"); using (var factory = new SecureMemorySecretFactory(configuration)) { } }
private void TestMlockConfigurationSettingForLinux() { Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); IConfigurationRoot configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string> { { "mlock", "disabled" } }).Build(); ISecureMemoryAllocator allocator = SecureMemorySecretFactory.ConfigureForLinux64(configuration); Assert.IsType <LinuxSecureMemoryAllocatorLP64>(allocator); Assert.IsNotType <LinuxProtectedMemoryAllocatorLP64>(allocator); }
private void TestInvalidConfiguration() { var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string> { { "secureHeapEngine", "magic-heap-engine2" } }).Build(); Debug.WriteLine("SecureMemorySecretFactoryTest.TestMmapConfiguration"); Assert.Throws <PlatformNotSupportedException>(() => { using (var factory = new SecureMemorySecretFactory(configuration)) { } }); }
private void TestWithSecretBytesMultiThreadedAccess() { Debug.WriteLine("TestWithSecretBytesMultiThreadedAccess start"); using (ISecretFactory secretFactory = new SecureMemorySecretFactory(configuration)) { byte[] secretBytes = { 0, 1, 2, 3 }; Debug.WriteLine("Creating secret"); using (Secret secret = secretFactory.CreateSecret(secretBytes.Clone() as byte[])) { // Submit large number of tasks to a reasonably sized thread pool to verify concurrency // semantics around the protected memory management const int numThreads = 100; // Get the current settings and try to force minWorkers ThreadPool.GetMinThreads(out _, out var currentMinIOC); Assert.True(ThreadPool.SetMinThreads(numThreads, currentMinIOC)); const int numTasks = numThreads * 1000; long completedTasks = 0; CountdownEvent countdown = new CountdownEvent(numTasks); Parallel.ForEach(Enumerable.Range(0, numTasks), i => { ThreadPool.QueueUserWorkItem( state => { secret.WithSecretBytes(decryptedBytes => { Assert.Equal(secretBytes, decryptedBytes); }); Interlocked.Increment(ref completedTasks); countdown.Signal(); }, null); }); // Wait for all threads to complete Debug.WriteLine("Waiting for threads"); countdown.Wait(); Debug.WriteLine("Threads finished"); Assert.Equal(numTasks, completedTasks); } } Debug.WriteLine("TestWithSecretBytesMultiThreadedAccess end"); }
private void EndToEndOpenSSLTest() { Trace.Listeners.Clear(); var consoleListener = new ConsoleTraceListener(); Trace.Listeners.Add(consoleListener); var dictionary = new Dictionary <string, string> { { "secureHeapEngine", "openssl11" }, { "heapSize", "32000" }, { "minimumAllocationSize", "128" } }; var configuration = new ConfigurationBuilder() .AddInMemoryCollection(dictionary) .Build(); try { Debug.WriteLine("SampleTest.EndToEndOpenSSLTest"); using (ISecretFactory secretFactory = new SecureMemorySecretFactory(configuration)) { var secretBytes = new byte[] { 0, 1, 2, 3 }; using (var secret = secretFactory.CreateSecret(secretBytes.Clone() as byte[])) { secret.WithSecretBytes(decryptedBytes => Assert.Equal(secretBytes, decryptedBytes)); } } Debug.WriteLine("SampleTest.EndToEndOpenSSLTest finish"); } catch (Exception e) { Debug.WriteLine("SampleTest.EndToEndOpenSSLTest exception: " + e.Message); } }