Blame gio/tests/thumbnail-verification.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2013 Collabora, Ltd.
Packit ae235b
 *
Packit ae235b
 * This work is provided "as is"; redistribution and modification
Packit ae235b
 * in whole or in part, in any medium, physical or electronic is
Packit ae235b
 * permitted without restriction.
Packit ae235b
 *
Packit ae235b
 * This work is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit ae235b
 *
Packit ae235b
 * In no event shall the authors or contributors be liable for any
Packit ae235b
 * direct, indirect, incidental, special, exemplary, or consequential
Packit ae235b
 * damages (including, but not limited to, procurement of substitute
Packit ae235b
 * goods or services; loss of use, data, or profits; or business
Packit ae235b
 * interruption) however caused and on any theory of liability, whether
Packit ae235b
 * in contract, strict liability, or tort (including negligence or
Packit ae235b
 * otherwise) arising in any way out of the use of this software, even
Packit ae235b
 * if advised of the possibility of such damage.
Packit ae235b
 *
Packit ae235b
 * Author: Philip Withnall <philip.withnall@collabora.co.uk>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define GIO_COMPILATION 1
Packit ae235b
#include "../thumbnail-verify.c"
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_validity (void)
Packit ae235b
{
Packit ae235b
  struct
Packit ae235b
    {
Packit ae235b
      const gchar *filename;  /* name of a file in the tests/thumbnails dir */
Packit ae235b
      guint64 mtime;  /* asserted mtime of @filename */
Packit ae235b
      guint64 size;  /* asserted size of @filename */
Packit ae235b
      gboolean expected_validity;  /* should thumbnail_verify() succeed? */
Packit ae235b
    }
Packit ae235b
  tests[] =
Packit ae235b
    {
Packit ae235b
      /*
Packit ae235b
       * Tests with well-formed PNG files.
Packit ae235b
       *
Packit ae235b
       * Note that these files have all been brutally truncated to a reasonable
Packit ae235b
       * size, so aren't actually valid PNG files. Their headers are valid,
Packit ae235b
       * however, and that's all we care about.
Packit ae235b
       */
Packit ae235b
Packit ae235b
      /* Test that validation succeeds against a valid PNG file with URI,
Packit ae235b
       * mtime and size which match the expected values. */
Packit ae235b
      { "valid.png", 1382429848, 93654, TRUE },
Packit ae235b
      /* Test that validation succeeds with URI and mtime, but no size in the
Packit ae235b
       * tEXt data. */
Packit ae235b
      { "valid-no-size.png", 1382429848, 93633, TRUE },
Packit ae235b
      /* Test that a missing file fails validation. */
Packit ae235b
      { "missing.png", 123456789, 12345, FALSE },
Packit ae235b
      /* Test that an existing file with no tEXt data fails validation. */
Packit ae235b
      { "no-text-data.png", 123 /* invalid */, 26378, FALSE },
Packit ae235b
      /* Test that a URI mismatch fails validation. */
Packit ae235b
      { "uri-mismatch.png" /* invalid */, 1382429848, 93654, FALSE },
Packit ae235b
      /* Test that an mtime mismatch fails validation. */
Packit ae235b
      { "valid.png", 123 /* invalid */, 93654, FALSE },
Packit ae235b
      /* Test that a valid URI and mtime, but a mismatched size, fails
Packit ae235b
       * validation. */
Packit ae235b
      { "valid.png", 1382429848, 123 /* invalid */, FALSE },
Packit ae235b
      /* Test that validation succeeds with an mtime of 0. */
Packit ae235b
      { "mtime-zero.png", 0, 93621, TRUE },
Packit ae235b
      /* Test that validation fails if the mtime is only a prefix match. */
Packit ae235b
      { "valid.png", 9848 /* invalid */, 93654, FALSE },
Packit ae235b
Packit ae235b
      /*
Packit ae235b
       * Tests with PNG files which have malicious or badly-formed headers.
Packit ae235b
       *
Packit ae235b
       * As above, the files have all been truncated to reduce their size.
Packit ae235b
       */
Packit ae235b
Packit ae235b
      /* Check a corrupted PNG header fails validation. */
Packit ae235b
      { "bad-header.png", 1382429848, 93654, FALSE },
Packit ae235b
      /* Check a PNG header by itself fails. */
Packit ae235b
      { "header-only.png", 1382429848, 8, FALSE },
Packit ae235b
      /* Check a PNG header and initial chunk size fails. */
Packit ae235b
      { "header-and-chunk-size.png", 1382429848, 20, FALSE },
Packit ae235b
      /* Check a huge chunk size fails. */
Packit ae235b
      { "huge-chunk-size.png", 1382429848, 93654, FALSE },
Packit ae235b
      /* Check that an empty key fails. */
Packit ae235b
      { "empty-key.png", 1382429848, 93654, FALSE },
Packit ae235b
      /* Check that an over-long value fails (even if nul-terminated). */
Packit ae235b
      { "overlong-value.png", 1382429848, 93660, FALSE },
Packit ae235b
    };
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  /* Run all the tests. */
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (tests); i++)
Packit ae235b
    {
Packit ae235b
      GLocalFileStat stat_buf;
Packit ae235b
      const gchar *thumbnail_path;
Packit ae235b
      gchar *file_uri;
Packit ae235b
      gboolean result;
Packit ae235b
Packit ae235b
      thumbnail_path = g_test_get_filename (G_TEST_DIST, "thumbnails",
Packit ae235b
                                            tests[i].filename, NULL);
Packit ae235b
      file_uri = g_strconcat ("file:///tmp/", tests[i].filename, NULL);
Packit ae235b
      stat_buf.st_mtime = tests[i].mtime;
Packit ae235b
      stat_buf.st_size = tests[i].size;
Packit ae235b
Packit ae235b
      result = thumbnail_verify (thumbnail_path, file_uri, &stat_buf);
Packit ae235b
Packit ae235b
      g_free (file_uri);
Packit ae235b
Packit ae235b
      g_assert (result == tests[i].expected_validity);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/png-thumbs/validity", test_validity);
Packit ae235b
Packit ae235b
  return g_test_run ();
Packit ae235b
}