Cleanup root directory from misc files
[platform/upstream/gstreamer.git] / subprojects / gst-ci / fuzzing / localfuzzer.c
1 /* GStreamer
2  * Copyright (C) 2017 Edward Hervey <bilboed@bilboed.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /* Local fuzzer runner */
21 #include <glib.h>
22
23 extern int LLVMFuzzerTestOneInput (const guint8 * data, size_t size);
24
25 static void
26 test_file (gchar * filename)
27 {
28   GDir *dir;
29   gchar *path;
30   gchar *contents;
31   gsize length;
32
33   /* if filename is a directory, process the contents */
34   if ((dir = g_dir_open (filename, 0, NULL))) {
35     const gchar *entry;
36
37     while ((entry = g_dir_read_name (dir))) {
38       gchar *spath;
39
40       spath = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
41       test_file (spath);
42       g_free (spath);
43     }
44
45     g_dir_close (dir);
46     return;
47   }
48
49   /* Make sure path is absolute */
50   if (!g_path_is_absolute (filename)) {
51     gchar *curdir;
52
53     curdir = g_get_current_dir ();
54     path = g_build_filename (curdir, filename, NULL);
55     g_free (curdir);
56   } else
57     path = g_strdup (filename);
58
59   /* Check if path exists */
60   if (g_file_get_contents (path, &contents, &length, NULL)) {
61     g_print (">>> %s (%" G_GSIZE_FORMAT " bytes)\n", path, length);
62     LLVMFuzzerTestOneInput ((const guint8 *) contents, length);
63     g_free (contents);
64   }
65
66   g_free (path);
67 }
68
69 int
70 main (int argc, gchar ** argv)
71 {
72   gint i;
73
74   for (i = 1; i < argc; i++)
75     test_file (argv[i]);
76
77   return 0;
78 }