Ejemplo n.º 1
0
        private void InitializeRequestFilter(IRequestFilter requestFilter)
        {
            var factory = new RuleFactory();

            foreach (var item in appSettings.AllowedUrlRegexps)
            {
                var rule = factory.CreateRule(FilterRuleType.Regex);
                rule.Initialize(new FilterRuleSettings {
                    Expression = item, Result = FilterResult.Allow
                });
                requestFilter.Load(rule);
            }
            foreach (var item in appSettings.AllowedUrls)
            {
                var rule = factory.CreateRule(FilterRuleType.Simplified);
                rule.Initialize(new FilterRuleSettings {
                    Expression = item, Result = FilterResult.Allow
                });
                requestFilter.Load(rule);
            }

            if (requestFilter.Process(new Request {
                Url = startUrl
            }) != FilterResult.Allow)
            {
                var rule = factory.CreateRule(FilterRuleType.Simplified);
                rule.Initialize(new FilterRuleSettings {
                    Expression = startUrl, Result = FilterResult.Allow
                });
                requestFilter.Load(rule);
                logger.Debug($"Automatically created filter rule to allow start URL '{startUrl}'.");
            }
        }
        private void InitializeRequestFilter(IRequestFilter requestFilter)
        {
            if (settings.Filter.ProcessContentRequests || settings.Filter.ProcessMainRequests)
            {
                var factory = new RuleFactory();

                foreach (var settings in settings.Filter.Rules)
                {
                    var rule = factory.CreateRule(settings.Type);

                    rule.Initialize(settings);
                    requestFilter.Load(rule);
                }

                logger.Debug($"Initialized request filter with {settings.Filter.Rules.Count} rule(s).");

                if (requestFilter.Process(new Request {
                    Url = settings.StartUrl
                }) != FilterResult.Allow)
                {
                    var rule = factory.CreateRule(FilterRuleType.Simplified);

                    rule.Initialize(new FilterRuleSettings {
                        Expression = settings.StartUrl, Result = FilterResult.Allow
                    });
                    requestFilter.Load(rule);

                    logger.Debug($"Automatically created filter rule to allow start URL{(WindowSettings.UrlPolicy.CanLog() ? $" '{settings.StartUrl}'" : "")}.");
                }
            }
        }
Ejemplo n.º 3
0
        private void InitializeControl()
        {
            var contextMenuHandler = new ContextMenuHandler();
            var displayHandler     = new DisplayHandler();
            var downloadLogger     = logger.CloneFor($"{nameof(DownloadHandler)} {Id}");
            var downloadHandler    = new DownloadHandler(appConfig, settings, downloadLogger);
            var keyboardHandler    = new KeyboardHandler();
            var lifeSpanHandler    = new LifeSpanHandler();
            var requestFilter      = new RequestFilter();
            var requestLogger      = logger.CloneFor($"{nameof(RequestHandler)} {Id}");
            var requestHandler     = new RequestHandler(appConfig, settings.Filter, requestFilter, requestLogger, text);

            displayHandler.FaviconChanged  += DisplayHandler_FaviconChanged;
            displayHandler.ProgressChanged += DisplayHandler_ProgressChanged;
            downloadHandler.ConfigurationDownloadRequested += DownloadHandler_ConfigurationDownloadRequested;
            keyboardHandler.ReloadRequested    += ReloadRequested;
            keyboardHandler.ZoomInRequested    += ZoomInRequested;
            keyboardHandler.ZoomOutRequested   += ZoomOutRequested;
            keyboardHandler.ZoomResetRequested += ZoomResetRequested;
            lifeSpanHandler.PopupRequested     += LifeSpanHandler_PopupRequested;
            requestHandler.RequestBlocked      += RequestHandler_RequestBlocked;

            if (settings.Filter.ProcessContentRequests || settings.Filter.ProcessMainRequests)
            {
                var factory = new RuleFactory();

                foreach (var settings in settings.Filter.Rules)
                {
                    var rule = factory.CreateRule(settings.Type);

                    rule.Initialize(settings);
                    requestFilter.Load(rule);
                }

                logger.Debug($"Initialized request filter with {settings.Filter.Rules.Count} rule(s).");
            }

            control = new BrowserControl(contextMenuHandler, displayHandler, downloadHandler, keyboardHandler, lifeSpanHandler, requestHandler, url);
            control.AddressChanged      += Control_AddressChanged;
            control.LoadingStateChanged += Control_LoadingStateChanged;
            control.TitleChanged        += Control_TitleChanged;

            control.Initialize();
            logger.Debug("Initialized browser control.");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Main entry-point for this application.
        /// </summary>
        static void Main()
        {
            // Create a SettingsHelper instance by calling SettingsHelper
            // .NewConfiguration, as you see here, for each plug-in, if that
            // is the scenario you are working on, or for each application,
            // where you want to log data. It will help to make sure that
            // changes to the configuration in one plug-in don't interfere
            // with another NLog configuration for another plug-in.
            var newConfiguration = SettingsHelper.NewConfiguration("NLogConfigTest",
                                                                   "YoderZone");

            // You will need to create at least one target and one rule. The factory
            // classes in NLogConfig can help with both.
            var fileTarget = FileTargetFactory.CreateFileTarget("TestLog",
                                                                "Test${shortdate}.log", newConfiguration);

            // The loggerNamePattern in CreateRule must match the name of the
            // Logger that will save messages for this rule. Usually the logger will
            // be named the same as the class it has been created in. In this test
            // for example the name of the Logger is "YoderZone.NLogConfig.Text.Program."
            // You can give it any name you wish, but remember to configure a pattern
            // to match its name in the rule.
            var loggingRule = RuleFactory.CreateRule("YoderZone.NLogConfig.Test.*",
                                                     fileTarget);

            // The targets and rules need to be added to the SettingsHelper
            // instance created with the NewConfiguration command. Set the
            // deferUpdate parameter to true for all additions until the last one.
            // When the last addition occurs, the SettingsHelper instance will
            // update NLog so it knows about the new settings.
            newConfiguration.AddTarget(fileTarget, true);
            newConfiguration.AddRule("TestLog", loggingRule);

            // Then use the Logger in the class as you would normally by
            // calling its methods so send logging messages.
            logger.Debug("Debug");
            logger.Error("Error");
            logger.Info("Info");
        }
Ejemplo n.º 5
0
 public void SelectRuleType(int type)
 {
     rule = RuleFactory.CreateRule(type);
 }
 public void MustCreateCorrectRules()
 {
     Assert.IsInstanceOfType(sut.CreateRule(FilterRuleType.Regex), typeof(RegexRule));
     Assert.IsInstanceOfType(sut.CreateRule(FilterRuleType.Simplified), typeof(SimplifiedRule));
 }
        public void CreateRule_GivenRuleDataModelWithComposedNodeAndChildNodesOfEachDataType_ReturnsRuleInstance()
        {
            // Arrange
            dynamic content = new ExpandoObject();

            content.Prop1 = 123;
            content.Prop2 = "Sample string";
            content.Prop3 = 500.34m;

            ValueConditionNodeDataModel integerConditionNodeDataModel = new ValueConditionNodeDataModel
            {
                ConditionType   = "SampleIntegerCondition",
                DataType        = DataTypes.Integer,
                LogicalOperator = LogicalOperators.Eval,
                Operand         = 20,
                Operator        = Operators.GreaterThan
            };

            ValueConditionNodeDataModel stringConditionNodeDataModel = new ValueConditionNodeDataModel
            {
                ConditionType   = "SampleStringCondition",
                DataType        = DataTypes.String,
                LogicalOperator = LogicalOperators.Eval,
                Operand         = "TEST",
                Operator        = Operators.Equal
            };

            ValueConditionNodeDataModel decimalConditionNodeDataModel = new ValueConditionNodeDataModel
            {
                ConditionType   = "SampleDecimalCondition",
                DataType        = DataTypes.Decimal,
                LogicalOperator = LogicalOperators.Eval,
                Operand         = 50.3m,
                Operator        = Operators.LesserThanOrEqual
            };

            ValueConditionNodeDataModel booleanConditionNodeDataModel = new ValueConditionNodeDataModel
            {
                ConditionType   = "SampleBooleanCondition",
                DataType        = DataTypes.Boolean,
                LogicalOperator = LogicalOperators.Eval,
                Operand         = true,
                Operator        = Operators.NotEqual
            };

            RuleDataModel ruleDataModel = new RuleDataModel
            {
                Content       = content,
                ContentType   = "ContentTypeSample",
                DateBegin     = new System.DateTime(2020, 1, 1),
                DateEnd       = null,
                Id            = ObjectId.GenerateNewId(),
                Name          = "My rule used for testing purposes",
                Priority      = 1,
                RootCondition = new ComposedConditionNodeDataModel
                {
                    LogicalOperator     = LogicalOperators.And,
                    ChildConditionNodes = new ConditionNodeDataModel[]
                    {
                        integerConditionNodeDataModel,
                        stringConditionNodeDataModel,
                        decimalConditionNodeDataModel,
                        booleanConditionNodeDataModel
                    }
                }
            };

            IContentSerializationProvider <ContentType> contentSerializationProvider = Mock.Of <IContentSerializationProvider <ContentType> >();

            RuleFactory <ContentType, ConditionType> ruleFactory = new RuleFactory <ContentType, ConditionType>(contentSerializationProvider);

            // Act
            Rule <ContentType, ConditionType> rule = ruleFactory.CreateRule(ruleDataModel);

            // Assert
            rule.Should().NotBeNull();
            rule.ContentContainer.Should().NotBeNull()
            .And.BeOfType <SerializedContentContainer <ContentType> >();
            rule.DateBegin.Should().Be(ruleDataModel.DateBegin);
            rule.DateEnd.Should().BeNull();
            rule.Name.Should().Be(ruleDataModel.Name);
            rule.Priority.Should().Be(ruleDataModel.Priority);
            rule.RootCondition.Should().BeOfType <ComposedConditionNode <ConditionType> >();

            ComposedConditionNode <ConditionType> composedConditionNode = rule.RootCondition.As <ComposedConditionNode <ConditionType> >();

            composedConditionNode.LogicalOperator.Should().Be(LogicalOperators.And);
            composedConditionNode.ChildConditionNodes.Should().HaveCount(4);

            IEnumerable <IntegerConditionNode <ConditionType> > integerConditionNodes = composedConditionNode.ChildConditionNodes.OfType <IntegerConditionNode <ConditionType> >();

            integerConditionNodes.Should().HaveCount(1);
            IntegerConditionNode <ConditionType> integerConditionNode = integerConditionNodes.First();

            integerConditionNode.Should().NotBeNull();
            integerConditionNode.ConditionType.Should().Match <ConditionType>(x => x == Enum.Parse <ConditionType>(integerConditionNodeDataModel.ConditionType));
            integerConditionNode.DataType.Should().Be(integerConditionNodeDataModel.DataType);
            integerConditionNode.LogicalOperator.Should().Be(integerConditionNodeDataModel.LogicalOperator);
            integerConditionNode.Operand.Should().Match(x => object.Equals(x, integerConditionNodeDataModel.Operand));
            integerConditionNode.Operator.Should().Be(integerConditionNodeDataModel.Operator);

            IEnumerable <StringConditionNode <ConditionType> > stringConditionNodes = composedConditionNode.ChildConditionNodes.OfType <StringConditionNode <ConditionType> >();

            stringConditionNodes.Should().HaveCount(1);
            StringConditionNode <ConditionType> stringConditionNode = stringConditionNodes.First();

            stringConditionNode.Should().NotBeNull();
            stringConditionNode.ConditionType.Should().Match <ConditionType>(x => x == Enum.Parse <ConditionType>(stringConditionNodeDataModel.ConditionType));
            stringConditionNode.DataType.Should().Be(stringConditionNodeDataModel.DataType);
            stringConditionNode.LogicalOperator.Should().Be(stringConditionNodeDataModel.LogicalOperator);
            stringConditionNode.Operand.Should().Match(x => object.Equals(x, stringConditionNodeDataModel.Operand));
            stringConditionNode.Operator.Should().Be(stringConditionNodeDataModel.Operator);

            IEnumerable <DecimalConditionNode <ConditionType> > decimalConditionNodes = composedConditionNode.ChildConditionNodes.OfType <DecimalConditionNode <ConditionType> >();

            decimalConditionNodes.Should().HaveCount(1);
            DecimalConditionNode <ConditionType> decimalConditionNode = decimalConditionNodes.First();

            decimalConditionNode.Should().NotBeNull();
            decimalConditionNode.ConditionType.Should().Match <ConditionType>(x => x == Enum.Parse <ConditionType>(decimalConditionNodeDataModel.ConditionType));
            decimalConditionNode.DataType.Should().Be(decimalConditionNodeDataModel.DataType);
            decimalConditionNode.LogicalOperator.Should().Be(decimalConditionNodeDataModel.LogicalOperator);
            decimalConditionNode.Operand.Should().Match(x => object.Equals(x, decimalConditionNodeDataModel.Operand));
            decimalConditionNode.Operator.Should().Be(decimalConditionNodeDataModel.Operator);

            IEnumerable <BooleanConditionNode <ConditionType> > booleanConditionNodes = composedConditionNode.ChildConditionNodes.OfType <BooleanConditionNode <ConditionType> >();

            booleanConditionNodes.Should().HaveCount(1);
            BooleanConditionNode <ConditionType> booleanConditionNode = booleanConditionNodes.First();

            booleanConditionNode.Should().NotBeNull();
            booleanConditionNode.ConditionType.Should().Match <ConditionType>(x => x == Enum.Parse <ConditionType>(booleanConditionNodeDataModel.ConditionType));
            booleanConditionNode.DataType.Should().Be(booleanConditionNodeDataModel.DataType);
            booleanConditionNode.LogicalOperator.Should().Be(booleanConditionNodeDataModel.LogicalOperator);
            booleanConditionNode.Operand.Should().Be(Convert.ToBoolean(booleanConditionNodeDataModel.Operand));
            booleanConditionNode.Operator.Should().Be(booleanConditionNodeDataModel.Operator);
        }
        public void CreateRule_GivenRuleWithComposedNodeAndChildNodesOfEachDataType_ReturnsRuleDataModelInstance()
        {
            // Arrange
            dynamic content = new ExpandoObject();

            content.Prop1 = 123;
            content.Prop2 = "Sample string";
            content.Prop3 = 500.34m;

            IContentSerializer contentSerializer = Mock.Of <IContentSerializer>();

            Mock.Get(contentSerializer)
            .Setup(x => x.Deserialize(It.IsAny <object>(), It.IsAny <Type>()))
            .Returns((object)content);

            IContentSerializationProvider <ContentType> contentSerializationProvider = Mock.Of <IContentSerializationProvider <ContentType> >();

            Mock.Get(contentSerializationProvider)
            .Setup(x => x.GetContentSerializer(ContentType.ContentTypeSample))
            .Returns(contentSerializer);

            BooleanConditionNode <ConditionType> booleanConditionNode = null;
            DecimalConditionNode <ConditionType> decimalConditionNode = null;
            IntegerConditionNode <ConditionType> integerConditionNode = null;
            StringConditionNode <ConditionType>  stringConditionNode  = null;

            Rule <ContentType, ConditionType> rule1 = RuleBuilder.NewRule <ContentType, ConditionType>()
                                                      .WithName("My rule used for testing purposes")
                                                      .WithDateBegin(new DateTime(2020, 1, 1))
                                                      .WithPriority(1)
                                                      .WithSerializedContent(ContentType.ContentTypeSample, (object)content, contentSerializationProvider)
                                                      .WithCondition(cnb => cnb.AsComposed()
                                                                     .WithLogicalOperator(LogicalOperators.And)
                                                                     .AddCondition(cnb1 => booleanConditionNode = cnb1.AsValued(ConditionType.SampleBooleanCondition)
                                                                                                                  .OfDataType <bool>()
                                                                                                                  .WithComparisonOperator(Operators.NotEqual)
                                                                                                                  .SetOperand(true)
                                                                                                                  .Build() as BooleanConditionNode <ConditionType>)
                                                                     .AddCondition(cnb1 => decimalConditionNode = cnb1.AsValued(ConditionType.SampleDecimalCondition)
                                                                                                                  .OfDataType <decimal>()
                                                                                                                  .WithComparisonOperator(Operators.LesserThanOrEqual)
                                                                                                                  .SetOperand(50.3m)
                                                                                                                  .Build() as DecimalConditionNode <ConditionType>)
                                                                     .AddCondition(cnb1 => integerConditionNode = cnb1.AsValued(ConditionType.SampleIntegerCondition)
                                                                                                                  .OfDataType <int>()
                                                                                                                  .WithComparisonOperator(Operators.GreaterThan)
                                                                                                                  .SetOperand(20)
                                                                                                                  .Build() as IntegerConditionNode <ConditionType>)
                                                                     .AddCondition(cnb1 => stringConditionNode = cnb1.AsValued(ConditionType.SampleStringCondition)
                                                                                                                 .OfDataType <string>()
                                                                                                                 .WithComparisonOperator(Operators.Equal)
                                                                                                                 .SetOperand("TEST")
                                                                                                                 .Build() as StringConditionNode <ConditionType>)
                                                                     .Build())
                                                      .Build().Rule;

            RuleFactory <ContentType, ConditionType> ruleFactory = new RuleFactory <ContentType, ConditionType>(contentSerializationProvider);

            // Act
            RuleDataModel rule = ruleFactory.CreateRule(rule1);

            // Assert
            rule.Should().NotBeNull();
            object content1 = rule.Content;

            content1.Should().NotBeNull()
            .And.BeSameAs(content);
            rule.DateBegin.Should().Be(rule.DateBegin);
            rule.DateEnd.Should().BeNull();
            rule.Name.Should().Be(rule.Name);
            rule.Priority.Should().Be(rule.Priority);
            rule.RootCondition.Should().BeOfType <ComposedConditionNodeDataModel>();

            ComposedConditionNodeDataModel composedConditionNodeDataModel = rule.RootCondition.As <ComposedConditionNodeDataModel>();

            composedConditionNodeDataModel.LogicalOperator.Should().Be(LogicalOperators.And);
            composedConditionNodeDataModel.ChildConditionNodes.Should().HaveCount(4);

            IEnumerable <ValueConditionNodeDataModel> valueConditionNodeDataModels = composedConditionNodeDataModel.ChildConditionNodes.OfType <ValueConditionNodeDataModel>();

            valueConditionNodeDataModels.Should().HaveCount(4);
            ValueConditionNodeDataModel integerConditionNodeDataModel = valueConditionNodeDataModels.First(v => v.DataType == DataTypes.Integer);

            integerConditionNodeDataModel.Should().NotBeNull();
            integerConditionNodeDataModel.ConditionType.Should().Match <string>(x => integerConditionNode.ConditionType == Enum.Parse <ConditionType>(x));
            integerConditionNodeDataModel.DataType.Should().Be(integerConditionNode.DataType);
            integerConditionNodeDataModel.LogicalOperator.Should().Be(integerConditionNode.LogicalOperator);
            integerConditionNodeDataModel.Operand.Should().Match(x => object.Equals(x, integerConditionNode.Operand));
            integerConditionNodeDataModel.Operator.Should().Be(integerConditionNode.Operator);

            ValueConditionNodeDataModel stringConditionNodeDataModel = valueConditionNodeDataModels.First(v => v.DataType == DataTypes.String);

            stringConditionNodeDataModel.Should().NotBeNull();
            stringConditionNodeDataModel.ConditionType.Should().Match <string>(x => stringConditionNode.ConditionType == Enum.Parse <ConditionType>(x));
            stringConditionNodeDataModel.DataType.Should().Be(stringConditionNode.DataType);
            stringConditionNodeDataModel.LogicalOperator.Should().Be(stringConditionNode.LogicalOperator);
            stringConditionNodeDataModel.Operand.Should().Match(x => object.Equals(x, stringConditionNode.Operand));
            stringConditionNodeDataModel.Operator.Should().Be(stringConditionNode.Operator);

            ValueConditionNodeDataModel decimalConditionNodeDataModel = valueConditionNodeDataModels.First(v => v.DataType == DataTypes.Decimal);

            decimalConditionNodeDataModel.Should().NotBeNull();
            decimalConditionNodeDataModel.ConditionType.Should().Match <string>(x => decimalConditionNode.ConditionType == Enum.Parse <ConditionType>(x));
            decimalConditionNodeDataModel.DataType.Should().Be(decimalConditionNode.DataType);
            decimalConditionNodeDataModel.LogicalOperator.Should().Be(decimalConditionNode.LogicalOperator);
            decimalConditionNodeDataModel.Operand.Should().Match(x => object.Equals(x, decimalConditionNode.Operand));
            decimalConditionNodeDataModel.Operator.Should().Be(decimalConditionNode.Operator);

            ValueConditionNodeDataModel booleanConditionNodeDataModel = valueConditionNodeDataModels.First(v => v.DataType == DataTypes.Boolean);

            booleanConditionNodeDataModel.Should().NotBeNull();
            booleanConditionNodeDataModel.ConditionType.Should().Match <string>(x => booleanConditionNode.ConditionType == Enum.Parse <ConditionType>(x));
            booleanConditionNodeDataModel.DataType.Should().Be(booleanConditionNode.DataType);
            booleanConditionNodeDataModel.LogicalOperator.Should().Be(booleanConditionNode.LogicalOperator);
            booleanConditionNodeDataModel.Operand.Should().Be(Convert.ToBoolean(booleanConditionNode.Operand));
            booleanConditionNodeDataModel.Operator.Should().Be(booleanConditionNode.Operator);
        }
        public void CreateRule_GivenNullRuleDataModel_ThrowsArgumentNullException()
        {
            // Arrange
            RuleDataModel ruleDataModel = null;

            IContentSerializationProvider <ContentType> contentSerializationProvider = Mock.Of <IContentSerializationProvider <ContentType> >();

            RuleFactory <ContentType, ConditionType> ruleFactory = new RuleFactory <ContentType, ConditionType>(contentSerializationProvider);

            // Act
            ArgumentNullException argumentNullException = Assert.Throws <ArgumentNullException>(() => ruleFactory.CreateRule(ruleDataModel));

            // Assert
            argumentNullException.Should().NotBeNull();
            argumentNullException.ParamName.Should().Be(nameof(ruleDataModel));
        }
Ejemplo n.º 10
0
        public void CreateRule_GivenNullRule_ThrowsArgumentNullException()
        {
            // Arrange
            Rule <ContentType, ConditionType> rule = null;

            RuleFactory <ContentType, ConditionType> ruleFactory = new RuleFactory <ContentType, ConditionType>();

            // Act
            ArgumentNullException argumentNullException = Assert.Throws <ArgumentNullException>(() => ruleFactory.CreateRule(rule));

            // Assert
            argumentNullException.Should().NotBeNull();
            argumentNullException.ParamName.Should().Be(nameof(rule));
        }
Ejemplo n.º 11
0
        public void CreateRule_GivenInvalidRuleName_ThrowsInvalidRuleException()
        {
            // Arrange
            dynamic content = new ExpandoObject();

            content.Prop1 = 123;
            content.Prop2 = "Sample string";
            content.Prop3 = 500.34m;

            RuleDataModel <ContentType, ConditionType> ruleDataModel = new RuleDataModel <ContentType, ConditionType>
            {
                Content       = content,
                ContentType   = ContentType.ContentTypeSample,
                DateBegin     = new DateTime(2020, 1, 1),
                DateEnd       = null,
                Name          = null,
                Priority      = 1,
                RootCondition = null
            };

            RuleFactory <ContentType, ConditionType> ruleFactory = new RuleFactory <ContentType, ConditionType>();

            // Act
            InvalidRuleException invalidRuleException = Assert.Throws <InvalidRuleException>(() => ruleFactory.CreateRule(ruleDataModel));

            // Assert
            invalidRuleException.Should().NotBeNull();
            invalidRuleException.Errors.Should().NotBeEmpty();
            invalidRuleException.Errors.Should().Contain("'Name' must not be empty.");
        }