Add more FPDFAttachmentEmbedderTests.

In particular, demonstrate FPDFDoc_AddAttachment() cannot add
attachments to PDFs that do not have any to start with.

Bug: pdfium:1502
Change-Id: I0b25b9417fbec72a62b9ff4a20c3902394beadf6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/67950
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_attachment_embeddertest.cpp b/fpdfsdk/fpdf_attachment_embeddertest.cpp
index aee00a9..675a468 100644
--- a/fpdfsdk/fpdf_attachment_embeddertest.cpp
+++ b/fpdfsdk/fpdf_attachment_embeddertest.cpp
@@ -22,6 +22,10 @@
   ASSERT_TRUE(OpenDocument("embedded_attachments.pdf"));
   EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document()));
 
+  // Try to retrieve attachments at bad indices.
+  EXPECT_FALSE(FPDFDoc_GetAttachment(document(), -1));
+  EXPECT_FALSE(FPDFDoc_GetAttachment(document(), 2));
+
   // Retrieve the first attachment.
   FPDF_ATTACHMENT attachment = FPDFDoc_GetAttachment(document(), 0);
   ASSERT_TRUE(attachment);
@@ -87,6 +91,16 @@
   EXPECT_EQ(kCheckSumW, GetPlatformWString(buf.data()));
 }
 
+TEST_F(FPDFAttachmentEmbedderTest, NoAttachmentToExtract) {
+  // Open a file with no attachments.
+  ASSERT_TRUE(OpenDocument("hello_world.pdf"));
+  EXPECT_EQ(0, FPDFDoc_GetAttachmentCount(document()));
+
+  // Try to retrieve attachments at bad indices.
+  EXPECT_FALSE(FPDFDoc_GetAttachment(document(), -1));
+  EXPECT_FALSE(FPDFDoc_GetAttachment(document(), 0));
+}
+
 TEST_F(FPDFAttachmentEmbedderTest, AddAttachments) {
   // Open a file with two attachments.
   ASSERT_TRUE(OpenDocument("embedded_attachments.pdf"));
@@ -226,6 +240,20 @@
             GetPlatformWString(buf.data()));
 }
 
+TEST_F(FPDFAttachmentEmbedderTest, AddAttachmentsToFileWithNoAttachments) {
+  // Open a file with no attachments.
+  ASSERT_TRUE(OpenDocument("hello_world.pdf"));
+  EXPECT_EQ(0, FPDFDoc_GetAttachmentCount(document()));
+
+  // Add an attachment to the beginning of the embedded file list.
+  ScopedFPDFWideString file_name = GetFPDFWideString(L"0.txt");
+  FPDF_ATTACHMENT attachment =
+      FPDFDoc_AddAttachment(document(), file_name.get());
+  // TODO(crbug.com/pdfium/1502): This should return true. Fix the bug and do
+  // additional verifications here.
+  EXPECT_FALSE(attachment);
+}
+
 TEST_F(FPDFAttachmentEmbedderTest, DeleteAttachment) {
   // Open a file with two attachments.
   ASSERT_TRUE(OpenDocument("embedded_attachments.pdf"));