public void TestInProcEnforce_01_ErrorPropertiesAreReturned() { #region setup MockUserAction.MockActionInitializer configuration = Utilities.ThreadSafeLazyLoadSingletonT<MockUserAction.MockActionInitializer>.Instance; configuration.SupportedFiles = SupportedFileSet.CreateFileCollection(".*", ".email"); configuration.ExecuteMethod = new MockUserAction.ExecuteMethod(delegate(IActionData3 data, ActionPropertySet props) { throw new AppDomainUnloadedException("BCL adventures"); }); #endregion using (var cs = new ContractSerialization()) { Request request = cs.LoadRequest(Path.Combine(TESTPATH, "TestEnforce_06_WordAttachment_NoSpecificPolicyType.request")); Response response = cs.LoadResponse(Path.Combine(TESTPATH, "TestEnforce_06_WordAttachment_NoSpecificPolicyType.response")); //execute EnforceResponse enforcedResponse = GetContentEnforcer().Enforce(request, response); //verify Assert.IsNotNull(enforcedResponse.ModifiedRequest, "Expecting original request to be returned"); CustomProperty property = null; property = GetCustomPropertyByKey(enforcedResponse.Properties, Workshare.PolicyContent.ContentEnforcer.ErrorActionKey); Assert.IsNotNull(property); Assert.IsTrue(property.Value.StartsWith("MockUser"), "Expecting action name to be returned"); property = GetCustomPropertyByKey(enforcedResponse.Properties, ContentEnforcer.ErrorContentKey); Assert.IsNotNull(property); Assert.AreEqual("test.doc", property.Value, "Expecting contentid to be returned"); // this is encoded in the request xml above property = GetCustomPropertyByKey(enforcedResponse.Properties, ContentEnforcer.ErrorMessageKey); Assert.IsNotNull(property); Assert.IsNotNull(property.Value); property = GetCustomPropertyByKey(enforcedResponse.Properties, ContentEnforcer.ErrorResultKey); Assert.IsNotNull(property); Assert.IsNotNull(property.Value); property = GetCustomPropertyByKey(enforcedResponse.Properties, ContentEnforcer.ErrorStackKey); Assert.IsNotNull(property); Assert.IsNotNull(property.Value); } }
/// <summary> /// This method measures the size of the request file in bits. /// </summary> /// <param name="requestFile"></param> /// <returns></returns> /// <remarks>For throughput statistics, plain filesize is unsuitable for use as the xml file contains /// metadata that does not bear any relation to the size of the file in question. /// /// Only measure the parts of the Request that will /// be evaluated by FCS: the Attachment contents and the body Properties. ///</remarks> internal static long GetRequestSizeInBytes(FileInfo requestFile) { using (var cs = new ContractSerialization()) { Request request = cs.LoadRequest(requestFile.FullName); long totalBytes = 0; if (request.Properties != null) { List<CustomProperty> props = new List<CustomProperty>(request.Properties); //the body is the only property that should be factored into the request size. bool gotBody = false; props.ForEach(delegate(CustomProperty prop) { if (!gotBody && (0 == string.Compare("Body", prop.Name, StringComparison.InvariantCultureIgnoreCase) || 0 == string.Compare("FormattedBody", prop.Name, StringComparison.InvariantCultureIgnoreCase))) { gotBody = true; totalBytes += prop.Value.Length*2; } }); } if (request.Attachments != null) { List<Attachment> attachments = new List<Attachment>(request.Attachments); attachments.ForEach(delegate(Attachment attachment) { //Add up all the byte array lengths. totalBytes += attachment.Content.Length; }); } return totalBytes; } }
public void TestEnforce_06_WordAttachment_NoSpecificPolicyType() { #region setup MockUserAction.MockActionInitializer configuration = Utilities.ThreadSafeLazyLoadSingletonT<MockUserAction.MockActionInitializer>.Instance; configuration.SupportedFiles = SupportedFileSet.CreateFileCollection(".*", ".email"); configuration.ExecuteMethod = new MockUserAction.ExecuteMethod(delegate(IActionData3 data, ActionPropertySet props) { if (data.Properties.ContainsKey("FileName")) { string fileBefore = data.Properties["FileName"]; string fileAfter = Path.ChangeExtension(data.Properties["FileName"], "txt"); File.Move(fileBefore, fileAfter); data.Properties["FileName"] = fileAfter; data.Properties["DisplayName"] = Path.GetFileName(fileAfter); } return data.Properties["FileName"]; }); #endregion using (var cs = new ContractSerialization()) { Request request = cs.LoadRequest(Path.Combine(TESTPATH, "TestEnforce_06_WordAttachment_NoSpecificPolicyType.request")); Response response = cs.LoadResponse(Path.Combine(TESTPATH, "TestEnforce_06_WordAttachment_NoSpecificPolicyType.response")); //execute EnforceResponse enforcedResponse = GetContentEnforcer().Enforce(request, response); //verify Attachment firstEnforcedAttachment = enforcedResponse.ModifiedRequest.Attachments[0]; string fileExt_before = Path.GetExtension(request.Attachments[0].Name); string fileExt_after = Path.GetExtension(firstEnforcedAttachment.Name); Assert.AreNotEqual(fileExt_before, fileExt_after, "Expected the file extension to change after enforcement."); Assert.AreEqual(".txt", fileExt_after, "Expected file to be converted to plain text"); } }
/// <summary> /// Gets the response time (in millisec) for the engine to process the given file. /// </summary> /// <param name="filePath">The file you are testing</param> /// <returns>measurement in milliseconds</returns> internal static long GetResponseTimeForFile(IContentScanner scanner, string filePath, ProcessLevel level, RunAt runAt) { using (var cs = new ContractSerialization()) { Request request = cs.LoadRequest(filePath); Response response = null; TimeSpan timeSpan = Measure(delegate() { response = scanner.Scan(request, null, level, runAt); }); AssertValidResponse(response); //Essential to validate, but it should not be part of what is timed. return Convert.ToInt64(timeSpan.TotalMilliseconds); } }
public void Test_14_ClientEmail_ActionThatSupportsOnlyEmail() { //Even if an action only supports the email file type, //It must still be triggered in the scanner when an email *attachment* satisfies the trigger condition. try { GetPolicyCache().Clear(); IContentScanner scannerService = GetContentScanner(); PolicyItem policy = new PolicyItem("ActionThatSupportsOnlyEmail", File.ReadAllBytes(Path.Combine(TESTPATH, "ActionThatSupportsOnlyEmail.runtimepolicy"))); policy.RestrictedTargets = new RunAt[] {RunAt.Client}; GetPolicyCache().Clear(); GetPolicyCache().Add(policy); using (var cs = new ContractSerialization()) { Request request = cs.LoadRequest(Path.Combine(TESTPATH, "ActionThatSupportsOnlyEmail.request")); Response response = scannerService.Scan(request, string.Empty, ProcessLevel.Actions, RunAt.Client); AssertValid(response, ProcessLevel.Actions); Assert.AreEqual(2, response.Contents.Length); Assert.AreEqual(FileType.Email.ToString(), response.Contents[0].ContentType); Assert.AreEqual(FileType.XMLDocument.ToString(), response.Contents[1].ContentType); //Single policyset, single policy... Assert.AreEqual(1, response.Contents[0].PolicySets.Length); Assert.AreEqual(1, response.Contents[0].PolicySets[0].Policies.Length); Assert.AreEqual(1, response.Contents[1].PolicySets.Length); Assert.AreEqual(1, response.Contents[1].PolicySets[0].Policies.Length); Assert.IsFalse(response.Contents[0].PolicySets[0].Policies[0].Triggered, "The violation is not in the email body, this should not have triggered."); Assert.IsTrue(response.Contents[1].PolicySets[0].Policies[0].Triggered, "The violation is in the attachment - this should have triggered."); Assert.AreEqual(0, response.Contents[0].PolicySets[0].Policies[0].Actions.Length, "No violations in content item 0, so no actions expected."); Assert.AreEqual(1, response.Contents[1].PolicySets[0].Policies[0].Actions.Length); Assert.AreEqual(1, response.ResolvedActions.Length); } } finally { GetPolicyCache().Clear(); InitPolicies(); } }
public void Test_12_MailClientProfanity() { try { IContentScanner scannerService = GetContentScanner(); PolicyItem hrPolicy = new PolicyItem("HRPolicy", File.ReadAllBytes(Path.Combine(TESTPATH, "Human Resources.runtimepolicy"))); hrPolicy.RestrictedTargets = new RunAt[] { RunAt.Client }; GetPolicyCache().Clear(); GetPolicyCache().Add(hrPolicy); using (var cs = new ContractSerialization()) { Request request = cs.LoadRequest(Path.Combine(TESTPATH, "TestProfanityIsTriggered.request")); Response response = scannerService.Scan(request, string.Empty, ProcessLevel.Actions, RunAt.Client); AssertValid(response, ProcessLevel.Actions); } } finally { GetPolicyCache().Clear(); InitPolicies(); } }
public void Test_11_DocRegistration() { IContentScanner scannerService = GetContentScanner(); using (var cs = new ContractSerialization()) { Request request = cs.LoadRequest(Path.Combine(TESTPATH, "Test_12_DocRegistration.request")); Response response = scannerService.Scan(request, null, ProcessLevel.Actions, RunAt.Server); //TODO: Was this request supposed to actually trigger any actions? //At least it no longer throws. } }