[cairo-commit] 2 commits - test/pdf-tagged-text.c

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Jan 19 11:12:29 UTC 2021


 test/pdf-tagged-text.c |   80 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 79 insertions(+), 1 deletion(-)

New commits:
commit a88696ed1563351883a520941e7ab4d331c12d11
Merge: 01f38097a 4e02f705e
Author: Heiko Lewin <hlewin at gmx.de>
Date:   Tue Jan 19 11:12:27 2021 +0000

    Merge branch 'extend-test-pdf-tagged-text' into 'master'
    
    Add tests for PDF metadata
    
    See merge request cairo/cairo!100

commit 4e02f705e498885ad7cdf1e18313e68b06b9228e
Author: Sven Neumann <sven at svenfoo.org>
Date:   Mon Jan 4 12:45:38 2021 +0100

    Add tests for PDF metadata
    
    Extend the "pdf-tagged-text" test so that it does some basic checks
    on the PDF file it creates. This covers the date fields as well as
    some other metadata. More checks can and should be added.

diff --git a/test/pdf-tagged-text.c b/test/pdf-tagged-text.c
index 5c1b82bc2..8ee48c4d7 100644
--- a/test/pdf-tagged-text.c
+++ b/test/pdf-tagged-text.c
@@ -29,6 +29,17 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_MMAP
+#include <sys/mman.h>
+#endif
 
 #include <cairo.h>
 #include <cairo-pdf.h>
@@ -364,12 +375,77 @@ create_document (cairo_surface_t *surface, cairo_t *cr)
     cairo_tag_end (cr, "Document");
 }
 
+static cairo_test_status_t
+check_contains_string(cairo_test_context_t *ctx, const void *hay, size_t size, const char *needle)
+{
+    if (memmem(hay, size, needle, strlen(needle)))
+        return CAIRO_TEST_SUCCESS;
+
+    cairo_test_log (ctx, "Failed to find expected string in generated PDF: %s\n", needle);
+    return CAIRO_TEST_FAILURE;
+}
+
+static cairo_test_status_t
+check_created_pdf(cairo_test_context_t *ctx, const char* filename)
+{
+    cairo_test_status_t result = CAIRO_TEST_SUCCESS;
+    int fd;
+    struct stat st;
+    void *contents;
+
+    fd = open(filename, O_RDONLY, 0);
+    if (fd < 0) {
+        cairo_test_log (ctx, "Failed to open generated PDF file %s: %s\n", filename, strerror(errno));
+        return CAIRO_TEST_FAILURE;
+    }
+
+    if (fstat(fd, &st) == -1)
+    {
+        cairo_test_log (ctx, "Failed to stat generated PDF file %s: %s\n", filename, strerror(errno));
+        close(fd);
+        return CAIRO_TEST_FAILURE;
+    }
+
+    if (st.st_size == 0)
+    {
+        cairo_test_log (ctx, "Generated PDF file %s is empty\n", filename);
+        close(fd);
+        return CAIRO_TEST_FAILURE;
+    }
+
+#ifdef HAVE_MMAP
+    contents = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+    if (contents == NULL)
+    {
+        cairo_test_log (ctx, "Failed to mmap generated PDF file %s: %s\n", filename, strerror(errno));
+        close(fd);
+        return CAIRO_TEST_FAILURE;
+    }
+
+    /* check metadata */
+    result |= check_contains_string(ctx, contents, st.st_size, "/Title (PDF Features Test)");
+    result |= check_contains_string(ctx, contents, st.st_size, "/Author (cairo test suite)");
+    result |= check_contains_string(ctx, contents, st.st_size, "/Creator (pdf-features)");
+    result |= check_contains_string(ctx, contents, st.st_size, "/CreationDate (20160101123456+10'30')");
+    result |= check_contains_string(ctx, contents, st.st_size, "/ModDate (20160621054321Z)");
+
+    // TODO: add more checks
+
+    munmap(contents, st.st_size);
+#endif
+
+    close(fd);
+
+    return result;
+}
+
 static cairo_test_status_t
 preamble (cairo_test_context_t *ctx)
 {
     cairo_surface_t *surface;
     cairo_t *cr;
     cairo_status_t status, status2;
+    cairo_test_status_t result;
     char *filename;
     const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
 
@@ -396,9 +472,11 @@ preamble (cairo_test_context_t *ctx)
 	return CAIRO_TEST_FAILURE;
     }
 
+    result = check_created_pdf(ctx, filename);
+
     free (filename);
 
-    return CAIRO_TEST_SUCCESS;
+    return result;
 }
 
 CAIRO_TEST (pdf_tagged_text,


More information about the cairo-commit mailing list