[cairo-commit] test/create-for-stream.c

Chris Wilson ickle at kemper.freedesktop.org
Tue Sep 25 16:32:08 PDT 2007


 test/create-for-stream.c |   57 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 13 deletions(-)

New commits:
diff-tree 1e7f0bd4d5148940fe963eb20782bffe5ee735bd (from 1469de5211e84e40490fa612538986768748bd55)
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Wed Sep 26 00:28:47 2007 +0100

    [test/create-for-stream] Add test case for user error propagation.
    
    From bug https://bugs.freedesktop.org/show_bug.cgi?id=7049, we find
    that the error status from the user supplied write function to
    cairo_*_surface_create_for_stream is ignored and not propagated back
    to the surface/context - leading to silent data loss. Incorporate
    the suggested test case, a write function that simply returns
    CAIRO_STATUS_WRITE_ERROR, into create-for-stream.c.

diff --git a/test/create-for-stream.c b/test/create-for-stream.c
index 9068be8..4a58285 100644
--- a/test/create-for-stream.c
+++ b/test/create-for-stream.c
@@ -91,6 +91,14 @@ typedef struct _write_closure {
 } write_closure_t;
 
 static cairo_status_t
+bad_write (void		*closure,
+	   const unsigned char	*data,
+	   unsigned int	 length)
+{
+    return CAIRO_STATUS_WRITE_ERROR;
+}
+
+static cairo_status_t
 test_write (void		*closure,
 	    const unsigned char	*data,
 	    unsigned int	 length)
@@ -122,7 +130,8 @@ typedef cairo_surface_t *
 			 double			height_in_points);
 
 static cairo_test_status_t
-test_surface (const char		 *filename,
+test_surface (const char                 *backend,
+	      const char		 *filename,
 	      file_constructor_t	 file_constructor,
 	      stream_constructor_t	 stream_constructor)
 {
@@ -132,6 +141,28 @@ test_surface (const char		 *filename,
     cairo_status_t status;
     FILE *fp;
 
+    /* test propagation of user errors */
+    surface = stream_constructor (bad_write, &wc,
+				  WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
+
+    status = cairo_surface_status (surface);
+    if (status) {
+	cairo_test_log ("%s: Failed to create surface for stream.\n", backend);
+	return CAIRO_TEST_FAILURE;
+    }
+
+    draw_to (surface);
+
+    status = cairo_surface_status (surface);
+    cairo_surface_destroy (surface);
+
+    if (status != CAIRO_STATUS_WRITE_ERROR) {
+	cairo_test_log ("%s: Error: expected \"write error\", but received \"%s\".\n",
+			backend, cairo_status_to_string (status));
+	return CAIRO_TEST_FAILURE;
+    }
+
+    /* construct the real surface */
     wc.status = CAIRO_TEST_SUCCESS;
     wc.index = 0;
 
@@ -140,7 +171,7 @@ test_surface (const char		 *filename,
 
     status = cairo_surface_status (surface);
     if (status) {
-	cairo_test_log ("Failed to create surface for stream\n");
+	cairo_test_log ("%s: Failed to create surface for stream.\n", backend);
 	return CAIRO_TEST_FAILURE;
     }
 
@@ -158,8 +189,8 @@ test_surface (const char		 *filename,
 
     status = cairo_surface_status (surface);
     if (status) {
-	cairo_test_log ("Failed to create surface for file %s: %s\n",
-			filename, cairo_status_to_string (status));
+	cairo_test_log ("%s: Failed to create surface for file %s: %s.\n",
+			backend, filename, cairo_status_to_string (status));
 	return CAIRO_TEST_FAILURE;
     }
 
@@ -169,21 +200,21 @@ test_surface (const char		 *filename,
 
     fp = fopen (filename, "r");
     if (fp == NULL) {
-	cairo_test_log ("Failed to open %s for reading: %s\n",
-			filename, strerror (errno));
+	cairo_test_log ("%s: Failed to open %s for reading: %s.\n",
+			backend, filename, strerror (errno));
 	return CAIRO_TEST_FAILURE;
     }
 
     if (fread (file_contents, 1, wc.index, fp) != wc.index) {
-	cairo_test_log ("Failed to read %s: %s\n",
-			filename, strerror (errno));
+	cairo_test_log ("%s: Failed to read %s: %s.\n",
+			backend, filename, strerror (errno));
 	fclose (fp);
 	return CAIRO_TEST_FAILURE;
     }
 
     if (memcmp (file_contents, wc.buffer, wc.index) != 0) {
-	cairo_test_log ("Stream based output differ from file output for %s\n",
-			filename);
+	cairo_test_log ("%s: Stream based output differ from file output for %s.\n",
+			backend, filename);
 	fclose (fp);
 	return CAIRO_TEST_FAILURE;
     }
@@ -203,7 +234,7 @@ main (void)
     cairo_test_init ("create-for-stream");
 
 #if CAIRO_HAS_PS_SURFACE
-    test_status = test_surface ("create-for-stream.ps",
+    test_status = test_surface ("ps", "create-for-stream.ps",
 			        cairo_ps_surface_create,
 			        cairo_ps_surface_create_for_stream);
     if (status == CAIRO_TEST_SUCCESS)
@@ -211,7 +242,7 @@ main (void)
 #endif
 
 #if CAIRO_HAS_PDF_SURFACE
-    test_status = test_surface ("create-for-stream.pdf",
+    test_status = test_surface ("pdf", "create-for-stream.pdf",
 			        cairo_pdf_surface_create,
 			        cairo_pdf_surface_create_for_stream);
     if (status == CAIRO_TEST_SUCCESS)
@@ -219,7 +250,7 @@ main (void)
 #endif
 
 #if CAIRO_HAS_SVG_SURFACE
-    test_status = test_surface ("create-for-stream.svg",
+    test_status = test_surface ("svg", "create-for-stream.svg",
 			        cairo_svg_surface_create,
 			        cairo_svg_surface_create_for_stream);
     if (status == CAIRO_TEST_SUCCESS)


More information about the cairo-commit mailing list