blob: d873d9b5ce27d4105076211f5e5d4b10e912e8c4 [file] [log] [blame]
Jane Liu53aafa92017-07-12 19:55:02 -04001// 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
5#include "public/fpdf_attachment.h"
Jane Liu18ae06d2017-07-18 10:15:16 -04006#include "public/fpdfview.h"
Jane Liu53aafa92017-07-12 19:55:02 -04007#include "testing/embedder_test.h"
8
9class FPDFAttachmentEmbeddertest : public EmbedderTest {};
10
11TEST_F(FPDFAttachmentEmbeddertest, ExtractAttachments) {
12 // Open a file with two attachments.
13 ASSERT_TRUE(OpenDocument("embedded_attachments.pdf"));
14 EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document()));
15
Jane Liu18ae06d2017-07-18 10:15:16 -040016 // Retrieve the first attachment.
17 FPDF_ATTACHMENT attachment = FPDFDoc_GetAttachment(document(), 0);
18 ASSERT_TRUE(attachment);
19
Jane Liu53aafa92017-07-12 19:55:02 -040020 // Check that the name of the first attachment is correct.
Jane Liu18ae06d2017-07-18 10:15:16 -040021 unsigned long len = FPDFAttachment_GetName(attachment, nullptr, 0);
Jane Liu53aafa92017-07-12 19:55:02 -040022 std::vector<char> buf(len);
Jane Liu18ae06d2017-07-18 10:15:16 -040023 EXPECT_EQ(12u, FPDFAttachment_GetName(attachment, buf.data(), len));
Jane Liu53aafa92017-07-12 19:55:02 -040024 EXPECT_STREQ(L"1.txt",
25 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
26 .c_str());
Jane Liu18ae06d2017-07-18 10:15:16 -040027
28 // Check that the content of the first attachment is correct.
29 len = FPDFAttachment_GetFile(attachment, nullptr, 0);
30 buf.clear();
31 buf.resize(len);
32 ASSERT_EQ(4u, FPDFAttachment_GetFile(attachment, buf.data(), len));
33 EXPECT_EQ(std::string("test"), std::string(buf.data(), 4));
34
35 // Check that a non-existent key does not exist.
36 EXPECT_FALSE(
37 FPDFAttachment_HasKey(attachment, GetFPDFWideString(L"none").get()));
38
39 // Check that the string value of a non-string dictionary entry is empty.
40 std::unique_ptr<unsigned short, pdfium::FreeDeleter> size_key =
41 GetFPDFWideString(L"Size");
42 EXPECT_EQ(FPDF_OBJECT_NUMBER,
43 FPDFAttachment_GetValueType(attachment, size_key.get()));
44 EXPECT_EQ(2u, FPDFAttachment_GetStringValue(attachment, size_key.get(),
45 nullptr, 0));
46
47 // Check that the creation date of the first attachment is correct.
48 std::unique_ptr<unsigned short, pdfium::FreeDeleter> date_key =
49 GetFPDFWideString(L"CreationDate");
50 len = FPDFAttachment_GetStringValue(attachment, date_key.get(), nullptr, 0);
51 buf.clear();
52 buf.resize(len);
53 EXPECT_EQ(48u, FPDFAttachment_GetStringValue(attachment, date_key.get(),
54 buf.data(), len));
55 EXPECT_STREQ(L"D:20170712214438-07'00'",
56 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
57 .c_str());
58
59 // Retrieve the second attachment.
60 attachment = FPDFDoc_GetAttachment(document(), 1);
61 ASSERT_TRUE(attachment);
62
63 // Retrieve the second attachment file.
64 len = FPDFAttachment_GetFile(attachment, nullptr, 0);
65 buf.clear();
66 buf.resize(len);
67 EXPECT_EQ(5869u, FPDFAttachment_GetFile(attachment, buf.data(), len));
68
69 // Check that the calculated checksum of the file data matches expectation.
70 const char kCheckSum[] = "72afcddedf554dda63c0c88e06f1ce18";
71 const wchar_t kCheckSumW[] = L"<72AFCDDEDF554DDA63C0C88E06F1CE18>";
72 const std::string generated_checksum =
73 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len);
74 EXPECT_EQ(kCheckSum, generated_checksum);
75
76 // Check that the stored checksum matches expectation.
77 std::unique_ptr<unsigned short, pdfium::FreeDeleter> checksum_key =
78 GetFPDFWideString(L"CheckSum");
79 len =
80 FPDFAttachment_GetStringValue(attachment, checksum_key.get(), nullptr, 0);
81 buf.clear();
82 buf.resize(len);
83 EXPECT_EQ(70u, FPDFAttachment_GetStringValue(attachment, checksum_key.get(),
84 buf.data(), len));
85 EXPECT_EQ(kCheckSumW,
86 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data())));
Jane Liu53aafa92017-07-12 19:55:02 -040087}