public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = null;
            if (currentValue == null)
            {
                CodeClass currentDSAClass = ExpressionEvaluationHelper.EvaluateExpression((IDictionaryService)GetService(typeof(IDictionaryService)),
                                                currentDSAClassExpression) as CodeClass;
                string methodName = ExpressionEvaluationHelper.EvaluateExpression((IDictionaryService)GetService(typeof(IDictionaryService)),
                                                methodNameExpression) as string;

                bool built = true;
                object builtValue = ExpressionEvaluationHelper.EvaluateExpression((IDictionaryService)GetService(typeof(IDictionaryService)),
                                                builtExpression);
                if (builtValue != null)
                { built = (bool)builtValue; }

                if (currentDSAClass != null && built)
                {
                    Type dsaType = DteConverter.ToType(currentDSAClass);
                    if (dsaType != null)
                    {
                        MethodInfo method=dsaType.GetMethod(methodName);
                        if (method != null
                            && method.IsStatic
                            && method.GetParameters().Length==0)
                        {
                            newValue = method.Invoke(null, null);
                            newValue = MethodBehaviorHelper.TranslateToOfflineBehavior(newValue);
                        }
                    }
                }

            }
            return currentValue!=newValue;
        }
        public static MethodBehaviors BuildBehaviors(Type proxyType, MethodBehaviors behaviorOverrides, List <MethodInfo> methodsList)
        {
            if (behaviorOverrides == null)
            {
                behaviorOverrides = new MethodBehaviors();
            }
            List <MethodInfo> methods = ProxyMethodsConverter.GetMethods(proxyType, methodsList);

            // Copy received ones so that we don't modify original argument value.
            MethodBehaviors behaviors = new MethodBehaviors();

            if (behaviorOverrides != null)
            {
                foreach (MethodBehavior behavior in behaviorOverrides)
                {
                    behaviors.Add(behavior);
                    MethodBehaviorHelper.RemoveMethod(methods, behavior.Method);
                }
            }

            // Add the remaining ones as not-overriden behaviors.
            foreach (MethodInfo method in methods)
            {
                behaviors.Add(new MethodBehavior(method, false));
            }

            return(behaviors);
        }
Ejemplo n.º 3
0
        public void TwoMethodHasDifferentParameters()
        {
            Type       mockProxyAgentType = typeof(MockProxyAgent);
            MethodInfo helloWorldMethod1  = mockProxyAgentType.GetMethod("HelloWorld", new Type[] {});
            MethodInfo helloWorldMethod2  = mockProxyAgentType.GetMethod("HelloWorld", new Type[] { typeof(string) });

            Assert.IsFalse(MethodBehaviorHelper.HasSameParameterTypes(helloWorldMethod1, helloWorldMethod2));
        }
Ejemplo n.º 4
0
        public void ShouldReturnMethodsFromType()
        {
            Type mockProxyType        = typeof(MockProxy);
            List <MethodInfo> methods = MethodBehaviorHelper.GetProxyMethods(mockProxyType);

            Assert.IsTrue(methods.Contains(mockProxyType.GetMethod("HelloWorld", new Type[] {})));
            Assert.IsTrue(methods.Contains(mockProxyType.GetMethod("HelloWorld", new Type[] { typeof(string) })));
            Assert.IsTrue(methods.Contains(mockProxyType.GetMethod("Foo", new Type[] {})));
        }
Ejemplo n.º 5
0
        public Configuration(ConfigData configData) : this()
        {
            if (configData.TaintTypes != null)
            {
                RegisterTaintTypes(configData.TaintTypes);
            }

            ReportAnalysisCompletion = configData.ReportAnalysisCompletion ?? false;
            AuditMode = configData.AuditMode ?? false;
            MinimumPasswordValidatorProperties = configData.MinimumPasswordValidatorProperties ?? 0;
            PasswordValidatorRequiredLength    = configData.PasswordValidatorRequiredLength ?? 0;

            if (configData.PasswordValidatorRequiredProperties != null)
            {
                foreach (var data in configData.PasswordValidatorRequiredProperties)
                {
                    _PasswordValidatorRequiredProperties.Add(data);
                }
            }

            foreach (var data in configData.Behavior)
            {
                ConfigurationBehavior[data.Key] = CreateBehavior(data.Value);
            }

            foreach (var source in configData.TaintEntryPoints)
            {
                if (source.Value?.Method?.ArgTypes != null)
                {
                    throw new Exception("Taint entry point ArgTypes are not supported.");
                }

                _TaintEntryPoints.Add(MethodBehaviorHelper.GetMethodBehaviorKey(source.Value.Namespace,
                                                                                source.Value.ClassName,
                                                                                source.Value.Name,
                                                                                source.Value?.Method?.ArgTypes));
            }

            foreach (var data in configData.CsrfProtection)
            {
                AddCsrfProtectionToConfiguration(data);
            }

            if (configData.PasswordFields != null)
            {
                foreach (var data in configData.PasswordFields)
                {
                    _PasswordFields.Add(data.ToUpperInvariant());
                }
            }

            foreach (var data in configData.ConstantFields)
            {
                _ConstantFields.Add(data);
            }
        }
Ejemplo n.º 6
0
        public void ShouldReturnMethodNames()
        {
            List <MethodInfo> methods = new List <MethodInfo>();

            methods.AddRange(typeof(MockProxy).GetMethods());

            List <string> methodNames = MethodBehaviorHelper.GetMethodNames(methods);

            Assert.IsTrue(methodNames.Contains("HelloWorld"));
            Assert.IsTrue(methodNames.Contains("Foo"));
        }
Ejemplo n.º 7
0
        public void ShouldReturnAllMethodsForSameType()
        {
            Type mockProxyType         = typeof(MockProxy);
            List <MethodInfo> methods1 = new List <MethodInfo>(mockProxyType.GetMethods());
            List <MethodInfo> methods2 = new List <MethodInfo>(mockProxyType.GetMethods());

            List <MethodInfo> filteredMethods = MethodBehaviorHelper.GetEqualMethods(methods1, methods2);

            Assert.AreNotEqual(0, methods1.Count);
            Assert.AreEqual(methods1.Count, methods2.Count);
            Assert.AreEqual(filteredMethods.Count, methods1.Count);
        }
Ejemplo n.º 8
0
        public void ShouldTranslateMockOfflineBehavior()
        {
            MockOfflineBehavior mockOffBehavior = new MockOfflineBehavior(DateTime.Now, 123, 456, "XXX");

            Microsoft.Practices.SmartClientFactory.DSACompatibleTypes.OfflineBehavior behavior =
                MethodBehaviorHelper.TranslateToOfflineBehavior(mockOffBehavior);

            Assert.AreEqual(mockOffBehavior.Expiration, behavior.Expiration);
            Assert.AreEqual(mockOffBehavior.MaxRetries, behavior.MaxRetries);
            Assert.AreEqual(mockOffBehavior.Stamps, behavior.Stamps);
            Assert.AreEqual(mockOffBehavior.Tag, behavior.Tag);
        }
Ejemplo n.º 9
0
        public void ShouldnotRemoveUnequalMethod()
        {
            List <MethodInfo> agentMethods = new List <MethodInfo>(typeof(MockProxyAgent).GetMethods());
            MethodInfo        proxyMethod  = typeof(MockProxy).GetMethod("NotAgentedMethod", new Type[] {});
            int methodsCount = agentMethods.Count;

            MethodBehaviorHelper.RemoveMethod(agentMethods, proxyMethod);

            Assert.IsNotNull(proxyMethod);
            Assert.AreNotEqual(0, methodsCount);
            Assert.AreEqual(methodsCount, agentMethods.Count);
        }
Ejemplo n.º 10
0
        public void ShouldRemoveEqualNotSameMethod()
        {
            List <MethodInfo> agentMethods = new List <MethodInfo>(typeof(MockProxyAgent).GetMethods());
            MethodInfo        proxyMethod  = typeof(MockProxy).GetMethod("HelloWorld", new Type[] { typeof(string) });
            bool containsTargetMethod      = agentMethods.Contains(proxyMethod);
            int  methodsCount = agentMethods.Count;

            MethodBehaviorHelper.RemoveMethod(agentMethods, proxyMethod);

            Assert.IsNotNull(proxyMethod);
            Assert.AreNotEqual(0, methodsCount);
            Assert.IsFalse(containsTargetMethod);
            Assert.AreEqual(methodsCount - 1, agentMethods.Count);
        }
Ejemplo n.º 11
0
        public void ShouldReturnSameMethodsFromAgentThanProxy()
        {
            Type mockProxyAgentType = typeof(MockProxyAgent);
            Type mockProxyType      = typeof(MockProxy);

            List <MethodInfo> agentMethods = MethodBehaviorHelper.GetAgentMethods(mockProxyAgentType, mockProxyType);

            Assert.AreEqual(3, agentMethods.Count);
            Assert.IsFalse(
                agentMethods.Contains(mockProxyAgentType.GetMethod("HelloWorld", new Type[] { typeof(OfflineBehavior) })));
            Assert.IsFalse(
                agentMethods.Contains(
                    mockProxyAgentType.GetMethod("HelloWorld", new Type[] { typeof(string), typeof(OfflineBehavior) })));
            Assert.IsFalse(agentMethods.Contains(mockProxyAgentType.GetMethod("Foo", new Type[] { typeof(OfflineBehavior) })));
        }
        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            newValue = null;
            if (currentValue == null)
            {
                CodeClass currentDSAClass = ExpressionEvaluationHelper.EvaluateExpression((IDictionaryService)GetService(typeof(IDictionaryService)),
                                                                                          currentDSAClassExpression) as CodeClass;
                Type proxyType = ExpressionEvaluationHelper.EvaluateExpression((IDictionaryService)GetService(typeof(IDictionaryService)),
                                                                               proxyTypeExpression) as Type;

                if (currentDSAClass != null && proxyType != null)
                {
                    Type dsaType = DteConverter.ToType(currentDSAClass);
                    if (dsaType != null)
                    {
                        newValue = MethodBehaviorHelper.GetMethodsBehavior(dsaType, proxyType);
                    }
                }
            }
            return(currentValue != newValue);
        }
Ejemplo n.º 13
0
        private KeyValuePair <string, MethodBehavior> CreateBehavior(object behavior)
        {
            var behaviorDict = (Dictionary <object, object>)behavior;

            string nameSpace = null;

            if (behaviorDict.TryGetValue("Namespace", out object value))
            {
                nameSpace = (string)value;
            }

            string className = null;

            if (behaviorDict.TryGetValue("ClassName", out value))
            {
                className = (string)value;
            }

            string name = null;

            if (behaviorDict.TryGetValue("Name", out value))
            {
                name = (string)value;
            }

            string argTypes = null;
            Dictionary <object, object> ifCondition         = null;
            IReadOnlyList <object>      injectableArguments = null;
            Dictionary <object, object> condition           = null;

            Dictionary <object, object> method = null;

            if (behaviorDict.TryGetValue("Method", out value))
            {
                method = (Dictionary <object, object>)value;
                if (method.TryGetValue("ArgTypes", out value))
                {
                    argTypes = (string)value;
                    ValidateArgTypes(argTypes, nameSpace, className, name);
                }

                if (method.TryGetValue("If", out value))
                {
                    ifCondition = (Dictionary <object, object>)value;
                }

                if (method.TryGetValue("InjectableArguments", out value))
                {
                    injectableArguments = (IReadOnlyList <object>)value;
                }

                if (method.TryGetValue("Condition", out value))
                {
                    condition = (Dictionary <object, object>)value;
                    ValidateCondition(condition);
                }
            }

            object injectable = null;

            if (behaviorDict.TryGetValue("Field", out value))
            {
                var field = (Dictionary <object, object>)value;

                if (field.TryGetValue("Injectable", out value))
                {
                    injectable = value;
                }
            }

            var key = MethodBehaviorHelper.GetMethodBehaviorKey(nameSpace, className, name, argTypes);

            var mainPostConditions = GetPostConditions(method);

            return(new KeyValuePair <string, MethodBehavior>(key, new MethodBehavior(condition,
                                                                                     GetPreConditions(ifCondition, mainPostConditions),
                                                                                     mainPostConditions,
                                                                                     GetInjectableArguments(injectableArguments),
                                                                                     GetField(injectable))));
        }
Ejemplo n.º 14
0
        public void MergeWith(ConfigData config)
        {
            if (config.TaintTypes != null)
            {
                RegisterTaintTypes(config.TaintTypes);
            }

            if (config.ReportAnalysisCompletion.HasValue)
            {
                ReportAnalysisCompletion = config.ReportAnalysisCompletion.Value;
            }

            if (config.AuditMode.HasValue)
            {
                AuditMode = config.AuditMode.Value;
            }

            if (config.MinimumPasswordValidatorProperties.HasValue)
            {
                MinimumPasswordValidatorProperties = config.MinimumPasswordValidatorProperties.Value;
            }

            if (config.PasswordValidatorRequiredLength.HasValue)
            {
                PasswordValidatorRequiredLength = config.PasswordValidatorRequiredLength.Value;
            }

            if (config.PasswordValidatorRequiredProperties != null)
            {
                foreach (var property in config.PasswordValidatorRequiredProperties)
                {
                    _PasswordValidatorRequiredProperties.Add(property);
                }
            }

            if (config.Behavior != null)
            {
                foreach (var behavior in config.Behavior)
                {
                    if (behavior.Value == default(MethodBehaviorData))
                    {
                        ConfigurationBehavior.Remove(behavior.Key);
                    }
                    else
                    {
                        ConfigurationBehavior[behavior.Key] = CreateBehavior(behavior.Value);
                    }
                }
            }

            if (config.TaintEntryPoints != null)
            {
                foreach (var source in config.TaintEntryPoints)
                {
                    if (source.Value == default(TaintEntryPointData))
                    {
                        _TaintEntryPoints.Remove(source.Key);
                    }
                    else
                    {
                        if (source.Value?.Method?.ArgTypes != null)
                        {
                            throw new Exception("Taint entry point ArgTypes are not supported.");
                        }

                        _TaintEntryPoints.Add(MethodBehaviorHelper.GetMethodBehaviorKey(source.Value.Namespace,
                                                                                        source.Value.ClassName,
                                                                                        source.Value.Name,
                                                                                        source.Value?.Method?.ArgTypes));
                    }
                }
            }

            if (config.CsrfProtection != null)
            {
                foreach (var data in config.CsrfProtection)
                {
                    if (data.Class == null && data.AntiCsrfAttributes == null && data.Method == null && data.Parameter == null)
                    {
                        if (_CsrfGroups.TryGetValue(data.Name, out var node))
                        {
                            _CsrfGroupsList.Remove(node);
                            _CsrfGroups.Remove(data.Name);
                        }
                    }
                    else
                    {
                        AddCsrfProtectionToConfiguration(data);
                    }
                }
            }

            if (config.PasswordFields != null)
            {
                foreach (var field in config.PasswordFields)
                {
                    _PasswordFields.Add(field.ToUpperInvariant());
                }
            }

            if (config.WebConfigFiles != null)
            {
                WebConfigFilesRegex = new Regex(config.WebConfigFiles, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            }

            if (config.ConstantFields != null)
            {
                foreach (var field in config.ConstantFields)
                {
                    _ConstantFields.Add(field);
                }
            }
        }
Ejemplo n.º 15
0
        public void MergeWith(ConfigData config)
        {
            if (config.TaintTypes != null)
            {
                RegisterTaintTypes(config.TaintTypes);
            }

            if (config.AuditMode.HasValue)
            {
                AuditMode = config.AuditMode.Value;
            }

            if (config.MinimumPasswordValidatorProperties.HasValue)
            {
                MinimumPasswordValidatorProperties = config.MinimumPasswordValidatorProperties.Value;
            }

            if (config.PasswordValidatorRequiredLength.HasValue)
            {
                PasswordValidatorRequiredLength = config.PasswordValidatorRequiredLength.Value;
            }

            if (config.PasswordValidatorRequiredProperties != null)
            {
                foreach (var property in config.PasswordValidatorRequiredProperties)
                {
                    _PasswordValidatorRequiredProperties.Add(property);
                }
            }

            if (config.Behavior != null)
            {
                foreach (var behavior in config.Behavior)
                {
                    if (behavior.Value == default(MethodBehaviorData))
                    {
                        ConfigurationBehavior.Remove(behavior.Key);
                    }
                    else
                    {
                        ConfigurationBehavior[behavior.Key] = CreateBehavior(behavior.Value);
                    }
                }
            }

            if (config.TaintEntryPoints != null)
            {
                foreach (var source in config.TaintEntryPoints)
                {
                    if (source.Value == default(TaintEntryPointData))
                    {
                        _TaintEntryPoints.Remove(source.Key);
                    }
                    else
                    {
                        if (source.Value?.Method?.ArgTypes != null)
                        {
                            throw new Exception("Taint entry point ArgTypes are not supported.");
                        }

                        _TaintEntryPoints.Add(MethodBehaviorHelper.GetMethodBehaviorKey(source.Value.Namespace,
                                                                                        source.Value.ClassName,
                                                                                        source.Value.Name,
                                                                                        source.Value?.Method?.ArgTypes));
                    }
                }
            }

            if (config.CsrfProtectionAttributes != null)
            {
                foreach (var data in config.CsrfProtectionAttributes)
                {
                    AddAntiCsrfTAttributeToConfiguration(data);
                }
            }

            if (config.PasswordFields != null)
            {
                foreach (var field in config.PasswordFields)
                {
                    _PasswordFields.Add(field.ToUpperInvariant());
                }
            }

            if (config.ConstantFields != null)
            {
                foreach (var field in config.ConstantFields)
                {
                    _ConstantFields.Add(field);
                }
            }
        }