public void AzureCertificateTest() { StartTest(MethodBase.GetCurrentMethod().Name, testStartTime); // Certificate files to test string cerFileName = Convert.ToString(TestContext.DataRow["cerFileName"]); string pfxFileName = Convert.ToString(TestContext.DataRow["pfxFileName"]); string password = Convert.ToString(TestContext.DataRow["password"]); string thumbprintAlgorithm = Convert.ToString(TestContext.DataRow["algorithm"]); // Create a certificate X509Certificate2 certCreated = Utilities.CreateCertificate(password); byte[] certData = certCreated.Export(X509ContentType.Pfx, password); File.WriteAllBytes(pfxFileName, certData); byte[] certData2 = certCreated.Export(X509ContentType.Cert); File.WriteAllBytes(cerFileName, certData2); // Install the .cer file to local machine. StoreLocation certStoreLocation = StoreLocation.CurrentUser; StoreName certStoreName = StoreName.My; X509Certificate2 installedCert = Utilities.InstallCert(cerFileName, certStoreLocation, certStoreName); // Certificate1: get it from the installed certificate. PSObject cert1 = vmPowershellCmdlets.RunPSScript( String.Format("Get-Item cert:\\{0}\\{1}\\{2}", certStoreLocation.ToString(), certStoreName.ToString(), installedCert.Thumbprint))[0]; string cert1data = Convert.ToBase64String(((X509Certificate2)cert1.BaseObject).RawData); // Certificate2: get it from .pfx file. X509Certificate2Collection cert2 = new X509Certificate2Collection(); cert2.Import(pfxFileName, password, X509KeyStorageFlags.PersistKeySet); string cert2data = Convert.ToBase64String(cert2[0].RawData); // Certificate3: get it from .cer file. X509Certificate2Collection cert3 = new X509Certificate2Collection(); cert3.Import(cerFileName); string cert3data = Convert.ToBase64String(cert3[0].RawData); try { RemoveAllExistingCerts(defaultService); // Add a cert item vmPowershellCmdlets.AddAzureCertificate(defaultService, cert1); CertificateContext getCert1 = vmPowershellCmdlets.GetAzureCertificate(defaultService)[0]; Console.WriteLine("Cert is added: {0}", getCert1.Thumbprint); Assert.AreEqual(getCert1.Data, cert1data, "Cert is different!!"); vmPowershellCmdlets.RemoveAzureCertificate(defaultService, getCert1.Thumbprint, thumbprintAlgorithm); pass = Utilities.CheckRemove(vmPowershellCmdlets.GetAzureCertificate, defaultService, getCert1.Thumbprint, thumbprintAlgorithm); // Add .pfx file vmPowershellCmdlets.AddAzureCertificate(defaultService, pfxFileName, password); CertificateContext getCert2 = vmPowershellCmdlets.GetAzureCertificate(defaultService, cert2[0].Thumbprint, thumbprintAlgorithm)[0]; Console.WriteLine("Cert is added: {0}", cert2[0].Thumbprint); Assert.AreEqual(getCert2.Data, cert2data, "Cert is different!!"); vmPowershellCmdlets.RemoveAzureCertificate(defaultService, cert2[0].Thumbprint, thumbprintAlgorithm); pass &= Utilities.CheckRemove(vmPowershellCmdlets.GetAzureCertificate, defaultService, cert2[0].Thumbprint, thumbprintAlgorithm); // Add .cer file vmPowershellCmdlets.AddAzureCertificate(defaultService, cerFileName); CertificateContext getCert3 = vmPowershellCmdlets.GetAzureCertificate(defaultService, cert3[0].Thumbprint, thumbprintAlgorithm)[0]; Console.WriteLine("Cert is added: {0}", cert3[0].Thumbprint); Assert.AreEqual(getCert3.Data, cert3data, "Cert is different!!"); vmPowershellCmdlets.RemoveAzureCertificate(defaultService, cert3[0].Thumbprint, thumbprintAlgorithm); pass &= Utilities.CheckRemove(vmPowershellCmdlets.GetAzureCertificate, defaultService, cert3[0].Thumbprint, thumbprintAlgorithm); } catch (Exception e) { pass = false; Assert.Fail(e.ToString()); } finally { Utilities.UninstallCert(installedCert, certStoreLocation, certStoreName); RemoveAllExistingCerts(defaultService); } }