示例#1
0
        /// <inheritdoc/>
        public override bool TryMarkOwner(MethodDefinitionBuilder method, string owner)
        {
            SafeDebug.AssumeNotNull(method, "method");
            SafeDebug.AssumeNotNull(owner, "owner");

            method.CustomAttributes.Add(new CustomAttributeBuilder(XunitTestFrameworkMetadata.Ctor_TraitAttribute,
                                                                   MetadataExpression.String("Owner"),
                                                                   MetadataExpression.String(owner)));

            return(true);
        }
示例#2
0
        /// <inheritdoc/>
        public override void MarkIgnored(MethodDefinitionBuilder method, string message)
        {
            SafeDebug.AssumeNotNull(method, "method");

            var fact = method.CustomAttributes
                       .Cast <CustomAttributeBuilder>()
                       .FirstOrDefault(a => a.Constructor.SerializableName == XunitTestFrameworkMetadata.Ctor_FactAttribute);

            if (fact != null)
            {
                fact.AddNamedArgument(XunitTestFrameworkMetadata.Property_Skip, MetadataExpression.String(message));
                return;
            }

            SafeDebug.FailUnreachable();
        }
        private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, string message, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType)
        {
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            bool             success         = false;

            if (feedbackManager != null)
            {
                ConstructorInfo attrCtor = attributeType.GetConstructor(new System.Type[1] {
                    typeof(string)
                });
                MethodDefinitionName target = Translate(method, output, mdDecoder);

                string           group = Guid.NewGuid().ToString();
                ICustomAttribute ca    = new CustomAttributeBuilder(MetadataFromReflection.GetMethod(attrCtor), MetadataExpression.String(message));

                CodeUpdate update = CodeUpdate.AddAttribute("Regression", target, ca);

                CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                    success = true;
                }
                catch { }
            }
            return(success);
        }
        private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Assembly assembly, string message, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType)
        {
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            bool             success         = true;

            if (feedbackManager != null)
            {
                ConstructorInfo attrCtor = attributeType.GetConstructor(new System.Type[1] {
                    typeof(string)
                });

#if DEBUG_PEX_BY_XML
                SafeSimpleXmlWriter writer = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + ".xml"), true);
                target.WriteXml(writer, "method");
                writer.Close();

                SafeSimpleXmlWriter writer2 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "2.xml"), true);
                attributeCtorName2.WriteXml(writer2, "method");
                writer2.Close();
#endif
                string           group = Guid.NewGuid().ToString();
                ICustomAttribute ca    = new CustomAttributeBuilder(MetadataFromReflection.GetMethod(attrCtor), MetadataExpression.String(message));

                CodeUpdate update =
                    CodeUpdate.AddAttribute("Regression",
                                            new Microsoft.ExtendedReflection.Metadata.Names.ShortAssemblyName(mdDecoder.Name(assembly), null),
                                            ca);

                CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                    success = true;
                }
                catch { }
            }
            return(success);
        }
        private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, Enum outcome, string message, int primaryILOffset, int methodILOffset, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType)
        {
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            bool             success         = false;

            if (feedbackManager != null)
            {
                PropertyInfo    outcomeProp   = attributeType.GetProperty("Outcome");
                PropertyInfo    messageProp   = attributeType.GetProperty("Message");
                PropertyInfo    primaryILProp = attributeType.GetProperty("PrimaryILOffset");
                PropertyInfo    methodILProp  = attributeType.GetProperty("MethodILOffset");
                ConstructorInfo attrCtor      = attributeType.GetConstructor(new System.Type[0]);

                MethodDefinitionName target = Translate(method, output, mdDecoder);

                MethodName attributeCtorName = MetadataFromReflection.GetMethod(attrCtor).SerializableName;

#if DEBUG_PEX_BY_XML
                SafeSimpleXmlWriter writer = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + ".xml"), true);
                target.WriteXml(writer, "method");
                writer.Close();

                SafeSimpleXmlWriter writer2 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "2.xml"), true);
                attributeCtorName2.WriteXml(writer2, "method");
                writer2.Close();
#endif
                string group = Guid.NewGuid().ToString();

                var outcomeArg = AttributeArgument.Named(MetadataFromReflection.GetProperty(outcomeProp), MetadataExpression.EnumValue(outcome));
#if DEBUG_PEX_BY_XML
                SafeSimpleXmlWriter writer3 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "3.xml"), true);
                outcomeArg.WriteXml(writer3, "method");
                writer3.Close();
#endif
                CodeUpdate update =
                    CodeUpdate.AddAttribute("Regression", target, attributeCtorName,
                                            outcomeArg,
                                            AttributeArgument.Named(MetadataFromReflection.GetProperty(messageProp), MetadataExpression.String(message)),
                                            AttributeArgument.Named(MetadataFromReflection.GetProperty(primaryILProp), MetadataExpression.I4(MetadataFromReflection.GetType(typeof(int)), primaryILOffset)),
                                            AttributeArgument.Named(MetadataFromReflection.GetProperty(methodILProp), MetadataExpression.I4(MetadataFromReflection.GetType(typeof(int)), methodILOffset))
                                            );

                CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                    success = true;
                }
                catch { }
            }
            return(success);
        }
        /// <summary>
        /// Extract all the tokens in the template that need replacing with actual values
        /// </summary>
        /// <param name="template"></param>
        /// <param name="entities"></param>
        /// <returns></returns>
        private static List<MetadataExpression> ExtractTokens(string template,  out Dictionary<string, Dictionary<string, HashSet<string>>> entities)
        {
            List<MetadataExpression> expressions = new List<MetadataExpression>();
            Regex expression = new Regex(@"\<@([\w\.\>\<\/\-\=\""\'\s\{\}]+)@\>");
            var matches = expression.Matches(template);

            foreach (Match match in matches)
            {
                // Get a list of entities, attributes and metatadata to retrieve
                MetadataExpression expr = new MetadataExpression(match.Groups[1].Value, match.Index, match.Length);
                expressions.Add(expr);
            }

            // Analysis of the expressions
            entities = new Dictionary<string, Dictionary<string, HashSet<string>>>();

            foreach (var expr in expressions)
            {
                switch (expr.ExpressionType)
                {
                    case TokenExpressionType.EntityMetadata:
                        if (!entities.ContainsKey(expr.Entity))
                        {
                            entities.Add(expr.Entity, new Dictionary<string, HashSet<string>>());
                        }
                        var attributes = entities[expr.Entity];

                        if (!attributes.ContainsKey(expr.Attribute))
                        {
                            attributes.Add(expr.Attribute, new HashSet<string>());
                        }
                        var properties = attributes[expr.Attribute];
                        if (!properties.Contains(expr.PropertyName))
                            properties.Add(expr.PropertyName);

                        break;
                }
            }
            return expressions;
        }