LCOV - code coverage report
Current view: top level - glib/gio/tests - thumbnail-verification.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 15 15 100.0 %
Date: 2024-04-23 05:16:05 Functions: 2 2 100.0 %
Branches: 2 2 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  *
       3                 :            :  * Copyright (C) 2013 Collabora, Ltd.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       6                 :            :  *
       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.
      10                 :            :  *
      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.
      14                 :            :  *
      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.
      23                 :            :  *
      24                 :            :  * Author: Philip Withnall <philip.withnall@collabora.co.uk>
      25                 :            :  */
      26                 :            : 
      27                 :            : #define GIO_COMPILATION 1
      28                 :            : #include "../thumbnail-verify.c"
      29                 :            : 
      30                 :            : static void
      31                 :          1 : test_validity (void)
      32                 :            : {
      33                 :            :   struct
      34                 :            :     {
      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? */
      39                 :            :     }
      40                 :          1 :   tests[] =
      41                 :            :     {
      42                 :            :       /*
      43                 :            :        * Tests with well-formed PNG files.
      44                 :            :        *
      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.
      48                 :            :        */
      49                 :            : 
      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
      54                 :            :        * tEXt data. */
      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
      65                 :            :        * validation. */
      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 },
      71                 :            : 
      72                 :            :       /*
      73                 :            :        * Tests with PNG files which have malicious or badly-formed headers.
      74                 :            :        *
      75                 :            :        * As above, the files have all been truncated to reduce their size.
      76                 :            :        */
      77                 :            : 
      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 },
      90                 :            :     };
      91                 :            :   guint i;
      92                 :            : 
      93                 :            :   /* Run all the tests. */
      94         [ +  + ]:         16 :   for (i = 0; i < G_N_ELEMENTS (tests); i++)
      95                 :            :     {
      96                 :            :       GLocalFileStat stat_buf;
      97                 :            :       const gchar *thumbnail_path;
      98                 :            :       gchar *file_uri;
      99                 :            :       gboolean result;
     100                 :            : 
     101                 :         15 :       thumbnail_path = g_test_get_filename (G_TEST_DIST, "thumbnails",
     102                 :            :                                             tests[i].filename, NULL);
     103                 :         15 :       file_uri = g_strconcat ("file:///tmp/", tests[i].filename, NULL);
     104                 :            : #ifdef HAVE_STATX
     105                 :         15 :       stat_buf.stx_mtime.tv_sec = tests[i].mtime;
     106                 :         15 :       stat_buf.stx_size = tests[i].size;
     107                 :            : #else
     108                 :            : #ifdef G_OS_WIN32
     109                 :            :       stat_buf.st_mtim.tv_sec = tests[i].mtime;
     110                 :            : #else
     111                 :            :       stat_buf.st_mtime = tests[i].mtime;
     112                 :            : #endif
     113                 :            :       stat_buf.st_size = tests[i].size;
     114                 :            : #endif
     115                 :            : 
     116                 :         15 :       result = thumbnail_verify (thumbnail_path, file_uri, &stat_buf);
     117                 :            : 
     118                 :         15 :       g_free (file_uri);
     119                 :            : 
     120                 :         15 :       g_assert (result == tests[i].expected_validity);
     121                 :            :     }
     122                 :          1 : }
     123                 :            : 
     124                 :            : int
     125                 :          1 : main (int   argc,
     126                 :            :       char *argv[])
     127                 :            : {
     128                 :          1 :   g_test_init (&argc, &argv, NULL);
     129                 :            : 
     130                 :          1 :   g_test_add_func ("/png-thumbs/validity", test_validity);
     131                 :            : 
     132                 :          1 :   return g_test_run ();
     133                 :            : }

Generated by: LCOV version 1.14