Пример #1
0
        private void MultiThreadedWithSecretBytesAccess()
        {
            ISecretFactory secretFactory = new ProtectedMemorySecretFactory();
            Secret         secret        = secretFactory.CreateSecret(payload.Clone() as byte[]);

            // Get the current settings and try to force minWorkers
            ThreadPool.GetMinThreads(out _, out var currentMinIOC);
            Assert.True(ThreadPool.SetMinThreads(NumThreads, currentMinIOC));

            int completedTasks = 0;

            Parallel.ForEach(Enumerable.Range(0, NumThreads), i =>
            {
                try
                {
                    secret.WithSecretBytes(decryptedBytes =>
                    {
                        Assert.Equal(payload, decryptedBytes);
                        Interlocked.Increment(ref completedTasks);
                    });
                }
                catch (ThreadInterruptedException e)
                {
                    Logger.LogError(e, "Unexpected error during call");
                    throw;
                }
            });
            Assert.Equal(NumThreads, completedTasks);
        }
Пример #2
0
        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 ProtectedMemorySecretFactory(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);
            }
        }
Пример #3
0
        private void EndToEndTest()
        {
            ISecretFactory secretFactory = new ProtectedMemorySecretFactory();
            var            secretBytes   = new byte[] { 0, 1, 2, 3 };
            var            secret        = secretFactory.CreateSecret(secretBytes.Clone() as byte[]);

            secret.WithSecretBytes(decryptedBytes => Assert.Equal(secretBytes, decryptedBytes));
        }
Пример #4
0
        private void TestDoubleDispose()
        {
            var factory = new ProtectedMemorySecretFactory(configuration);

            factory.Dispose();
            Assert.Throws <Exception>(() => {
                factory.Dispose();
            });
        }
Пример #5
0
 private void TestCreateSecretCharArray()
 {
     Debug.WriteLine("ProtectedMemorySecretFactoryTest.TestCreateSecretCharArray");
     using (var factory = new ProtectedMemorySecretFactory(configuration))
     {
         using Secret secret = factory.CreateSecret(new[] { 'a', 'b' });
         Assert.Equal(typeof(ProtectedMemorySecret), secret.GetType());
     }
 }
Пример #6
0
        private void TestInvalidConfiguration()
        {
            var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "secureHeapEngine", "openssl11" }  // Note the missing "required" settings
            }).Build();

            Debug.WriteLine("ProtectedMemorySecretFactoryTest.TestInvalidConfiguration");
            using (var factory = new ProtectedMemorySecretFactory(configuration))
            {
            }
        }
Пример #7
0
        private void TestMmapConfiguration()
        {
            var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "secureHeapEngine", "mmap" }
            }).Build();

            Debug.WriteLine("ProtectedMemorySecretFactoryTest.TestMmapConfiguration");
            using (var factory = new ProtectedMemorySecretFactory(configuration))
            {
            }
        }
Пример #8
0
        private void TestOpenSSLConfiguration()
        {
            Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
            var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "secureHeapEngine", "openssl11" }
            }).Build();

            Debug.WriteLine("ProtectedMemorySecretFactoryTest.TestOpenSSLConfiguration");
            using (var factory = new ProtectedMemorySecretFactory(configuration))
            {
            }
        }
Пример #9
0
        private void TestInvalidConfiguration()
        {
            var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>()
            {
                { "secureHeapEngine", "magic-heap-engine2" }
            }).Build();

            Debug.WriteLine("ProtectedMemorySecretFactoryTest.TestMmapConfiguration");
            Assert.Throws <PlatformNotSupportedException>(() =>
            {
                using (var factory = new ProtectedMemorySecretFactory(configuration))
                {
                }
            });
        }
Пример #10
0
        private void TestWithSecretBytesMultiThreadedAccess()
        {
            Debug.WriteLine("TestWithSecretBytesMultiThreadedAccess start");
            using (ISecretFactory secretFactory = new ProtectedMemorySecretFactory(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");
        }
Пример #11
0
        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 ProtectedMemorySecretFactory(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);
            }
        }
 public ProtectedMemorySecretFactoryTest()
 {
     protectedMemorySecretFactory = new ProtectedMemorySecretFactory();
 }