public async Task AutoSave_WhenInterval0_DoesNotAutoSave()
        {
            // Arrange
            var path     = Path.GetTempFileName() + ".json";
            var pk       = new byte[] { 0, 1, 2, 3, 4 };
            var instance = new FileSystemKeyStore(path);

            instance.PinForHost("jmaxxz.com", pk);

            // Act
            instance.AutoSaveInterval = TimeSpan.FromSeconds(0);
            await Task.Delay(2000);

            // Assert
            Assert.IsFalse(File.Exists(path));
        }
        public void Save_WritesConfigurationToDist()
        {
            // Arrange
            var path     = Path.GetTempFileName() + ".json";
            var pk       = new byte[] { 0, 1, 2, 3, 4 };
            var instance = new FileSystemKeyStore(path);

            instance.PinForHost("jmaxxz.com", pk);

            // Act
            instance.Save();

            // Assert
            var secondInstance = new FileSystemKeyStore(path);

            secondInstance.Reload();
            Assert.IsTrue(secondInstance.MatchesExisting("jmaxxz.com", pk));
        }
        public async Task AutoSave_OnInterval_CommitsToDisk()
        {
            // Arrange
            var path     = Path.GetTempFileName() + ".json";
            var pk       = new byte[] { 0, 1, 2, 3, 4 };
            var instance = new FileSystemKeyStore(path);

            instance.PinForHost("jmaxxz.com", pk);

            // Act
            instance.AutoSaveInterval = TimeSpan.FromSeconds(0.5);
            await Task.Delay(2000);

            // Assert
            var secondInstance = new FileSystemKeyStore(path);

            secondInstance.Reload();
            Assert.IsTrue(secondInstance.MatchesExisting("jmaxxz.com", pk));
        }
        public void Reload_ReadsConfigurationFromDisk()
        {
            // Arrange
            var path     = Path.GetTempFileName() + ".json";
            var existing = new List <HostKeyPair>()
            {
                new HostKeyPair {
                    Host = "example0", PublicKey = new byte[] { 0, 0, 0 }
                },
                new HostKeyPair {
                    Host = "example1", PublicKey = new byte[] { 1, 1, 1 }
                },
                new HostKeyPair {
                    Host = "example2", PublicKey = new byte[] { 2, 2, 2 }
                },
            };

            var instance   = new FileSystemKeyStore(path);
            var serializer = new DataContractJsonSerializer(typeof(List <HostKeyPair>));

            using (var fileStream = File.OpenWrite(path))
            {
                serializer.WriteObject(fileStream, existing);
            }


            instance.PinForHost("example3", new byte[] { 3, 3, 3 });

            // Act
            instance.Reload();

            // Assert
            Assert.IsTrue(instance.MatchesExisting(existing[0].Host, existing[0].PublicKey));
            Assert.IsTrue(instance.MatchesExisting(existing[1].Host, existing[1].PublicKey));
            Assert.IsTrue(instance.MatchesExisting(existing[2].Host, existing[2].PublicKey));
            Assert.IsFalse(instance.MatchesExisting("example3", new byte[] { 3, 3, 3 }));
        }