test: 'scaling-crash-test' added
[profile/ivi/pixman.git] / test / scaling-crash-test.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "pixman.h"
6
7 /*
8  * We have a source image filled with solid color, set NORMAL or PAD repeat,
9  * and some transform which results in nearest neighbour scaling.
10  *
11  * The expected result is the destination image filled with this solid
12  * color.
13  */
14 static int
15 do_test (int32_t                dst_size,
16          int32_t                src_size,
17          int32_t                src_offs,
18          int32_t                scale_factor,
19          pixman_repeat_t        repeat)
20 {
21     int i;
22     pixman_image_t *   src_img;
23     pixman_image_t *   dst_img;
24     pixman_transform_t transform;
25     uint32_t *         srcbuf;
26     uint32_t *         dstbuf;
27
28     srcbuf = (uint32_t *)malloc (src_size * 4);
29     dstbuf = (uint32_t *)malloc (dst_size * 4);
30
31     /* horizontal test */
32     memset (srcbuf, 0xCC, src_size * 4);
33     memset (dstbuf, 0x33, dst_size * 4);
34
35     src_img = pixman_image_create_bits (
36         PIXMAN_a8r8g8b8, src_size, 1, srcbuf, src_size * 4);
37     dst_img = pixman_image_create_bits (
38         PIXMAN_a8r8g8b8, dst_size, 1, dstbuf, dst_size * 4);
39
40     pixman_transform_init_scale (&transform, scale_factor, 65536);
41     pixman_image_set_transform (src_img, &transform);
42     pixman_image_set_repeat (src_img, repeat);
43     pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
44
45     pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img,
46                             src_offs, 0, 0, 0, 0, 0, dst_size, 1);
47
48     pixman_image_unref (src_img);
49     pixman_image_unref (dst_img);
50
51     for (i = 0; i < dst_size; i++)
52     {
53         if (dstbuf[i] != 0xCCCCCCCC)
54         {
55             free (srcbuf);
56             free (dstbuf);
57             return 1;
58         }
59     }
60
61     /* vertical test */
62     memset (srcbuf, 0xCC, src_size * 4);
63     memset (dstbuf, 0x33, dst_size * 4);
64
65     src_img = pixman_image_create_bits (
66         PIXMAN_a8r8g8b8, 1, src_size, srcbuf, 4);
67     dst_img = pixman_image_create_bits (
68         PIXMAN_a8r8g8b8, 1, dst_size, dstbuf, 4);
69
70     pixman_transform_init_scale (&transform, 65536, scale_factor);
71     pixman_image_set_transform (src_img, &transform);
72     pixman_image_set_repeat (src_img, repeat);
73     pixman_image_set_filter (src_img, PIXMAN_FILTER_NEAREST, NULL, 0);
74
75     pixman_image_composite (PIXMAN_OP_SRC, src_img, NULL, dst_img,
76                             0, src_offs, 0, 0, 0, 0, 1, dst_size);
77
78     pixman_image_unref (src_img);
79     pixman_image_unref (dst_img);
80
81     for (i = 0; i < dst_size; i++)
82     {
83         if (dstbuf[i] != 0xCCCCCCCC)
84         {
85             free (srcbuf);
86             free (dstbuf);
87             return 1;
88         }
89     }
90
91     free (srcbuf);
92     free (dstbuf);
93     return 0;
94 }
95
96 int
97 main (int argc, char *argv[])
98 {
99     pixman_disable_out_of_bounds_workaround ();
100
101     /* can potentially crash */
102     assert (do_test (
103         48000, 32767, 1, 65536 * 128, PIXMAN_REPEAT_NORMAL) == 0);
104
105     /* can potentially get into a deadloop */
106     assert (do_test (
107         16384, 65536, 32, 32768, PIXMAN_REPEAT_NORMAL) == 0);
108
109 #if 0
110     /* can potentially access memory outside source image buffer */
111     assert (do_test (
112         10, 10, 0, 1, PIXMAN_REPEAT_PAD) == 0);
113     assert (do_test (
114         10, 10, 0, 0, PIXMAN_REPEAT_PAD) == 0);
115 #endif
116
117 #if 0
118     /* can potentially provide invalid results (out of range matrix stuff) */
119     assert (do_test (
120         48000, 32767, 16384, 65536 * 128, PIXMAN_REPEAT_NORMAL) == 0);
121 #endif
122
123     return 0;
124 }