From c8f6ab628c4bdf90dcee3d5ac153beb043a42f92 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 22 Jan 2015 11:20:06 -0800 Subject: Merge to XFA: Add embeddertests. This brings in: 83f96a0 Add API tests: NamedDestsByName, DestGetPageIndex. 9dcd7b8 Add tests for GetNamedDests() API. TBR=bo_xu@foxitsoftware.com Review URL: https://codereview.chromium.org/849693007 --- fpdfsdk/src/fpdfdoc_embeddertest.cpp | 38 ++++++++++ fpdfsdk/src/fpdfview.cpp | 5 +- fpdfsdk/src/fpdfview_embeddertest.cpp | 137 ++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 fpdfsdk/src/fpdfdoc_embeddertest.cpp (limited to 'fpdfsdk/src') diff --git a/fpdfsdk/src/fpdfdoc_embeddertest.cpp b/fpdfsdk/src/fpdfdoc_embeddertest.cpp new file mode 100644 index 0000000000..dadaf1d809 --- /dev/null +++ b/fpdfsdk/src/fpdfdoc_embeddertest.cpp @@ -0,0 +1,38 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "../../testing/embedder_test.h" +#include "../../fpdfsdk/include/fpdfview.h" +#include "../../fpdfsdk/include/fpdfdoc.h" +#include "testing/gtest/include/gtest/gtest.h" + +class FPDFDocEmbeddertest : public EmbedderTest { +}; + +TEST_F(FPDFDocEmbeddertest, DestGetPageIndex) { + EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); + + // NULL FPDF_DEST case. + EXPECT_EQ(0, FPDFDest_GetPageIndex(document(), nullptr)); + + // Page number directly in item from Dests NameTree. + FPDF_DEST dest = FPDF_GetNamedDestByName(document(), "First"); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(1, FPDFDest_GetPageIndex(document(), dest)); + + // Page number via object reference in item from Dests NameTree. + dest = FPDF_GetNamedDestByName(document(), "Next"); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(1, FPDFDest_GetPageIndex(document(), dest)); + + // Page number directly in item from Dests dictionary. + dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(11, FPDFDest_GetPageIndex(document(), dest)); + + // Invalid object reference in item from Dests NameTree. + dest = FPDF_GetNamedDestByName(document(), "LastAlternate"); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(0, FPDFDest_GetPageIndex(document(), dest)); +} diff --git a/fpdfsdk/src/fpdfview.cpp b/fpdfsdk/src/fpdfview.cpp index 4114dac01c..feec289614 100644 --- a/fpdfsdk/src/fpdfview.cpp +++ b/fpdfsdk/src/fpdfview.cpp @@ -1048,8 +1048,10 @@ DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, pDestObj = nameTree.LookupValue(index, bsName); } if (!pDestObj) return NULL; - if (pDestObj->GetType() == PDFOBJ_DICTIONARY) + if (pDestObj->GetType() == PDFOBJ_DICTIONARY) { pDestObj = ((CPDF_Dictionary*)pDestObj)->GetArray(FX_BSTRC("D")); + if (!pDestObj) return NULL; + } if (pDestObj->GetType() != PDFOBJ_ARRAY) return NULL; CFX_WideString wsName = PDF_DecodeText(bsName); CFX_ByteString utf16Name = wsName.UTF16LE_Encode(); @@ -1058,6 +1060,7 @@ DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, buflen = len; } else if (buflen >= len) { memcpy(buffer, utf16Name.c_str(), len); + buflen = len; } else { buflen = -1; } diff --git a/fpdfsdk/src/fpdfview_embeddertest.cpp b/fpdfsdk/src/fpdfview_embeddertest.cpp index 94a51094a8..ee32727ace 100644 --- a/fpdfsdk/src/fpdfview_embeddertest.cpp +++ b/fpdfsdk/src/fpdfview_embeddertest.cpp @@ -2,7 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include +#include + #include "../../testing/embedder_test.h" +#include "../../fpdfsdk/include/fpdfview.h" #include "testing/gtest/include/gtest/gtest.h" class FPDFViewEmbeddertest : public EmbedderTest { @@ -40,3 +44,136 @@ TEST_F(FPDFViewEmbeddertest, ViewerRef) { EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document())); } +TEST_F(FPDFViewEmbeddertest, NamedDests) { + EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); + long buffer_size; + char fixed_buffer[512]; + FPDF_DEST dest; + + // Query the size of the first item. + buffer_size = 2000000; // Absurdly large, check not used for this case. + dest = FPDF_GetNamedDest(document(), 0, nullptr, buffer_size); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(12u, buffer_size); + + // Try to retrieve the first item with too small a buffer. + buffer_size = 10; + dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(-1, buffer_size); + + // Try to retrieve the first item with correctly sized buffer. Item is + // taken from Dests NameTree in named_dests.pdf. + buffer_size = 12; + dest = FPDF_GetNamedDest(document(), 0, fixed_buffer, buffer_size); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(12u, buffer_size); + EXPECT_EQ(std::string("F\0i\0r\0s\0t\0\0\0", 12), + std::string(fixed_buffer, buffer_size)); + + // Try to retrieve the second item with ample buffer. Item is taken + // from Dests NameTree but has a sub-dictionary in named_dests.pdf. + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), 1, fixed_buffer, buffer_size); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(10u, buffer_size); + EXPECT_EQ(std::string("N\0e\0x\0t\0\0\0", 10), + std::string(fixed_buffer, buffer_size)); + + // Try to retrieve third item with ample buffer. Item is taken + // from Dests NameTree but has a bad sub-dictionary in named_dests.pdf. + // in named_dests.pdf). + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), 2, fixed_buffer, buffer_size); + EXPECT_EQ(nullptr, dest); + EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. + + // Try to retrieve the forth item with ample buffer. Item is taken + // from Dests NameTree but has a vale of the wrong type in named_dests.pdf. + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), 3, fixed_buffer, buffer_size); + EXPECT_EQ(nullptr, dest); + EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. + + // Try to retrieve fifth item with ample buffer. Item taken from the + // old-style Dests dictionary object in named_dests.pdf. + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), 4, fixed_buffer, buffer_size); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(30u, buffer_size); + EXPECT_EQ( + std::string("F\0i\0r\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 30), + std::string(fixed_buffer, buffer_size)); + + // Try to retrieve sixth item with ample buffer. Item istaken from the + // old-style Dests dictionary object but has a sub-dictionary in + // named_dests.pdf. + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), 5, fixed_buffer, buffer_size); + EXPECT_NE(nullptr, dest); + EXPECT_EQ(28u, buffer_size); + EXPECT_EQ( + std::string("L\0a\0s\0t\0A\0l\0t\0e\0r\0n\0a\0t\0e\0\0\0", 28), + std::string(fixed_buffer, buffer_size)); + + // Try to retrieve non-existent item with ample buffer. + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), 6, fixed_buffer, buffer_size); + EXPECT_EQ(nullptr, dest); + EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. + + // Try to underflow/overflow the integer index. + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), std::numeric_limits::max(), + fixed_buffer, buffer_size); + EXPECT_EQ(nullptr, dest); + EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. + + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), std::numeric_limits::min(), + fixed_buffer, buffer_size); + EXPECT_EQ(nullptr, dest); + EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. + + buffer_size = sizeof(fixed_buffer); + dest = FPDF_GetNamedDest(document(), -1, fixed_buffer, buffer_size); + EXPECT_EQ(nullptr, dest); + EXPECT_EQ(sizeof(fixed_buffer), buffer_size); // unmodified. +} + +TEST_F(FPDFViewEmbeddertest, NamedDestsByName) { + EXPECT_TRUE(OpenDocument("testing/resources/named_dests.pdf")); + + // Null pointer returns NULL. + FPDF_DEST dest = FPDF_GetNamedDestByName(document(), nullptr); + EXPECT_EQ(nullptr, dest); + + // Empty string returns NULL. + dest = FPDF_GetNamedDestByName(document(), ""); + EXPECT_EQ(nullptr, dest); + + // Item from Dests NameTree. + dest = FPDF_GetNamedDestByName(document(), "First"); + EXPECT_NE(nullptr, dest); + + long ignore_len = 0; + FPDF_DEST dest_by_index = + FPDF_GetNamedDest(document(), 0, nullptr, ignore_len); + EXPECT_EQ(dest_by_index, dest); + + // Item from Dests dictionary. + dest = FPDF_GetNamedDestByName(document(), "FirstAlternate"); + EXPECT_NE(nullptr, dest); + + ignore_len = 0; + dest_by_index = FPDF_GetNamedDest(document(), 4, nullptr, ignore_len); + EXPECT_EQ(dest_by_index, dest); + + // Bad value type for item from Dests NameTree array. + dest = FPDF_GetNamedDestByName(document(), "WrongType"); + EXPECT_EQ(nullptr, dest); + + // No such destination in either Dest NameTree or dictionary. + dest = FPDF_GetNamedDestByName(document(), "Bogus"); + EXPECT_EQ(nullptr, dest); +} -- cgit v1.2.3