/// <summary> /// Utility method to assist in making sure a set of mappings are correctly registered in CryptoConfig2 /// </summary> private static void CheckMappings(IEnumerable <Mapping> mappings) { foreach (Mapping mapping in mappings) { object algorithm = CryptoConfig2.CreateFromName(mapping.Name); Assert.IsNotNull(algorithm, "Failed to create algorithm in CryptoConfig2 for " + mapping.Name); Assert.AreEqual(mapping.ExpectedType, algorithm.GetType(), "Failed to map CryptoConfig2 for " + mapping.Name); } }
public void CryptoConfig2AddMappingTest() { List <Mapping> addedMappings = new List <Mapping>(); // Make sure that registering a class a class without any alises works CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm1)); addedMappings.Add(new Mapping { Name = "NewAlgorithm1", ExpectedType = typeof(NewAlgorithm1) }); addedMappings.Add(new Mapping { Name = "Security.Cryptography.Test.NewAlgorithm1", ExpectedType = typeof(NewAlgorithm1) }); CheckMappings(addedMappings); // Re-registering the same class with some new aliases should also work CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm1), "NA1", "NewAlg1"); addedMappings.Add(new Mapping { Name = "NA1", ExpectedType = typeof(NewAlgorithm1) }); addedMappings.Add(new Mapping { Name = "NewAlg1", ExpectedType = typeof(NewAlgorithm1) }); CheckMappings(addedMappings); // Adding alaises even once we've already added alaises should continue to work CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm1), "New-Alg-1"); addedMappings.Add(new Mapping { Name = "New-Alg-1", ExpectedType = typeof(NewAlgorithm1) }); CheckMappings(addedMappings); // Add an algorithm and some alaises at the same time, should work the same as doing the above all at once CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm2), "NA2", "NewAlg2", "New-Alg-2"); addedMappings.Add(new Mapping { Name = "NewAlgorithm2", ExpectedType = typeof(NewAlgorithm2) }); addedMappings.Add(new Mapping { Name = "Security.Cryptography.Test.NewAlgorithm2", ExpectedType = typeof(NewAlgorithm2) }); addedMappings.Add(new Mapping { Name = "NA2", ExpectedType = typeof(NewAlgorithm2) }); addedMappings.Add(new Mapping { Name = "NewAlg2", ExpectedType = typeof(NewAlgorithm2) }); addedMappings.Add(new Mapping { Name = "New-Alg-2", ExpectedType = typeof(NewAlgorithm2) }); CheckMappings(addedMappings); }
public void CryptoConfig2CreateFactoryTest() { // Test that getting an AES factory can produce a delegate for AES types Func <object> aesFactory = CryptoConfig2.CreateFactoryFromName("AES"); Aes aes = aesFactory() as Aes; Assert.IsNotNull(aes); // Ensure the type is the same as the CreateFromName type Aes createdAes = CryptoConfig2.CreateFromName("AES") as Aes; Assert.AreEqual(createdAes.GetType(), aes.GetType()); // Ensure the factory returns different instances each time Aes aes2 = aesFactory() as Aes; Assert.IsNotNull(aes2); Assert.AreNotSame(aes, aes2); }
public void CryptoConfig2OldNameMapTest() { Mapping[] cryptoConfigMappings = new Mapping[] { // Just provide a few selections from mscorlib's crypto config mappings to ensure that they // are all working as we expect new Mapping { Name = "RSA", ExpectedType = typeof(RSACryptoServiceProvider) }, new Mapping { Name = "System.Security.Cryptography.RSACryptoServiceProvider", ExpectedType = typeof(RSACryptoServiceProvider) }, new Mapping { Name = "System.Security.Cryptography.SHA1Managed", ExpectedType = typeof(SHA1Managed) } }; foreach (Mapping mapping in cryptoConfigMappings) { object algorithm = CryptoConfig2.CreateFromName(mapping.Name); Assert.IsNotNull(algorithm, "Failed to create algorithm in CryptoConfig for " + mapping.Name); Assert.AreEqual(mapping.ExpectedType, algorithm.GetType(), "Failed to map CryptoConfig for " + mapping.Name); } }
public void CryptoConfig2AddDuplicateMappingTest() { CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm5), "NA5", "NewAlg5", "New-Alg5"); CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm6), "NA5"); }
public void CryptoConfig2AddNullMappingTest() { CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm4), "NA4", "NewAlg4", null); }
public void CryptoConfig2AddEmptyMappingTest() { CryptoConfig2.AddAlgorithm(typeof(NewAlgorithm3), "NA3", "", "New-Alg-3"); }