public void TestProcessFile_TraceFiles()
		{
			string rulesxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\rules-en-GB.xml"));
			string objectsxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\PolicyObjects.xml"));

			Workshare.Policy.Engine.UroPolicyEngine engineU = new Workshare.Policy.Engine.UroPolicyEngine();

			IPolicyCache cache = new MockPolicyCache();
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").Content = rulesxml;
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").ObjectReferences = objectsxml;
			engineU.PolicyCache = cache;
 
			UniversalRequestObject uro = GetUro3(false);

			uro.Properties[MailMessagePropertyKeys.Body] = "This is a safe email";
			TestHelpers.AddAttachment(uro, new Workshare.Policy.Engine.File(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\AppSpec.doc"), "AppSpec"));

			PolicyResponseObject upi = engineU.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject;

			upi = engineU.ProcessRoutings(upi) as PolicyResponseObject;
			upi = engineU.ProcessActions(upi) as PolicyResponseObject;

			Assert.AreEqual(2, upi.ContentCollection.Count);

			ContentItem content = upi.ContentCollection[1] as ContentItem;
			PolicySetResponse policySet = content.PolicySetCollection[0] as PolicySetResponse;
			IPolicyResponse policyResponse = policySet.PolicyReportCollection[0];
			Assert.AreEqual("Classified Information Disclosure", policyResponse.Name);
			Assert.IsFalse(policyResponse.Triggered);

			policyResponse = policySet.PolicyReportCollection[1];
			Assert.AreEqual("Hidden Information Disclosure", policyResponse.Name);
			Assert.IsFalse(policyResponse.Triggered);

			policyResponse = policySet.PolicyReportCollection[2];
			Assert.AreEqual("Private Information Disclosure", policyResponse.Name);
			Assert.IsFalse(policyResponse.Triggered);

			policyResponse = policySet.PolicyReportCollection[3];
			Assert.AreEqual("Profane Language", policyResponse.Name);
			Assert.IsFalse(policyResponse.Triggered);

			policyResponse = policySet.PolicyReportCollection[4];
			Assert.AreEqual("Discriminatory Language", policyResponse.Name);
			Assert.IsFalse(policyResponse.Triggered);

			policyResponse = policySet.PolicyReportCollection[5];
			Assert.AreEqual("Metadata/Security Disclosure", policyResponse.Name);
			Assert.IsTrue(policyResponse.Triggered);

			IUniversalRequestObject uroOut = engineU.ExecuteActions(upi);
			Assert.AreEqual(1, uroOut.Attachments.Count);
		}
		public void TestProcessActions_LotsOfViolations()
		{
			string rulesxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestProcessActionsRulesU.xml"));
			string objectsxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestProcessActionsObjects.xml"));

			Workshare.Policy.Engine.UroPolicyEngine engineU = new Workshare.Policy.Engine.UroPolicyEngine();

			IPolicyCache cache = new MockPolicyCache(1);
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").Content = rulesxml;
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").ObjectReferences = objectsxml;
			engineU.PolicyCache = cache;

			UniversalRequestObject uro = GetUro3(false);

			uro.Properties[MailMessagePropertyKeys.Body] = "This is a safe email";
			TestHelpers.AddAttachment(uro, new File(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\F*****g Everything.doc"), "F*****g Everything"));

			PolicyResponseObject upi = engineU.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject;

			upi = engineU.ProcessRoutings(upi) as PolicyResponseObject;

			upi = engineU.ProcessActions(upi) as PolicyResponseObject;

			Assert.AreEqual(2, upi.ContentCollection.Count);

			ContentItem content = upi.ContentCollection[1] as ContentItem;
			PolicySetResponse policySet = content.PolicySetCollection[0] as PolicySetResponse;
			Assert.AreEqual("Clean", policySet.PolicyReportCollection[3].ActionCollection[0].Type);

			IUniversalRequestObject uroOut = engineU.ExecuteActions(upi);
			Assert.AreEqual(0, uroOut.Attachments.Count);
		}
		public void TestProcessActions_MetaDataViolation()
		{
			string rulesxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestProcessActionsRulesU.xml"));
			string objectsxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestProcessActionsObjects.xml"));

			Workshare.Policy.Engine.UroPolicyEngine engineU = new Workshare.Policy.Engine.UroPolicyEngine();

			IPolicyCache cache = new MockPolicyCache(1);
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").Content = rulesxml;
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").ObjectReferences = objectsxml;
			engineU.PolicyCache = cache;

			UniversalRequestObject uro = GetUro3(false);

			uro.Properties[MailMessagePropertyKeys.Body] = "This is a safe email";
			string dirtyDoc =
				Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(
					@"\projects\Hygiene\src\TestDocuments\Dirty -MetaDataVoilation.doc");
			string tempDirtyDoc = System.IO.Path.GetTempFileName();
            if (System.IO.File.Exists(tempDirtyDoc))
            {
                System.IO.File.Delete(tempDirtyDoc);
            }
            tempDirtyDoc = Path.ChangeExtension(tempDirtyDoc, ".doc");
			System.IO.File.Copy(dirtyDoc, tempDirtyDoc);
			TestHelpers.AddAttachment(uro, new Workshare.Policy.Engine.File(tempDirtyDoc, "Dirty"));

			PolicyResponseObject upi = engineU.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject;

			upi = engineU.ProcessRoutings(upi) as PolicyResponseObject;

			upi = engineU.ProcessActions(upi) as PolicyResponseObject;

			Assert.AreEqual(2, upi.ContentCollection.Count);

			ContentItem content = upi.ContentCollection[1] as ContentItem;
			PolicySetResponse policySet = content.PolicySetCollection[0] as PolicySetResponse;
			Assert.AreEqual("Clean", policySet.PolicyReportCollection[3].ActionCollection[0].Type);

			IUniversalRequestObject uroOut = engineU.ExecuteActions(upi);
			Assert.AreEqual(1, uroOut.Attachments.Count);

			if (System.IO.File.Exists(tempDirtyDoc))
			{
				System.IO.File.Delete(tempDirtyDoc);
			}
		}
		public void TestEnforce_MailMessageEntity_ExecuteAction()
		{
			string rulesxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\EnforceRulesU.xml"));
			string objectsxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\EnforceObjectsU.xml"));

			UroPolicyEngine engine = new UroPolicyEngine();
			
			IPolicyCache cache = new MockPolicyCache();
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").Content = rulesxml;
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").ObjectReferences = objectsxml;
			engine.PolicyCache = cache;

			//            MockUroPolicyAuditor auditor = new MockUroPolicyAuditor();
			//            engine.SetPolicyAuditor(auditor);

			UniversalRequestObject uro = TestHelpers.GetUro(false);
			TestHelpers.AddAttachment(uro, "Doc", Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestPrivacy.doc"), "TestPrivacy.doc");
			TestHelpers.AddAttachment(uro, "Doc", Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestProfanity.doc"), "TestProfanity.doc");

			PolicyResponseObject pui = engine.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject;
			PolicyResponseObject pui1 = engine.ProcessRoutings(pui) as PolicyResponseObject;
			PolicyResponseObject pui2 = engine.ProcessActions(pui1) as PolicyResponseObject;

			ContentItem content = pui.ContentCollection[2] as ContentItem;
			PolicySetResponse policySet = content.PolicySetCollection[0] as PolicySetResponse;
			IPolicyResponse pi1 = policySet.PolicyReportCollection[2];
			Assert.AreEqual("Profanity", pi1.Name);
			Assert.IsTrue(pi1.Triggered);

			IUniversalRequestObject uroOut = engine.ExecuteActions(pui2);

			Assert.AreEqual(0, uroOut.Attachments.Count, "Expecting an empty shell of a URO with no attachments");

			// ????: once a block action is introduced, all bets are off :), specifically it means that 
			// _nothing_ else will get executed.
			//
			//Assert.IsTrue(uroOut.Attachments.Count == 1); // the profanity got cleaned!

			//IPolicyResponseObject pui3 = engine.ProcessConditions(RunAt.Client, uroOut);
			//content = pui.ContentCollection[1] as ContentItem;
			//policySet = content.PolicySetCollection[0] as PolicySetResponse;
			//IPolicyResponse pi3 = policySet.PolicyReportCollection[2];

			//Assert.AreEqual("Profanity", pi3.Name);
			//Assert.IsFalse(pi3.Triggered); // shows it got cleaned!
		}
		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 TestMultiEnforce()
		{
			string rulesxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\EnforceRulesU.xml"));
			string objectsxml = System.IO.File.ReadAllText(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\EnforceObjectsU.xml"));

			UroPolicyEngine engine = new UroPolicyEngine();

			IPolicyCache cache = new MockPolicyCache();
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").Content = rulesxml;
			cache.PolicySets[0].LatestVersion.GetCompiledPolicySet("SMTP", "Client").ObjectReferences = objectsxml;
			engine.PolicyCache = cache;

			UniversalRequestObject uro = TestHelpers.GetUro(false);
			TestHelpers.AddAttachment(uro, "Doc", Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\F*****g Everything.doc"), "F*****g Everything.doc");

			DateTime dtStart1 = DateTime.Now;
			PolicyResponseObject pui = engine.ProcessConditions(RunAt.Client, uro) as PolicyResponseObject;
			DateTime dtEnd1 = DateTime.Now;
			TimeSpan ts1 = dtEnd1 - dtStart1;
			PolicyResponseObject pui1 = engine.ProcessRoutings(pui) as PolicyResponseObject;
			PolicyResponseObject pui2 = engine.ProcessActions(pui1) as PolicyResponseObject;

			ContentItem content = pui.ContentCollection[1] as ContentItem;
			PolicySetResponse policySet = content.PolicySetCollection[0] as PolicySetResponse;
			IPolicyResponse pi1 = policySet.PolicyReportCollection[3];
			Assert.IsTrue(pi1.Name == "Metadata");
			Assert.IsTrue(pi1.Triggered);
			// I don't really understand what this next line was testing.  It was too vague to be
			// of any real use...
			//Assert.IsTrue(pi1.Actions[0].Properties.Count == 4); // ["TrackChanges"]) == false);

			IUniversalRequestObject uroOut = engine.ExecuteActions(pui2);

			Assert.IsTrue(uroOut.Attachments.Count == 0);    // the profanity got cleaned!

			UniversalRequestObject uro2 = TestHelpers.GetUro(false);
			TestHelpers.AddAttachment(uro2, "Doc", Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestProfanity.doc"), "TestProfanity.doc");

			DateTime dtStart2 = DateTime.Now;
			PolicyResponseObject pui3 = engine.ProcessConditions(RunAt.Client, uro2) as PolicyResponseObject;
			DateTime dtEnd2 = DateTime.Now;
			TimeSpan ts2 = dtEnd2 - dtStart2;
			engine.ProcessRoutings(pui3);
			engine.ProcessActions(pui3);
			// NOW!  there were trackchanges in first doc and not in the second
			content = pui3.ContentCollection[1] as ContentItem;
			policySet = content.PolicySetCollection[0] as PolicySetResponse;
			Assert.IsTrue(policySet.PolicyReportCollection[2].ActionCollection.Count > 0);

			// and again! this time with same file
			UniversalRequestObject uro3 = TestHelpers.GetUro(false);
			TestHelpers.AddAttachment(uro3, "Doc", Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestProfanity.doc"), "TestProfanity.doc");
			DateTime dtStart3 = DateTime.Now;
			pui3 = engine.ProcessConditions(RunAt.Client, uro3) as PolicyResponseObject;
			DateTime dtEnd3 = DateTime.Now;
			TimeSpan ts3 = dtEnd3 - dtStart3;
			engine.ProcessRoutings(pui3);
			engine.ProcessActions(pui3);
			// NOW!  there were trackchanges in first doc and not in the second
			content = pui3.ContentCollection[1] as ContentItem;
			policySet = content.PolicySetCollection[0] as PolicySetResponse;
			Assert.IsTrue(policySet.PolicyReportCollection[2].ActionCollection.Count > 0);
		}