Пример #1
0
        public async Task RoundtripTokenCredentialsWithEncryptionTest()
        {
            // Arrange
            string iothubHostName    = "iothub1.azure.net";
            string callerProductInfo = "productInfo";
            string sasToken          = TokenHelper.CreateSasToken($"{iothubHostName}/devices/device1/modules/moduleId");
            var    identity          = Mock.Of <IIdentity>(i => i.Id == "d1");
            var    credentials       = new TokenCredentials(identity, sasToken, callerProductInfo);

            var            dbStoreProvider       = new InMemoryDbStoreProvider();
            IStoreProvider storeProvider         = new StoreProvider(dbStoreProvider);
            var            encryptedStore        = new EncryptedStore <string, string>(storeProvider.GetEntityStore <string, string>("tokenCredentials"), new TestEncryptionProvider());
            var            tokenCredentialsStore = new TokenCredentialsCache(encryptedStore);

            // Act
            await tokenCredentialsStore.Add(credentials);

            Option <IClientCredentials> storedCredentials = await tokenCredentialsStore.Get(identity);

            // Assert
            Assert.True(storedCredentials.HasValue);
            var storedTokenCredentials = storedCredentials.OrDefault() as ITokenCredentials;

            Assert.NotNull(storedTokenCredentials);
            Assert.Equal(sasToken, storedTokenCredentials.Token);
        }
Пример #2
0
        public async Task RoundtripNonTokenCredentialsTest()
        {
            // Arrange
            string callerProductInfo = "productInfo";
            var    identity          = Mock.Of <IIdentity>(i => i.Id == "d1");
            var    credentials       = new X509CertCredentials(identity, callerProductInfo);

            var            dbStoreProvider       = new InMemoryDbStoreProvider();
            IStoreProvider storeProvider         = new StoreProvider(dbStoreProvider);
            var            encryptedStore        = new EncryptedStore <string, string>(storeProvider.GetEntityStore <string, string>("tokenCredentials"), new TestEncryptionProvider());
            var            tokenCredentialsStore = new TokenCredentialsCache(encryptedStore);

            // Act
            await tokenCredentialsStore.Add(credentials);

            Option <IClientCredentials> storedCredentials = await tokenCredentialsStore.Get(identity);

            // Assert
            Assert.False(storedCredentials.HasValue);
        }