Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 1 | // Copyright 2017 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jane Liu | f63e813 | 2017-07-25 18:11:27 -0400 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 9 | #include "public/fpdf_attachment.h" |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 10 | #include "public/fpdfview.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 11 | #include "testing/embedder_test.h" |
Lei Zhang | b6992dd | 2019-02-05 23:30:20 +0000 | [diff] [blame^] | 12 | #include "testing/fx_string_testhelpers.h" |
Lei Zhang | 4c64e96 | 2019-02-05 19:24:12 +0000 | [diff] [blame] | 13 | #include "testing/utils/hash.h" |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 14 | |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 15 | static constexpr char kDateKey[] = "CreationDate"; |
| 16 | static constexpr char kChecksumKey[] = "CheckSum"; |
| 17 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 18 | class FPDFAttachmentEmbedderTest : public EmbedderTest {}; |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 19 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 20 | TEST_F(FPDFAttachmentEmbedderTest, ExtractAttachments) { |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 21 | // Open a file with two attachments. |
| 22 | ASSERT_TRUE(OpenDocument("embedded_attachments.pdf")); |
| 23 | EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document())); |
| 24 | |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 25 | // Retrieve the first attachment. |
| 26 | FPDF_ATTACHMENT attachment = FPDFDoc_GetAttachment(document(), 0); |
| 27 | ASSERT_TRUE(attachment); |
| 28 | |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 29 | // Check that the name of the first attachment is correct. |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 30 | unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 31 | std::vector<char> buf(len); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 32 | EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 33 | EXPECT_STREQ(L"1.txt", |
| 34 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 35 | .c_str()); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 36 | |
| 37 | // Check that the content of the first attachment is correct. |
| 38 | len = FPDFAttachment_GetFile(attachment, nullptr, 0); |
| 39 | buf.clear(); |
| 40 | buf.resize(len); |
| 41 | ASSERT_EQ(4u, FPDFAttachment_GetFile(attachment, buf.data(), len)); |
| 42 | EXPECT_EQ(std::string("test"), std::string(buf.data(), 4)); |
| 43 | |
| 44 | // Check that a non-existent key does not exist. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 45 | EXPECT_FALSE(FPDFAttachment_HasKey(attachment, "none")); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 46 | |
| 47 | // Check that the string value of a non-string dictionary entry is empty. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 48 | static constexpr char kSizeKey[] = "Size"; |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 49 | EXPECT_EQ(FPDF_OBJECT_NUMBER, |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 50 | FPDFAttachment_GetValueType(attachment, kSizeKey)); |
| 51 | EXPECT_EQ(2u, |
| 52 | FPDFAttachment_GetStringValue(attachment, kSizeKey, nullptr, 0)); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 53 | |
| 54 | // Check that the creation date of the first attachment is correct. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 55 | len = FPDFAttachment_GetStringValue(attachment, kDateKey, nullptr, 0); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 56 | buf.clear(); |
| 57 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 58 | EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, kDateKey, buf.data(), |
| 59 | len)); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 60 | EXPECT_STREQ(L"D:20170712214438-07'00'", |
| 61 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 62 | .c_str()); |
| 63 | |
| 64 | // Retrieve the second attachment. |
| 65 | attachment = FPDFDoc_GetAttachment(document(), 1); |
| 66 | ASSERT_TRUE(attachment); |
| 67 | |
| 68 | // Retrieve the second attachment file. |
| 69 | len = FPDFAttachment_GetFile(attachment, nullptr, 0); |
| 70 | buf.clear(); |
| 71 | buf.resize(len); |
| 72 | EXPECT_EQ(5869u, FPDFAttachment_GetFile(attachment, buf.data(), len)); |
| 73 | |
| 74 | // Check that the calculated checksum of the file data matches expectation. |
| 75 | const char kCheckSum[] = "72afcddedf554dda63c0c88e06f1ce18"; |
| 76 | const wchar_t kCheckSumW[] = L"<72AFCDDEDF554DDA63C0C88E06F1CE18>"; |
| 77 | const std::string generated_checksum = |
| 78 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len); |
| 79 | EXPECT_EQ(kCheckSum, generated_checksum); |
| 80 | |
| 81 | // Check that the stored checksum matches expectation. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 82 | len = FPDFAttachment_GetStringValue(attachment, kChecksumKey, nullptr, 0); |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 83 | buf.clear(); |
| 84 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 85 | EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, kChecksumKey, |
Jane Liu | 18ae06d | 2017-07-18 10:15:16 -0400 | [diff] [blame] | 86 | buf.data(), len)); |
| 87 | EXPECT_EQ(kCheckSumW, |
| 88 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 89 | } |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 90 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 91 | TEST_F(FPDFAttachmentEmbedderTest, AddAttachments) { |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 92 | // Open a file with two attachments. |
| 93 | ASSERT_TRUE(OpenDocument("embedded_attachments.pdf")); |
| 94 | EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document())); |
| 95 | |
| 96 | // Check that adding an attachment with an empty name would fail. |
| 97 | EXPECT_FALSE(FPDFDoc_AddAttachment(document(), nullptr)); |
| 98 | |
| 99 | // Add an attachment to the beginning of the embedded file list. |
| 100 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> file_name = |
| 101 | GetFPDFWideString(L"0.txt"); |
| 102 | FPDF_ATTACHMENT attachment = |
| 103 | FPDFDoc_AddAttachment(document(), file_name.get()); |
| 104 | |
| 105 | // Check that writing to a file with nullptr but non-zero bytes would fail. |
| 106 | EXPECT_FALSE(FPDFAttachment_SetFile(attachment, document(), nullptr, 10)); |
| 107 | |
| 108 | // Set the new attachment's file. |
| 109 | constexpr char kContents1[] = "Hello!"; |
| 110 | EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), kContents1, |
| 111 | strlen(kContents1))); |
| 112 | |
| 113 | // Verify the name of the new attachment (i.e. the first attachment). |
| 114 | attachment = FPDFDoc_GetAttachment(document(), 0); |
| 115 | ASSERT_TRUE(attachment); |
| 116 | unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0); |
| 117 | std::vector<char> buf(len); |
| 118 | EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); |
| 119 | EXPECT_STREQ(L"0.txt", |
| 120 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 121 | .c_str()); |
| 122 | |
| 123 | // Verify the content of the new attachment (i.e. the first attachment). |
| 124 | len = FPDFAttachment_GetFile(attachment, nullptr, 0); |
| 125 | buf.clear(); |
| 126 | buf.resize(len); |
| 127 | ASSERT_EQ(6u, FPDFAttachment_GetFile(attachment, buf.data(), len)); |
| 128 | EXPECT_EQ(std::string(kContents1), std::string(buf.data(), 6)); |
| 129 | |
| 130 | // Add an attachment to the end of the embedded file list and set its file. |
| 131 | file_name = GetFPDFWideString(L"z.txt"); |
| 132 | attachment = FPDFDoc_AddAttachment(document(), file_name.get()); |
| 133 | constexpr char kContents2[] = "World!"; |
| 134 | EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), kContents2, |
| 135 | strlen(kContents2))); |
| 136 | EXPECT_EQ(4, FPDFDoc_GetAttachmentCount(document())); |
| 137 | |
| 138 | // Verify the name of the new attachment (i.e. the fourth attachment). |
| 139 | attachment = FPDFDoc_GetAttachment(document(), 3); |
| 140 | ASSERT_TRUE(attachment); |
| 141 | len = FPDFAttachment_GetName(attachment, nullptr, 0); |
| 142 | buf.clear(); |
| 143 | buf.resize(len); |
| 144 | EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); |
| 145 | EXPECT_STREQ(L"z.txt", |
| 146 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 147 | .c_str()); |
| 148 | |
| 149 | // Verify the content of the new attachment (i.e. the fourth attachment). |
| 150 | len = FPDFAttachment_GetFile(attachment, nullptr, 0); |
| 151 | buf.clear(); |
| 152 | buf.resize(len); |
| 153 | ASSERT_EQ(6u, FPDFAttachment_GetFile(attachment, buf.data(), len)); |
| 154 | EXPECT_EQ(std::string(kContents2), std::string(buf.data(), 6)); |
| 155 | } |
| 156 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 157 | TEST_F(FPDFAttachmentEmbedderTest, AddAttachmentsWithParams) { |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 158 | // Open a file with two attachments. |
| 159 | ASSERT_TRUE(OpenDocument("embedded_attachments.pdf")); |
| 160 | EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document())); |
| 161 | |
| 162 | // Add an attachment to the embedded file list. |
| 163 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> file_name = |
| 164 | GetFPDFWideString(L"5.txt"); |
| 165 | FPDF_ATTACHMENT attachment = |
| 166 | FPDFDoc_AddAttachment(document(), file_name.get()); |
| 167 | constexpr char kContents[] = "Hello World!"; |
| 168 | EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), kContents, |
| 169 | strlen(kContents))); |
| 170 | |
| 171 | // Set the date to be an arbitrary value. |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 172 | constexpr wchar_t kDateW[] = L"D:20170720161527-04'00'"; |
| 173 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> ws_date = |
| 174 | GetFPDFWideString(kDateW); |
| 175 | EXPECT_TRUE( |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 176 | FPDFAttachment_SetStringValue(attachment, kDateKey, ws_date.get())); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 177 | |
| 178 | // Set the checksum to be an arbitrary value. |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 179 | constexpr wchar_t kCheckSumW[] = L"<ABCDEF01234567899876543210FEDCBA>"; |
| 180 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> ws_checksum = |
| 181 | GetFPDFWideString(kCheckSumW); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 182 | EXPECT_TRUE(FPDFAttachment_SetStringValue(attachment, kChecksumKey, |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 183 | ws_checksum.get())); |
| 184 | |
| 185 | // Verify the name of the new attachment (i.e. the second attachment). |
| 186 | attachment = FPDFDoc_GetAttachment(document(), 1); |
| 187 | ASSERT_TRUE(attachment); |
| 188 | unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0); |
| 189 | std::vector<char> buf(len); |
| 190 | EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); |
| 191 | EXPECT_STREQ(L"5.txt", |
| 192 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 193 | .c_str()); |
| 194 | |
| 195 | // Verify the content of the new attachment. |
| 196 | len = FPDFAttachment_GetFile(attachment, nullptr, 0); |
| 197 | buf.clear(); |
| 198 | buf.resize(len); |
| 199 | ASSERT_EQ(12u, FPDFAttachment_GetFile(attachment, buf.data(), len)); |
| 200 | EXPECT_EQ(std::string(kContents), std::string(buf.data(), 12)); |
| 201 | |
| 202 | // Verify the creation date of the new attachment. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 203 | len = FPDFAttachment_GetStringValue(attachment, kDateKey, nullptr, 0); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 204 | buf.clear(); |
| 205 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 206 | EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, kDateKey, buf.data(), |
| 207 | len)); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 208 | EXPECT_STREQ(kDateW, |
| 209 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 210 | .c_str()); |
| 211 | |
| 212 | // Verify the checksum of the new attachment. |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 213 | len = FPDFAttachment_GetStringValue(attachment, kChecksumKey, nullptr, 0); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 214 | buf.clear(); |
| 215 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 216 | EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, kChecksumKey, |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 217 | buf.data(), len)); |
| 218 | EXPECT_STREQ(kCheckSumW, |
| 219 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 220 | .c_str()); |
| 221 | |
| 222 | // Overwrite the existing file with empty content, and check that the checksum |
| 223 | // gets updated to the correct value. |
| 224 | EXPECT_TRUE(FPDFAttachment_SetFile(attachment, document(), nullptr, 0)); |
| 225 | EXPECT_EQ(0u, FPDFAttachment_GetFile(attachment, nullptr, 0)); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 226 | len = FPDFAttachment_GetStringValue(attachment, kChecksumKey, nullptr, 0); |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 227 | buf.clear(); |
| 228 | buf.resize(len); |
Lei Zhang | df064df | 2017-08-31 02:33:27 -0700 | [diff] [blame] | 229 | EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, kChecksumKey, |
Jane Liu | 54a4214 | 2017-07-24 16:40:54 -0400 | [diff] [blame] | 230 | buf.data(), len)); |
| 231 | EXPECT_EQ(L"<D41D8CD98F00B204E9800998ECF8427E>", |
| 232 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))); |
Jane Liu | f63e813 | 2017-07-25 18:11:27 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Lei Zhang | ab41f25 | 2018-12-23 03:10:50 +0000 | [diff] [blame] | 235 | TEST_F(FPDFAttachmentEmbedderTest, DeleteAttachment) { |
Jane Liu | f63e813 | 2017-07-25 18:11:27 -0400 | [diff] [blame] | 236 | // Open a file with two attachments. |
| 237 | ASSERT_TRUE(OpenDocument("embedded_attachments.pdf")); |
| 238 | EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document())); |
| 239 | |
| 240 | // Verify the name of the first attachment. |
| 241 | FPDF_ATTACHMENT attachment = FPDFDoc_GetAttachment(document(), 0); |
| 242 | unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0); |
| 243 | std::vector<char> buf(len); |
| 244 | EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len)); |
| 245 | EXPECT_STREQ(L"1.txt", |
| 246 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 247 | .c_str()); |
| 248 | |
| 249 | // Delete the first attachment. |
| 250 | EXPECT_TRUE(FPDFDoc_DeleteAttachment(document(), 0)); |
| 251 | EXPECT_EQ(1, FPDFDoc_GetAttachmentCount(document())); |
| 252 | |
| 253 | // Verify the name of the new first attachment. |
| 254 | attachment = FPDFDoc_GetAttachment(document(), 0); |
| 255 | len = FPDFAttachment_GetName(attachment, nullptr, 0); |
| 256 | buf.clear(); |
| 257 | buf.resize(len); |
| 258 | EXPECT_EQ(26u, FPDFAttachment_GetName(attachment, buf.data(), len)); |
| 259 | EXPECT_STREQ(L"attached.pdf", |
| 260 | GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())) |
| 261 | .c_str()); |
| 262 | } |