public void TestDecryptNullRequestAttachments() { using (ContentEncryptionManager manager = new ContentEncryptionManager()) { manager.DecryptRequestAttachments(null, null); } }
public void TestReEncryptNullResponseAttachments() { using (ContentEncryptionManager manager = new ContentEncryptionManager()) { bool val; manager.ReencryptResponseAttachments(null, out val); } }
public void ExecuteEncryptionTestScript(string scriptName, bool shouldSucceed) { try { ContentEncryptionManager.ClearPasswordCache(); string scriptPath = Path.Combine(TestFileDirectory, shouldSucceed ? "succeed" : "fail", scriptName); var passwordInfo = ParsePasswordInfo(scriptPath); using (RequestCreator rc = new RequestCreator()) { // Create a request based on the script file using (Request request = rc.CreateRequest(scriptPath)) { // Check attachments read into request correctly Assert.IsTrue(CheckAttachments(scriptPath, request), "CheckAttachments failed after initial request creation, script:" + scriptPath); IContentEncryptionUi ui = new MockContentEncryptionUi(passwordInfo, DecryptResult.Ok, DecryptionErrorAction.Cancel); using (var app = new WsApplication(new MSOutlook.Application(), false)) { using (Workshare.PolicyMarshaller.MailAttachmentTransform mat = new MailAttachmentTransform(Interop.Options.OptionApi.GetInt("LevelOfProcessingEmbeddedMessages"), app)) using (ContentEncryptionManager manager = new ContentEncryptionManager(mat.ExpandMSG, mat.PackMSG)) { // Check decryption Assert.AreEqual(shouldSucceed, manager.DecryptRequestAttachments(request, ui), "DecryptRequestAttachments response incorrect, script:" + scriptPath); if (shouldSucceed)// No point proceeding if we were expecting failure { EnforceResponse spoofResponse = new EnforceResponse(); spoofResponse.ModifiedRequest = request; // Now ensure we can re-encrypt it bool cancelSend = true; Assert.IsTrue(manager.ReencryptResponseAttachments(spoofResponse, out cancelSend), "ReencryptResponseAttachments failed, script:" + scriptPath); Assert.IsFalse(cancelSend, "cancelSend true, should be false, script:" + scriptPath); Assert.IsTrue(CheckAttachments(scriptPath, request), "CheckAttachments failed after re-encryption, script:" + scriptPath); // Ensure the encryption went OK CheckFilesEncrypted(passwordInfo, manager, spoofResponse); } } } } } } catch (Exception ex) { Assert.Fail(string.Format("Caught exception while executing: {0}. Exception: {1}", Path.GetFileName(scriptName), ex.Message)); throw; } finally { // Occasionally seeing a COM error when re-running tests, suspect its down to the Request writer not having cleaned up fully. GC.Collect(); GC.WaitForPendingFinalizers(); } }
public void TestAssociateZipActionArchiveWithContent(string docFileName, string zipFileName, string password) { // Use function currying to keep the password. Func<FileType, string, string, string, ContentItem> CreateContentItem = (ft, fp, ci, pi) => CreateZipActionContentItem(password, ft, fp, ci, pi); // A message with null parent ID, because it's the top-most element. const string emailBodyId = "1"; const string emailBodyName = "message subject/body"; ContentItem emailContentItem = CreateContentItem(FileType.Email, emailBodyName, emailBodyId, null); var emailAttachment = new Attachment(emailBodyName, emailBodyName, FileType.Email.ToString(), emailBodyId, emailBodyId, true); // A zip with parent being email const string zipId = "2"; string zipPath = Path.Combine(TestFileDirectory, zipFileName); ContentItem zipContentItem = CreateContentItem(FileType.ZIP, zipPath, zipId, emailBodyId); var zipAttachment = new Attachment(zipPath, zipPath, FileType.ZIP.ToString(), zipId, zipId, true); zipAttachment.File = new FcsFile(zipPath, Path.GetFileName(zipPath), zipId); // A doc with parent being zip. const string docId = "3"; string docPath = Path.Combine(TestFileDirectory, docFileName); ContentItem docContentItem = CreateContentItem(FileType.WordDocument, docPath, docId, zipId); var docAttachment = new Attachment(docPath, docPath, FileType.WordDocument.ToString(), docId, docId, true); docAttachment.File = new FcsFile(docPath, Path.GetFileName(docPath), docId); using (ContentEncryptionManager manager = new ContentEncryptionManager()) { if (!string.IsNullOrEmpty(password)) { manager.AddZipActionEncryptor(zipId, zipPath, password); } var contents = new ContentItem[] { emailContentItem, zipContentItem, docContentItem }; var response = new Response { Contents = contents }; var attachments = new Attachment[] { emailAttachment, zipAttachment, docAttachment }; var request = new Request { Attachments = attachments }; var enforce = new EnforceResponse { ModifiedRequest = request }; // The zip and it's document are not yet associated. Assert.AreEqual(0, zipAttachment.File.Files.Count, "The zip IFile should not contain any files before association."); Assert.IsFalse(zipAttachment.File.IsExpanded); if (!string.IsNullOrEmpty(password)) { Assert.AreEqual(null, zipAttachment.File.Password, "The zip IFile should not yet contain the password."); } bool cancelSend; manager.AssociateZipActionArchiveWithContent(enforce, response, out cancelSend); // The zip and it's document should now be associated. Assert.AreEqual(1, zipAttachment.File.Files.Count, "The zip IFile should contain 1 file after association."); Assert.IsTrue(zipAttachment.File.IsExpanded); if (!string.IsNullOrEmpty(password)) { Assert.AreEqual(password, zipAttachment.File.Password, "The zip IFile should now contain the correct password."); } } }
public void TestAddZipActionEncryptor_DocumentAlreadyZipped(string docFileName, string zipFileName, string password) { // Use function currying to keep the password. Func<FileType, string, string, string, ContentItem> CreateContentItem = (ft, fp, ci, pi) => CreateZipActionContentItem(password, ft, fp, ci, pi); // A message with null parent ID, because it's the top-most element. const string emailBodyId = "1"; const string emailBodyName = "message subject/body"; ContentItem emailContentItem = CreateContentItem(FileType.Email, emailBodyName, emailBodyId, null); // A zip with parent being email const string zipId = "2"; string zipPath = Path.Combine(TestFileDirectory, zipFileName); ContentItem zipContentItem = CreateContentItem(FileType.ZIP, zipPath, zipId, emailBodyId); var zipAttachment = new Attachment(zipPath, zipPath, FileType.ZIP.ToString(), zipId, zipId, true); // A doc with parent being zip. const string docId = "3"; string docPath = Path.Combine(TestFileDirectory, docFileName); ContentItem docContentItem = CreateContentItem(FileType.WordDocument, docPath, docId, zipId); using (ContentEncryptionManager manager = new ContentEncryptionManager()) { var response = new Response { Contents = new[] { emailContentItem, zipContentItem, docContentItem } }; manager.AddZipActionEncryptor(response); IContentEncryption contentEncryption = manager.GetAssociatedEncryptionObject(zipAttachment); Assert.IsNotNull(contentEncryption, "Expected the encryption map to contain the zip encryption entry."); Assert.AreEqual(zipPath, contentEncryption.Name, "The zip encryption name was wrong."); Assert.AreEqual(password, contentEncryption.OpenPassword, "The zip encryption \"open password\" was wrong."); } }
public void TestAddZipActionEncryptor_DocumentWillBeZipped(string docFileName, string password) { // Use function currying to keep the password. Func<FileType, string, string, string, ContentItem> CreateContentItem = (ft, fp, ci, pi) => CreateZipActionContentItem(password, ft, fp, ci, pi); // A message with null parent ID, because it's the top-most element. const string emailBodyId = "1"; const string emailBodyName = "message subject/body"; ContentItem emailContentItem = CreateContentItem(FileType.Email, emailBodyName, emailBodyId, null); // A doc with parent being the email. const string docId = "2"; string docPath = Path.Combine(TestFileDirectory, docFileName); ContentItem docContentItem = CreateContentItem(FileType.WordDocument, docPath, docId, emailBodyId); // Expected info that the newly created zip should have. const string zipId = docId; string zipPath = Path.ChangeExtension(docPath, ".zip"); var zipAttachment = new Attachment(zipPath, zipPath, FileType.ZIP.ToString(), zipId, zipId, true); using (ContentEncryptionManager manager = new ContentEncryptionManager()) { var response = new Response { Contents = new[] { emailContentItem, docContentItem } }; manager.AddZipActionEncryptor(response); ContentItem foundDocItem = response.Contents.SingleOrDefault(i => i.Name == docPath); ContentItem foundZipItem = response.Contents.SingleOrDefault(i => i.Name == zipPath); // Test that the doc content item still exists and a new zip content item has been added. Assert.IsNotNull(foundDocItem, "Unable to find the document content item."); Assert.IsNotNull(foundZipItem, "Unable to find the zip content item."); // Make that Assert.AreNotEqual(docId, foundDocItem.Id, "The document content ID should have been changed."); Assert.AreEqual(zipId, foundDocItem.ParentId, "The document content item's parent ID should now be the document's original ID."); Assert.AreEqual(zipId, foundZipItem.Id, "The zip content ID should now be the document's original content ID."); Assert.AreEqual(emailBodyId, foundZipItem.ParentId, "The zip content item's parent ID should be the message ID."); IContentEncryption contentEncryption = manager.GetAssociatedEncryptionObject(zipAttachment); Assert.IsNotNull(contentEncryption, "Expected the encryption map to contain the zip encryption entry."); Assert.AreEqual(zipPath, contentEncryption.Name, "The zip encryption name was wrong."); Assert.AreEqual(password, contentEncryption.OpenPassword, "The zip encryption \"open password\" was wrong."); } }
private static void CheckFilesEncrypted(Dictionary<string, MockContentEncryptionUi.PasswordInfo> passwordInfo, ContentEncryptionManager manager, EnforceResponse spoofResponse) { // Ensure all attachments are still encrypted/decrypted as defined in the encryptedDict foreach (var attachment in spoofResponse.ModifiedRequest.Attachments) { string relativePath = MockContentEncryptionUi.RemoveTempDirs(attachment.FileName); if (passwordInfo.ContainsKey(relativePath))// Its has an entry in the pwd dictionary so should be passworded { Assert.IsTrue(manager.IsEncrypted(attachment)); } } }