Example #1
0
        public async Task KeyResetTest()
        {
            // Arrange
            var encryptionProvider = new ResettableEncryptionProvider();
            IEntityStore <string, string>       entityStore    = GetEntityStore <string, string>("smokeTest");
            IKeyValueStore <string, TestDevice> encryptedStore = new EncryptedStore <string, TestDevice>(entityStore, encryptionProvider);
            string key    = "device1";
            var    device = new TestDevice(Guid.NewGuid().ToString(), new KeyAuth(Guid.NewGuid().ToString()));

            // Act / Assert
            bool contains = await encryptedStore.Contains("device1");

            Assert.False(contains);

            await encryptedStore.Put(key, device);

            contains = await encryptedStore.Contains("device1");

            Assert.True(contains);

            Option <TestDevice> retrievedValue = await encryptedStore.Get("device1");

            Assert.True(retrievedValue.HasValue);
            Assert.Equal(device.GenId, retrievedValue.OrDefault().GenId);
            Assert.Equal(device.Auth.Key, retrievedValue.OrDefault().Auth.Key);

            encryptionProvider.Reset();

            retrievedValue = await encryptedStore.Get("device1");

            Assert.False(retrievedValue.HasValue);

            contains = await encryptedStore.Contains("device1");

            Assert.False(contains);

            await encryptedStore.Put(key, device);

            contains = await encryptedStore.Contains("device1");

            Assert.True(contains);

            retrievedValue = await encryptedStore.Get("device1");

            Assert.True(retrievedValue.HasValue);
            Assert.Equal(device.GenId, retrievedValue.OrDefault().GenId);
            Assert.Equal(device.Auth.Key, retrievedValue.OrDefault().Auth.Key);
        }
Example #2
0
        public async Task BatchWithKeyResetTest()
        {
            // Arrange
            var encryptionProvider = new ResettableEncryptionProvider();
            IEntityStore <string, string>       entityStore    = GetEntityStore <string, string>("smokeTest");
            IKeyValueStore <string, TestDevice> encryptedStore = new EncryptedStore <string, TestDevice>(entityStore, encryptionProvider);
            IDictionary <string, TestDevice>    devices        = new Dictionary <string, TestDevice>();

            for (int i = 0; i < 10; i++)
            {
                devices[$"d{i}"] = new TestDevice(Guid.NewGuid().ToString(), new KeyAuth(Guid.NewGuid().ToString()));
            }

            // Act
            foreach (KeyValuePair <string, TestDevice> device in devices)
            {
                await encryptedStore.Put(device.Key, device.Value);
            }

            IDictionary <string, TestDevice> obtainedDevices = new Dictionary <string, TestDevice>();
            await encryptedStore.IterateBatch(
                10,
                (key, device) =>
            {
                obtainedDevices[key] = device;
                return(Task.CompletedTask);
            });

            // Assert
            Assert.Equal(devices.Count, obtainedDevices.Count);

            foreach (KeyValuePair <string, TestDevice> device in devices)
            {
                Assert.Equal(device.Value.GenId, obtainedDevices[device.Key].GenId);
                Assert.Equal(device.Value.Auth.Key, obtainedDevices[device.Key].Auth.Key);
            }

            // Act
            Option <(string key, TestDevice value)> first = await encryptedStore.GetFirstEntry();

            Option <(string key, TestDevice value)> last = await encryptedStore.GetLastEntry();

            // Assert
            Assert.True(first.HasValue);
            Assert.True(last.HasValue);

            Assert.Equal("d0", first.OrDefault().key);
            Assert.Equal(devices["d0"].GenId, first.OrDefault().value.GenId);
            Assert.Equal("d9", last.OrDefault().key);
            Assert.Equal(devices["d9"].GenId, last.OrDefault().value.GenId);

            // Act
            encryptionProvider.Reset();

            obtainedDevices = new Dictionary <string, TestDevice>();
            await encryptedStore.IterateBatch(
                10,
                (key, device) =>
            {
                obtainedDevices[key] = device;
                return(Task.CompletedTask);
            });

            // Assert
            Assert.Equal(0, obtainedDevices.Count);

            // Act
            first = await encryptedStore.GetFirstEntry();

            last = await encryptedStore.GetLastEntry();

            // Assert
            Assert.False(first.HasValue);
            Assert.False(last.HasValue);

            // Act
            foreach (KeyValuePair <string, TestDevice> device in devices)
            {
                await encryptedStore.Put(device.Key, device.Value);
            }

            obtainedDevices = new Dictionary <string, TestDevice>();
            await encryptedStore.IterateBatch(
                10,
                (key, device) =>
            {
                obtainedDevices[key] = device;
                return(Task.CompletedTask);
            });

            // Assert
            Assert.Equal(devices.Count, obtainedDevices.Count);

            foreach (KeyValuePair <string, TestDevice> device in devices)
            {
                Assert.Equal(device.Value.GenId, obtainedDevices[device.Key].GenId);
                Assert.Equal(device.Value.Auth.Key, obtainedDevices[device.Key].Auth.Key);
            }

            // Act
            first = await encryptedStore.GetFirstEntry();

            last = await encryptedStore.GetLastEntry();

            // Assert
            Assert.True(first.HasValue);
            Assert.True(last.HasValue);

            Assert.Equal("d0", first.OrDefault().key);
            Assert.Equal(devices["d0"].GenId, first.OrDefault().value.GenId);
            Assert.Equal("d9", last.OrDefault().key);
            Assert.Equal(devices["d9"].GenId, last.OrDefault().value.GenId);
        }