public void SetUp() { _innerProviderStub = MockRepository.GenerateStub <IPermissionProvider>(); _cacheDecorator = new CachingPermissionProviderDecorator(_innerProviderStub); _type = typeof(SecurableObject); _methodInformation = MethodInfoAdapter.Create(_type.GetMethod("Save")); }
public void GetParameters_MethodWithParameters() { var method = typeof(ClassWithReferenceType <SimpleReferenceType>).GetMethod("TestMethodWithParameters"); var adapter = MethodInfoAdapter.Create(method); Assert.That(adapter.GetParameters().Length, Is.EqualTo(2)); }
public void FindInterfaceImplementation_ImplementationIsInterface() { var methodInfo = typeof(IInterfaceWithReferenceType <object>).GetMethod("get_ImplicitInterfaceScalar"); var adapter = MethodInfoAdapter.Create(methodInfo); adapter.FindInterfaceImplementation(typeof(IInterfaceWithReferenceType <object>)); }
public void SubstitutedBy_PropertyWriteAccessDenied() { var securityClient = _securityTestHelper.CreatedStubbedSecurityClient <User> (); var methodInformation = MethodInfoAdapter.Create(typeof(User).GetProperty("SubstitutedBy").GetSetMethod(true)); Assert.That(securityClient.HasPropertyWriteAccess(_user, methodInformation), Is.False); }
public void Roles_PropertyWriteAccessGranted() { var securityClient = _securityTestHelper.CreatedStubbedSecurityClient <User> (SecurityManagerAccessTypes.AssignRole); var methodInformation = MethodInfoAdapter.Create(typeof(User).GetProperty("Roles").GetSetMethod(true)); Assert.That(securityClient.HasPropertyWriteAccess(_user, methodInformation), Is.True); }
public void Invoke_NullParameterForMethod_GetExceptionFromReflectionApi() { var methodInfo = typeof(string).GetMethod("Insert", new[] { typeof(int), typeof(string) }); var adapter = MethodInfoAdapter.Create(methodInfo); adapter.Invoke("Test", new object[] { 5, null }); }
public void SetUp() { _methodInfo = typeof(string).GetMethod("Format", new[] { typeof(string), typeof(object) }); _methodInfoAdapter = MethodInfoAdapter.Create(_methodInfo); _converter = new MethodInfoAdapterConverter(); }
private IMethodInformation GetMethod(Type type, string methodName, BindingFlags bindingFlags) { if (!TypeHasMember(type, methodName, bindingFlags)) { throw new ArgumentException(string.Format("The method '{0}' could not be found.", methodName), "methodName"); } var foundMembers = new List <MemberInfo> (); for (Type currentType = type; currentType != null; currentType = currentType.BaseType) { foundMembers.AddRange(currentType.FindMembers(MemberTypes.Method, bindingFlags | BindingFlags.DeclaredOnly, IsSecuredMethod, methodName)); } if (foundMembers.Count == 0) { return(new NullMethodInformation()); } var foundMethodInfo = (MethodInfo)foundMembers[0]; if (type.BaseType != null && foundMethodInfo.DeclaringType == type && TypeHasMember(type.BaseType, methodName, bindingFlags)) { throw new ArgumentException( string.Format( "The DemandPermissionAttribute must not be defined on methods overriden or redefined in derived classes. " + "A method '{0}' exists in class '{1}' and its base class.", methodName, type.FullName), "methodName"); } return(MethodInfoAdapter.Create(foundMethodInfo)); }
public void GetRequiredMethodPermissionsCacheForMethodWithoutAttributes() { IMethodInformation methodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("Save")); var requiredAccessTypes = _permissionReflector.GetRequiredMethodPermissions(typeof(SecurableObject), methodInformation); Assert.That(_permissionReflector.GetRequiredMethodPermissions(typeof(SecurableObject), methodInformation), Is.SameAs(requiredAccessTypes)); }
public void Test_ManySide_RecursiveSecurity_ChecksAccessOnNestedCall() { SecurableObject securableObject = _testHelper.CreateSecurableObject(); SecurableObject otherObject = _testHelper.CreateSecurableObject(); _testHelper.Transaction.ExecuteInScope(() => securableObject.OtherChildren.Add(_testHelper.CreateSecurableObject())); var setMethodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetProperty("Children").GetGetMethod()); var securableEndPointDefintion = securableObject.ID.ClassDefinition.GetRelationEndPointDefinition(typeof(SecurableObject).FullName + ".Children"); _testHelper.ExpectPermissionReflectorGetRequiredMethodPermissions(setMethodInformation, TestAccessTypes.First); _testHelper.ExpectPermissionReflectorGetRequiredMethodPermissions(setMethodInformation, TestAccessTypes.First); HasAccessDelegate hasAccess = delegate { _extension.RelationReading(_testHelper.Transaction, otherObject, securableEndPointDefintion, ValueAccess.Current); return(true); }; _testHelper.ExpectObjectSecurityStrategyHasAccess(securableObject, TestAccessTypes.First, hasAccess); _testHelper.ExpectObjectSecurityStrategyHasAccess(otherObject, TestAccessTypes.First, true); _testHelper.ReplayAll(); _extension.RelationReading(_testHelper.Transaction, securableObject, securableEndPointDefintion, ValueAccess.Current); _testHelper.VerifyAll(); }
public void Test_ManySideRemove_AccessedViaDomainObject() { SecurableObject securableObject = _testHelper.CreateSecurableObject(); SecurableObject oldObject = _testHelper.CreateSecurableObject(); var childrenPropertyInfo = typeof(SecurableObject).GetProperty("Children"); _testHelper.Transaction.ExecuteInScope(() => securableObject.Children.Add(oldObject)); _testHelper.AddExtension(_extension); using (_testHelper.Ordered()) { _testHelper.ExpectPermissionReflectorGetRequiredMethodPermissions(MethodInfoAdapter.Create(childrenPropertyInfo.GetGetMethod()), TestAccessTypes.First); _testHelper.ExpectObjectSecurityStrategyHasAccess(securableObject, TestAccessTypes.First, true); _testHelper.ExpectPermissionReflectorGetRequiredMethodPermissions(_setMethodInformation, TestAccessTypes.Second); _testHelper.ExpectObjectSecurityStrategyHasAccess(oldObject, TestAccessTypes.Second, true); var childrenSetMethodInformation = new NullMethodInformation(); _testHelper.ExpectPermissionReflectorGetRequiredMethodPermissions(childrenSetMethodInformation, GeneralAccessTypes.Edit); _testHelper.ExpectObjectSecurityStrategyHasAccess(securableObject, GeneralAccessTypes.Edit, true); } _testHelper.ReplayAll(); _testHelper.Transaction.ExecuteInScope(() => securableObject.Children.Remove(oldObject)); _testHelper.VerifyAll(); }
public void Test_OverriddenMethodFromBaseMethod() { var methodInformation = _resolver.GetMethodInformation(typeof(DerivedSecurableObject), "Record", MemberAffiliation.Instance); var expectedMethodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("Record")); Assert.That(methodInformation, Is.Not.Null); Assert.That(methodInformation, Is.EqualTo(expectedMethodInformation)); }
public void Invoke_WrongInstanceForMethod_GetExceptionFromReflectionApi() { var methodInfo = typeof(ClassWithBaseMember).GetMethod("BaseMethod"); var adapter = MethodInfoAdapter.Create(methodInfo); var result = adapter.Invoke("Test", new object[0]); Assert.That(result, Is.Null); }
public void Invoke_BaseMethod() { var methodInfo = typeof(ClassWithBaseMember).GetMethod("BaseMethod"); var adapter = MethodInfoAdapter.Create(methodInfo); var result = adapter.Invoke(new ClassWithBaseMember(), new object[] { }); Assert.That(result, Is.Null); }
public void GetGetMethod_PublicProperty() { var getMethod = _adapter.GetGetMethod(false); var expectedMethod = MethodInfoAdapter.Create(typeof(ClassWithReferenceType <SimpleReferenceType>).GetMethod("get_NotVisibleAttributeScalar")); Assert.That(getMethod, Is.Not.Null); Assert.That(getMethod, Is.EqualTo(expectedMethod)); }
public void Test_MethodOfDerivedClass() { var methodInformation = _resolver.GetMethodInformation(typeof(DerivedSecurableObject), "CreateForSpecialCase", MemberAffiliation.Static); var expectedMethodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("CreateForSpecialCase")); Assert.That(methodInformation, Is.Not.Null); Assert.That(methodInformation, Is.EqualTo(expectedMethodInformation)); }
public void Test_OverloadedMethodWithOneAttributes() { var methodInformation = _resolver.GetMethodInformation(typeof(SecurableObject), "IsValid", MemberAffiliation.Static); var expectedMethodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("IsValid", new [] { typeof(SecurableObject) })); Assert.That(methodInformation, Is.Not.Null); Assert.That(methodInformation, Is.EqualTo(expectedMethodInformation)); }
public void Test_MethodWithOneAttribute() { var methodInformation = _resolver.GetMethodInformation(typeof(SecurableObject), "Load", MemberAffiliation.Instance); var expectedMethodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("Load", new Type[] {})); Assert.That(methodInformation, Is.Not.Null); Assert.That(methodInformation, Is.EqualTo(expectedMethodInformation)); }
public void CanBeSetFromOutside_IsBasedOnGetSetMethod_FalseForImplementationOnlyPropertySetter() { var methodInfoAdapter = MethodInfoAdapter.Create(typeof(object).GetMethod("ToString")); _implementationPropertyInformationStub.Stub(stub => stub.GetSetMethod(false)).Return(methodInfoAdapter); Assert.That(_mixinIntroducedPropertyInformation.CanBeSetFromOutside, Is.False); }
public void GetOriginalDeclaration() { var objToReturn = MethodInfoAdapter.Create(typeof(string).GetMethod("get_Length")); _implementationMethodInformationStub.Stub(stub => stub.GetOriginalDeclaration()).Return(objToReturn); Assert.That(_interfaceImplementationMethodInformation.GetOriginalDeclaration(), Is.SameAs(objToReturn)); }
public void FindInterfaceImplementation() { var methodInfoAdapter = MethodInfoAdapter.Create(typeof(object).GetMethod("ToString")); _implementationMethodInformationStub.Stub(stub => stub.FindInterfaceImplementation(typeof(bool))).Return(methodInfoAdapter); Assert.That(_interfaceImplementationMethodInformation.FindInterfaceImplementation(typeof(bool)), Is.SameAs(methodInfoAdapter)); }
public void GetRequiredMethodPermissionsOverriddenMethodWithPermissionFromBaseMethod() { IMethodInformation methodInformation = MethodInfoAdapter.Create(typeof(DerivedSecurableObject).GetMethod("Record")); var requiredAccessTypes = _permissionReflector.GetRequiredMethodPermissions(typeof(DerivedSecurableObject), methodInformation); Assert.That(requiredAccessTypes.Count, Is.EqualTo(1)); Assert.That(requiredAccessTypes, Has.Member(GeneralAccessTypes.Edit)); }
public void GetRequiredMethodPermissionsStaticMethod() { IMethodInformation methodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("CreateForSpecialCase")); var requiredAccessTypes = _permissionReflector.GetRequiredMethodPermissions(typeof(SecurableObject), methodInformation); Assert.That(requiredAccessTypes.Count, Is.EqualTo(1)); Assert.That(requiredAccessTypes[0], Is.EqualTo(GeneralAccessTypes.Create)); }
public void GetRequiredMethodPermissionsMethodWithoutAttributes() { IMethodInformation methodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("Save")); var requiredAccessTypes = _permissionReflector.GetRequiredMethodPermissions(typeof(SecurableObject), methodInformation); Assert.That(requiredAccessTypes, Is.Not.Null); Assert.That(requiredAccessTypes, Is.Empty); }
public void Create_ReturnsSameInstance_ForMethodsReflectedFromDifferentLevelsInHierarchy() { var methodViaBase = typeof(ClassWithReferenceType <SimpleReferenceType>).GetMethod("TestMethod"); var methodViaDerived = typeof(DerivedClassWithReferenceType <SimpleReferenceType>).GetMethod("TestMethod"); Assert.That(methodViaBase, Is.Not.SameAs(methodViaDerived)); Assert.That(MethodInfoAdapter.Create(methodViaBase), Is.SameAs(MethodInfoAdapter.Create(methodViaDerived))); }
public void FindDeclaringProperty_NoPropertyCanBeFound() { var methodInfo = typeof(ClassWithBaseMember).GetMethod("BaseMethod"); var adapter = MethodInfoAdapter.Create(methodInfo); var result = adapter.FindDeclaringProperty(); Assert.That(result, Is.Null); }
public void FindDeclaringProperty_PropertyFound() { var methodInfo = typeof(ClassWithReferenceType <object>).GetMethod("get_ImplicitInterfaceScalar"); var adapter = MethodInfoAdapter.Create(methodInfo); var result = adapter.FindDeclaringProperty(); CheckProperty(TypeAdapter.Create(typeof(ClassWithReferenceType <object>)), "ImplicitInterfaceScalar", result); }
public void FilterMultipleAccessTypes() { IMethodInformation methodInformation = MethodInfoAdapter.Create(typeof(SecurableObject).GetMethod("Close")); var requiredAccessTypes = _permissionReflector.GetRequiredMethodPermissions(typeof(SecurableObject), methodInformation); Assert.That(requiredAccessTypes.Count, Is.EqualTo(2)); Assert.That(requiredAccessTypes, Has.Member(GeneralAccessTypes.Edit)); Assert.That(requiredAccessTypes, Has.Member(GeneralAccessTypes.Find)); }
public void Equals() { Assert.That(_adapter.Equals(null), Is.False); Assert.That(_adapter.Equals("test"), Is.False); Assert.That(_adapter.Equals(MethodInfoAdapter.Create(typeof(ClassWithOverridingMember).GetMethod("BaseMethod"))), Is.False); Assert.That(_adapter.Equals(MethodInfoAdapter.Create(_method)), Is.True); Assert.That(_adapter.Equals(MethodInfoAdapter.Create(typeof(ClassWithReferenceType <SimpleReferenceType>).GetMethod("TestMethod"))), Is.True); }
public void GetHashcode() { Assert.That( _adapter.GetHashCode(), Is.EqualTo(MethodInfoAdapter.Create(typeof(ClassWithReferenceType <SimpleReferenceType>).GetMethod("TestMethod")).GetHashCode())); Assert.That( MethodInfoAdapter.Create(typeof(int[]).GetMethod("ToString")).GetHashCode(), Is.EqualTo(MethodInfoAdapter.Create(typeof(int[]).GetMethod("ToString")).GetHashCode())); }