public void GetX509IdentityTest()
        {
            string iothubHostName    = "iothub1.azure.net";
            string callerProductInfo = "productInfo";
            string deviceId          = "device1";
            string moduleId          = "module1";
            string deviceClientType  = "customDeviceClient1";
            var    identityFactory   = new ClientCredentialsFactory(iothubHostName, callerProductInfo);

            // device test
            IClientCredentials identityTry1 = identityFactory.GetWithX509Cert(deviceId, null, deviceClientType);

            Assert.IsType <DeviceIdentity>(identityTry1.Identity);
            Assert.Equal("device1", identityTry1.Identity.Id);
            Assert.Equal($"{callerProductInfo} customDeviceClient1", identityTry1.ProductInfo);
            Assert.Equal(AuthenticationType.X509Cert, identityTry1.AuthenticationType);

            // module test
            IClientCredentials identityTry2 = identityFactory.GetWithX509Cert(deviceId, moduleId, deviceClientType);

            Assert.IsType <ModuleIdentity>(identityTry2.Identity);
            Assert.Equal("device1/module1", identityTry2.Identity.Id);
            Assert.Equal($"{callerProductInfo} customDeviceClient1", identityTry2.ProductInfo);
            Assert.Equal(AuthenticationType.X509Cert, identityTry1.AuthenticationType);
        }
Example #2
0
        public void GetX509IdentityTest()
        {
            string iothubHostName    = "iothub1.azure.net";
            string callerProductInfo = "productInfo";
            string deviceId          = "device1";
            string moduleId          = "module1";
            string deviceClientType  = "customDeviceClient1";
            var    clientCertificate = TestCertificateHelper.GenerateSelfSignedCert("some_cert");
            var    clientCertChain   = new List <X509Certificate2>()
            {
                clientCertificate
            };
            Option <string> modelId   = Option.Some("modelId1");
            Option <string> authChain = Option.Some("device1;edge1");

            var identityFactory = new ClientCredentialsFactory(new IdentityProvider(iothubHostName), callerProductInfo);

            // device test
            IClientCredentials identityTry1 = identityFactory.GetWithX509Cert(deviceId, null, deviceClientType, clientCertificate, clientCertChain, modelId, authChain);

            Assert.IsType <DeviceIdentity>(identityTry1.Identity);
            Assert.IsType <X509CertCredentials>(identityTry1);
            Assert.Equal(clientCertificate, (identityTry1 as ICertificateCredentials)?.ClientCertificate);
            Assert.Equal(clientCertChain, (identityTry1 as ICertificateCredentials)?.ClientCertificateChain);
            Assert.Equal("device1", identityTry1.Identity.Id);
            Assert.Equal($"customDeviceClient1 {callerProductInfo}", identityTry1.ProductInfo);
            Assert.Equal(AuthenticationType.X509Cert, identityTry1.AuthenticationType);
            Assert.Equal(modelId, identityTry1.ModelId);
            Assert.Equal(authChain, identityTry1.AuthChain);

            // module test
            IClientCredentials identityTry2 = identityFactory.GetWithX509Cert(deviceId, moduleId, deviceClientType, clientCertificate, clientCertChain, modelId, authChain);

            Assert.IsType <ModuleIdentity>(identityTry2.Identity);
            Assert.IsType <X509CertCredentials>(identityTry2);
            Assert.Equal(clientCertificate, (identityTry2 as ICertificateCredentials)?.ClientCertificate);
            Assert.Equal(clientCertChain, (identityTry2 as ICertificateCredentials)?.ClientCertificateChain);
            Assert.Equal("device1/module1", identityTry2.Identity.Id);
            Assert.Equal($"customDeviceClient1 {callerProductInfo}", identityTry2.ProductInfo);
            Assert.Equal(AuthenticationType.X509Cert, identityTry1.AuthenticationType);
            Assert.Equal(modelId, identityTry2.ModelId);
            Assert.Equal(authChain, identityTry2.AuthChain);
        }