Add fuzz harness for libyang2
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3404319..ff20fb5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -52,6 +52,8 @@
option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
endif()
+
+option(ENABLE_FUZZ_TARGETS "Build target programs suitable for fuzzing with AFL" OFF)
#option(ENABLE_CALLGRIND_TESTS "Build performance tests to be run with callgrind" OFF)
#option(ENABLE_CACHE "Enable data caching for schemas and hash tables for data (time-efficient at the cost of increased space-complexity)" ON)
@@ -367,6 +369,10 @@
endif(CMOCKA_FOUND)
endif(ENABLE_BUILD_TESTS)
+if(ENABLE_BUILD_FUZZ_TARGETS)
+ add_subdirectory(tests/fuzz)
+endif(ENABLE_BUILD_FUZZ_TARGETS)
+
#if(GEN_LANGUAGE_BINDINGS AND GEN_CPP_BINDINGS)
# add_subdirectory(swig)
#endif()
diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt
new file mode 100644
index 0000000..8ed9c90
--- /dev/null
+++ b/tests/fuzz/CMakeLists.txt
@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 2.8.12)
+
+set(fuzz_targets yangfuzz)
+
+foreach(target_name IN LISTS fuzz_targets)
+ add_executable(${target_name} ${target_name}.c)
+ target_link_libraries(${target_name} yang)
+endforeach(target_name)
diff --git a/tests/fuzz/yangfuzz.c b/tests/fuzz/yangfuzz.c
new file mode 100644
index 0000000..52940b3
--- /dev/null
+++ b/tests/fuzz/yangfuzz.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "libyang.h"
+
+int main(int argc, char **argv) {
+
+ if (argc != 2) {
+ fprintf(stderr, "invalid usage\n");
+ exit(EXIT_FAILURE);
+ }
+
+
+ struct ly_ctx *ctx = NULL;
+ LY_ERR err;
+ while (__AFL_LOOP(100)) {
+ err = ly_ctx_new(NULL, 0, &ctx);
+ if (err != LY_SUCCESS) {
+ fprintf(stderr, "Failed to create context\n");
+ exit(EXIT_FAILURE);
+ }
+
+ lys_parse_path(ctx, argv[1], LYS_IN_YANG);
+ ly_ctx_destroy(ctx, NULL);
+ }
+}