static async Task <IClientCredentials> GetClientCredentials(string iotHubHostName, string deviceId, string userName, string token, bool isCertAuthAllowed = false, string productInfo = "", X509Certificate2 certificate = null, IList <X509Certificate2> chain = null) { var authenticator = Mock.Of <IAuthenticator>(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>()) == Task.FromResult(true)); var factory = new ClientCredentialsFactory(new IdentityProvider(iotHubHostName), productInfo); var credentialIdentityProvider = new DeviceIdentityProvider(authenticator, factory, isCertAuthAllowed); if ((certificate != null) && (chain != null)) { credentialIdentityProvider.RemoteCertificateValidationCallback(certificate, chain); } ProtocolGateway.Identity.IDeviceIdentity deviceIdentity = await credentialIdentityProvider.GetAsync(deviceId, userName, token, null); Assert.NotNull(deviceIdentity); IClientCredentials clientCredentials = (deviceIdentity as ProtocolGatewayIdentity)?.ClientCredentials; return(clientCredentials); }
public async Task GetDeviceIdentityTest( string iotHubHostName, string clientId, string username, string password, X509Certificate2 certificate, IList <X509Certificate2> chain, bool authRetVal, Type expectedType) { var authenticator = new Mock <IAuthenticator>(); authenticator.Setup(a => a.AuthenticateAsync(It.IsAny <IClientCredentials>())).ReturnsAsync(authRetVal); DeviceIdentityProvider deviceIdentityProvider = new DeviceIdentityProvider(authenticator.Object, new ClientCredentialsFactory(new IdentityProvider(iotHubHostName)), true); if (certificate != null) { deviceIdentityProvider.RemoteCertificateValidationCallback(certificate, chain); } IDeviceIdentity deviceIdentity = await deviceIdentityProvider.GetAsync(clientId, username, password, null); Assert.IsAssignableFrom(expectedType, deviceIdentity); }