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 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_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."); } }