1
/*
2
 * Copyright © 2017 Uli Schlachter
3
 *
4
 * Permission to use, copy, modify, distribute, and sell this software
5
 * and its documentation for any purpose is hereby granted without
6
 * fee, provided that the above copyright notice appear in all copies
7
 * and that both that copyright notice and this permission notice
8
 * appear in supporting documentation, and that the name of
9
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Red Hat, Inc. makes no representations about the
12
 * suitability of this software for any purpose.  It is provided "as
13
 * is" without express or implied warranty.
14
 *
15
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
 *
23
 * Author: Uli Schlachter <psychon@znc.in>
24
 */
25

            
26
#include "cairo-test.h"
27

            
28
#include <stdlib.h>
29

            
30
static const unsigned char broken_png_data[] = {
31
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
32
  0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02,
33
  0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0xff, 0x00, 0x00, 0x00,
34
};
35
static const size_t broken_png_data_length
36
    = sizeof(broken_png_data) / sizeof(broken_png_data[0]);
37

            
38
static cairo_status_t
39
4
read_png_from_data (void *closure, unsigned char *data, unsigned int length)
40
{
41
4
    size_t *offset = closure;
42
4
    size_t remaining = broken_png_data_length - *offset;
43

            
44
4
    if (remaining < length)
45
	return CAIRO_STATUS_READ_ERROR;
46

            
47
4
    memcpy (data, &broken_png_data[*offset], length);
48
4
    *offset += length;
49

            
50
4
    return CAIRO_STATUS_SUCCESS;
51
}
52

            
53
static cairo_test_status_t
54
1
preamble (cairo_test_context_t *ctx)
55
{
56
    cairo_surface_t *surface;
57
    cairo_status_t status;
58
    cairo_status_t expected;
59
1
    size_t offset = 0;
60

            
61
1
    surface = cairo_image_surface_create_from_png_stream (read_png_from_data,
62
							  &offset);
63

            
64
1
    expected = CAIRO_STATUS_PNG_ERROR;
65
1
    status = cairo_surface_status (surface);
66
1
    cairo_surface_destroy (surface);
67
1
    if (status != expected) {
68
	cairo_test_log (ctx,
69
			"Error: expected error %s, but got %s\n",
70
			cairo_status_to_string (expected),
71
			cairo_status_to_string (status));
72

            
73
	return CAIRO_TEST_FAILURE;
74
    }
75

            
76
1
    return CAIRO_TEST_SUCCESS;
77
}
78

            
79
1
CAIRO_TEST (create_from_broken_png_stream,
80
	    "Tests the creation of a PNG from malformed data",
81
	    "png", /* keywords */
82
	    NULL, /* requirements */
83
	    0, 0,
84
	    preamble, NULL)