Esempio n. 1
0
        public static void SetValue(this SecurityObjectPermission operationPermission, ISecurityObjectPermission permission)
        {
            operationPermission.Operations     = permission.Operations;
            operationPermission.OperationState = permission.OperationState;
            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            // TODO: more checks are needed...
            operationPermission.Type     = ((ParameterExpression)criteriaSerializer.Deserialize(permission.StringType)).Type;
            operationPermission.Criteria = (LambdaExpression)criteriaSerializer.Deserialize(permission.StringCriteria);
        }
        public void ObjectPropertyMethodDeserializeTest()
        {
            Expression <Func <SecurityDbContext, DbContextObject1, bool> > criteria = (db, obj) => obj.Description.StartsWith("hello");

            string propertyType        = "System.String";
            string methodResultType    = "System.Boolean";
            string methodObjectType    = "System.String";
            string methodParameterType = "System.String";
            string objectType          = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElement(objectType);

            XElement body = new XElement("MethodCallExpression",
                                         new XAttribute("NodeType", "Call"),
                                         new XAttribute("CanReduce", "false"),
                                         SerializeTestHelper.GetTypeElement(methodResultType),
                                         SerializeTestHelper.GetSingleParameterMethod("StartsWith", methodObjectType, methodParameterType),
                                         new XElement("Object",
                                                      SerializeTestHelper.GetMemberExpression(objectType, "obj", propertyType, "Description")
                                                      ),
                                         new XElement("Arguments",
                                                      SerializeTestHelper.GetConstantExpression(methodParameterType, "hello")
                                                      )
                                         );

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            criteriaSerializer.RegisterAdditionalAssemblies(GetAdditionalAssemblies());
            Expression deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void LiftedArithmeticPlusDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int?, bool> > criteria = (db, obj) => obj + 1 < 5;

            string   objectType = "System.Nullable`1|System.Int32";
            XElement nominal    = SerializeTestHelper.CreateBaseCriteriaXElement(objectType);

            XElement left = SerializeTestHelper.GetBinaryExpression("Add",
                                                                    SerializeTestHelper.SetLifted(SerializeTestHelper.GetConstantConvertUnaryExpression(objectType, "System.Int32", "1"), true, true),
                                                                    SerializeTestHelper.GetParameterExpression(objectType, "obj"),
                                                                    objectType);
            XElement right = SerializeTestHelper.GetConstantConvertUnaryExpression(objectType, "System.Int32", "5");

            SerializeTestHelper.SetLifted(left, true, true);
            SerializeTestHelper.SetLifted(right, true, true);

            XElement body = SerializeTestHelper.GetBinaryExpression("LessThan", right, left);

            SerializeTestHelper.SetLifted(body, true, false);

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer   = new CriteriaSerializer();
            Expression         deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void ComplexDeserializeTest()
        {
            Expression <Func <DbContextMultiClass, DbContextObject1, bool> > criteria = (db, obj) => obj.ItemCount < db.dbContextDbSet1.Count();

            string propertyType         = "System.Int32";
            string objectType           = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";
            string dbContextType        = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextMultiClass";
            string methodCallResultType = "System.Int32";
            string methodObjectType     = "System.Linq.Queryable";
            string methodParameterType  = "System.Linq.IQueryable`1|DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";
            string dbSetMemberType      = "Microsoft.EntityFrameworkCore.DbSet`1|DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";
            string dbSetPropertyName    = "dbContextDbSet1";
            string dbSetType            = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextMultiClass";

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElement(objectType, dbContextType);

            XElement memberExpression = new XElement("MemberExpression",
                                                     new XAttribute("NodeType", "MemberAccess"),
                                                     new XAttribute("CanReduce", "false"),
                                                     SerializeTestHelper.GetTypeElement(dbSetMemberType),
                                                     new XElement("Member",
                                                                  new XAttribute("MemberType", "Property"),
                                                                  new XAttribute("PropertyName", dbSetPropertyName),
                                                                  new XElement("DeclaringType",
                                                                               new XElement("Type",
                                                                                            new XAttribute("Name", dbSetType)
                                                                                            )
                                                                               )
                                                                  ),
                                                     new XElement("Expression",
                                                                  SerializeTestHelper.GetParameterExpression(dbContextType, "db")
                                                                  )
                                                     );

            XElement right = new XElement("MethodCallExpression",
                                          new XAttribute("NodeType", "Call"),
                                          new XAttribute("CanReduce", "false"),
                                          SerializeTestHelper.GetTypeElement(methodCallResultType),
                                          SerializeTestHelper.GetSingleParameterMethod("Count", methodObjectType, methodParameterType, new string[] { objectType }),
                                          new XElement("Object"),
                                          new XElement("Arguments",
                                                       memberExpression
                                                       )
                                          );

            XElement body = SerializeTestHelper.GetBinaryExpression("LessThan",
                                                                    right,
                                                                    SerializeTestHelper.GetMemberExpression(objectType, "obj", propertyType, "ItemCount"));

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            criteriaSerializer.RegisterAdditionalAssemblies(GetAdditionalAssemblies());
            Expression deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void ReturnFalseDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => false;

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();
            XElement body    = SerializeTestHelper.GetConstantExpression("System.Boolean", "False");

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer   = new CriteriaSerializer();
            Expression         deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void GreaterThanOrEqualConstantDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => obj >= 5;

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();
            XElement body    = SerializeTestHelper.GetIntegerBinaryExpression("GreaterThanOrEqual", "5");

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer   = new CriteriaSerializer();
            Expression         deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void LogicalNotDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => !(obj < 5);

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();
            XElement body    = SerializeTestHelper.GetUnaryExpression("Not", "System.Boolean", SerializeTestHelper.GetIntegerBinaryExpression("LessThan", "5"));

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer   = new CriteriaSerializer();
            Expression         deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void MultipleDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => obj < 5;
            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();

            XElement body = SerializeTestHelper.GetIntegerBinaryExpression("LessThan", "5");

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            criteriaSerializer.RegisterAdditionalAssemblies(GetAdditionalAssemblies());

            Expression deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);

            Expression <Func <SecurityDbContext, DbContextObject1, bool> > criteria2 = (db, obj) => obj.ItemCount < 3;

            string propertyType = "System.Int32";
            string objectType   = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";

            XElement nominal2 = SerializeTestHelper.CreateBaseCriteriaXElement(objectType);

            XElement body2 = SerializeTestHelper.GetBinaryExpression("LessThan",
                                                                     SerializeTestHelper.GetConstantExpression("System.Int32", "3"),
                                                                     SerializeTestHelper.GetMemberExpression(objectType, "obj", propertyType, "ItemCount"));

            SerializeTestHelper.SetLambdaBody(nominal2, body2);

            Expression deserializedCriteria2 = criteriaSerializer.Deserialize(nominal2);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria2, deserializedCriteria2);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void LogicalOrDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => obj < 5 || obj > 1;

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();
            XElement left    = SerializeTestHelper.GetIntegerBinaryExpression("LessThan", "5");
            XElement right   = SerializeTestHelper.GetIntegerBinaryExpression("GreaterThan", "1");

            XElement body = SerializeTestHelper.GetBinaryExpression("OrElse", right, left);

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer   = new CriteriaSerializer();
            Expression         deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void DeserializeFromStringTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => obj < 5;
            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();

            XElement body = SerializeTestHelper.GetIntegerBinaryExpression("LessThan", "5");

            SerializeTestHelper.SetLambdaBody(nominal, body);

            string nominalString = nominal.ToString();

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            criteriaSerializer.RegisterAdditionalAssemblies(GetAdditionalAssemblies());
            Expression deserializedCriteria = criteriaSerializer.Deserialize(nominalString);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
Esempio n. 11
0
        public void CriteriaSection_CanBeSerializedAndDeserialized_Using_Serializer()
        {
            CriteriaSerializer serializer = new CriteriaSerializer();

            BreachAgeGeneralCriteriaSection criteria = new BreachAgeGeneralCriteriaSection
            {
                Session                = ComplianceSession.New,
                SessionId              = 2,
                ShowBreachAges         = true,
                ShowOverriddenBreaches = true,
                ShowRuleGroups         = false
            };

            string xml = serializer.Serialize <BreachAgeGeneralCriteriaSection>(criteria);
            BreachAgeGeneralCriteriaSection crit = serializer.Deserialize <BreachAgeGeneralCriteriaSection>(xml);

            Assert.AreEqual(CompactXml("<BreachAgeGeneralCriteriaSection session=\"New\" session-id=\"2\" show-breach-ages=\"true\" show-overridden-breaches=\"true\" show-rule-groups=\"false\" />"), CompactXml(xml));
        }
        public void BadXmlStringDeserializeTest()
        {
            string nominalString = "SomeBadString";

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            bool withXmlException = false;

            try {
                Expression deserializedCriteria = criteriaSerializer.Deserialize(nominalString);
            }
            catch (System.Xml.XmlException) {
                withXmlException = true;
            }
            catch (Exception e) {
                Assert.Fail(e.Message);
            }
            Assert.IsTrue(withXmlException);
        }
        public void ConvertToDoubleDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => obj / 2.0 < 5.2;

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();
            XElement left    = SerializeTestHelper.GetBinaryExpression("Divide",
                                                                       SerializeTestHelper.GetConstantExpression("System.Double", "2"),
                                                                       SerializeTestHelper.GetParameterConvertUnaryExpression("System.Double", "System.Int32", "obj"),
                                                                       "System.Double");
            XElement right = SerializeTestHelper.GetConstantExpression("System.Double", "5.2");
            XElement body  = SerializeTestHelper.GetBinaryExpression("LessThan", right, left);

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer   = new CriteriaSerializer();
            Expression         deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void BadCriteriaDeserializeTest()
        {
            string nominalString = "<DeclaringType><Type Name=\"System.Linq.Queryable\" /></DeclaringType> ";

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            bool withNotSupportedException = false;

            try {
                Expression deserializedCriteria = criteriaSerializer.Deserialize(nominalString);
            }
            catch (NotSupportedException) {
                withNotSupportedException = true;
            }
            catch (Exception e) {
                Assert.Fail(e.Message);
            }
            Assert.IsTrue(withNotSupportedException);
        }
        public void ArithmeticXorDeserializeTest()
        {
            Expression <Func <SecurityDbContext, int, bool> > criteria = (db, obj) => (obj ^ 2) < 5;

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElementWithIntObj();
            XElement left    = SerializeTestHelper.GetBinaryExpression("ExclusiveOr",
                                                                       SerializeTestHelper.GetConstantExpression("System.Int32", "2"),
                                                                       SerializeTestHelper.GetParameterExpression("System.Int32", "obj"),
                                                                       "System.Int32");
            XElement right = SerializeTestHelper.GetConstantExpression("System.Int32", "5");
            XElement body  = SerializeTestHelper.GetBinaryExpression("LessThan", right, left);

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer   = new CriteriaSerializer();
            Expression         deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void NotEqualNullDeserializeTest()
        {
            Expression <Func <SecurityDbContext, DbContextObject1, bool> > criteria = (db, obj) => obj != null;

            string objectType = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElement(objectType);

            XElement body = SerializeTestHelper.GetBinaryExpression("NotEqual",
                                                                    SerializeTestHelper.GetConstantExpression("System.Object", ""),
                                                                    SerializeTestHelper.GetParameterExpression(objectType, "obj"));

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            criteriaSerializer.RegisterAdditionalAssemblies(GetAdditionalAssemblies());
            Expression deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void TwoPropertiesWithImplicitConversionDeserializeTest()
        {
            Expression <Func <SecurityDbContext, DbContextObject1, bool> > criteria = (db, obj) => obj.ItemCount < obj.DecimalItem;

            string property1Type = "System.Int32";
            string property2Type = "System.Decimal";
            string property1Name = "ItemCount";
            string property2Name = "DecimalItem";
            string objectName    = "obj";
            string objectType    = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";

            XElement nominal = SerializeTestHelper.CreateBaseCriteriaXElement(objectType);

            XElement implicitConversionMethod = SerializeTestHelper.GetSingleParameterMethod("op_Implicit", "System.Decimal", "System.Int32");
            XElement lessThanMethod           = SerializeTestHelper.GetMethod("op_LessThan", "System.Decimal", new string[] { "System.Decimal", "System.Decimal" });

            XElement right = SerializeTestHelper.GetMemberExpression(objectType, objectName, property2Type, property2Name);
            XElement left  = SerializeTestHelper.GetMemberConvertUnaryExpression(property2Type, objectType, objectName, property1Type, property1Name);

            SerializeTestHelper.SetMethodElement(left, implicitConversionMethod);

            XElement body = SerializeTestHelper.GetBinaryExpression("LessThan",
                                                                    right,
                                                                    left
                                                                    );

            SerializeTestHelper.SetMethodElement(body, lessThanMethod);

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            criteriaSerializer.RegisterAdditionalAssemblies(GetAdditionalAssemblies());
            Expression deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        public void ObjectNullablleIsNotNullSerializeTest()
        {
            Expression <Func <SecurityDbContext, DbContextObject1, bool> > criteria = (db, obj) => obj.ItemCountNull != null;

            string   objectType   = "DevExpress.EntityFramework.SecurityDataStore.Tests.DbContexts.DbContextObject1";
            string   propertyType = "System.Nullable`1|System.Int32";
            XElement nominal      = SerializeTestHelper.CreateBaseCriteriaXElement(objectType);

            XElement left  = SerializeTestHelper.GetMemberExpression(objectType, "obj", propertyType, "ItemCountNull");
            XElement right = SerializeTestHelper.GetConstantExpression(propertyType, "");

            XElement body = SerializeTestHelper.GetBinaryExpression("NotEqual", right, left);

            SerializeTestHelper.SetLifted(body, true, false);

            SerializeTestHelper.SetLambdaBody(nominal, body);

            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            criteriaSerializer.RegisterAdditionalAssemblies(GetAdditionalAssemblies());
            Expression deserializedCriteria = criteriaSerializer.Deserialize(nominal);

            SerializeTestHelper.CheckIfExpressionsAreEqual(criteria, deserializedCriteria);
        }
        private SecurityTypePermission FindFirstTypePermission(ISecurityRole role, Type type)
        {
            CriteriaSerializer criteriaSerializer = new CriteriaSerializer();

            return(role.TypePermissions.OfType <SecurityTypePermission>().FirstOrDefault(p => ((ParameterExpression)criteriaSerializer.Deserialize(p.StringType)).Type == type));
        }