public async Task StoresMetadataCorrectly() { (var authenticator, _, var usernameParser, var credFactory, var sysIdProvider) = SetupAcceptEverything(); var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("productInfo"); var metadataStore = new MetadataStore(store, "productInfo"); string modelIdString = "dtmi:test:modelId;1"; using (var sut = new AuthAgentProtocolHead(authenticator, metadataStore, usernameParser, credFactory, sysIdProvider, config)) { await sut.StartAsync(); dynamic content = new ExpandoObject(); content.version = "2020-04-20"; content.username = $"testhub/device/api-version=2018-06-30&model-id={modelIdString}"; // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Synthetic password used in tests")] content.password = "******"; dynamic response = await PostAsync(content, this.url); Assert.Equal(200, (int)response.result); var modelId = (await metadataStore.GetMetadata("device")).ModelId; Assert.True(modelId.HasValue); Assert.Equal(modelIdString, modelId.GetOrElse("impossibleValue")); } }
public async Task SetModelIdTest() { // Arrange string modelId = "dtmi:test:modelId;1"; string clientVersion = $"TestClientVersion{Guid.NewGuid().ToString()}"; var identity = Mock.Of <IDeviceIdentity>(i => i.Id == "d1" && i.DeviceId == "d1"); var deviceListener = Mock.Of <IDeviceListener>(); var connectionHandler = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener)); var amqpAuthenticator = new Mock <IAmqpAuthenticator>(); amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1", Option.Some(modelId))).ReturnsAsync(true); Mock <ICbsNode> cbsNodeMock = amqpAuthenticator.As <ICbsNode>(); ICbsNode cbsNode = cbsNodeMock.Object; var amqpConnection = Mock.Of <IAmqpConnection>( c => c.FindExtension <IConnectionHandler>() == connectionHandler && c.FindExtension <ICbsNode>() == cbsNode); var amqpSession = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection); var amqpLink = Mock.Of <IReceivingAmqpLink>(l => l.Session == amqpSession && l.IsReceiver && l.Settings == new AmqpLinkSettings() && l.State == AmqpObjectState.Opened); var linkSettings = new AmqpLinkSettings { Properties = new Fields() { { IotHubAmqpProperty.ClientVersion, clientVersion }, { IotHubAmqpProperty.ModelId, modelId } } }; Mock.Get(amqpLink).SetupGet(l => l.Settings).Returns(linkSettings); var requestUri = new Uri("amqps://foo.bar/devices/d1/messages/events"); var boundVariables = new Dictionary <string, string> { { "deviceid", "d1" } }; var messageConverter = new AmqpMessageConverter(); var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("modelId"); var metadataStore = new MetadataStore(store, "testProductInfo"); ILinkHandler linkHandler = new EventsLinkHandler(identity, amqpLink, requestUri, boundVariables, connectionHandler, messageConverter, metadataStore); // Act await linkHandler.OpenAsync(TimeSpan.FromSeconds(1)); // Assert ConnectionMetadata connectionMetadata = await metadataStore.GetMetadata(identity.Id); Option <string> modelIdFromStore = connectionMetadata.ModelId; Assert.True(modelIdFromStore.HasValue); modelIdFromStore.ForEach(m => Assert.Equal(modelId, m)); }
public async Task ProductInfoMigrationTest() { string testProductInfo = "testProductInfo"; var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); Mock <IEntityStore <string, string> > store = new Mock <IEntityStore <string, string> >(); store.Setup(m => m.Get(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(testProductInfo))); var metadataStore = new MetadataStore(store.Object, this.dummyProductInfo); string productInfo = (await metadataStore.GetMetadata("id")).ProductInfo; Assert.Equal(testProductInfo, productInfo); }
public async Task SetMetadataSmokeTest() { // Arrange string edgeProductInfo = "IoTEdge 1.0.7"; var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("connectionMetadata"); var metadataStore = new MetadataStore(store, edgeProductInfo); string id = "testId"; string productInfo = "testProductInfo"; string productInfo2 = "testProductInfo2"; string modelId = "testModelId"; string modelId2 = "testModelId"; // Act await metadataStore.SetMetadata(id, productInfo, Option.Some(modelId)); ConnectionMetadata connectionMetadata = await metadataStore.GetMetadata(id); // Assert Assert.True(connectionMetadata.ModelId.HasValue); connectionMetadata.ModelId.ForEach(m => Assert.Equal(modelId, m)); Assert.Equal(productInfo, connectionMetadata.ProductInfo); // Act await metadataStore.SetModelId(id, modelId2); connectionMetadata = await metadataStore.GetMetadata(id); // Assert Assert.True(connectionMetadata.ModelId.HasValue); connectionMetadata.ModelId.ForEach(m => Assert.Equal(modelId2, m)); // Act await metadataStore.SetProductInfo(id, productInfo2); connectionMetadata = await metadataStore.GetMetadata(id); // Assert Assert.Equal(productInfo2, connectionMetadata.ProductInfo); }
public async Task ModelIdMigrationTest() { string testProductInfo = "testProductInfo"; string id = "id"; var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); Mock <IEntityStore <string, string> > store = new Mock <IEntityStore <string, string> >(); store.Setup(m => m.Get(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(testProductInfo))); var metadataStore = new MetadataStore(store.Object, this.dummyProductInfo); ConnectionMetadata connectionMetadata = await metadataStore.GetMetadata(id); Assert.Equal(testProductInfo, connectionMetadata.ProductInfo); Assert.False(connectionMetadata.ModelId.HasValue); }
public async Task NoProductInfoTest() { // Arrange string edgeProductInfo = "IoTEdge 1.0.7"; var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("connectionMetadata"); var metadataStore = new MetadataStore(store, edgeProductInfo); // Act string productInfoValue = (await metadataStore.GetMetadata("id")).ProductInfo; // Assert Assert.Equal(string.Empty, productInfoValue); }
public async Task WhitespaceModelIdTest() { // Arrange var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("connectionMetadata"); var metadataStore = new MetadataStore(store, this.dummyProductInfo); // Act await metadataStore.SetModelId("id", " "); Option <string> modelId = (await metadataStore.GetMetadata("id")).ModelId; // Assert Assert.False(modelId.HasValue); }
public async Task SmokeTest() { // Arrange string edgeProductInfo = "IoTEdge 1.0.7"; var storeProvider = new StoreProvider(new InMemoryDbStoreProvider()); IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("connectionMetadata"); var metadataStore = new MetadataStore(store, edgeProductInfo); var deviceProductInfos = new Dictionary <string, string> { ["d1"] = Guid.NewGuid().ToString(), ["d2"] = Guid.NewGuid().ToString(), ["d3"] = Guid.NewGuid().ToString(), ["d3/m1"] = Guid.NewGuid().ToString(), ["d3/m2"] = Guid.NewGuid().ToString() }; var deviceToModelIds = new Dictionary <string, string> { ["d1"] = "dtmi:example:capabailityModels:MXChip;1", ["d2"] = "dtmi:example2:capabailityModels:MXChip;1", ["d3"] = "dtmi:example3:capabailityModels:MXChip;1", ["d3/m1"] = "dtmi:example4:capabailityModels:MXChip;1", ["d3/m2"] = "dtmi:example5:capabailityModels:MXChip;1" }; // Act foreach (KeyValuePair <string, string> kvp in deviceProductInfos) { await metadataStore.SetProductInfo(kvp.Key, kvp.Value); } foreach (KeyValuePair <string, string> kvp in deviceToModelIds) { await metadataStore.SetModelId(kvp.Key, kvp.Value); } var receivedDeviceInfos = new Dictionary <string, string>(); var receivedEdgeDeviceInfos = new Dictionary <string, string>(); var receivedDeviceModelIds = new Dictionary <string, string>(); foreach (string id in deviceProductInfos.Keys) { ConnectionMetadata connectionMetadata = await metadataStore.GetMetadata(id); string productInfo = connectionMetadata.ProductInfo; string deviceEdgeProductInfo = connectionMetadata.EdgeProductInfo; receivedDeviceInfos.Add(id, productInfo); receivedEdgeDeviceInfos.Add(id, deviceEdgeProductInfo); } foreach (string id in deviceToModelIds.Keys) { ConnectionMetadata connectionMetadata = await metadataStore.GetMetadata(id); Option <string> modelId = connectionMetadata.ModelId; modelId.ForEach(m => receivedDeviceModelIds.Add(id, m)); } // Assert Assert.Equal(deviceProductInfos.Count, receivedDeviceInfos.Count); Assert.Equal(deviceProductInfos.Count, receivedEdgeDeviceInfos.Count); Assert.Equal(deviceToModelIds.Count, receivedDeviceModelIds.Count); foreach (KeyValuePair <string, string> kvp in deviceProductInfos) { Assert.Equal(kvp.Value, receivedDeviceInfos[kvp.Key]); Assert.Equal($"{kvp.Value} {edgeProductInfo}", receivedEdgeDeviceInfos[kvp.Key]); } foreach (KeyValuePair <string, string> kvp in deviceToModelIds) { Assert.Equal(kvp.Value, receivedDeviceModelIds[kvp.Key]); } }