From 54a4214c86bc790cc2a3ae454b1aa709e868fa1a Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Mon, 24 Jul 2017 16:40:54 -0400 Subject: Basic APIs and tests for adding attachments 1. Added APIs for adding attachments, setting attachment files, and modifying attachment dictionary entries. * Added two embedder tests covering all new APIs. Bug=pdfium:174 Change-Id: I65f43cd6ca4887b71f9f7bcee64a87ba6b7e2706 Reviewed-on: https://pdfium-review.googlesource.com/8671 Commit-Queue: Jane Liu Reviewed-by: Lei Zhang Reviewed-by: dsinclair --- fpdfsdk/fpdfattachment_embeddertest.cpp | 150 ++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) (limited to 'fpdfsdk/fpdfattachment_embeddertest.cpp') diff --git a/fpdfsdk/fpdfattachment_embeddertest.cpp b/fpdfsdk/fpdfattachment_embeddertest.cpp index d873d9b5ce..f4d0bfcfae 100644 --- a/fpdfsdk/fpdfattachment_embeddertest.cpp +++ b/fpdfsdk/fpdfattachment_embeddertest.cpp @@ -85,3 +85,153 @@ TEST_F(FPDFAttachmentEmbeddertest, ExtractAttachments) { EXPECT_EQ(kCheckSumW, GetPlatformWString(reinterpret_cast(buf.data()))); } + +TEST_F(FPDFAttachmentEmbeddertest, AddAttachments) { + // Open a file with two attachments. + ASSERT_TRUE(OpenDocument("embedded_attachments.pdf")); + EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document())); + + // Check that adding an attachment with an empty name would fail. + EXPECT_FALSE(FPDFDoc_AddAttachment(document(), nullptr)); + + // Add an attachment to the beginning of the embedded file list. + std::unique_ptr file_name = + GetFPDFWideString(L"0.txt"); + FPDF_ATTACHMENT attachment = + FPDFDoc_AddAttachment(document(), file_name.get()); + + // Check that writing to a file with nullptr but non-zero bytes would fail. + EXPECT_FALSE(FPDFAttachment_SetFile(attachment, document(), nullptr, 10)); + + // Set the new attachment's file. + constexpr char kContents1[] = "Hello!"; + EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), kContents1, + strlen(kContents1))); + + // Verify the name of the new attachment (i.e. the first attachment). + attachment = FPDFDoc_GetAttachment(document(), 0); + ASSERT_TRUE(attachment); + unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0); + std::vector buf(len); + EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); + EXPECT_STREQ(L"0.txt", + GetPlatformWString(reinterpret_cast(buf.data())) + .c_str()); + + // Verify the content of the new attachment (i.e. the first attachment). + len = FPDFAttachment_GetFile(attachment, nullptr, 0); + buf.clear(); + buf.resize(len); + ASSERT_EQ(6u, FPDFAttachment_GetFile(attachment, buf.data(), len)); + EXPECT_EQ(std::string(kContents1), std::string(buf.data(), 6)); + + // Add an attachment to the end of the embedded file list and set its file. + file_name = GetFPDFWideString(L"z.txt"); + attachment = FPDFDoc_AddAttachment(document(), file_name.get()); + constexpr char kContents2[] = "World!"; + EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), kContents2, + strlen(kContents2))); + EXPECT_EQ(4, FPDFDoc_GetAttachmentCount(document())); + + // Verify the name of the new attachment (i.e. the fourth attachment). + attachment = FPDFDoc_GetAttachment(document(), 3); + ASSERT_TRUE(attachment); + len = FPDFAttachment_GetName(attachment, nullptr, 0); + buf.clear(); + buf.resize(len); + EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); + EXPECT_STREQ(L"z.txt", + GetPlatformWString(reinterpret_cast(buf.data())) + .c_str()); + + // Verify the content of the new attachment (i.e. the fourth attachment). + len = FPDFAttachment_GetFile(attachment, nullptr, 0); + buf.clear(); + buf.resize(len); + ASSERT_EQ(6u, FPDFAttachment_GetFile(attachment, buf.data(), len)); + EXPECT_EQ(std::string(kContents2), std::string(buf.data(), 6)); +} + +TEST_F(FPDFAttachmentEmbeddertest, AddAttachmentsWithParams) { + // Open a file with two attachments. + ASSERT_TRUE(OpenDocument("embedded_attachments.pdf")); + EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document())); + + // Add an attachment to the embedded file list. + std::unique_ptr file_name = + GetFPDFWideString(L"5.txt"); + FPDF_ATTACHMENT attachment = + FPDFDoc_AddAttachment(document(), file_name.get()); + constexpr char kContents[] = "Hello World!"; + EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), kContents, + strlen(kContents))); + + // Set the date to be an arbitrary value. + std::unique_ptr date_key = + GetFPDFWideString(L"CreationDate"); + constexpr wchar_t kDateW[] = L"D:20170720161527-04'00'"; + std::unique_ptr ws_date = + GetFPDFWideString(kDateW); + EXPECT_TRUE( + FPDFAttachment_SetStringValue(attachment, date_key.get(), ws_date.get())); + + // Set the checksum to be an arbitrary value. + std::unique_ptr checksum_key = + GetFPDFWideString(L"CheckSum"); + constexpr wchar_t kCheckSumW[] = L""; + std::unique_ptr ws_checksum = + GetFPDFWideString(kCheckSumW); + EXPECT_TRUE(FPDFAttachment_SetStringValue(attachment, checksum_key.get(), + ws_checksum.get())); + + // Verify the name of the new attachment (i.e. the second attachment). + attachment = FPDFDoc_GetAttachment(document(), 1); + ASSERT_TRUE(attachment); + unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0); + std::vector buf(len); + EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); + EXPECT_STREQ(L"5.txt", + GetPlatformWString(reinterpret_cast(buf.data())) + .c_str()); + + // Verify the content of the new attachment. + len = FPDFAttachment_GetFile(attachment, nullptr, 0); + buf.clear(); + buf.resize(len); + ASSERT_EQ(12u, FPDFAttachment_GetFile(attachment, buf.data(), len)); + EXPECT_EQ(std::string(kContents), std::string(buf.data(), 12)); + + // Verify the creation date of the new attachment. + len = FPDFAttachment_GetStringValue(attachment, date_key.get(), nullptr, 0); + buf.clear(); + buf.resize(len); + EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, date_key.get(), + buf.data(), len)); + EXPECT_STREQ(kDateW, + GetPlatformWString(reinterpret_cast(buf.data())) + .c_str()); + + // Verify the checksum of the new attachment. + len = + FPDFAttachment_GetStringValue(attachment, checksum_key.get(), nullptr, 0); + buf.clear(); + buf.resize(len); + EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, checksum_key.get(), + buf.data(), len)); + EXPECT_STREQ(kCheckSumW, + GetPlatformWString(reinterpret_cast(buf.data())) + .c_str()); + + // Overwrite the existing file with empty content, and check that the checksum + // gets updated to the correct value. + EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), nullptr, 0)); + EXPECT_EQ(0u, FPDFAttachment_GetFile(attachment, nullptr, 0)); + len = + FPDFAttachment_GetStringValue(attachment, checksum_key.get(), nullptr, 0); + buf.clear(); + buf.resize(len); + EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, checksum_key.get(), + buf.data(), len)); + EXPECT_EQ(L"", + GetPlatformWString(reinterpret_cast(buf.data()))); +} \ No newline at end of file -- cgit v1.2.3