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, false);

            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 PersistedTokenCredentialsCache(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);
            Assert.Equal(credentials.IsUpdatable, storedTokenCredentials.IsUpdatable);
        }
        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 PersistedTokenCredentialsCache(encryptedStore);

            // Act
            await tokenCredentialsStore.Add(credentials);

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

            // Assert
            Assert.False(storedCredentials.HasValue);
        }
Beispiel #3
0
        public async Task RoundtripNonTokenCredentialsTest()
        {
            // Arrange
            string callerProductInfo = "productInfo";
            var    identity          = Mock.Of <IIdentity>(i => i.Id == "d1");
            var    clientCertificate = Util.Test.Common.CertificateHelper.GenerateSelfSignedCert("client cert");
            var    clientCertChain   = new List <X509Certificate2>();
            var    credentials       = new X509CertCredentials(identity, callerProductInfo, Option.None <string>(), Option.None <string>(), clientCertificate, clientCertChain);

            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 PersistedTokenCredentialsCache(encryptedStore);

            // Act
            await tokenCredentialsStore.Add(credentials);

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

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