Add a test PDF with an empty xref table.

Test loading it with various document loading functions to make sure
they behave consistently with regards to whether the document needs to
have its xref table rebuilt.

Right now, it does require a rebuild. Most PDF viewers, along with
pdfium_test, are fine with the empty xref table. pdfium_test uses a
separate FPDF_FILEACCESS implementation from pdfium_embeddertests, so
pdfium_test actually behaves inconsistently with this test.

It is preferable to avoid rebuilding the xref table. Rebuilding requires
more work and may not always rebuild correctly. There will be a
follow-up to skip the rebuild.

BUG=pdfium:1197

Change-Id: Ic6378d8514ce122b1072be30e0e06d69dec5c4ed
Reviewed-on: https://pdfium-review.googlesource.com/c/47570
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_view_embeddertest.cpp b/fpdfsdk/fpdf_view_embeddertest.cpp
index 807c8fe..301eaa3 100644
--- a/fpdfsdk/fpdf_view_embeddertest.cpp
+++ b/fpdfsdk/fpdf_view_embeddertest.cpp
@@ -680,3 +680,29 @@
   ASSERT_TRUE(OpenDocument("bug_664284.pdf"));
   EXPECT_FALSE(FPDF_DocumentHasValidCrossReferenceTable(document()));
 }
+
+// Related to https://crbug.com/pdfium/1197
+// TODO(thestig): FPDF_DocumentHasValidCrossReferenceTable() should return true
+// in this test.
+TEST_F(FPDFViewEmbedderTest, LoadDocumentWithEmptyXRefConsistently) {
+  ASSERT_TRUE(OpenDocument("empty_xref.pdf"));
+  EXPECT_FALSE(FPDF_DocumentHasValidCrossReferenceTable(document()));
+
+  std::string file_path;
+  ASSERT_TRUE(PathService::GetTestFilePath("empty_xref.pdf", &file_path));
+  {
+    ScopedFPDFDocument doc(FPDF_LoadDocument(file_path.c_str(), ""));
+    ASSERT_TRUE(doc);
+    EXPECT_FALSE(FPDF_DocumentHasValidCrossReferenceTable(doc.get()));
+  }
+  {
+    size_t file_length = 0;
+    std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
+        GetFileContents(file_path.c_str(), &file_length);
+    ASSERT(file_contents);
+    ScopedFPDFDocument doc(
+        FPDF_LoadMemDocument(file_contents.get(), file_length, ""));
+    ASSERT_TRUE(doc);
+    EXPECT_FALSE(FPDF_DocumentHasValidCrossReferenceTable(doc.get()));
+  }
+}
diff --git a/testing/resources/empty_xref.pdf b/testing/resources/empty_xref.pdf
new file mode 100644
index 0000000..ed02848
--- /dev/null
+++ b/testing/resources/empty_xref.pdf
@@ -0,0 +1,70 @@
+%PDF-1.7
+% ò¤ô
+1 0 obj <<
+  /Type /Catalog
+  /Pages 2 0 R
+>>
+endobj
+2 0 obj <<
+  /Type /Pages
+  /MediaBox [ 0 0 200 200 ]
+  /Count 1
+  /Kids [ 3 0 R ]
+>>
+endobj
+3 0 obj <<
+  /Type /Page
+  /Parent 2 0 R
+  /Resources <<
+    /Font <<
+      /F1 4 0 R
+      /F2 5 0 R
+    >>
+  >>
+  /Contents 6 0 R
+>>
+endobj
+4 0 obj <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /Times-Roman
+>>
+endobj
+5 0 obj <<
+  /Type /Font
+  /Subtype /Type1
+  /BaseFont /Helvetica
+>>
+endobj
+6 0 obj <<
+>>
+stream
+BT
+20 50 Td
+/F1 12 Tf
+(Hello, world!) Tj
+0 50 Td
+/F2 16 Tf
+(Goodbye, world!) Tj
+ET
+endstream
+endobj
+xref
+0 7
+0000000000 65535 f 
+0000000015 00000 n 
+0000000068 00000 n 
+0000000161 00000 n 
+0000000303 00000 n 
+0000000381 00000 n 
+0000000457 00000 n 
+trailer<< /Root 1 0 R /Size 7 >>
+startxref
+578
+%%EOF
+xref
+0 0
+trailer<< /Root 1 0 R /Size 0 /Prev 578 >>
+startxref
+780
+%%EOF