nil-surface.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include "cairo-test.h"
  26. #include <stddef.h>
  27. /* Test to verify fixes for the following similar bugs:
  28. *
  29. * https://bugs.freedesktop.org/show_bug.cgi?id=4088
  30. * https://bugs.freedesktop.org/show_bug.cgi?id=3915
  31. * https://bugs.freedesktop.org/show_bug.cgi?id=9906
  32. */
  33. static cairo_test_status_t
  34. draw (cairo_t *cr, int width, int height)
  35. {
  36. const cairo_test_context_t *ctx = cairo_test_get_context (cr);
  37. cairo_surface_t *surface;
  38. cairo_pattern_t *pattern;
  39. cairo_t *cr2;
  40. /*
  41. * 1. Test file-not-found from surface->pattern->cairo_t
  42. */
  43. /* Make a custom context to not interfere with the one passed in. */
  44. cr2 = cairo_create (cairo_get_target (cr));
  45. /* First, let's make a nil surface. */
  46. surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
  47. /* Let the error propagate into a nil pattern. */
  48. pattern = cairo_pattern_create_for_surface (surface);
  49. /* Then let it propagate into the cairo_t. */
  50. cairo_set_source (cr2, pattern);
  51. cairo_paint (cr2);
  52. cairo_pattern_destroy (pattern);
  53. cairo_surface_destroy (surface);
  54. /* Check that the error made it all that way. */
  55. if (cairo_status (cr2) != CAIRO_STATUS_FILE_NOT_FOUND) {
  56. cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
  57. cairo_status_to_string (cairo_status (cr2)),
  58. cairo_status_to_string (CAIRO_STATUS_FILE_NOT_FOUND));
  59. cairo_destroy (cr2);
  60. return CAIRO_TEST_FAILURE;
  61. }
  62. cairo_destroy (cr2);
  63. /*
  64. * 2. Test NULL pointer pattern->cairo_t
  65. */
  66. cr2 = cairo_create (cairo_get_target (cr));
  67. /* First, trigger the NULL pointer status. */
  68. pattern = cairo_pattern_create_for_surface (NULL);
  69. /* Then let it propagate into the cairo_t. */
  70. cairo_set_source (cr2, pattern);
  71. cairo_paint (cr2);
  72. cairo_pattern_destroy (pattern);
  73. /* Check that the error made it all that way. */
  74. if (cairo_status (cr2) != CAIRO_STATUS_NULL_POINTER) {
  75. cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
  76. cairo_status_to_string (cairo_status (cr2)),
  77. cairo_status_to_string (CAIRO_STATUS_NULL_POINTER));
  78. cairo_destroy (cr2);
  79. return CAIRO_TEST_FAILURE;
  80. }
  81. cairo_destroy (cr2);
  82. /*
  83. * 3. Test that cairo_surface_finish can accept NULL or a nil
  84. * surface without crashing.
  85. */
  86. cairo_surface_finish (NULL);
  87. surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
  88. cairo_surface_finish (surface);
  89. cairo_surface_destroy (surface);
  90. /*
  91. * 4. OK, we're straying from the original name, but it's still a
  92. * similar kind of testing of error paths. Here we're making sure
  93. * we can still call a cairo_get_* function after triggering an
  94. * INVALID_RESTORE error.
  95. */
  96. cr2 = cairo_create (cairo_get_target (cr));
  97. /* Trigger invalid restore. */
  98. cairo_restore (cr2);
  99. if (cairo_status (cr2) != CAIRO_STATUS_INVALID_RESTORE) {
  100. cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
  101. cairo_status_to_string (cairo_status (cr2)),
  102. cairo_status_to_string (CAIRO_STATUS_INVALID_RESTORE));
  103. cairo_destroy (cr2);
  104. return CAIRO_TEST_FAILURE;
  105. }
  106. /* Test that we can still call cairo_get_fill_rule without crashing. */
  107. cairo_get_fill_rule (cr2);
  108. cairo_destroy (cr2);
  109. /*
  110. * 5. Create a cairo_t for the NULL surface.
  111. */
  112. cr2 = cairo_create (NULL);
  113. if (cairo_status (cr2) != CAIRO_STATUS_NULL_POINTER) {
  114. cairo_test_log (ctx, "Error: Received status of \"%s\" rather than expected \"%s\"\n",
  115. cairo_status_to_string (cairo_status (cr2)),
  116. cairo_status_to_string (CAIRO_STATUS_NULL_POINTER));
  117. cairo_destroy (cr2);
  118. return CAIRO_TEST_FAILURE;
  119. }
  120. /* Test that get_target returns something valid */
  121. if (cairo_get_target (cr2) == NULL) {
  122. cairo_test_log (ctx, "Error: cairo_get_target() returned NULL\n");
  123. cairo_destroy (cr2);
  124. return CAIRO_TEST_FAILURE;
  125. }
  126. /* Test that push_group doesn't crash */
  127. cairo_push_group (cr2);
  128. cairo_stroke (cr2);
  129. pattern = cairo_pop_group (cr2);
  130. cairo_pattern_destroy (pattern);
  131. cairo_destroy (cr2);
  132. return CAIRO_TEST_SUCCESS;
  133. }
  134. CAIRO_TEST (nil_surface,
  135. "Test that nil surfaces do not make cairo crash.",
  136. "api", /* keywords */
  137. NULL, /* requirements */
  138. 1, 1,
  139. NULL, draw)