1
/*
2
 * Copyright © 2005 Red Hat, Inc.
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: Carl D. Worth <cworth@cworth.org>
24
 */
25

            
26
#include "cairo-test.h"
27
#include <stddef.h>
28

            
29
/* Test to verify fixes for the following similar bugs:
30
 *
31
 *	https://bugs.freedesktop.org/show_bug.cgi?id=4088
32
 *	https://bugs.freedesktop.org/show_bug.cgi?id=3915
33
 *	https://bugs.freedesktop.org/show_bug.cgi?id=9906
34
 */
35

            
36
static cairo_test_status_t
37
3
draw (cairo_t *cr, int width, int height)
38
{
39
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
40
    cairo_surface_t *surface;
41
    cairo_pattern_t *pattern;
42
    cairo_t *cr2;
43

            
44
    /*
45
     * 1. Test file-not-found from surface->pattern->cairo_t
46
     */
47

            
48
    /* Make a custom context to not interfere with the one passed in. */
49
3
    cr2 = cairo_create (cairo_get_target (cr));
50

            
51
    /* First, let's make a nil surface. */
52
3
    surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
53

            
54
    /* Let the error propagate into a nil pattern. */
55
3
    pattern = cairo_pattern_create_for_surface (surface);
56

            
57
    /* Then let it propagate into the cairo_t. */
58
3
    cairo_set_source (cr2, pattern);
59
3
    cairo_paint (cr2);
60

            
61
3
    cairo_pattern_destroy (pattern);
62
3
    cairo_surface_destroy (surface);
63

            
64
    /* Check that the error made it all that way. */
65
3
    if (cairo_status (cr2) != CAIRO_STATUS_FILE_NOT_FOUND) {
66
	cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
67
			cairo_status_to_string (cairo_status (cr2)),
68
			cairo_status_to_string (CAIRO_STATUS_FILE_NOT_FOUND));
69
	cairo_destroy (cr2);
70
	return CAIRO_TEST_FAILURE;
71
    }
72

            
73
3
    cairo_destroy (cr2);
74

            
75
    /*
76
     * 2. Test NULL pointer pattern->cairo_t
77
     */
78
3
    cr2 = cairo_create (cairo_get_target (cr));
79

            
80
    /* First, trigger the NULL pointer status. */
81
3
    pattern = cairo_pattern_create_for_surface (NULL);
82

            
83
    /* Then let it propagate into the cairo_t. */
84
3
    cairo_set_source (cr2, pattern);
85
3
    cairo_paint (cr2);
86

            
87
3
    cairo_pattern_destroy (pattern);
88

            
89
    /* Check that the error made it all that way. */
90
3
    if (cairo_status (cr2) != CAIRO_STATUS_NULL_POINTER) {
91
	cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
92
			cairo_status_to_string (cairo_status (cr2)),
93
			cairo_status_to_string (CAIRO_STATUS_NULL_POINTER));
94
	cairo_destroy (cr2);
95
	return CAIRO_TEST_FAILURE;
96
    }
97

            
98
3
    cairo_destroy (cr2);
99

            
100
    /*
101
     * 3. Test that cairo_surface_finish can accept NULL or a nil
102
     *    surface without crashing.
103
     */
104

            
105
3
    cairo_surface_finish (NULL);
106

            
107
3
    surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
108
3
    cairo_surface_finish (surface);
109
3
    cairo_surface_destroy (surface);
110

            
111
    /*
112
     * 4. OK, we're straying from the original name, but it's still a
113
     * similar kind of testing of error paths. Here we're making sure
114
     * we can still call a cairo_get_* function after triggering an
115
     * INVALID_RESTORE error.
116
     */
117
3
    cr2 = cairo_create (cairo_get_target (cr));
118

            
119
    /* Trigger invalid restore. */
120
3
    cairo_restore (cr2);
121
3
    if (cairo_status (cr2) != CAIRO_STATUS_INVALID_RESTORE) {
122
	cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
123
			cairo_status_to_string (cairo_status (cr2)),
124
			cairo_status_to_string (CAIRO_STATUS_INVALID_RESTORE));
125
	cairo_destroy (cr2);
126
	return CAIRO_TEST_FAILURE;
127
    }
128

            
129
    /* Test that we can still call cairo_get_fill_rule without crashing. */
130
3
    cairo_get_fill_rule (cr2);
131

            
132
3
    cairo_destroy (cr2);
133

            
134
    /*
135
     * 5. Create a cairo_t for the NULL surface.
136
     */
137
3
    cr2 = cairo_create (NULL);
138

            
139
3
    if (cairo_status (cr2) != CAIRO_STATUS_NULL_POINTER) {
140
	cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
141
			cairo_status_to_string (cairo_status (cr2)),
142
			cairo_status_to_string (CAIRO_STATUS_NULL_POINTER));
143
	cairo_destroy (cr2);
144
	return CAIRO_TEST_FAILURE;
145
    }
146

            
147
    /* Test that get_target returns something valid */
148
3
    if (cairo_get_target (cr2) == NULL) {
149
	cairo_test_log (ctx, "Error: cairo_get_target() returned NULL\n");
150
	cairo_destroy (cr2);
151
	return CAIRO_TEST_FAILURE;
152
    }
153

            
154
    /* Test that push_group doesn't crash */
155
3
    cairo_push_group (cr2);
156
3
    cairo_stroke (cr2);
157
3
    pattern = cairo_pop_group (cr2);
158
3
    cairo_pattern_destroy (pattern);
159

            
160
3
    cairo_destroy (cr2);
161

            
162
3
    return CAIRO_TEST_SUCCESS;
163
}
164

            
165
1
CAIRO_TEST (nil_surface,
166
	    "Test that nil surfaces do not make cairo crash.",
167
	    "api", /* keywords */
168
	    NULL, /* requirements */
169
	    1, 1,
170
	    NULL, draw)