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