Add FPDFFlattenEmbedderTest.FlatWithBadFont

Demonstrate how PDFium-flattened output carries over an out-of-spec font
encoding from the input.

Bug: 344775293
Change-Id: Ic7dab7408a6b14b6494ae69818cc34ed43e841f9
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/120850
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/fpdfsdk/fpdf_flatten_embeddertest.cpp b/fpdfsdk/fpdf_flatten_embeddertest.cpp
index 26fafc6..5815912 100644
--- a/fpdfsdk/fpdf_flatten_embeddertest.cpp
+++ b/fpdfsdk/fpdf_flatten_embeddertest.cpp
@@ -7,8 +7,11 @@
 #include "public/fpdf_flatten.h"
 #include "public/fpdfview.h"
 #include "testing/embedder_test.h"
+#include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using testing::HasSubstr;
+
 namespace {
 
 class FPDFFlattenEmbedderTest : public EmbedderTest {};
@@ -39,6 +42,22 @@
   UnloadPage(page);
 }
 
+TEST_F(FPDFFlattenEmbedderTest, FlatWithBadFont) {
+  ASSERT_TRUE(OpenDocument("344775293.pdf"));
+  FPDF_PAGE page = LoadPage(0);
+  EXPECT_TRUE(page);
+
+  FORM_OnLButtonDown(form_handle(), page, 0, 20, 30);
+  FORM_OnLButtonUp(form_handle(), page, 0, 20, 30);
+
+  EXPECT_EQ(FLATTEN_SUCCESS, FPDFPage_Flatten(page, FLAT_PRINT));
+  EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+
+  // TODO(crbug.com/344775293): HasSubstr() should be negated with Not().
+  EXPECT_THAT(GetString(), HasSubstr("/PDFDocEncoding"));
+  UnloadPage(page);
+}
+
 TEST_F(FPDFFlattenEmbedderTest, Bug861842) {
   const char* checkbox_checksum = []() {
     if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
diff --git a/testing/resources/344775293.in b/testing/resources/344775293.in
new file mode 100644
index 0000000..153d68d
--- /dev/null
+++ b/testing/resources/344775293.in
@@ -0,0 +1,101 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /AcroForm <<
+    /DA (/ZaDb 10 Tf 0 g)
+    /DR <<
+      /Font <<
+        /ZaDb 5 0 R
+      >>
+    >>
+    /Fields [4 0 R]
+  >>
+  /Pages 2 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Count 1
+  /Kids [3 0 R]
+>>
+endobj
+{{object 3 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /Annots [4 0 R]
+  /MediaBox [0 0 100 100]
+>>
+endobj
+{{object 4 0}} <<
+  /Type /Annot
+  /Subtype /Widget
+  /FT /Btn
+  /AP <<
+    /N <<
+      /Off 8 0 R
+      /Yes 9 0 R
+    >>
+  >>
+  /AS /Off
+  /DA (/ZaDb 0 Tf 0 g)
+  /F 4
+  /MK <<
+    /BC [0]
+    /BG [1]
+    /CA (4)
+  >>
+  /P 3 0 R
+  /Rect [10 20 25 35]
+  /T (CheckBox)
+>>
+endobj
+{{object 5 0}} <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /ZapfDingbats
+  /Encoding 6 0 R
+  /Name /ZaDb
+>>
+endobj
+{{object 6 0}} <<
+  /Type /Encoding
+  /BaseEncoding /PDFDocEncoding
+>>
+endobj
+{{object 8 0}} <<
+  /Type /XObject
+  /Subtype /Form
+  /FormType 1
+  /BBox [0 0 15 15]
+  {{streamlen}}
+>>
+stream
+1 g
+0 0 15 15 re
+f
+endstream
+endobj
+{{object 9 0}} <<
+  /Type /XObject
+  /Subtype /Form
+  /FormType 1
+  /BBox [0 0 15 15]
+  /Resources <<
+    /Font <<
+      /ZaDb 5 0 R
+    >>
+  >>
+  {{streamlen}}
+>>
+stream
+BT
+/ZaDb 12 Tf
+2.5 2.5 Td
+(4) Tj
+ET
+endstream
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/344775293.pdf b/testing/resources/344775293.pdf
new file mode 100644
index 0000000..609c78d
--- /dev/null
+++ b/testing/resources/344775293.pdf
@@ -0,0 +1,117 @@
+%PDF-1.7
+% ò¤ô
+1 0 obj <<
+  /Type /Catalog
+  /AcroForm <<
+    /DA (/ZaDb 10 Tf 0 g)
+    /DR <<
+      /Font <<
+        /ZaDb 5 0 R
+      >>
+    >>
+    /Fields [4 0 R]
+  >>
+  /Pages 2 0 R
+>>
+endobj
+2 0 obj <<
+  /Type /Pages
+  /Count 1
+  /Kids [3 0 R]
+>>
+endobj
+3 0 obj <<
+  /Type /Page
+  /Parent 2 0 R
+  /Annots [4 0 R]
+  /MediaBox [0 0 100 100]
+>>
+endobj
+4 0 obj <<
+  /Type /Annot
+  /Subtype /Widget
+  /FT /Btn
+  /AP <<
+    /N <<
+      /Off 8 0 R
+      /Yes 9 0 R
+    >>
+  >>
+  /AS /Off
+  /DA (/ZaDb 0 Tf 0 g)
+  /F 4
+  /MK <<
+    /BC [0]
+    /BG [1]
+    /CA (4)
+  >>
+  /P 3 0 R
+  /Rect [10 20 25 35]
+  /T (CheckBox)
+>>
+endobj
+5 0 obj <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /ZapfDingbats
+  /Encoding 6 0 R
+  /Name /ZaDb
+>>
+endobj
+6 0 obj <<
+  /Type /Encoding
+  /BaseEncoding /PDFDocEncoding
+>>
+endobj
+8 0 obj <<
+  /Type /XObject
+  /Subtype /Form
+  /FormType 1
+  /BBox [0 0 15 15]
+  /Length 19
+>>
+stream
+1 g
+0 0 15 15 re
+f
+endstream
+endobj
+9 0 obj <<
+  /Type /XObject
+  /Subtype /Form
+  /FormType 1
+  /BBox [0 0 15 15]
+  /Resources <<
+    /Font <<
+      /ZaDb 5 0 R
+    >>
+  >>
+  /Length 36
+>>
+stream
+BT
+/ZaDb 12 Tf
+2.5 2.5 Td
+(4) Tj
+ET
+endstream
+endobj
+xref
+0 10
+0000000000 65535 f 
+0000000015 00000 n 
+0000000196 00000 n 
+0000000259 00000 n 
+0000000354 00000 n 
+0000000625 00000 n 
+0000000736 00000 n 
+0000000000 65535 f 
+0000000807 00000 n 
+0000000945 00000 n 
+trailer <<
+  /Root 1 0 R
+  /Size 10
+>>
+startxref
+1159
+%%EOF