/// <summary> /// Checks initial value, sets and then checks value. Works with multiple properties. /// </summary> /// <param name="context"> <see cref="GetSetContext"/>, drives the test.</param> public static void GetSet(GetSetContext context) { Type type = context.Object.GetType(); foreach (KeyValuePair <string, List <object> > propertyKV in context.PropertyNamesAndSetGetValue) { PropertyInfo propertyInfo = type.GetProperty(propertyKV.Key); try { if (propertyInfo.GetMethod != null) { object initialValue = propertyInfo.GetValue(context.Object, null); if ((initialValue == null && propertyKV.Value[0] != null)) { context.Errors.Add(propertyKV.Key + ": initial value == null && expected != null, expect initial value: " + propertyKV.Value[0].ToString()); } else if (initialValue != null && propertyKV.Value[0] == null) { context.Errors.Add(propertyKV.Key + ": initial value != null && expected == null, initial value: " + initialValue.ToString()); } else if (initialValue != null && !initialValue.Equals(propertyKV.Value[0])) { context.Errors.Add(propertyKV.Key + ", initial value != expected. expected: " + propertyKV.Value[0].ToString() + ", was: " + initialValue.ToString()); } } if (propertyInfo.SetMethod != null) { for (int i = 1; i < propertyKV.Value.Count; i++) { propertyInfo.SetValue(context.Object, propertyKV.Value[i]); object getVal = propertyInfo.GetValue(context.Object, null); if ((getVal == null && propertyKV.Value[i] != null)) { context.Errors.Add(propertyKV.Key + "( " + i.ToString() + "), Get returned null, set was: " + propertyKV.Value[i].ToString()); } else if (getVal != null && propertyKV.Value[i] == null) { context.Errors.Add(propertyKV.Key + "( " + i.ToString() + "), Get not null, set was null, get was: " + getVal); } else if (getVal != null && !getVal.Equals(propertyKV.Value[i])) { context.Errors.Add(propertyKV.Key + "( " + i.ToString() + ") Set did not equal get: " + propertyKV.Value[i].ToString() + ", " + getVal + "."); } } } } catch (Exception ex) { context.Errors.Add(ex.ToString()); } } }
public void GetSets() { TestUtilities.WriteHeader("TokenValidationResultTests.GetSets()"); TokenValidationResult tokenValidationResult = new TokenValidationResult(); Type type = typeof(TokenValidationResult); PropertyInfo[] properties = type.GetProperties(); if (properties.Length != 9) { Assert.True(false, "Number of public fields has changed from 9 to: " + properties.Length + ", adjust tests"); } GetSetContext context = new GetSetContext { PropertyNamesAndSetGetValue = new List <KeyValuePair <string, List <object> > > { new KeyValuePair <string, List <object> >("ClaimsIdentity", new List <object> { (ClaimsIdentity)null, new ClaimsIdentity(), new ClaimsIdentity() }), new KeyValuePair <string, List <object> >("Exception", new List <object> { (Exception)null, new Exception(), new Exception() }), new KeyValuePair <string, List <object> >("Issuer", new List <object> { (string)null, "issuer", "issuer2" }), new KeyValuePair <string, List <object> >("IsValid", new List <object> { false, false, true }), new KeyValuePair <string, List <object> >("SecurityToken", new List <object> { (SecurityToken)null, new JsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor())), new JsonWebToken(Default.Jwt(Default.SecurityTokenDescriptor())) }), new KeyValuePair <string, List <object> >("TokenContext", new List <object> { (CallContext)null, new CallContext(), new CallContext() }), new KeyValuePair <string, List <object> >("TokenType", new List <object> { (string)null, "JWTToken", "JwtToken2" }), new KeyValuePair <string, List <object> >("PropertyBag", new List <object> { tokenValidationResult.PropertyBag }) }, Object = tokenValidationResult, }; TestUtilities.GetSet(context); TestUtilities.AssertFailIfErrors("TokenValidationResultTests.GetSets", context.Errors); }
public void GetSets() { TokenValidationParameters validationParameters = new TokenValidationParameters(); Type type = typeof(TokenValidationParameters); PropertyInfo[] properties = type.GetProperties(); if (properties.Length != 48) Assert.True(false, "Number of public fields has changed from 44 to: " + properties.Length + ", adjust tests"); GetSetContext context = new GetSetContext { PropertyNamesAndSetGetValue = new List<KeyValuePair<string, List<object>>> { new KeyValuePair<string, List<object>>("ActorValidationParameters", new List<object>{(TokenValidationParameters)null, new TokenValidationParameters(), new TokenValidationParameters()}), new KeyValuePair<string, List<object>>("AuthenticationType", new List<object>{(string)null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString()}), //new KeyValuePair<string, List<object>>("CertificateValidator", new List<object>{(string)null, X509CertificateValidator.None, X509CertificateValidatorEx.None}), new KeyValuePair<string, List<object>>("ClockSkew", new List<object>{TokenValidationParameters.DefaultClockSkew, TimeSpan.FromHours(2), TimeSpan.FromMinutes(1)}), new KeyValuePair<string, List<object>>("IgnoreTrailingSlashWhenValidatingAudience", new List<object>{true, false, true}), new KeyValuePair<string, List<object>>("IssuerSigningKey", new List<object>{(SecurityKey)null, KeyingMaterial.DefaultX509Key_2048, KeyingMaterial.RsaSecurityKey_2048}), new KeyValuePair<string, List<object>>("IssuerSigningKeys", new List<object>{(IEnumerable<SecurityKey>)null, new List<SecurityKey>{KeyingMaterial.DefaultX509Key_2048, KeyingMaterial.RsaSecurityKey_1024}, new List<SecurityKey>()}), new KeyValuePair<string, List<object>>("NameClaimType", new List<object>{ClaimsIdentity.DefaultNameClaimType, Guid.NewGuid().ToString(), Guid.NewGuid().ToString()}), new KeyValuePair<string, List<object>>("PropertyBag", new List<object>{(IDictionary<string, Object>)null, new Dictionary<string, Object> {{"CustomKey", "CustomValue"}}, new Dictionary<string, Object>()}), new KeyValuePair<string, List<object>>("RoleClaimType", new List<object>{ClaimsIdentity.DefaultRoleClaimType, Guid.NewGuid().ToString(), Guid.NewGuid().ToString()}), new KeyValuePair<string, List<object>>("RequireExpirationTime", new List<object>{true, false, true}), new KeyValuePair<string, List<object>>("RequireSignedTokens", new List<object>{true, false, true}), new KeyValuePair<string, List<object>>("SaveSigninToken", new List<object>{false, true, false}), new KeyValuePair<string, List<object>>("ValidateActor", new List<object>{false, true, false}), new KeyValuePair<string, List<object>>("ValidateAudience", new List<object>{true, false, true}), new KeyValuePair<string, List<object>>("ValidateIssuer", new List<object>{true, false, true}), new KeyValuePair<string, List<object>>("ValidateLifetime", new List<object>{true, false, true}), new KeyValuePair<string, List<object>>("ValidateTokenReplay", new List<object>{false, true, false}), new KeyValuePair<string, List<object>>("ValidIssuer", new List<object>{(string)null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString()}), new KeyValuePair<string, List<object>>("ConfigurationManager", new List<object>{(BaseConfigurationManager)null, new ConfigurationManager<OpenIdConnectConfiguration>("http://someaddress.com", new OpenIdConnectConfigurationRetriever()), new ConfigurationManager<WsFederationConfiguration>("http://someaddress.com", new WsFederationConfigurationRetriever()) }), }, Object = validationParameters, }; TestUtilities.GetSet(context); TestUtilities.AssertFailIfErrors("TokenValidationParametersTests: GetSets", context.Errors); Assert.Null(validationParameters.AudienceValidator); Assert.Null(validationParameters.LifetimeValidator); Assert.Null(validationParameters.IssuerSigningKeyResolver); Assert.Null(validationParameters.IssuerValidator); Assert.Null(validationParameters.TypeValidator); Assert.Null(validationParameters.ValidAudiences); Assert.Null(validationParameters.ValidIssuers); Assert.Null(validationParameters.SignatureValidator); }
public void GetSets() { TestUtilities.WriteHeader($"{this}.GetSets"); var context = new CompareContext($"{this}.GetSets"); CryptoProviderFactory cryptoProviderFactory = new CryptoProviderFactory(); Type type = typeof(CryptoProviderFactory); PropertyInfo[] properties = type.GetProperties(); if (properties.Length != 7) { Assert.True(false, "Number of public fields has changed from 7 to: " + properties.Length + ", adjust tests"); } CustomCryptoProvider customCryptoProvider = new CustomCryptoProvider(); GetSetContext getSetContext = new GetSetContext { PropertyNamesAndSetGetValue = new List <KeyValuePair <string, List <object> > > { new KeyValuePair <string, List <object> >("SignatureProviderObjectPoolCacheSize", new List <object> { CryptoProviderFactory.DefaultSignatureProviderObjectPoolCacheSize, 20, 10 }), new KeyValuePair <string, List <object> >("CacheSignatureProviders", new List <object> { CryptoProviderFactory.DefaultCacheSignatureProviders, false, true }), new KeyValuePair <string, List <object> >("CustomCryptoProvider", new List <object> { (ICryptoProvider)null, customCryptoProvider, null }), }, Object = cryptoProviderFactory, }; TestUtilities.GetSet(getSetContext); cryptoProviderFactory.SignatureProviderObjectPoolCacheSize = 42; cryptoProviderFactory.CacheSignatureProviders = false; cryptoProviderFactory.CustomCryptoProvider = customCryptoProvider; CryptoProviderFactory clone = new CryptoProviderFactory(cryptoProviderFactory); IdentityComparer.CompareAllPublicProperties(clone, cryptoProviderFactory, context); try { cryptoProviderFactory.SignatureProviderObjectPoolCacheSize = 0; context.AddDiff("cryptoProviderFactory.SignatureProviderObjectPoolCacheSize = 0; - Succeeded"); } catch { } try { cryptoProviderFactory.SignatureProviderObjectPoolCacheSize = -1; context.AddDiff("cryptoProviderFactory.SignatureProviderObjectPoolCacheSize = -1; - Succeeded"); } catch { } context.Diffs.AddRange(getSetContext.Errors); TestUtilities.AssertFailIfErrors(context); }
public void GetSets() { TokenValidationParameters validationParameters = new TokenValidationParameters(); Type type = typeof(TokenValidationParameters); PropertyInfo[] properties = type.GetProperties(); if (properties.Length != 32) { Assert.True(false, "Number of public fields has changed from 32 to: " + properties.Length + ", adjust tests"); } GetSetContext context = new GetSetContext { PropertyNamesAndSetGetValue = new List <KeyValuePair <string, List <object> > > { new KeyValuePair <string, List <object> >("ActorValidationParameters", new List <object> { (TokenValidationParameters)null, new TokenValidationParameters(), new TokenValidationParameters() }), new KeyValuePair <string, List <object> >("AuthenticationType", new List <object> { (string)null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }), //new KeyValuePair<string, List<object>>("CertificateValidator", new List<object>{(string)null, X509CertificateValidator.None, X509CertificateValidatorEx.None}), new KeyValuePair <string, List <object> >("ClockSkew", new List <object> { TokenValidationParameters.DefaultClockSkew, TimeSpan.FromHours(2), TimeSpan.FromMinutes(1) }), new KeyValuePair <string, List <object> >("IssuerSigningKey", new List <object> { (SecurityKey)null, KeyingMaterial.DefaultX509Key_2048, KeyingMaterial.RsaSecurityKey_2048 }), new KeyValuePair <string, List <object> >("IssuerSigningKeys", new List <object> { (IEnumerable <SecurityKey>)null, new List <SecurityKey> { KeyingMaterial.DefaultX509Key_2048, KeyingMaterial.RsaSecurityKey_1024 }, new List <SecurityKey>() }), new KeyValuePair <string, List <object> >("NameClaimType", new List <object> { ClaimsIdentity.DefaultNameClaimType, Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }), new KeyValuePair <string, List <object> >("RoleClaimType", new List <object> { ClaimsIdentity.DefaultRoleClaimType, Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }), new KeyValuePair <string, List <object> >("RequireExpirationTime", new List <object> { true, false, true }), new KeyValuePair <string, List <object> >("RequireSignedTokens", new List <object> { true, false, true }), new KeyValuePair <string, List <object> >("SaveSigninToken", new List <object> { false, true, false }), new KeyValuePair <string, List <object> >("ValidateActor", new List <object> { false, true, false }), new KeyValuePair <string, List <object> >("ValidateAudience", new List <object> { true, false, true }), new KeyValuePair <string, List <object> >("ValidateIssuer", new List <object> { true, false, true }), new KeyValuePair <string, List <object> >("ValidateLifetime", new List <object> { true, false, true }), new KeyValuePair <string, List <object> >("ValidIssuer", new List <object> { (string)null, Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }), }, Object = validationParameters, }; TestUtilities.GetSet(context); TestUtilities.AssertFailIfErrors("TokenValidationParametersTests: GetSets", context.Errors); Assert.Null(validationParameters.AudienceValidator); Assert.Null(validationParameters.LifetimeValidator); Assert.Null(validationParameters.IssuerSigningKeyResolver); Assert.Null(validationParameters.IssuerValidator); Assert.Null(validationParameters.ValidAudiences); Assert.Null(validationParameters.ValidIssuers); Assert.Null(validationParameters.SignatureValidator); }