public NetworkingModule Build() { if (IdProvider == null) { IdProvider = new IdProvider(); } if (NetworkingEntityFactory == null) { NetworkingEntityFactory = new NetworkingEntityFactory(); } if (TokenProvider == null) { TokenProvider = new ClientTokenProvider(); } if (NetworkingClientFactory == null) { NetworkingClientFactory = new NetworkingClientFactory(); } if (AuthenticationProviderFactory == null) { AuthenticationProviderFactory = new AuthenticationProviderFactory(); } if (StreamingHandlerFactory == null) { StreamingHandlerFactory = new ClientEntityStreamingHandlerFactory(); } return(new NetworkingModule(Ip, Port, IdProvider, NetworkingEntityFactory, TokenProvider, NetworkingClientFactory, AuthenticationProviderFactory, StreamingHandlerFactory)); }
public async Task GetDeviceTokenTest() { // Arrange ISignatureProvider signatureProvider = new TestSignatureProvider(); string iotHubHostName = "testIoThub"; string deviceId = "testDeviceId"; ITokenProvider tokenProvider = new ClientTokenProvider(signatureProvider, iotHubHostName, deviceId, TimeSpan.FromHours(1)); var keys = new[] { "sr", "sig", "se" }; // Act string token = await tokenProvider.GetTokenAsync(Option.None <TimeSpan>()); // Assert Assert.NotNull(token); string[] lines = token.Split(); Assert.Equal(2, lines.Length); Assert.Equal("SharedAccessSignature", lines[0]); string[] parts = lines[1].Split('&'); Assert.Equal(3, parts.Length); foreach (string part in parts) { string[] kvp = part.Split("="); Assert.True(keys.Contains(kvp[0])); Assert.NotNull(kvp[1]); } }
async Task <ICloudProxy> GetCloudProxyFromConnectionStringKey(string connectionStringConfigKey, IEdgeHub edgeHub) { const int ConnectionPoolSize = 10; string deviceConnectionString = await SecretsHelper.GetSecretFromConfigKey(connectionStringConfigKey); string deviceId = ConnectionStringHelper.GetDeviceId(deviceConnectionString); string iotHubHostName = ConnectionStringHelper.GetHostName(deviceConnectionString); string sasKey = ConnectionStringHelper.GetSharedAccessKey(deviceConnectionString); var converters = new MessageConverterProvider( new Dictionary <Type, IMessageConverter>() { { typeof(Message), new DeviceClientMessageConverter() }, { typeof(Twin), new TwinMessageConverter() }, { typeof(TwinCollection), new TwinCollectionMessageConverter() } }); var credentialsCache = Mock.Of <ICredentialsCache>(); var metadataStore = new Mock <IMetadataStore>(); metadataStore.Setup(m => m.GetMetadata(It.IsAny <string>())).ReturnsAsync(new ConnectionMetadata("dummyValue")); ICloudConnectionProvider cloudConnectionProvider = new CloudConnectionProvider( converters, ConnectionPoolSize, new ClientProvider(Option.None <string>()), Option.None <UpstreamProtocol>(), Mock.Of <Util.ITokenProvider>(), Mock.Of <IDeviceScopeIdentitiesCache>(), credentialsCache, Mock.Of <IIdentity>(i => i.Id == $"{deviceId}/$edgeHub"), TimeSpan.FromMinutes(60), true, TimeSpan.FromSeconds(20), false, Option.None <IWebProxy>(), metadataStore.Object, true, true); cloudConnectionProvider.BindEdgeHub(edgeHub); var clientTokenProvider = new ClientTokenProvider(new SharedAccessKeySignatureProvider(sasKey), iotHubHostName, deviceId, TimeSpan.FromHours(1)); string token = await clientTokenProvider.GetTokenAsync(Option.None <TimeSpan>()); var deviceIdentity = new DeviceIdentity(iotHubHostName, deviceId); var clientCredentials = new TokenCredentials(deviceIdentity, token, string.Empty, Option.None <string>(), Option.None <string>(), false); Try <ICloudConnection> cloudConnection = await cloudConnectionProvider.Connect(clientCredentials, (_, __) => { }); Assert.True(cloudConnection.Success); Assert.True(cloudConnection.Value.IsActive); Assert.True(cloudConnection.Value.CloudProxy.HasValue); return(cloudConnection.Value.CloudProxy.OrDefault()); }
public async Task GetModuleTokenTestCacheTest() { // Arrange var signatureProvider = new Mock <ISignatureProvider>(); signatureProvider.Setup(s => s.SignAsync(It.IsAny <string>())).Returns(Task.FromResult(Guid.NewGuid().ToString())); string iotHubHostName = "testIoThub"; string deviceId = "testDeviceId"; string moduleId = "$edgeHub"; ITokenProvider tokenProvider = new ClientTokenProvider(signatureProvider.Object, iotHubHostName, deviceId, moduleId, TimeSpan.FromSeconds(30)); // Act string token = await tokenProvider.GetTokenAsync(Option.None <TimeSpan>()); // Assert Assert.NotNull(token); signatureProvider.Verify(s => s.SignAsync(It.IsAny <string>()), Times.Once); // Act var tasks = new List <Task <string> >(); for (int i = 0; i < 5; i++) { tasks.Add(tokenProvider.GetTokenAsync(Option.None <TimeSpan>())); } string[] tokens = await Task.WhenAll(tasks); // Assert Assert.NotNull(tokens); Assert.Equal(5, tokens.Length); for (int i = 0; i < 5; i++) { Assert.Equal(token, tokens[i]); } signatureProvider.Verify(s => s.SignAsync(It.IsAny <string>()), Times.Once); await Task.Delay(TimeSpan.FromSeconds(15)); // Act string newToken = await tokenProvider.GetTokenAsync(Option.None <TimeSpan>()); // Assert Assert.NotNull(newToken); Assert.NotEqual(token, newToken); signatureProvider.Verify(s => s.SignAsync(It.IsAny <string>()), Times.AtMost(2)); }