[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / tests / thumbnail-verification.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2013 Collabora, Ltd.
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  *
22  * Author: Philip Withnall <philip.withnall@collabora.co.uk>
23  */
24
25 #define GIO_COMPILATION 1
26 #include "../thumbnail-verify.c"
27
28 static void
29 test_validity (void)
30 {
31   struct
32     {
33       const gchar *filename;  /* name of a file in the tests/thumbnails dir */
34       guint64 mtime;  /* asserted mtime of @filename */
35       guint64 size;  /* asserted size of @filename */
36       gboolean expected_validity;  /* should thumbnail_verify() succeed? */
37     }
38   tests[] =
39     {
40       /*
41        * Tests with well-formed PNG files.
42        *
43        * Note that these files have all been brutally truncated to a reasonable
44        * size, so aren't actually valid PNG files. Their headers are valid,
45        * however, and that's all we care about.
46        */
47
48       /* Test that validation succeeds against a valid PNG file with URI,
49        * mtime and size which match the expected values. */
50       { "valid.png", 1382429848, 93654, TRUE },
51       /* Test that validation succeeds with URI and mtime, but no size in the
52        * tEXt data. */
53       { "valid-no-size.png", 1382429848, 93633, TRUE },
54       /* Test that a missing file fails validation. */
55       { "missing.png", 123456789, 12345, FALSE },
56       /* Test that an existing file with no tEXt data fails validation. */
57       { "no-text-data.png", 123 /* invalid */, 26378, FALSE },
58       /* Test that a URI mismatch fails validation. */
59       { "uri-mismatch.png" /* invalid */, 1382429848, 93654, FALSE },
60       /* Test that an mtime mismatch fails validation. */
61       { "valid.png", 123 /* invalid */, 93654, FALSE },
62       /* Test that a valid URI and mtime, but a mismatched size, fails
63        * validation. */
64       { "valid.png", 1382429848, 123 /* invalid */, FALSE },
65       /* Test that validation succeeds with an mtime of 0. */
66       { "mtime-zero.png", 0, 93621, TRUE },
67       /* Test that validation fails if the mtime is only a prefix match. */
68       { "valid.png", 9848 /* invalid */, 93654, FALSE },
69
70       /*
71        * Tests with PNG files which have malicious or badly-formed headers.
72        *
73        * As above, the files have all been truncated to reduce their size.
74        */
75
76       /* Check a corrupted PNG header fails validation. */
77       { "bad-header.png", 1382429848, 93654, FALSE },
78       /* Check a PNG header by itself fails. */
79       { "header-only.png", 1382429848, 8, FALSE },
80       /* Check a PNG header and initial chunk size fails. */
81       { "header-and-chunk-size.png", 1382429848, 20, FALSE },
82       /* Check a huge chunk size fails. */
83       { "huge-chunk-size.png", 1382429848, 93654, FALSE },
84       /* Check that an empty key fails. */
85       { "empty-key.png", 1382429848, 93654, FALSE },
86       /* Check that an over-long value fails (even if nul-terminated). */
87       { "overlong-value.png", 1382429848, 93660, FALSE },
88     };
89   guint i;
90
91   /* Run all the tests. */
92   for (i = 0; i < G_N_ELEMENTS (tests); i++)
93     {
94       GStatBuf stat_buf;
95       const gchar *thumbnail_path;
96       gchar *file_uri;
97       gboolean result;
98
99       thumbnail_path = g_test_get_filename (G_TEST_DIST, "thumbnails",
100                                             tests[i].filename, NULL);
101       file_uri = g_strconcat ("file:///tmp/", tests[i].filename, NULL);
102       stat_buf.st_mtime = tests[i].mtime;
103       stat_buf.st_size = tests[i].size;
104
105       result = thumbnail_verify (thumbnail_path, file_uri, &stat_buf);
106
107       g_free (file_uri);
108
109       g_assert (result == tests[i].expected_validity);
110     }
111 }
112
113 int
114 main (int   argc,
115       char *argv[])
116 {
117   g_test_init (&argc, &argv, NULL);
118
119   g_test_add_func ("/png-thumbs/validity", test_validity);
120
121   return g_test_run ();
122 }