Add a generic property test
[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 #include "../thumbnail-verify.c"
26
27 static void
28 test_validity (void)
29 {
30   struct
31     {
32       const gchar *filename;  /* name of a file in the tests/thumbnails dir */
33       guint64 mtime;  /* asserted mtime of @filename */
34       guint64 size;  /* asserted size of @filename */
35       gboolean expected_validity;  /* should thumbnail_verify() succeed? */
36     }
37   tests[] =
38     {
39       /*
40        * Tests with well-formed PNG files.
41        *
42        * Note that these files have all been brutally truncated to a reasonable
43        * size, so aren't actually valid PNG files. Their headers are valid,
44        * however, and that's all we care about.
45        */
46
47       /* Test that validation succeeds against a valid PNG file with URI,
48        * mtime and size which match the expected values. */
49       { "valid.png", 1382429848, 93654, TRUE },
50       /* Test that validation succeeds with URI and mtime, but no size in the
51        * tEXt data. */
52       { "valid-no-size.png", 1382429848, 93633, TRUE },
53       /* Test that a missing file fails validation. */
54       { "missing.png", 123456789, 12345, FALSE },
55       /* Test that an existing file with no tEXt data fails validation. */
56       { "no-text-data.png", 123 /* invalid */, 26378, FALSE },
57       /* Test that a URI mismatch fails validation. */
58       { "uri-mismatch.png" /* invalid */, 1382429848, 93654, FALSE },
59       /* Test that an mtime mismatch fails validation. */
60       { "valid.png", 123 /* invalid */, 93654, FALSE },
61       /* Test that a valid URI and mtime, but a mismatched size, fails
62        * validation. */
63       { "valid.png", 1382429848, 123 /* invalid */, FALSE },
64       /* Test that validation succeeds with an mtime of 0. */
65       { "mtime-zero.png", 0, 93621, TRUE },
66       /* Test that validation fails if the mtime is only a prefix match. */
67       { "valid.png", 9848 /* invalid */, 93654, FALSE },
68
69       /*
70        * Tests with PNG files which have malicious or badly-formed headers.
71        *
72        * As above, the files have all been truncated to reduce their size.
73        */
74
75       /* Check a corrupted PNG header fails validation. */
76       { "bad-header.png", 1382429848, 93654, FALSE },
77       /* Check a PNG header by itself fails. */
78       { "header-only.png", 1382429848, 8, FALSE },
79       /* Check a PNG header and initial chunk size fails. */
80       { "header-and-chunk-size.png", 1382429848, 20, FALSE },
81       /* Check a huge chunk size fails. */
82       { "huge-chunk-size.png", 1382429848, 93654, FALSE },
83       /* Check that an empty key fails. */
84       { "empty-key.png", 1382429848, 93654, FALSE },
85       /* Check that an over-long value fails (even if nul-terminated). */
86       { "overlong-value.png", 1382429848, 93660, FALSE },
87     };
88   guint i;
89
90   /* Run all the tests. */
91   for (i = 0; i < G_N_ELEMENTS (tests); i++)
92     {
93       GStatBuf stat_buf;
94       const gchar *thumbnail_path;
95       gchar *file_uri;
96       gboolean result;
97
98       thumbnail_path = g_test_get_filename (G_TEST_DIST, "thumbnails",
99                                             tests[i].filename, NULL);
100       file_uri = g_strconcat ("file:///tmp/", tests[i].filename, NULL);
101       stat_buf.st_mtime = tests[i].mtime;
102       stat_buf.st_size = tests[i].size;
103
104       result = thumbnail_verify (thumbnail_path, file_uri, &stat_buf);
105
106       g_free (file_uri);
107
108       g_assert (result == tests[i].expected_validity);
109     }
110 }
111
112 int
113 main (int   argc,
114       char *argv[])
115 {
116   g_test_init (&argc, &argv, NULL);
117
118   g_test_add_func ("/png-thumbs/validity", test_validity);
119
120   return g_test_run ();
121 }