public void TestProcessZipPolicy() { string testPath = Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\"); UroPolicyEngine engine = new UroPolicyEngine(); IPolicyCache cache = new MockPolicyCache(-1); Assert.AreEqual(0, cache.PolicySets.Count, "Expected not policy sets to exist"); IPolicySetCache policySetCache = cache.NewPolicySet("Test"); Assert.AreEqual(1, cache.PolicySets.Count, "Expected one policy sets to exist"); policySetCache.LatestVersion.Content = System.IO.File.ReadAllText(testPath + "BlockZipFilesAndZipWordFilesPolicy.xml"); engine.PolicyCache = cache; UniversalRequestObject uro = new UniversalRequestObject(); uro.Properties.Add(MailMessagePropertyKeys.FileHeader, "Stuff"); uro.Properties.Add(MailMessagePropertyKeys.Body, "You breaka my policy, I breaka you're middle toe"); uro.Properties.Add(MailMessagePropertyKeys.Subject, "Zip my attachment if they are word doc and block zip attachments"); uro.Properties.Add(MailMessagePropertyKeys.Attachments, "I am a dirty zip.zip; Dirty.doc"); // Add a zip attachment RequestAttachment zipAttachment = TestHelpers.AddAttachment(uro, RequestObjectContentType.ZIP, testPath + "I am a dirty zip.zip", "I am a dirty zip.zip"); // Add a word doc RequestAttachment wordAttachment = TestHelpers.AddAttachment(uro, RequestObjectContentType.DOC, testPath + "Dirty.doc", "Dirty.doc"); uro.Source.PolicyType = uro.PolicyType; uro.Source.RoutingType = RoutingTypes.Source; IRoutingItem fromRoutingItem = new RoutingItem("*****@*****.**"); fromRoutingItem.Properties.Add(SMTPItemPropertyKeys.AddressType, AddressType.From); fromRoutingItem.Properties.Add(SMTPItemPropertyKeys.DisplayName, "pair"); uro.Source.Items.Add(fromRoutingItem); uro.Destination.PolicyType = uro.PolicyType; uro.Destination.RoutingType = RoutingTypes.Destination; IRoutingItem toRoutingItem = new RoutingItem("sam test"); toRoutingItem.Properties.Add(SMTPItemPropertyKeys.AddressType, AddressType.To); toRoutingItem.Properties.Add(SMTPItemPropertyKeys.DisplayName, "sam test"); uro.Destination.Items.Add(toRoutingItem); uro.PolicyType = PolicyType.ClientEmail; uro.DataTimeStamp = DateTime.Now; PolicyResponseObject conditionPolicyInfo = engine.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject; PolicyResponseObject rountingPolicyInfo = engine.ProcessRoutings(conditionPolicyInfo) as PolicyResponseObject; PolicyResponseObject actionPolicyInfo = engine.ProcessActions(rountingPolicyInfo) as PolicyResponseObject; Assert.AreEqual(4, actionPolicyInfo.ContentCollection.Count); ContentItem content = actionPolicyInfo.ContentCollection[1] as ContentItem; PolicySetResponse policySet = content.PolicySetCollection[0] as PolicySetResponse; IPolicyResponse zipPolicyInfo = policySet.PolicyReportCollection[1]; Assert.IsTrue(zipPolicyInfo.Triggered); content = actionPolicyInfo.ContentCollection[1] as ContentItem; policySet = content.PolicySetCollection[0] as PolicySetResponse; IPolicyResponse wordPolicyInfo = policySet.PolicyReportCollection[0]; Assert.IsFalse(wordPolicyInfo.Triggered); // Remove the zip attachment. uro.Attachments.Remove(zipAttachment); conditionPolicyInfo = engine.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject; rountingPolicyInfo = engine.ProcessRoutings(conditionPolicyInfo) as PolicyResponseObject; actionPolicyInfo = engine.ProcessActions(rountingPolicyInfo) as PolicyResponseObject; content = actionPolicyInfo.ContentCollection[1] as ContentItem; policySet = content.PolicySetCollection[0] as PolicySetResponse; zipPolicyInfo = policySet.PolicyReportCollection[1]; Assert.IsFalse(zipPolicyInfo.Triggered); content = actionPolicyInfo.ContentCollection[1] as ContentItem; policySet = content.PolicySetCollection[0] as PolicySetResponse; wordPolicyInfo = policySet.PolicyReportCollection[0]; Assert.IsTrue(wordPolicyInfo.Triggered); Assert.AreEqual(1, actionPolicyInfo.UniversalRequestObject.Attachments.Count); Assert.AreEqual(RequestObjectContentType.DOC, actionPolicyInfo.UniversalRequestObject.Attachments[0].ContentType); Assert.AreEqual("Dirty.doc", Path.GetFileName(actionPolicyInfo.UniversalRequestObject.Attachments[0].Name)); IUniversalRequestObject outputUro = engine.ExecuteActions(actionPolicyInfo); Assert.AreEqual(1, outputUro.Attachments.Count, "Expected a zip attachment to be created"); IRequestAttachment outputAttachment = outputUro.Attachments[0]; Assert.AreEqual("Dirty.zip", Path.GetFileName(outputUro.Attachments[0].Name), "Is LDAP configured correctly?"); CheckZipFile(outputUro.Attachments[0].Data.AsStream(), new string[] { "Dirty.doc" }, true, "", false); }
public void TestProcessPolicyWhenChangesMadeToPolicySetAfterPolicyEngineLoaded() { string testPath = Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\"); string rulesxml = System.IO.File.ReadAllText(testPath + "EnforceRulesU.xml"); string objectsxml = System.IO.File.ReadAllText(testPath + "EnforceObjectsU.xml"); Workshare.Policy.Engine.UroPolicyEngine engine = new Workshare.Policy.Engine.UroPolicyEngine(); IPolicyCache cache = new MockPolicyCache(-1); Assert.AreEqual(0, cache.PolicySets.Count, "Expected not policy sets to exist"); IPolicySetCache policySetCache = cache.NewPolicySet("Test"); Assert.AreEqual(1, cache.PolicySets.Count, "Expected one policy sets to exist"); policySetCache.LatestVersion.Content = System.IO.File.ReadAllText(testPath + "TestPolicyEngine.xml"); engine.PolicyCache = cache; UniversalRequestObject uro = new UniversalRequestObject(); uro.Properties.Add(MailMessagePropertyKeys.FileHeader, "Stuff"); uro.Properties.Add(MailMessagePropertyKeys.Body, "f**k"); uro.Properties.Add(MailMessagePropertyKeys.Subject, "Subject line"); uro.Properties.Add(MailMessagePropertyKeys.Attachments, ""); uro.Source.PolicyType = uro.PolicyType; uro.Source.RoutingType = RoutingTypes.Source; IRoutingItem fromRoutingItem = new RoutingItem("*****@*****.**"); fromRoutingItem.Properties.Add(SMTPItemPropertyKeys.AddressType, AddressType.From); uro.Source.Items.Add(fromRoutingItem); uro.Destination.PolicyType = uro.PolicyType; uro.Destination.RoutingType = RoutingTypes.Destination; IRoutingItem toRoutingItem = new RoutingItem("sam test"); toRoutingItem.Properties.Add(SMTPItemPropertyKeys.AddressType, AddressType.To); uro.Destination.Items.Add(toRoutingItem); uro.PolicyType = PolicyType.ClientEmail; uro.DataTimeStamp = System.DateTime.Now; PolicyResponseObject conditionPolicyInfo = engine.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject; PolicyResponseObject rountingPolicyInfo = engine.ProcessRoutings(conditionPolicyInfo) as PolicyResponseObject; PolicyResponseObject actionPolicyInfo = engine.ProcessActions(rountingPolicyInfo) as PolicyResponseObject; ContentItem content = actionPolicyInfo.ContentCollection[0] as ContentItem; PolicySetResponse policySet = content.PolicySetCollection[0] as PolicySetResponse; Assert.AreEqual(1, policySet.PolicyReportCollection[2].ExpressionCollection[0].ExpressionDetailCollection.Count); IPolicySetVersionCache latestVersion = cache.PolicySets[0].LatestVersion; cache.PolicySets[0].NewVersion(cache.PolicySets[0].LatestVersion.Content.Replace("(?i:f**k)", "(?i:jou ma se)")); conditionPolicyInfo = engine.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject; rountingPolicyInfo = engine.ProcessRoutings(conditionPolicyInfo) as PolicyResponseObject; actionPolicyInfo = engine.ProcessActions(rountingPolicyInfo) as PolicyResponseObject; content = actionPolicyInfo.ContentCollection[0] as ContentItem; policySet = content.PolicySetCollection[0] as PolicySetResponse; Assert.AreEqual(0, policySet.PolicyReportCollection[2].ExpressionCollection[0].ExpressionDetailCollection.Count); uro.Properties[MailMessagePropertyKeys.Body] = "jou ma se"; conditionPolicyInfo = engine.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject; rountingPolicyInfo = engine.ProcessRoutings(conditionPolicyInfo) as PolicyResponseObject; actionPolicyInfo = engine.ProcessActions(rountingPolicyInfo) as PolicyResponseObject; content = actionPolicyInfo.ContentCollection[0] as ContentItem; policySet = content.PolicySetCollection[0] as PolicySetResponse; Assert.AreEqual(1, policySet.PolicyReportCollection[2].ExpressionCollection[0].ExpressionDetailCollection.Count); }