From 94ebab3f13cb31378b83e726bd109e6cdc5c2582 Mon Sep 17 00:00:00 2001 From: Marcus Meissner Date: Mon, 24 Jul 2017 15:20:58 +0200 Subject: [PATCH] add a test-fuzzer that can be used to be called by AFL that tries to do various exif decoding on the passed file. --- test/Makefile.am | 4 +- test/test-fuzzer.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 test/test-fuzzer.c diff --git a/test/Makefile.am b/test/Makefile.am index d0a3551..a0e0566 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -10,12 +10,12 @@ SUBDIRS = nls # And this is just the lib - we don't have the program available # here yet. -TESTS = test-mem test-value test-integers test-parse test-tagtable test-sorted +TESTS = test-mem test-value test-integers test-parse test-tagtable test-sorted test-fuzzer TEST_IMAGES = $(top_srcdir)/daniel-andrews-sample.jpg export TEST_IMAGES check_PROGRAMS = test-mem test-mnote test-value test-integers test-parse \ - test-tagtable test-sorted + test-tagtable test-sorted test-fuzzer LDADD = $(top_builddir)/libexif/libexif.la $(LTLIBINTL) diff --git a/test/test-fuzzer.c b/test/test-fuzzer.c new file mode 100644 index 0000000..1ee7870 --- /dev/null +++ b/test/test-fuzzer.c @@ -0,0 +1,117 @@ +/**file test-fuzzer.c + * from test-parse.c and test-mnote.c + * + * \brief Completely parse all files given on the command line. + * + * Copyright (C) 2007 Hans Ulrich Niedermann + * Copyright 2002 Lutz M\uffffller + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + */ + +#include +#include +#include + +#include "libexif/exif-data.h" +#include "libexif/exif-system.h" + +/** Callback function handling an ExifEntry. */ +void content_foreach_func(ExifEntry *entry, void *callback_data); +void content_foreach_func(ExifEntry *entry, void *UNUSED(callback_data)) +{ + char buf[2000]; + exif_entry_get_value(entry, buf, sizeof(buf)); + printf(" Entry %p: %s (%s)\n" + " Size, Comps: %d, %d\n" + " Value: %s\n", + entry, + exif_tag_get_name(entry->tag), + exif_format_get_name(entry->format), + entry->size, + (int)(entry->components), + exif_entry_get_value(entry, buf, sizeof(buf))); +} + + +/** Callback function handling an ExifContent (corresponds 1:1 to an IFD). */ +void data_foreach_func(ExifContent *content, void *callback_data); +void data_foreach_func(ExifContent *content, void *callback_data) +{ + printf(" Content %p: ifd=%d\n", content, exif_content_get_ifd(content)); + exif_content_foreach_entry(content, content_foreach_func, callback_data); +} +static int +test_exif_data (ExifData *d) +{ + unsigned int i, c; + char v[1024], *p; + ExifMnoteData *md; + + fprintf (stdout, "Byte order: %s\n", + exif_byte_order_get_name (exif_data_get_byte_order (d))); + + md = exif_data_get_mnote_data (d); + if (!md) { + fprintf (stderr, "Could not parse maker note!\n"); + return 1; + } + + exif_mnote_data_ref (md); + exif_mnote_data_unref (md); + + c = exif_mnote_data_count (md); + for (i = 0; i < c; i++) { + fprintf (stdout, "Dumping entry number %i...\n", i); + fprintf (stdout, " Name: '%s'\n", + exif_mnote_data_get_name (md, i)); + fprintf (stdout, " Title: '%s'\n", + exif_mnote_data_get_title (md, i)); + fprintf (stdout, " Description: '%s'\n", + exif_mnote_data_get_description (md, i)); + p = exif_mnote_data_get_value (md, i, v, sizeof (v)); + if (p) { fprintf (stdout, " Value: '%s'\n", v); } + } + + return 0; +} + + + +/** Run EXIF parsing test on the given file. */ +static void test_parse(const char *filename, void *callback_data) +{ + ExifData *d; + + d = exif_data_new_from_file(filename); + exif_data_foreach_content(d, data_foreach_func, callback_data); + test_exif_data (d); + exif_data_unref(d); +} + + +/** Main program. */ +int main(const int argc, const char *argv[]) +{ + int i; + void *callback_data = NULL; + + for (i=1; i