Exemplo n.º 1
0
        public async Task BatchTest()
        {
            // Arrange
            IEncryptionProvider                 encryptionProvider = new TestEncryptionProvider();
            IEntityStore <string, string>       entityStore        = GetEntityStore <string, string>("smokeTest");
            IKeyValueStore <string, TestDevice> encryptedStore     = new UpdatableEncryptedStore <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);
        }
Exemplo n.º 2
0
        public async Task SmokeTest()
        {
            // Arrange
            IEncryptionProvider                 encryptionProvider = new TestEncryptionProvider();
            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);

            Option <string> storedValue = await entityStore.Get("device1");

            Assert.True(storedValue.HasValue);

            string deviceJson          = JsonConvert.SerializeObject(device);
            string encryptedDeviceJson = Convert.ToBase64String(Encoding.UTF8.GetBytes(deviceJson));

            Assert.Equal(encryptedDeviceJson, storedValue.OrDefault());

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

            Assert.False(retrievedValue.HasValue);

            await encryptedStore.Remove("device1");

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

            Assert.False(contains);

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

            Assert.False(retrievedValue.HasValue);
        }
Exemplo n.º 3
0
        public async Task SmokeTest()
        {
            // Arrange
            IEncryptionProvider                 encryptionProvider = new TestEncryptionProvider();
            IEntityStore <string, string>       entityStore        = GetEntityStore <string, string>("smokeTest");
            IKeyValueStore <string, TestDevice> encryptedStore     = new UpdatableEncryptedStore <string, TestDevice>(entityStore, encryptionProvider);
            string key       = "device1";
            var    device    = new TestDevice(Guid.NewGuid().ToString(), new KeyAuth(Guid.NewGuid().ToString()));
            var    deviceNew = new TestDevice(Guid.NewGuid().ToString(), new KeyAuth(Guid.NewGuid().ToString()));

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

            Assert.False(contains);
            contains = await entityStore.Contains("device1");

            Assert.False(contains);

            // Add a value to the underlying store and make sure encrypted store returns it correctly
            await entityStore.Put(key, device.ToJson());

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

            Assert.True(contains);
            contains = await entityStore.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);

            Option <string> storedValue = await entityStore.Get("device1");

            Assert.True(storedValue.HasValue);
            TestDevice storedTestDevice = storedValue.OrDefault().FromJson <TestDevice>();

            Assert.Equal(device.GenId, storedTestDevice.GenId);
            Assert.Equal(device.Auth.Key, storedTestDevice.Auth.Key);

            // Add a value to the encrypted store and make sure underlying store sees the encrypted value
            await encryptedStore.Put(key, deviceNew);

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

            Assert.True(contains);

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

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

            storedValue = await entityStore.Get("device1");

            Assert.True(storedValue.HasValue);

            string encryptedDeviceJson = Convert.ToBase64String(Encoding.UTF8.GetBytes(deviceNew.ToJson()));
            var    encryptedData       = new UpdatableEncryptedStore <string, TestDevice> .EncryptedData(true, encryptedDeviceJson);

            Assert.Equal(encryptedData.ToJson(), storedValue.OrDefault());

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

            Assert.False(retrievedValue.HasValue);

            await encryptedStore.Remove("device1");

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

            Assert.False(contains);

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

            Assert.False(retrievedValue.HasValue);
        }