xref: /aosp_15_r20/external/pdfium/core/fpdfapi/edit/cpdf_creator_embeddertest.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
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 <string.h>
6 
7 #include <string>
8 
9 #include "public/cpp/fpdf_scopers.h"
10 #include "public/fpdf_annot.h"
11 #include "public/fpdf_edit.h"
12 #include "public/fpdfview.h"
13 #include "testing/embedder_test.h"
14 #include "testing/fake_file_access.h"
15 #include "testing/gmock/include/gmock/gmock-matchers.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/utils/file_util.h"
18 
19 class CPDF_CreatorEmbedderTest : public EmbedderTest {};
20 
TEST_F(CPDF_CreatorEmbedderTest,SavedDocsAreEqualAfterParse)21 TEST_F(CPDF_CreatorEmbedderTest, SavedDocsAreEqualAfterParse) {
22   ASSERT_TRUE(OpenDocument("annotation_stamp_with_ap.pdf"));
23   // Save without additional data reading.
24   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
25   const std::string saved_doc_1 = GetString();
26   ClearString();
27 
28   {
29     // Do some read only operations.
30     ASSERT_GE(1, FPDF_GetPageCount(document()));
31     FPDF_PAGE page = LoadPage(0);
32     ASSERT_TRUE(page);
33     ScopedFPDFBitmap bitmap = RenderLoadedPageWithFlags(page, FPDF_ANNOT);
34     EXPECT_EQ(595, FPDFBitmap_GetWidth(bitmap.get()));
35     EXPECT_EQ(842, FPDFBitmap_GetHeight(bitmap.get()));
36     UnloadPage(page);
37   }
38 
39   // Save when we have additional loaded data.
40   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
41   const std::string saved_doc_2 = GetString();
42   ClearString();
43 
44   // The sizes of saved docs should be equal.
45   EXPECT_EQ(saved_doc_1.size(), saved_doc_2.size());
46 }
47 
TEST_F(CPDF_CreatorEmbedderTest,BUG_873)48 TEST_F(CPDF_CreatorEmbedderTest, BUG_873) {
49   ASSERT_TRUE(OpenDocument("embedded_attachments.pdf"));
50   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
51 
52   // Cannot match second part of the ID since it is randomly generated.
53   std::string saved_data = GetString();
54   const char kTrailerBeforeSecondID[] =
55       "trailer\r\n<</Info 9 0 R /Root 11 0 R /Size "
56       "36/ID[<D889EB6B9ADF88E5EDA7DC08FE85978B><";
57   ASSERT_THAT(saved_data, testing::HasSubstr(kTrailerBeforeSecondID));
58   size_t trailer_start = saved_data.find(kTrailerBeforeSecondID);
59   constexpr size_t kIdLen = 32;
60   size_t trailer_continuation =
61       trailer_start + strlen(kTrailerBeforeSecondID) + kIdLen;
62   std::string data_after_second_id = saved_data.substr(trailer_continuation);
63   EXPECT_THAT(data_after_second_id, testing::StartsWith(">]>>\r\n"));
64 }
65 
TEST_F(CPDF_CreatorEmbedderTest,SaveLinearizedInfo)66 TEST_F(CPDF_CreatorEmbedderTest, SaveLinearizedInfo) {
67   FileAccessForTesting file_acc("linearized.pdf");
68   FakeFileAccess fake_acc(&file_acc);
69 
70   CreateAvail(fake_acc.GetFileAvail(), fake_acc.GetFileAccess());
71   while (PDF_DATA_AVAIL !=
72          FPDFAvail_IsDocAvail(avail(), fake_acc.GetDownloadHints())) {
73     fake_acc.SetRequestedDataAvailable();
74   }
75 
76   SetDocumentFromAvail();
77   ASSERT_TRUE(document());
78 
79   // Load second page, to parse additional crossref sections.
80   while (PDF_DATA_AVAIL !=
81          FPDFAvail_IsPageAvail(avail(), 1, fake_acc.GetDownloadHints())) {
82     fake_acc.SetRequestedDataAvailable();
83   }
84   // Simulate downloading of whole file.
85   fake_acc.SetWholeFileAvailable();
86   // Save document.
87   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
88   const std::string saved_doc = GetString();
89 
90   EXPECT_THAT(saved_doc, ::testing::HasSubstr("/Info"));
91 }
92