internal XmlElement CreateResultsNode(XmlDocument xmlDocument, WsScanRule scanRule, WsMatchInRangeCollection results) { if(null == xmlDocument) throw new WsException("Null xml document pointer passed in"); if(null == scanRule) throw new WsException("Null scan rule passed in"); if(null == results) throw new WsException("Null results passed in"); XmlElement resultsNode = xmlDocument.CreateElement("results"); foreach(WsMatchInRange result in results) { foreach(string key in result.Matches.Keys) { XmlElement resultNode = CreateResultNode(xmlDocument, scanRule, result, key); if(null != resultNode) resultsNode.AppendChild(resultNode); } } if(0 == resultsNode.ChildNodes.Count) return null; return resultsNode; }
internal XmlElement CreateResultNode(XmlDocument xmlDocument, WsScanRule scanRule, WsMatchInRange result, string key) { if(null == xmlDocument) throw new WsException("Null xml document pointer passed in"); if(null == scanRule) throw new WsException("Null scan rule passed in"); if(null == result) throw new WsException("Null result passed in"); if(scanRule.Condition != result.Condition) return null; if((scanRule.Ranges.Count != 0) && (!scanRule.Ranges.Contains(result.RangeType))) return null; XmlElement resultNode = xmlDocument.CreateElement("result"); XmlAttribute matchAttribute = xmlDocument.CreateAttribute("match"); matchAttribute.Value = key; resultNode.Attributes.Append(matchAttribute); XmlAttribute contextAttribute = xmlDocument.CreateAttribute("context"); contextAttribute.Value = result.RangeType.ToString(); resultNode.Attributes.Append(contextAttribute); XmlAttribute countAttribute = xmlDocument.CreateAttribute("count"); countAttribute.Value = Convert.ToString(result.Matches[key]); resultNode.Attributes.Append(countAttribute); return resultNode; }
public void TestCreateRuleNullXmlDocument() { WsScanRules scanRules = new WsScanRules(); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); WsScanRule findSomeRule = new WsScanRule("Find some", "FindSomeIndex", "some", RuleLevels.High); xmlContentBuilder.CreateRuleNode(null, findSomeRule); }
public void TestConstructor() { WsScanRule scanRule = new WsScanRule("Test", "TestCondition", "(?:Test)", RuleLevels.High); Assert.AreEqual("Test", scanRule.Name); Assert.AreEqual("TestCondition", scanRule.Condition); Assert.AreEqual("(?:test)", scanRule.Expression); Assert.AreEqual(RuleLevels.High, scanRule.Level); Assert.AreEqual(0, scanRule.Ranges.Count); }
public void TestCreateResultNodeNullXmlDocument() { const string findSomeIndex = "FindSomeIndex"; WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Find some", findSomeIndex, "some", RuleLevels.High); scanRules.Add(findSomeRule); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); WsMatchInRange result = new WsMatchInRange(RangeTypes.Paragraph, findSomeIndex, new ArrayList()); xmlContentBuilder.CreateResultNode(null, findSomeRule, result, ""); }
public void TestCreateRuleNode() { WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Test rule", "Find some", "some", RuleLevels.High); scanRules.Add(findSomeRule); XmlDocument xmlDocument = new XmlDocument(); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); XmlElement ruleElement = xmlContentBuilder.CreateRuleNode(xmlDocument, findSomeRule); Assert.IsNotNull(ruleElement); Assert.AreEqual("<rule name=\"Find some\" level=\"High\" />", ruleElement.OuterXml); }
internal WsScanRules LoadScanRulesFromPolicy() { XmlDocument scanPolicy = GetPolicyFromDisk(); if(scanPolicy == null) scanPolicy = GetDefaultPolicy(); XmlNodeList rulesetNodes = scanPolicy.SelectNodes("/project/ruleset"); if(rulesetNodes == null) throw new WsException("Invalid policy file"); WsScanRules scanRules = new WsScanRules(); foreach(XmlNode rulesetNode in rulesetNodes) { foreach(XmlNode ruleNode in rulesetNode.ChildNodes) { XmlAttribute nameAttribute = ruleNode.Attributes["name"]; if (nameAttribute == null) continue; string rulename = nameAttribute.Value; XmlAttribute levelAttribute = ruleNode.Attributes["level"]; if (levelAttribute == null) continue; RuleLevels ruleLevel = RuleLevelNameToEnum(levelAttribute.Value); foreach(XmlNode conditionNode in ruleNode.ChildNodes) { XmlAttribute conditionAttribute = conditionNode.Attributes["index"]; if (conditionAttribute == null) continue; string conditionIndex = conditionAttribute.Value; XmlAttribute operandAttribute = conditionNode.Attributes["operand"]; if (operandAttribute == null) continue; string expression = operandAttribute.Value; WsScanRule scanRule = new WsScanRule(rulename, conditionIndex, expression, ruleLevel); foreach(XmlNode contextNode in conditionNode.ChildNodes) { XmlAttribute nameAttribute2 = contextNode.Attributes["name"]; if(nameAttribute2 == null) continue; scanRule.Ranges.Add(nameAttribute2.Value); } scanRules.Add(scanRule); } } } return scanRules; }
public IResultSet Process(string expression, params RangeTypes[] types) { WsScanRules rules = new WsScanRules(); WsScanRule rule = new WsScanRule("Find", "FindIndex", expression, RuleLevels.High); foreach(RangeTypes type in types) rule.Ranges.Add(type); rules.Add(rule); WsDocumentScanner scanner = new WsDocumentScanner(m_filename, rules); return scanner.Match(); }
internal XmlElement CreateRuleNode(XmlDocument xmlDocument, WsScanRule scanRule) { if(null == xmlDocument) throw new WsException("Null xml document pointer passed in"); if(null == scanRule) throw new WsException("Null scan rule passed in"); XmlElement ruleElement = xmlDocument.CreateElement("rule"); XmlAttribute nameAttribute = xmlDocument.CreateAttribute("name"); nameAttribute.Value = scanRule.Condition; ruleElement.Attributes.Append(nameAttribute); XmlAttribute levelAttribute = xmlDocument.CreateAttribute("level"); levelAttribute.Value = scanRule.Level.ToString(); ruleElement.Attributes.Append(levelAttribute); return ruleElement; }
public void TestConstructorNullExpression() { WsScanRule scanRule = new WsScanRule("Test", "TestCondition", null, RuleLevels.High); }
public void TestSetRegularExpression() { WsScanRule scanRule = new WsScanRule("Test", "TestCondition", "Test", RuleLevels.High); scanRule.SetRegularExpression("This is a test"); Assert.AreEqual("this is a test", scanRule.Expression); }
public void TestConstructorInvalidExpression() { WsScanRule scanRule = new WsScanRule("Test", "TestCondition", "", RuleLevels.High); }
public void TestBuildContentNoResults() { const string expectedContent = "<DISRules />"; WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Rule 1", "Find not exist", "not exist", RuleLevels.High); findSomeRule.Ranges.Add(RangeTypes.Paragraph); scanRules.Add(findSomeRule); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); string xmlContent = xmlContentBuilder.BuildContent(new WsMatchInRangeCollection()); Assert.AreEqual(expectedContent, xmlContent); }
public void TestSetRegularExpressionInvalidObject() { WsScanRule scanRule = new WsScanRule("Test", "TestCondition", "Test", RuleLevels.High); scanRule.SetRegularExpression(new WsException("This is a test.")); }
public void TestBuildContent() { string expectedContent = "<DISRules>" + "<rule name=\"Find some\" level=\"High\">" + "<results>" + "<result match=\"some\" context=\"Paragraph\" count=\"1\" />" + "<result match=\"Some\" context=\"Paragraph\" count=\"2\" />" + "</results>" + "</rule>" + "<rule name=\"Find that\" level=\"Medium\">" + "<results>" + "<result match=\"that\" context=\"Paragraph\" count=\"3\" />" + "</results>" + "</rule>" + "</DISRules>"; if (Is64Bit()) expectedContent = "<DISRules>" + "<rule name=\"Find some\" level=\"High\">" + "<results>" + "<result match=\"Some\" context=\"Paragraph\" count=\"2\" />" + "<result match=\"some\" context=\"Paragraph\" count=\"1\" />" + "</results>" + "</rule>" + "<rule name=\"Find that\" level=\"Medium\">" + "<results>" + "<result match=\"that\" context=\"Paragraph\" count=\"3\" />" + "</results>" + "</rule>" + "</DISRules>"; WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Rule 1", "Find some", "some", RuleLevels.High); findSomeRule.Ranges.Add(RangeTypes.Paragraph); scanRules.Add(findSomeRule); WsScanRule findThatRule = new WsScanRule("Rule 2", "Find that", "that", RuleLevels.Medium); findThatRule.Ranges.Add(RangeTypes.Paragraph); scanRules.Add(findThatRule); WsMatchInRangeCollection results = new WsMatchInRangeCollection(); ArrayList someData = new ArrayList(); someData.Add("Some"); someData.Add("Some"); someData.Add("some"); results.Add(new WsMatchInRange(RangeTypes.Paragraph, findSomeRule.Condition, someData)); ArrayList thatData = new ArrayList(); thatData.Add("that"); thatData.Add("that"); thatData.Add("that"); results.Add(new WsMatchInRange(RangeTypes.Paragraph, findThatRule.Condition, thatData)); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); string xmlContent = xmlContentBuilder.BuildContent(results); Assert.AreEqual(expectedContent, xmlContent); }
public void TestCreateResultNode() { const string findSomeIndex = "FindSomeIndex"; WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Find some", findSomeIndex, "some", RuleLevels.High); findSomeRule.Ranges.Add(RangeTypes.Paragraph); scanRules.Add(findSomeRule); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); ArrayList someData = new ArrayList(); someData.Add("Some"); someData.Add("Some"); someData.Add("some"); const string expectedResult1 = "<result match=\"Some\" context=\"Paragraph\" count=\"2\" />"; const string expectedResult2 = "<result match=\"some\" context=\"Paragraph\" count=\"1\" />"; XmlDocument xmlDocument = new XmlDocument(); WsMatchInRange result = new WsMatchInRange(RangeTypes.Paragraph, findSomeIndex, someData); XmlElement resultNode = xmlContentBuilder.CreateResultNode(xmlDocument, findSomeRule, result, "Some"); Assert.AreEqual(expectedResult1, resultNode.OuterXml); resultNode = xmlContentBuilder.CreateResultNode(xmlDocument, findSomeRule, result, "some"); Assert.AreEqual(expectedResult2, resultNode.OuterXml); }
public void TestCreateResultsNodeNoResults() { WsScanRules scanRules = new WsScanRules(); WsScanRule scanRule = new WsScanRule("Find some", "FindSomeIndex", "some", RuleLevels.High); scanRules.Add(scanRule); XmlDocument xmlDocument = new XmlDocument(); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); WsMatchInRangeCollection results = new WsMatchInRangeCollection(); XmlElement resultsElement = xmlContentBuilder.CreateResultsNode(xmlDocument, scanRule, results); Assert.IsNull(resultsElement, "Expected a null results node for no results"); }
public void TestCreateResultsNodeNoResultsName() { WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Find some", "FindSomeIndex", "some", RuleLevels.High); findSomeRule.Ranges.Add(RangeTypes.Paragraph); scanRules.Add(findSomeRule); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); WsMatchInRangeCollection results = new WsMatchInRangeCollection(); ArrayList someData = new ArrayList(); someData.Add("Some"); someData.Add("Some"); someData.Add("some"); results.Add(new WsMatchInRange(RangeTypes.Paragraph, findSomeRule.Condition, someData)); string expectedResultsXml = "<results>" + "<result match=\"some\" context=\"Paragraph\" count=\"1\" />" + "<result match=\"Some\" context=\"Paragraph\" count=\"2\" />" + "</results>"; if (Is64Bit()) expectedResultsXml = "<results>" + "<result match=\"Some\" context=\"Paragraph\" count=\"2\" />" + "<result match=\"some\" context=\"Paragraph\" count=\"1\" />" + "</results>"; XmlDocument xmlDocument = new XmlDocument(); XmlElement resultsElement = xmlContentBuilder.CreateResultsNode(xmlDocument, findSomeRule, results); Assert.AreEqual(expectedResultsXml, resultsElement.OuterXml); }
public void TestApply() { WsScanRule scanRule = new WsScanRule("Test", "TestCondition", "(?:Test)", RuleLevels.High); Assert.AreEqual(1, scanRule.Apply("This is a test to see if this works.").Count); Assert.AreEqual(0, scanRule.Apply("Some text.").Count); }
public void TestCreateResultsNodeNullScanRule() { WsScanRules scanRules = new WsScanRules(); WsScanRule scanRule = new WsScanRule("Find some", "FindSomeIndex", "some", RuleLevels.High); scanRules.Add(scanRule); XmlDocument xmlDocument = new XmlDocument(); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); WsMatchInRangeCollection results = new WsMatchInRangeCollection(); xmlContentBuilder.CreateResultsNode(xmlDocument, null, results); }
public void TestCreateResultNodeInContext() { const string findSomeIndex = "FindSomeIndex"; WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Find some", findSomeIndex, "some", RuleLevels.High); findSomeRule.Ranges.Add(RangeTypes.Paragraph); scanRules.Add(findSomeRule); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); ArrayList someData = new ArrayList(); someData.Add("Some"); someData.Add("Some"); XmlDocument xmlDocument = new XmlDocument(); WsMatchInRange result = new WsMatchInRange(RangeTypes.Comment, findSomeIndex, someData); XmlElement resultNode = xmlContentBuilder.CreateResultNode(xmlDocument, findSomeRule, result, "Some"); Assert.IsNull(resultNode, "Expected a null instance for a key that does not match the search data that is out of context"); }
public void TestConstructorNullName() { WsScanRule scanRule = new WsScanRule("", "TestCondition", "Test", RuleLevels.High); }
public void TestBuildContentNoContext() { const string expectedContent = "<DISRules>" + "<rule name=\"Find some\" level=\"High\">" + "<results>" + "<result match=\"Some\" context=\"Paragraph\" count=\"2\" />" + "<result match=\"some\" context=\"Comment\" count=\"3\" />" + "</results>" + "</rule>" + "</DISRules>"; WsScanRules scanRules = new WsScanRules(); WsScanRule findSomeRule = new WsScanRule("Rule 1", "Find some", "some", RuleLevels.High); scanRules.Add(findSomeRule); WsMatchInRangeCollection results = new WsMatchInRangeCollection(); ArrayList someDataInParagraph = new ArrayList(); someDataInParagraph.Add("Some"); someDataInParagraph.Add("Some"); results.Add(new WsMatchInRange(RangeTypes.Paragraph, findSomeRule.Condition, someDataInParagraph)); ArrayList someDataInComment = new ArrayList(); someDataInComment.Add("some"); someDataInComment.Add("some"); someDataInComment.Add("some"); results.Add(new WsMatchInRange(RangeTypes.Comment, findSomeRule.Condition, someDataInComment)); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); string xmlContent = xmlContentBuilder.BuildContent(results); Assert.AreEqual(expectedContent, xmlContent); }
public void TestCreateNullResultsNode() { WsScanRules scanRules = new WsScanRules(); WsScanRule scanRule = new WsScanRule("Find some", "FindSomeIndex", "some", RuleLevels.High); scanRules.Add(scanRule); XmlDocument xmlDocument = new XmlDocument(); XMLContentBuilder xmlContentBuilder = new XMLContentBuilder(scanRules); xmlContentBuilder.CreateResultsNode(xmlDocument, scanRule, null); }
public void TestRanges() { WsScanRule scanRule = new WsScanRule("Test", "TestCondition", "(?:Test)", RuleLevels.High); scanRule.Ranges.Add(RangeTypes.Paragraph); Assert.AreEqual(1, scanRule.Ranges.Count); Assert.AreEqual(RangeTypes.Paragraph, scanRule.Ranges[0]); scanRule.Ranges.Add(RangeTypes.HeaderAndFooter); Assert.AreEqual(2, scanRule.Ranges.Count); Assert.AreEqual(RangeTypes.Paragraph, scanRule.Ranges[0]); Assert.AreEqual(RangeTypes.HeaderAndFooter, scanRule.Ranges[1]); }