diff options
Diffstat (limited to 'src/lib-test')
-rw-r--r-- | src/lib-test/Makefile.am | 20 | ||||
-rw-r--r-- | src/lib-test/Makefile.in | 829 | ||||
-rw-r--r-- | src/lib-test/fuzzer.c | 87 | ||||
-rw-r--r-- | src/lib-test/fuzzer.h | 37 | ||||
-rw-r--r-- | src/lib-test/test-common.c | 465 | ||||
-rw-r--r-- | src/lib-test/test-common.h | 165 | ||||
-rw-r--r-- | src/lib-test/test-istream.c | 166 | ||||
-rw-r--r-- | src/lib-test/test-ostream.c | 194 | ||||
-rw-r--r-- | src/lib-test/test-subprocess.c | 392 | ||||
-rw-r--r-- | src/lib-test/test-subprocess.h | 41 |
10 files changed, 2396 insertions, 0 deletions
diff --git a/src/lib-test/Makefile.am b/src/lib-test/Makefile.am new file mode 100644 index 0000000..ce7417d --- /dev/null +++ b/src/lib-test/Makefile.am @@ -0,0 +1,20 @@ +noinst_LTLIBRARIES = libtest.la + +AM_CPPFLAGS = \ + -I$(top_srcdir)/src/lib \ + -I$(top_srcdir)/src/lib-charset + +libtest_la_SOURCES = \ + fuzzer.c \ + test-common.c \ + test-istream.c \ + test-ostream.c \ + test-subprocess.c + +headers = \ + fuzzer.h \ + test-common.h \ + test-subprocess.h + +pkginc_libdir=$(pkgincludedir) +pkginc_lib_HEADERS = $(headers) diff --git a/src/lib-test/Makefile.in b/src/lib-test/Makefile.in new file mode 100644 index 0000000..db7e3a2 --- /dev/null +++ b/src/lib-test/Makefile.in @@ -0,0 +1,829 @@ +# Makefile.in generated by automake 1.16.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2018 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + + +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +subdir = src/lib-test +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/ac_checktype2.m4 \ + $(top_srcdir)/m4/ac_typeof.m4 $(top_srcdir)/m4/arc4random.m4 \ + $(top_srcdir)/m4/blockdev.m4 $(top_srcdir)/m4/c99_vsnprintf.m4 \ + $(top_srcdir)/m4/clock_gettime.m4 $(top_srcdir)/m4/crypt.m4 \ + $(top_srcdir)/m4/crypt_xpg6.m4 $(top_srcdir)/m4/dbqlk.m4 \ + $(top_srcdir)/m4/dirent_dtype.m4 $(top_srcdir)/m4/dovecot.m4 \ + $(top_srcdir)/m4/fd_passing.m4 $(top_srcdir)/m4/fdatasync.m4 \ + $(top_srcdir)/m4/flexible_array_member.m4 \ + $(top_srcdir)/m4/glibc.m4 $(top_srcdir)/m4/gmtime_max.m4 \ + $(top_srcdir)/m4/gmtime_tm_gmtoff.m4 \ + $(top_srcdir)/m4/ioloop.m4 $(top_srcdir)/m4/iovec.m4 \ + $(top_srcdir)/m4/ipv6.m4 $(top_srcdir)/m4/libcap.m4 \ + $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/libwrap.m4 \ + $(top_srcdir)/m4/linux_mremap.m4 $(top_srcdir)/m4/ltoptions.m4 \ + $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ + $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/mmap_write.m4 \ + $(top_srcdir)/m4/mntctl.m4 $(top_srcdir)/m4/modules.m4 \ + $(top_srcdir)/m4/notify.m4 $(top_srcdir)/m4/nsl.m4 \ + $(top_srcdir)/m4/off_t_max.m4 $(top_srcdir)/m4/pkg.m4 \ + $(top_srcdir)/m4/pr_set_dumpable.m4 \ + $(top_srcdir)/m4/q_quotactl.m4 $(top_srcdir)/m4/quota.m4 \ + $(top_srcdir)/m4/random.m4 $(top_srcdir)/m4/rlimit.m4 \ + $(top_srcdir)/m4/sendfile.m4 $(top_srcdir)/m4/size_t_signed.m4 \ + $(top_srcdir)/m4/sockpeercred.m4 $(top_srcdir)/m4/sql.m4 \ + $(top_srcdir)/m4/ssl.m4 $(top_srcdir)/m4/st_tim.m4 \ + $(top_srcdir)/m4/static_array.m4 $(top_srcdir)/m4/test_with.m4 \ + $(top_srcdir)/m4/time_t.m4 $(top_srcdir)/m4/typeof.m4 \ + $(top_srcdir)/m4/typeof_dev_t.m4 \ + $(top_srcdir)/m4/uoff_t_max.m4 $(top_srcdir)/m4/vararg.m4 \ + $(top_srcdir)/m4/want_apparmor.m4 \ + $(top_srcdir)/m4/want_bsdauth.m4 \ + $(top_srcdir)/m4/want_bzlib.m4 \ + $(top_srcdir)/m4/want_cassandra.m4 \ + $(top_srcdir)/m4/want_cdb.m4 \ + $(top_srcdir)/m4/want_checkpassword.m4 \ + $(top_srcdir)/m4/want_clucene.m4 $(top_srcdir)/m4/want_db.m4 \ + $(top_srcdir)/m4/want_gssapi.m4 $(top_srcdir)/m4/want_icu.m4 \ + $(top_srcdir)/m4/want_ldap.m4 $(top_srcdir)/m4/want_lua.m4 \ + $(top_srcdir)/m4/want_lz4.m4 $(top_srcdir)/m4/want_lzma.m4 \ + $(top_srcdir)/m4/want_mysql.m4 $(top_srcdir)/m4/want_pam.m4 \ + $(top_srcdir)/m4/want_passwd.m4 $(top_srcdir)/m4/want_pgsql.m4 \ + $(top_srcdir)/m4/want_prefetch.m4 \ + $(top_srcdir)/m4/want_shadow.m4 \ + $(top_srcdir)/m4/want_sodium.m4 $(top_srcdir)/m4/want_solr.m4 \ + $(top_srcdir)/m4/want_sqlite.m4 \ + $(top_srcdir)/m4/want_stemmer.m4 \ + $(top_srcdir)/m4/want_systemd.m4 \ + $(top_srcdir)/m4/want_textcat.m4 \ + $(top_srcdir)/m4/want_unwind.m4 $(top_srcdir)/m4/want_zlib.m4 \ + $(top_srcdir)/m4/want_zstd.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(pkginc_lib_HEADERS) \ + $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +LTLIBRARIES = $(noinst_LTLIBRARIES) +libtest_la_LIBADD = +am_libtest_la_OBJECTS = fuzzer.lo test-common.lo test-istream.lo \ + test-ostream.lo test-subprocess.lo +libtest_la_OBJECTS = $(am_libtest_la_OBJECTS) +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__maybe_remake_depfiles = depfiles +am__depfiles_remade = ./$(DEPDIR)/fuzzer.Plo \ + ./$(DEPDIR)/test-common.Plo ./$(DEPDIR)/test-istream.Plo \ + ./$(DEPDIR)/test-ostream.Plo ./$(DEPDIR)/test-subprocess.Plo +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(libtest_la_SOURCES) +DIST_SOURCES = $(libtest_la_SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = f=`echo $$p | sed -e 's|^.*/||'`; +am__install_max = 40 +am__nobase_strip_setup = \ + srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*|]/\\\\&/g'` +am__nobase_strip = \ + for p in $$list; do echo "$$p"; done | sed -e "s|$$srcdirstrip/||" +am__nobase_list = $(am__nobase_strip_setup); \ + for p in $$list; do echo "$$p $$p"; done | \ + sed "s| $$srcdirstrip/| |;"' / .*\//!s/ .*/ ./; s,\( .*\)/[^/]*$$,\1,' | \ + $(AWK) 'BEGIN { files["."] = "" } { files[$$2] = files[$$2] " " $$1; \ + if (++n[$$2] == $(am__install_max)) \ + { print $$2, files[$$2]; n[$$2] = 0; files[$$2] = "" } } \ + END { for (dir in files) print dir, files[dir] }' +am__base_list = \ + sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \ + sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g' +am__uninstall_files_from_dir = { \ + test -z "$$files" \ + || { test ! -d "$$dir" && test ! -f "$$dir" && test ! -r "$$dir"; } \ + || { echo " ( cd '$$dir' && rm -f" $$files ")"; \ + $(am__cd) "$$dir" && rm -f $$files; }; \ + } +am__installdirs = "$(DESTDIR)$(pkginc_libdir)" +HEADERS = $(pkginc_lib_HEADERS) +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +ETAGS = etags +CTAGS = ctags +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +ACLOCAL_AMFLAGS = @ACLOCAL_AMFLAGS@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +APPARMOR_LIBS = @APPARMOR_LIBS@ +AR = @AR@ +AUTH_CFLAGS = @AUTH_CFLAGS@ +AUTH_LIBS = @AUTH_LIBS@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +BINARY_CFLAGS = @BINARY_CFLAGS@ +BINARY_LDFLAGS = @BINARY_LDFLAGS@ +BISON = @BISON@ +CASSANDRA_CFLAGS = @CASSANDRA_CFLAGS@ +CASSANDRA_LIBS = @CASSANDRA_LIBS@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CDB_LIBS = @CDB_LIBS@ +CFLAGS = @CFLAGS@ +CLUCENE_CFLAGS = @CLUCENE_CFLAGS@ +CLUCENE_LIBS = @CLUCENE_LIBS@ +COMPRESS_LIBS = @COMPRESS_LIBS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CRYPT_LIBS = @CRYPT_LIBS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DICT_LIBS = @DICT_LIBS@ +DLLIB = @DLLIB@ +DLLTOOL = @DLLTOOL@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +FLEX = @FLEX@ +FUZZER_CPPFLAGS = @FUZZER_CPPFLAGS@ +FUZZER_LDFLAGS = @FUZZER_LDFLAGS@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +KRB5CONFIG = @KRB5CONFIG@ +KRB5_CFLAGS = @KRB5_CFLAGS@ +KRB5_LIBS = @KRB5_LIBS@ +LD = @LD@ +LDAP_LIBS = @LDAP_LIBS@ +LDFLAGS = @LDFLAGS@ +LD_NO_WHOLE_ARCHIVE = @LD_NO_WHOLE_ARCHIVE@ +LD_WHOLE_ARCHIVE = @LD_WHOLE_ARCHIVE@ +LIBCAP = @LIBCAP@ +LIBDOVECOT = @LIBDOVECOT@ +LIBDOVECOT_COMPRESS = @LIBDOVECOT_COMPRESS@ +LIBDOVECOT_DEPS = @LIBDOVECOT_DEPS@ +LIBDOVECOT_DSYNC = @LIBDOVECOT_DSYNC@ +LIBDOVECOT_LA_LIBS = @LIBDOVECOT_LA_LIBS@ +LIBDOVECOT_LDA = @LIBDOVECOT_LDA@ +LIBDOVECOT_LDAP = @LIBDOVECOT_LDAP@ +LIBDOVECOT_LIBFTS = @LIBDOVECOT_LIBFTS@ +LIBDOVECOT_LIBFTS_DEPS = @LIBDOVECOT_LIBFTS_DEPS@ +LIBDOVECOT_LOGIN = @LIBDOVECOT_LOGIN@ +LIBDOVECOT_LUA = @LIBDOVECOT_LUA@ +LIBDOVECOT_LUA_DEPS = @LIBDOVECOT_LUA_DEPS@ +LIBDOVECOT_SQL = @LIBDOVECOT_SQL@ +LIBDOVECOT_STORAGE = @LIBDOVECOT_STORAGE@ +LIBDOVECOT_STORAGE_DEPS = @LIBDOVECOT_STORAGE_DEPS@ +LIBEXTTEXTCAT_CFLAGS = @LIBEXTTEXTCAT_CFLAGS@ +LIBEXTTEXTCAT_LIBS = @LIBEXTTEXTCAT_LIBS@ +LIBICONV = @LIBICONV@ +LIBICU_CFLAGS = @LIBICU_CFLAGS@ +LIBICU_LIBS = @LIBICU_LIBS@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBSODIUM_CFLAGS = @LIBSODIUM_CFLAGS@ +LIBSODIUM_LIBS = @LIBSODIUM_LIBS@ +LIBTIRPC_CFLAGS = @LIBTIRPC_CFLAGS@ +LIBTIRPC_LIBS = @LIBTIRPC_LIBS@ +LIBTOOL = @LIBTOOL@ +LIBUNWIND_CFLAGS = @LIBUNWIND_CFLAGS@ +LIBUNWIND_LIBS = @LIBUNWIND_LIBS@ +LIBWRAP_LIBS = @LIBWRAP_LIBS@ +LINKED_STORAGE_LDADD = @LINKED_STORAGE_LDADD@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBICONV = @LTLIBICONV@ +LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ +LUA_CFLAGS = @LUA_CFLAGS@ +LUA_LIBS = @LUA_LIBS@ +MAINT = @MAINT@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +MODULE_LIBS = @MODULE_LIBS@ +MODULE_SUFFIX = @MODULE_SUFFIX@ +MYSQL_CFLAGS = @MYSQL_CFLAGS@ +MYSQL_CONFIG = @MYSQL_CONFIG@ +MYSQL_LIBS = @MYSQL_LIBS@ +NM = @NM@ +NMEDIT = @NMEDIT@ +NOPLUGIN_LDFLAGS = @NOPLUGIN_LDFLAGS@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PANDOC = @PANDOC@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +PGSQL_CFLAGS = @PGSQL_CFLAGS@ +PGSQL_LIBS = @PGSQL_LIBS@ +PG_CONFIG = @PG_CONFIG@ +PIE_CFLAGS = @PIE_CFLAGS@ +PIE_LDFLAGS = @PIE_LDFLAGS@ +PKG_CONFIG = @PKG_CONFIG@ +PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ +PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ +QUOTA_LIBS = @QUOTA_LIBS@ +RANLIB = @RANLIB@ +RELRO_LDFLAGS = @RELRO_LDFLAGS@ +RPCGEN = @RPCGEN@ +RUN_TEST = @RUN_TEST@ +SED = @SED@ +SETTING_FILES = @SETTING_FILES@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +SQLITE_CFLAGS = @SQLITE_CFLAGS@ +SQLITE_LIBS = @SQLITE_LIBS@ +SQL_CFLAGS = @SQL_CFLAGS@ +SQL_LIBS = @SQL_LIBS@ +SSL_CFLAGS = @SSL_CFLAGS@ +SSL_LIBS = @SSL_LIBS@ +STRIP = @STRIP@ +SYSTEMD_CFLAGS = @SYSTEMD_CFLAGS@ +SYSTEMD_LIBS = @SYSTEMD_LIBS@ +VALGRIND = @VALGRIND@ +VERSION = @VERSION@ +ZSTD_CFLAGS = @ZSTD_CFLAGS@ +ZSTD_LIBS = @ZSTD_LIBS@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_CXX = @ac_ct_CXX@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +dict_drivers = @dict_drivers@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +moduledir = @moduledir@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +rundir = @rundir@ +runstatedir = @runstatedir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +sql_drivers = @sql_drivers@ +srcdir = @srcdir@ +ssldir = @ssldir@ +statedir = @statedir@ +sysconfdir = @sysconfdir@ +systemdservicetype = @systemdservicetype@ +systemdsystemunitdir = @systemdsystemunitdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +noinst_LTLIBRARIES = libtest.la +AM_CPPFLAGS = \ + -I$(top_srcdir)/src/lib \ + -I$(top_srcdir)/src/lib-charset + +libtest_la_SOURCES = \ + fuzzer.c \ + test-common.c \ + test-istream.c \ + test-ostream.c \ + test-subprocess.c + +headers = \ + fuzzer.h \ + test-common.h \ + test-subprocess.h + +pkginc_libdir = $(pkgincludedir) +pkginc_lib_HEADERS = $(headers) +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/lib-test/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/lib-test/Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-noinstLTLIBRARIES: + -test -z "$(noinst_LTLIBRARIES)" || rm -f $(noinst_LTLIBRARIES) + @list='$(noinst_LTLIBRARIES)'; \ + locs=`for p in $$list; do echo $$p; done | \ + sed 's|^[^/]*$$|.|; s|/[^/]*$$||; s|$$|/so_locations|' | \ + sort -u`; \ + test -z "$$locs" || { \ + echo rm -f $${locs}; \ + rm -f $${locs}; \ + } + +libtest.la: $(libtest_la_OBJECTS) $(libtest_la_DEPENDENCIES) $(EXTRA_libtest_la_DEPENDENCIES) + $(AM_V_CCLD)$(LINK) $(libtest_la_OBJECTS) $(libtest_la_LIBADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fuzzer.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test-common.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test-istream.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test-ostream.Plo@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test-subprocess.Plo@am__quote@ # am--include-marker + +$(am__depfiles_remade): + @$(MKDIR_P) $(@D) + @echo '# dummy' >$@-t && $(am__mv) $@-t $@ + +am--depfiles: $(am__depfiles_remade) + +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs +install-pkginc_libHEADERS: $(pkginc_lib_HEADERS) + @$(NORMAL_INSTALL) + @list='$(pkginc_lib_HEADERS)'; test -n "$(pkginc_libdir)" || list=; \ + if test -n "$$list"; then \ + echo " $(MKDIR_P) '$(DESTDIR)$(pkginc_libdir)'"; \ + $(MKDIR_P) "$(DESTDIR)$(pkginc_libdir)" || exit 1; \ + fi; \ + for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; \ + done | $(am__base_list) | \ + while read files; do \ + echo " $(INSTALL_HEADER) $$files '$(DESTDIR)$(pkginc_libdir)'"; \ + $(INSTALL_HEADER) $$files "$(DESTDIR)$(pkginc_libdir)" || exit $$?; \ + done + +uninstall-pkginc_libHEADERS: + @$(NORMAL_UNINSTALL) + @list='$(pkginc_lib_HEADERS)'; test -n "$(pkginc_libdir)" || list=; \ + files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \ + dir='$(DESTDIR)$(pkginc_libdir)'; $(am__uninstall_files_from_dir) + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) distdir-am + +distdir-am: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(LTLIBRARIES) $(HEADERS) +installdirs: + for dir in "$(DESTDIR)$(pkginc_libdir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-libtool clean-noinstLTLIBRARIES \ + mostlyclean-am + +distclean: distclean-am + -rm -f ./$(DEPDIR)/fuzzer.Plo + -rm -f ./$(DEPDIR)/test-common.Plo + -rm -f ./$(DEPDIR)/test-istream.Plo + -rm -f ./$(DEPDIR)/test-ostream.Plo + -rm -f ./$(DEPDIR)/test-subprocess.Plo + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: install-pkginc_libHEADERS + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f ./$(DEPDIR)/fuzzer.Plo + -rm -f ./$(DEPDIR)/test-common.Plo + -rm -f ./$(DEPDIR)/test-istream.Plo + -rm -f ./$(DEPDIR)/test-ostream.Plo + -rm -f ./$(DEPDIR)/test-subprocess.Plo + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-pkginc_libHEADERS + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \ + clean-generic clean-libtool clean-noinstLTLIBRARIES \ + cscopelist-am ctags ctags-am distclean distclean-compile \ + distclean-generic distclean-libtool distclean-tags distdir dvi \ + dvi-am html html-am info info-am install install-am \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-pkginc_libHEADERS install-ps \ + install-ps-am install-strip installcheck installcheck-am \ + installdirs maintainer-clean maintainer-clean-generic \ + mostlyclean mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool pdf pdf-am ps ps-am tags tags-am uninstall \ + uninstall-am uninstall-pkginc_libHEADERS + +.PRECIOUS: Makefile + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/src/lib-test/fuzzer.c b/src/lib-test/fuzzer.c new file mode 100644 index 0000000..c15fee7 --- /dev/null +++ b/src/lib-test/fuzzer.c @@ -0,0 +1,87 @@ +/* Copyright (c) 2020 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "lib-signals.h" +#include "net.h" +#include "istream.h" +#include "ostream.h" +#include "iostream-pump.h" +#include "fuzzer.h" + +#include <sys/socket.h> +#include <unistd.h> + +void fuzzer_init(struct fuzzer_context *fuzz_ctx) +{ + i_zero(fuzz_ctx); + if (!lib_is_initialized()) { + lib_init(); + lib_signals_init(); + lib_signals_ignore(SIGPIPE, TRUE); + } + fuzz_ctx->fd = -1; +} + +void fuzzer_deinit(struct fuzzer_context *fuzz_ctx) +{ + iostream_pump_destroy(&fuzz_ctx->pump); + /* ensure fd gets closed, we don't care + if this fails. */ + if (fuzz_ctx->fd > -1) + (void)close(fuzz_ctx->fd); + if (fuzz_ctx->fd_pump > -1) + (void)close(fuzz_ctx->fd_pump); + if (fuzz_ctx->ioloop != NULL) + io_loop_destroy(&fuzz_ctx->ioloop); +} + +static void pump_finished(enum iostream_pump_status status ATTR_UNUSED, + struct fuzzer_context *fuzz_ctx) +{ + struct istream *input = iostream_pump_get_input(fuzz_ctx->pump); + struct ostream *output = iostream_pump_get_output(fuzz_ctx->pump); + + switch (status) { + case IOSTREAM_PUMP_STATUS_INPUT_EOF: + break; + case IOSTREAM_PUMP_STATUS_INPUT_ERROR: + i_error("read(%s) failed: %s", i_stream_get_name(input), + i_stream_get_error(input)); + break; + case IOSTREAM_PUMP_STATUS_OUTPUT_ERROR: + i_error("write(%s) failed: %s", o_stream_get_name(output), + o_stream_get_error(output)); + break; + }; + + if (shutdown(o_stream_get_fd(output), SHUT_WR) < 0) + i_fatal("shutdown() failed: %m"); + iostream_pump_destroy(&fuzz_ctx->pump); +} + +int fuzzer_io_as_fd(struct fuzzer_context *fuzz_ctx, + const uint8_t *data, size_t size) +{ + int sfd[2]; + + if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfd) < 0) + i_fatal("socketpair() failed: %m"); + net_set_nonblock(sfd[0], TRUE); + net_set_nonblock(sfd[1], TRUE); + + struct istream *input = i_stream_create_from_data(data, size); + struct ostream *output = o_stream_create_fd(sfd[0], IO_BLOCK_SIZE); + i_stream_set_name(input, "(fuzzer data)"); + o_stream_set_name(output, "(fuzzer input to program)"); + o_stream_set_no_error_handling(output, TRUE); + + fuzz_ctx->pump = iostream_pump_create(input, output); + fuzz_ctx->fd_pump = sfd[0]; + fuzz_ctx->fd = sfd[1]; + iostream_pump_set_completion_callback(fuzz_ctx->pump, pump_finished, + fuzz_ctx); + i_stream_unref(&input); + o_stream_unref(&output); + iostream_pump_start(fuzz_ctx->pump); + return sfd[1]; +} diff --git a/src/lib-test/fuzzer.h b/src/lib-test/fuzzer.h new file mode 100644 index 0000000..e20ba13 --- /dev/null +++ b/src/lib-test/fuzzer.h @@ -0,0 +1,37 @@ +#ifndef FUZZER_H +#define FUZZER_H + +struct iostream_pump; +struct ioloop; + +struct fuzzer_context { + int fd, fd_pump; + struct iostream_pump *pump; + struct ioloop *ioloop; +}; + +#define FUZZ_BEGIN_DATA(data_arg, size_arg) \ + int LLVMFuzzerTestOneInput(data_arg, size_arg); \ + int LLVMFuzzerTestOneInput(data_arg, size_arg) { \ + struct fuzzer_context fuzz_ctx; \ + fuzzer_init(&fuzz_ctx); T_BEGIN { + +#define FUZZ_BEGIN_STR(str_arg) \ + FUZZ_BEGIN_DATA(const uint8_t *_param_data, size_t _param_size) \ + str_arg = t_strndup(_param_data, _param_size); + +#define FUZZ_BEGIN_FD \ + FUZZ_BEGIN_DATA(const uint8_t *_param_data, size_t _param_size) \ + fuzz_ctx.ioloop = io_loop_create(); \ + (void)fuzzer_io_as_fd(&fuzz_ctx, _param_data, _param_size); + +#define FUZZ_END \ + } T_END; fuzzer_deinit(&fuzz_ctx); return 0; } + +void fuzzer_init(struct fuzzer_context *fuzz_ctx); +void fuzzer_deinit(struct fuzzer_context *fuzz_ctx); + +int fuzzer_io_as_fd(struct fuzzer_context *fuzz_ctx, + const uint8_t *data, size_t size); + +#endif diff --git a/src/lib-test/test-common.c b/src/lib-test/test-common.c new file mode 100644 index 0000000..feea7ec --- /dev/null +++ b/src/lib-test/test-common.c @@ -0,0 +1,465 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "test-common.h" + +#include <stdio.h> +#include <setjmp.h> /* for fatal tests */ + +static bool test_deinit_lib; + +/* To test the firing of i_assert, we need non-local jumps, i.e. setjmp */ +static volatile bool expecting_fatal = FALSE; +static jmp_buf fatal_jmpbuf; + +#define OUT_NAME_ALIGN 70 + +static char *test_prefix; +static bool test_success; +static unsigned int failure_count; +static unsigned int total_count; +static unsigned int expected_errors; +static char *expected_error_str, *expected_fatal_str; +static test_fatal_callback_t *test_fatal_callback; +static void *test_fatal_context; + +void test_begin(const char *name) +{ + test_success = TRUE; + expected_errors = 0; + if (!expecting_fatal) + i_assert(test_prefix == NULL); + else + test_assert((test_success = (test_prefix == NULL))); + test_prefix = i_strdup(name); +} + +bool test_has_failed(void) +{ + return !test_success; +} + +void test_assert_failed(const char *code, const char *file, unsigned int line) +{ + printf("%s:%u: Assert failed: %s\n", file, line, code); + fflush(stdout); + test_success = FALSE; +#ifdef STATIC_CHECKER + i_unreached(); +#endif +} + +void test_assert_failed_idx(const char *code, const char *file, unsigned int line, long long i) +{ + printf("%s:%u: Assert(#%lld) failed: %s\n", file, line, i, code); + fflush(stdout); + test_success = FALSE; +#ifdef STATIC_CHECKER + i_unreached(); +#endif +} + +void test_assert_failed_strcmp_idx(const char *code, const char *file, unsigned int line, + const char * src, const char * dst, long long i) +{ + printf("%s:%u: Assert", file, line); + if (i == LLONG_MIN) + printf(" failed: %s\n", code); + else + printf("(#%lld) failed: %s\n", i, code); + if (src != NULL) + printf(" \"%s\" != ", src); + else + printf(" NULL != "); + if (dst != NULL) + printf("\"%s\"\n", dst); + else + printf("NULL\n"); + fflush(stdout); + test_success = FALSE; +#ifdef STATIC_CHECKER + i_unreached(); +#endif +} + +void test_assert_failed_cmp_intmax_idx(const char *code, const char *file, + unsigned int line, + intmax_t src, intmax_t dst, + const char *op, long long i) +{ + printf("%s:%u: Assert", file, line); + if (i == LLONG_MIN) + printf(" failed: %s\n", code); + else + printf("(#%lld) failed: %s\n", i, code); + printf(" %jd %s %jd is not true\n", src, op, dst); + fflush(stdout); + test_success = FALSE; +#ifdef STATIC_CHECKER + i_unreached(); +#endif +} + +void test_assert_failed_ucmp_intmax_idx(const char *code, const char *file, + unsigned int line, + uintmax_t src, uintmax_t dst, + const char *op, long long i) +{ + printf("%s:%u: Assert", file, line); + if (i == LLONG_MIN) + printf(" failed: %s\n", code); + else + printf("(#%lld) failed: %s\n", i, code); + printf(" %ju %s %ju is not true\n", src, op, dst); + fflush(stdout); + test_success = FALSE; +#ifdef STATIC_CHECKER + i_unreached(); +#endif +} + +#ifdef DEBUG +#include "randgen.h" +static void +test_dump_rand_state(void) +{ + static int64_t seen_seed = -1; + unsigned int seed; + if (rand_get_last_seed(&seed) < 0) { + if (seen_seed == -1) { + printf("test: random sequence not reproduceable, use DOVECOT_SRAND=kiss\n"); + seen_seed = -2; + } + return; + } + if (seed == seen_seed) + return; + seen_seed = seed; + printf("test: DOVECOT_SRAND random seed was %u\n", seed); +} +#else +static inline void test_dump_rand_state(void) { } +#endif + +void test_end(void) +{ + if (!expecting_fatal) + i_assert(test_prefix != NULL); + else + test_assert(test_prefix != NULL); + + test_out("", test_success); + if (!test_success) + test_dump_rand_state(); + i_free_and_null(test_prefix); + test_success = FALSE; +} + +void test_out(const char *name, bool success) +{ + test_out_reason(name, success, NULL); +} + +void test_out_quiet(const char *name, bool success) +{ + if (success) { + total_count++; + return; + } + test_out(name, success); +} + +void test_out_reason(const char *name, bool success, const char *reason) +{ + int i = 0; + + if (test_prefix != NULL) { + fputs(test_prefix, stdout); + i += strlen(test_prefix); + if (*name != '\0') { + putchar(':'); + i++; + } + putchar(' '); + i++; + } + if (*name != '\0') { + fputs(name, stdout); + putchar(' '); + i += strlen(name) + 1; + } + for (; i < OUT_NAME_ALIGN; i++) + putchar('.'); + fputs(" : ", stdout); + if (success) + fputs("ok", stdout); + else { + fputs("FAILED", stdout); + test_success = FALSE; + failure_count++; + } + if (reason != NULL && *reason != '\0') + printf(": %s", reason); + putchar('\n'); + fflush(stdout); + total_count++; +} + +void +test_expect_error_string_n_times(const char *substr, unsigned int times) +{ + i_assert(expected_errors == 0); + expected_errors = times; + expected_error_str = i_strdup(substr); +} +void +test_expect_error_string(const char *substr) +{ + test_expect_error_string_n_times(substr, 1); +} +void +test_expect_errors(unsigned int expected) +{ + i_assert(expected_errors == 0); + expected_errors = expected; +} +void +test_expect_no_more_errors(void) +{ + test_assert(expected_errors == 0 && expected_error_str == NULL); + i_free_and_null(expected_error_str); + expected_errors = 0; +} + +static bool ATTR_FORMAT(2, 0) +expect_error_check(char **error_strp, const char *format, va_list args) +{ + if (*error_strp == NULL) + return TRUE; + + bool suppress; + T_BEGIN { + /* test_assert() will reset test_success if need be. */ + const char *str = t_strdup_vprintf(format, args); + suppress = strstr(str, *error_strp) != NULL; + test_assert(suppress == TRUE); + i_free_and_null(*error_strp); + } T_END; + return suppress; +} + +static void ATTR_FORMAT(2, 0) +test_error_handler(const struct failure_context *ctx, + const char *format, va_list args) +{ + bool suppress = FALSE; + + if (expected_errors > 0) { + va_list args2; + VA_COPY(args2, args); + suppress = expect_error_check(&expected_error_str, format, args2); + expected_errors--; + va_end(args2); + } else { + test_success = FALSE; + } + + if (!suppress) { + test_dump_rand_state(); + default_error_handler(ctx, format, args); + } +} + +void test_expect_fatal_string(const char *substr) +{ + i_free(expected_fatal_str); + expected_fatal_str = i_strdup(substr); +} + +#undef test_fatal_set_callback +void test_fatal_set_callback(test_fatal_callback_t *callback, void *context) +{ + i_assert(test_fatal_callback == NULL); + test_fatal_callback = callback; + test_fatal_context = context; +} + +static void ATTR_FORMAT(2, 0) ATTR_NORETURN +test_fatal_handler(const struct failure_context *ctx, + const char *format, va_list args) +{ + /* Prevent recursion, we can't handle our own errors */ + i_set_fatal_handler(default_fatal_handler); + i_assert(expecting_fatal); /* if not at the right time, bail */ + + va_list args2; + VA_COPY(args2, args); + bool suppress = expect_error_check(&expected_fatal_str, format, args2); + va_end(args); + + if (suppress) { + if (test_fatal_callback != NULL) { + test_fatal_callback(test_fatal_context); + test_fatal_callback = NULL; + test_fatal_context = NULL; + } + + i_set_fatal_handler(test_fatal_handler); + longjmp(fatal_jmpbuf, 1); + } else { + default_fatal_handler(ctx, format, args); + } + i_unreached(); /* we simply can't get here */ +} + +static void test_init(void) +{ + test_prefix = NULL; + failure_count = 0; + total_count = 0; + + if (!lib_is_initialized()) { + lib_init(); + test_deinit_lib = TRUE; + } else + test_deinit_lib = FALSE; + + i_set_error_handler(test_error_handler); + /* Don't set fatal handler until actually needed for fatal testing */ +} + +static int test_deinit(void) +{ + i_assert(test_prefix == NULL); + printf("%u / %u tests failed\n", failure_count, total_count); + if (test_deinit_lib) + lib_deinit(); + return failure_count == 0 ? 0 : 1; +} + +static void test_run_funcs(void (*const test_functions[])(void)) +{ + unsigned int i; + + for (i = 0; test_functions[i] != NULL; i++) { + T_BEGIN { + test_functions[i](); + } T_END; + } +} +static void test_run_named_funcs(const struct named_test tests[], + const char *match) +{ + unsigned int i; + + for (i = 0; tests[i].func != NULL; i++) { + if (strstr(tests[i].name, match) != NULL) T_BEGIN { + tests[i].func(); + } T_END; + } +} + +static void run_one_fatal(test_fatal_func_t *fatal_function) +{ + static unsigned int index = 0; + for (;;) { + volatile int jumped = setjmp(fatal_jmpbuf); + if (jumped == 0) { + /* normal flow */ + expecting_fatal = TRUE; + enum fatal_test_state ret = fatal_function(index); + expecting_fatal = FALSE; + if (ret == FATAL_TEST_FINISHED) { + /* ran out of tests - good */ + index = 0; + break; + } else if (ret == FATAL_TEST_FAILURE) { + /* failed to fire assert - bad, but can continue */ + test_success = FALSE; + i_error("Desired assert failed to fire at step %i", index); + index++; + } else { /* FATAL_TEST_ABORT or other value */ + test_success = FALSE; + test_end(); + index = 0; + break; + } + } else { + /* assert fired, continue with next test */ + index++; + } + } +} +static void test_run_fatals(test_fatal_func_t *const fatal_functions[]) +{ + unsigned int i; + + for (i = 0; fatal_functions[i] != NULL; i++) { + T_BEGIN { + run_one_fatal(fatal_functions[i]); + } T_END; + } +} +static void test_run_named_fatals(const struct named_fatal fatals[], const char *match) +{ + unsigned int i; + + for (i = 0; fatals[i].func != NULL; i++) { + if (strstr(fatals[i].name, match) != NULL) T_BEGIN { + run_one_fatal(fatals[i].func); + } T_END; + } +} + +int test_run(void (*const test_functions[])(void)) +{ + test_init(); + test_run_funcs(test_functions); + return test_deinit(); +} +int test_run_named(const struct named_test tests[], const char *match) +{ + test_init(); + test_run_named_funcs(tests, match); + return test_deinit(); +} +int test_run_with_fatals(void (*const test_functions[])(void), + test_fatal_func_t *const fatal_functions[]) +{ + test_init(); + test_run_funcs(test_functions); + i_set_fatal_handler(test_fatal_handler); + test_run_fatals(fatal_functions); + return test_deinit(); +} +int test_run_named_with_fatals(const char *match, const struct named_test tests[], + const struct named_fatal fatals[]) +{ + test_init(); + test_run_named_funcs(tests, match); + i_set_fatal_handler(test_fatal_handler); + test_run_named_fatals(fatals, match); + return test_deinit(); +} + +void test_forked_end(void) +{ + i_set_error_handler(default_error_handler); + i_set_fatal_handler(default_fatal_handler); + + i_free_and_null(expected_error_str); + i_free_and_null(expected_fatal_str); + i_free_and_null(test_prefix); + t_pop_last_unsafe(); /* as we were within a T_BEGIN { tests[i].func(); } T_END */ +} + +void ATTR_NORETURN +test_exit(int status) +{ + i_free_and_null(expected_error_str); + i_free_and_null(expected_fatal_str); + i_free_and_null(test_prefix); + t_pop_last_unsafe(); /* as we were within a T_BEGIN { tests[i].func(); } T_END */ + lib_deinit(); + lib_exit(status); +} diff --git a/src/lib-test/test-common.h b/src/lib-test/test-common.h new file mode 100644 index 0000000..f845c0d --- /dev/null +++ b/src/lib-test/test-common.h @@ -0,0 +1,165 @@ +#ifndef TEST_COMMON_H +#define TEST_COMMON_H + +struct istream *test_istream_create(const char *data); +struct istream *test_istream_create_data(const void *data, size_t size); +void test_istream_set_size(struct istream *input, uoff_t size); +void test_istream_set_allow_eof(struct istream *input, bool allow); +void test_istream_set_max_buffer_size(struct istream *input, size_t size); + +struct ostream *test_ostream_create(buffer_t *output); +struct ostream *test_ostream_create_nonblocking(buffer_t *output, + size_t max_internal_buffer_size); +/* When output->used reaches max_size, start buffering output internally. + When internal buffer reaches max_internal_buffer_size, start returning 0 for + o_stream_send*(). */ +void test_ostream_set_max_output_size(struct ostream *output, size_t max_size); + +void test_begin(const char *name); +#define test_assert(code) STMT_START { \ + if (!(code)) test_assert_failed(#code, __FILE__, __LINE__); \ + } STMT_END +/* Additional parameter may be int or unsigned int, to indicate which of + * a barrage of tests have failed (such as in a loop). + */ +#define test_assert_idx(code, i) STMT_START { \ + if (!(code)) test_assert_failed_idx(#code, __FILE__, __LINE__, i); \ + } STMT_END +/* Additional parameters are s1 (source) and s2 (destination) string + * in strcmp(). + */ +#define test_assert_strcmp(s1, s2) STMT_START { \ + test_assert_strcmp_idx(s1, s2, LLONG_MIN); \ + } STMT_END + +/* Same as test_assert_strcmp expect that it takes an additional i as input. + * When i is greater than or equals 0 it is used to identify the barrage of + * tests failed like in test_assert_idx. +*/ +#define test_assert_strcmp_idx(_s1, _s2, i) STMT_START { \ + const char *_temp_s1 = (_s1); \ + const char *_temp_s2 = (_s2); \ + if ((null_strcmp(_temp_s1,_temp_s2) != 0)) \ + test_assert_failed_strcmp_idx("strcmp(" #_s1 "," #_s2 ")", \ + __FILE__, __LINE__, _temp_s1, _temp_s2, i); \ + } STMT_END + +#define test_assert_cmp(_value1, _op, _value2) \ + test_assert_cmp_idx(_value1, _op, _value2, LLONG_MIN) +#define test_assert_cmp_idx(_value1, _op, _value2, _idx) STMT_START { \ + intmax_t _temp_value1 = (_value1); \ + intmax_t _temp_value2 = (_value2); \ + if (!(_value1 _op _value2)) \ + test_assert_failed_cmp_intmax_idx( \ + #_value1 " " #_op " " #_value2, \ + __FILE__, __LINE__, _temp_value1, _temp_value2, \ + #_op, _idx); \ + } STMT_END + +#define test_assert_ucmp(_value1, _op, _value2) \ + test_assert_ucmp_idx(_value1, _op, _value2, LLONG_MIN) +#define test_assert_ucmp_idx(_value1, _op, _value2, _idx) STMT_START { \ + uintmax_t _temp_value1 = (_value1); \ + uintmax_t _temp_value2 = (_value2); \ + if (!(_value1 _op _value2)) \ + test_assert_failed_ucmp_intmax_idx( \ + #_value1 " " #_op " " #_value2, \ + __FILE__, __LINE__, _temp_value1, _temp_value2, \ + #_op, _idx); \ + } STMT_END + +#ifdef STATIC_CHECKER +# define ATTR_STATIC_CHECKER_NORETURN ATTR_NORETURN +#else +# define ATTR_STATIC_CHECKER_NORETURN +#endif + +void test_assert_failed(const char *code, const char *file, unsigned int line) + ATTR_STATIC_CHECKER_NORETURN; +void test_assert_failed_idx(const char *code, const char *file, unsigned int line, long long i) + ATTR_STATIC_CHECKER_NORETURN; +void test_assert_failed_strcmp_idx(const char *code, const char *file, unsigned int line, + const char * src, const char * dst, long long i) + ATTR_STATIC_CHECKER_NORETURN; +void test_assert_failed_cmp_intmax_idx(const char *code, const char *file, + unsigned int line, + intmax_t src, intmax_t dst, + const char *op, long long i) + ATTR_STATIC_CHECKER_NORETURN; +void test_assert_failed_ucmp_intmax_idx(const char *code, const char *file, + unsigned int line, + uintmax_t src, uintmax_t dst, + const char *op, long long i) + ATTR_STATIC_CHECKER_NORETURN; +bool test_has_failed(void); +/* If you're testing nasty cases which you want to warn, surround the noisy op with these */ +void test_expect_errors(unsigned int expected); +void test_expect_error_string(const char *substr); /* expect just 1 message matching the printf format */ +void test_expect_error_string_n_times(const char *substr, unsigned int times); /* expect just n messages matching the printf format */ +void test_expect_no_more_errors(void); +/* Note that test_expect_error{s,_string}() effectively begin with a check equivalent + to test_expect_no_more_errors(), so you don't need the latter explicitly if following + it with either of the former.*/ + +void test_end(void); + +void test_out(const char *name, bool success); +void test_out_quiet(const char *name, bool success); /* only prints failures */ +void test_out_reason(const char *name, bool success, const char *reason) + ATTR_NULL(3); + +int test_run(void (*const test_functions[])(void)) ATTR_WARN_UNUSED_RESULT; +struct named_test { + const char *name; + void (*func)(void); +}; +int test_run_named(const struct named_test tests[], const char *match) ATTR_WARN_UNUSED_RESULT; + +#define TEST_DECL(x) void x(void); +#define TEST_NAMELESS(x) x, /* Were you to want to use the X trick but not name the tests */ +#define TEST_NAMED(x) { .name = #x , .func = x }, + +enum fatal_test_state { + FATAL_TEST_FINISHED, /* no more test stages, don't call again */ + FATAL_TEST_FAILURE, /* single stage has failed, continue */ + FATAL_TEST_ABORT, /* something's gone horrifically wrong */ +}; +/* The fatal function is called first with stage=0. After each call the stage + is increased by 1. The idea is that each stage would be running an + individual test that is supposed to crash. The function is called until + FATAL_TEST_FINISHED or FATAL_TEST_ABORT is returned. */ +typedef enum fatal_test_state test_fatal_func_t(unsigned int stage); + +typedef void test_fatal_callback_t(void *context); + +struct named_fatal { + const char *name; + test_fatal_func_t *func; +}; +int test_run_with_fatals(void (*const test_functions[])(void), + test_fatal_func_t *const fatal_functions[]); +int test_run_named_with_fatals(const char *match, const struct named_test tests[], + const struct named_fatal fatals[]); + +/* Require the Fatal/Panic string to match this or the fatal test fails. */ +void test_expect_fatal_string(const char *substr); +/* Call the specified callback when a fatal is being triggered. This is mainly + intended to allow freeing memory so valgrind won't complain about memory + leaks. */ +void test_fatal_set_callback(test_fatal_callback_t *callback, void *context); +#define test_fatal_set_callback(callback, context) \ + test_fatal_set_callback(1 ? (test_fatal_callback_t *)callback : \ + CALLBACK_TYPECHECK(callback, void (*)(typeof(context))), \ + context) + +#define FATAL_DECL(x) enum fatal_test_state x(unsigned int); +#define FATAL_NAMELESS(x) x, /* Were you to want to use the X trick but not name the tests */ +#define FATAL_NAMED(x) { .name = #x , .func = x }, + +/* If a child is forked within test context, but wants to operate outside it, + then this will avoid valgrind leak errors */ +void test_forked_end(void); +/* If a fork() wants to exit(), then this will avoid valgrind leak errors */ +void test_exit(int status) ATTR_NORETURN; + +#endif diff --git a/src/lib-test/test-istream.c b/src/lib-test/test-istream.c new file mode 100644 index 0000000..1d20748 --- /dev/null +++ b/src/lib-test/test-istream.c @@ -0,0 +1,166 @@ +/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "memarea.h" +#include "istream-private.h" +#include "test-common.h" + +struct test_istream { + struct istream_private istream; + const void *orig_buffer; + unsigned int skip_diff; + size_t max_pos; + bool allow_eof; +}; + +static void test_buffer_free(unsigned char *buf) +{ + i_free(buf); +} + +static ssize_t test_read(struct istream_private *stream) +{ + struct test_istream *tstream = (struct test_istream *)stream; + unsigned int new_skip_diff; + size_t cur_max; + ssize_t ret; + + i_assert(stream->skip <= stream->pos); + + if (stream->pos - stream->skip >= tstream->istream.max_buffer_size) { + i_assert(stream->skip != stream->pos); + return -2; + } + + if (tstream->max_pos < stream->pos) { + /* we seeked past the end of file. */ + ret = 0; + } else { + /* copy data to a buffer in somewhat random place. this could + help catch bugs. */ + new_skip_diff = i_rand_limit(128); + stream->skip = (stream->skip - tstream->skip_diff) + + new_skip_diff; + stream->pos = (stream->pos - tstream->skip_diff) + + new_skip_diff; + tstream->max_pos = (tstream->max_pos - tstream->skip_diff) + + new_skip_diff; + tstream->skip_diff = new_skip_diff; + + cur_max = tstream->max_pos; + if (stream->max_buffer_size < SIZE_MAX - stream->skip && + cur_max > stream->skip + stream->max_buffer_size) + cur_max = stream->skip + stream->max_buffer_size; + + /* Reallocate the memory area if needed. Use exactly correct + buffer size so valgrind can catch read overflows. If a + correctly sized memarea already exists, use it only if + its refcount is 1. Otherwise with refcount>1 we could be + moving data within an existing memarea, which breaks + snapshots. */ + if (cur_max > 0 && (stream->buffer_size != cur_max || + stream->memarea == NULL || + memarea_get_refcount(stream->memarea) > 1)) { + void *old_w_buffer = stream->w_buffer; + stream->w_buffer = i_malloc(cur_max); + if (stream->buffer_size != 0) { + memcpy(stream->w_buffer, old_w_buffer, + I_MIN(stream->buffer_size, cur_max)); + } + stream->buffer = stream->w_buffer; + stream->buffer_size = cur_max; + + if (stream->memarea != NULL) + memarea_unref(&stream->memarea); + stream->memarea = memarea_init(stream->w_buffer, + stream->buffer_size, + test_buffer_free, + stream->w_buffer); + } + ssize_t size = cur_max - new_skip_diff; + if (size > 0) + memcpy(stream->w_buffer + new_skip_diff, + tstream->orig_buffer, (size_t)size); + + ret = cur_max - stream->pos; + stream->pos = cur_max; + } + + if (ret > 0) + return ret; + else if (!tstream->allow_eof || + stream->pos - tstream->skip_diff < (uoff_t)stream->statbuf.st_size) + return 0; + else { + stream->istream.eof = TRUE; + return -1; + } +} + +static void test_seek(struct istream_private *stream, uoff_t v_offset, + bool mark ATTR_UNUSED) +{ + struct test_istream *tstream = (struct test_istream *)stream; + + stream->istream.v_offset = v_offset; + stream->skip = v_offset + tstream->skip_diff; + stream->pos = stream->skip; +} + +struct istream *test_istream_create_data(const void *data, size_t size) +{ + struct test_istream *tstream; + + tstream = i_new(struct test_istream, 1); + tstream->orig_buffer = data; + + tstream->istream.read = test_read; + tstream->istream.seek = test_seek; + + tstream->istream.istream.blocking = FALSE; + tstream->istream.istream.seekable = TRUE; + i_stream_create(&tstream->istream, NULL, -1, 0); + tstream->istream.statbuf.st_size = tstream->max_pos = size; + tstream->allow_eof = TRUE; + tstream->istream.max_buffer_size = SIZE_MAX; + return &tstream->istream.istream; +} + +struct istream *test_istream_create(const char *data) +{ + return test_istream_create_data(data, strlen(data)); +} + +static struct test_istream *test_istream_find(struct istream *input) +{ + struct istream *in; + + for (in = input; in != NULL; in = in->real_stream->parent) { + if (in->real_stream->read == test_read) + return (struct test_istream *)in->real_stream; + } + i_panic("%s isn't test-istream", i_stream_get_name(input)); +} + +void test_istream_set_allow_eof(struct istream *input, bool allow) +{ + struct test_istream *tstream = test_istream_find(input); + + tstream->allow_eof = allow; +} + +void test_istream_set_max_buffer_size(struct istream *input, size_t size) +{ + struct test_istream *tstream = test_istream_find(input); + + tstream->istream.max_buffer_size = size; +} + +void test_istream_set_size(struct istream *input, uoff_t size) +{ + struct test_istream *tstream = test_istream_find(input); + + if (size > (uoff_t)tstream->istream.statbuf.st_size) + size = (uoff_t)tstream->istream.statbuf.st_size; + tstream->max_pos = size + tstream->skip_diff; +} diff --git a/src/lib-test/test-ostream.c b/src/lib-test/test-ostream.c new file mode 100644 index 0000000..4ca3a4a --- /dev/null +++ b/src/lib-test/test-ostream.c @@ -0,0 +1,194 @@ +/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "buffer.h" +#include "ostream-private.h" +#include "test-common.h" + +struct test_ostream { + struct ostream_private ostream; + buffer_t *internal_buf; + buffer_t *output_buf; + size_t max_output_size; + struct timeout *to; + bool flush_pending; +}; + +static void o_stream_test_destroy(struct iostream_private *stream) +{ + struct test_ostream *tstream = (struct test_ostream *)stream; + + timeout_remove(&tstream->to); + buffer_free(&tstream->internal_buf); +} + +static int o_stream_test_flush(struct ostream_private *stream) +{ + struct test_ostream *tstream = (struct test_ostream *)stream; + + if (tstream->internal_buf == NULL || tstream->internal_buf->used == 0) + return 1; + if (tstream->output_buf->used >= tstream->max_output_size) + return 0; + + size_t left = tstream->max_output_size - tstream->output_buf->used; + size_t n = I_MIN(left, tstream->internal_buf->used); + buffer_append(tstream->output_buf, tstream->internal_buf->data, n); + buffer_delete(tstream->internal_buf, 0, n); + return tstream->internal_buf->used == 0 ? 1 : 0; +} + +static ssize_t +o_stream_test_sendv(struct ostream_private *stream, + const struct const_iovec *iov, unsigned int iov_count) +{ + struct test_ostream *tstream = (struct test_ostream *)stream; + struct const_iovec cur_iov = { NULL, 0 }; + size_t left, n; + ssize_t ret = 0; + unsigned int i; + + /* first we need to try to flush the internal buffer */ + if ((ret = o_stream_test_flush(stream)) <= 0) + return ret; + + /* append to output_buf until max_output_size is reached */ + ret = 0; + for (i = 0; i < iov_count; i++) { + left = tstream->max_output_size < tstream->output_buf->used ? 0 : + tstream->max_output_size - tstream->output_buf->used; + n = I_MIN(left, iov[i].iov_len); + buffer_append(tstream->output_buf, iov[i].iov_base, n); + stream->ostream.offset += n; + ret += n; + if (n != iov[i].iov_len) { + cur_iov.iov_base = CONST_PTR_OFFSET(iov[i].iov_base, n); + cur_iov.iov_len = iov[i].iov_len - n; + break; + } + } + /* if we've internal_buf, append to it until max_buffer_size is + reached */ + if (i == iov_count || tstream->internal_buf == NULL) + return ret; + do { + left = tstream->ostream.max_buffer_size - + tstream->internal_buf->used; + n = I_MIN(left, cur_iov.iov_len); + buffer_append(tstream->internal_buf, cur_iov.iov_base, n); + stream->ostream.offset += n; + ret += n; + if (n != cur_iov.iov_len) + break; + if (++i < iov_count) + cur_iov = iov[i]; + } while (i < iov_count); + + tstream->flush_pending = TRUE; + return ret; +} + +static void test_ostream_send_more(struct test_ostream *tstream) +{ + struct ostream *ostream = &tstream->ostream.ostream; + int ret; + + o_stream_ref(ostream); + tstream->flush_pending = FALSE; + if (tstream->ostream.callback != NULL) + ret = tstream->ostream.callback(tstream->ostream.context); + else + ret = o_stream_test_flush(&tstream->ostream); + if (ret == 0 || (tstream->internal_buf != NULL && + tstream->internal_buf->used > 0)) + tstream->flush_pending = TRUE; + if (!tstream->flush_pending || + tstream->output_buf->used >= tstream->max_output_size) + timeout_remove(&tstream->to); + o_stream_unref(&ostream); +} + +static void test_ostream_set_send_more_timeout(struct test_ostream *tstream) +{ + if (tstream->to == NULL && tstream->flush_pending && + tstream->output_buf->used < tstream->max_output_size) + tstream->to = timeout_add_short(0, test_ostream_send_more, tstream); +} + +static void +o_stream_test_flush_pending(struct ostream_private *stream, bool set) +{ + struct test_ostream *tstream = (struct test_ostream *)stream; + + if (tstream->internal_buf != NULL && tstream->internal_buf->used > 0) { + /* we have internal data, won't reset flush_pending */ + i_assert(tstream->flush_pending); + } else { + tstream->flush_pending = set; + } + if (set) + test_ostream_set_send_more_timeout(tstream); +} + +static size_t +o_stream_test_get_buffer_used_size(const struct ostream_private *stream) +{ + const struct test_ostream *tstream = + (const struct test_ostream *)stream; + + return tstream->internal_buf == NULL ? 0 : + tstream->internal_buf->used; +} + +struct ostream *test_ostream_create(buffer_t *output) +{ + struct test_ostream *tstream; + struct ostream *ostream; + + tstream = i_new(struct test_ostream, 1); + tstream->ostream.max_buffer_size = SIZE_MAX; + tstream->ostream.iostream.destroy = o_stream_test_destroy; + tstream->ostream.sendv = o_stream_test_sendv; + tstream->ostream.flush = o_stream_test_flush; + tstream->ostream.flush_pending = o_stream_test_flush_pending; + tstream->ostream.get_buffer_used_size = + o_stream_test_get_buffer_used_size; + tstream->ostream.ostream.blocking = TRUE; + + tstream->output_buf = output; + tstream->max_output_size = SIZE_MAX; + ostream = o_stream_create(&tstream->ostream, NULL, -1); + o_stream_set_name(ostream, "(test-ostream)"); + return ostream; +} + +struct ostream *test_ostream_create_nonblocking(buffer_t *output, + size_t max_internal_buffer_size) +{ + struct test_ostream *tstream; + + tstream = (struct test_ostream *)test_ostream_create(output)->real_stream; + tstream->internal_buf = buffer_create_dynamic(default_pool, 128); + tstream->ostream.ostream.blocking = FALSE; + tstream->ostream.max_buffer_size = max_internal_buffer_size; + return &tstream->ostream.ostream; +} + +static struct test_ostream *test_ostream_find(struct ostream *output) +{ + struct ostream *out; + + for (out = output; out != NULL; out = out->real_stream->parent) { + if (out->real_stream->sendv == o_stream_test_sendv) + return (struct test_ostream *)out->real_stream; + } + i_panic("%s isn't test-ostream", o_stream_get_name(output)); +} + +void test_ostream_set_max_output_size(struct ostream *output, size_t max_size) +{ + struct test_ostream *tstream = test_ostream_find(output); + + tstream->max_output_size = max_size; + test_ostream_set_send_more_timeout(tstream); +} diff --git a/src/lib-test/test-subprocess.c b/src/lib-test/test-subprocess.c new file mode 100644 index 0000000..eeaae80 --- /dev/null +++ b/src/lib-test/test-subprocess.c @@ -0,0 +1,392 @@ +/* Copyright (c) 2020 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "lib-signals.h" +#include "hostpid.h" +#include "array.h" +#include "ioloop.h" +#include "sleep.h" +#include "test-common.h" +#include "test-subprocess.h" + +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> + +struct test_subprocess { + pid_t pid; +}; + +volatile sig_atomic_t test_subprocess_is_child = 0; +static bool test_subprocess_lib_init = FALSE; +static volatile bool test_subprocess_notification_signal_received[SIGUSR1 + 1]; +static struct event *test_subprocess_event = NULL; +static ARRAY(struct test_subprocess *) test_subprocesses = ARRAY_INIT; +static void (*test_subprocess_cleanup_callback)(void) = NULL; + +static void +test_subprocess_signal(const siginfo_t *si ATTR_UNUSED, + void *context ATTR_UNUSED) +{ + io_loop_stop(current_ioloop); +} + +static void test_subprocess_free_all(void) +{ + struct test_subprocess *subp; + + array_foreach_elem(&test_subprocesses, subp) + i_free(subp); + array_free(&test_subprocesses); +} + +static void ATTR_NORETURN +test_subprocess_child(int (*func)(void *context), void *context, + bool continue_test) +{ + int ret; + + if (!continue_test) + test_forked_end(); + + hostpid_init(); + + lib_signals_deinit(); + lib_signals_init(); + + lib_signals_set_handler(SIGTERM, + LIBSIG_FLAG_DELAYED | LIBSIG_FLAG_IOLOOP_AUTOMOVE, + test_subprocess_signal, NULL); + lib_signals_set_handler(SIGQUIT, + LIBSIG_FLAG_DELAYED | LIBSIG_FLAG_IOLOOP_AUTOMOVE, + test_subprocess_signal, NULL); + lib_signals_set_handler(SIGINT, + LIBSIG_FLAG_DELAYED | LIBSIG_FLAG_IOLOOP_AUTOMOVE, + test_subprocess_signal, NULL); + + ret = func(context); + + /* Prevent race condition */ + lib_signals_clear_handlers_and_ignore(SIGTERM); + + event_unref(&test_subprocess_event); + lib_signals_deinit(); + + if (!continue_test) { + lib_deinit(); + lib_exit(ret); + } + test_exit((test_has_failed() ? 1 : 0)); +} + +#undef test_subprocess_fork +void test_subprocess_fork(int (*func)(void *context), void *context, + bool continue_test) +{ + struct test_subprocess *subprocess; + + subprocess = i_new(struct test_subprocess, 1); + + lib_signals_ioloop_detach(); + + /* avoid races: fork the child process with test_subprocess_is_child + set to 1 in case it immediately receives a signal. */ + test_subprocess_is_child = 1; + if ((subprocess->pid = fork()) == (pid_t)-1) + i_fatal("test: sub-process: fork() failed: %m"); + if (subprocess->pid == 0) { + /* cannot include this in the list to avoid accidental + * kill of PID 0, so just free it here explicitly. */ + i_free(subprocess); + test_subprocess_free_all(); + + test_subprocess_child(func, context, continue_test); + i_unreached(); + } + test_subprocess_is_child = 0; + + array_push_back(&test_subprocesses, &subprocess); + lib_signals_ioloop_attach(); +} + +static void test_subprocess_verify_exit_status(int status) +{ + test_out_quiet("sub-process ended properly", + WIFEXITED(status) && WEXITSTATUS(status) == 0); + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) != 0) { + e_warning(test_subprocess_event, + "Sub-process exited with status %d", + WEXITSTATUS(status)); + } + } else if (WIFSIGNALED(status)) { + e_warning(test_subprocess_event, + "Sub-process forcibly terminated with signal %d", + WTERMSIG(status)); + } else if (WIFSTOPPED(status)) { + e_warning(test_subprocess_event, + "Sub-process stopped with signal %d", + WSTOPSIG(status)); + } else { + e_warning(test_subprocess_event, + "Sub-process terminated abnormally with status %d", + status); + } +} + +static void test_subprocess_kill_forced(struct test_subprocess *subp) +{ + i_assert(subp->pid > 0); + (void)kill(subp->pid, SIGKILL); + (void)waitpid(subp->pid, NULL, 0); +} + +void test_subprocess_kill_all(unsigned int timeout_secs) +{ + struct test_subprocess **subps; + unsigned int subps_count, subps_left, i; + + subps = array_get_modifiable(&test_subprocesses, &subps_count); + + /* Request children to terminate gently */ + for (i = 0; i < subps_count; i++) { + if (subps[i] == NULL || subps[i]->pid == (pid_t)-1) + continue; + + e_debug(test_subprocess_event, + "Terminating sub-process [%u]", i); + if (kill(subps[i]->pid, SIGTERM) < 0) { + e_error(test_subprocess_event, + "Failed to kill sub-process [%u] with SIGTERM: " + "%m", i); + } + } + + /* Wait for children */ + subps_left = subps_count; + while (subps_left > 0) { + int status; + pid_t wret = (pid_t)-1; + + alarm(timeout_secs); + wret = waitpid(-1, &status, 0); + alarm(0); + + test_assert(wret > 0); + if (wret < 0 && errno == EINTR) + e_warning(test_subprocess_event, + "Wait for sub-processes timed out"); + if (wret > 0) + test_subprocess_verify_exit_status(status); + + if (wret == 0) + break; + if (wret < 0) { + if (errno == ECHILD) + continue; + e_warning(test_subprocess_event, + "Wait for sub-processes failed: %m"); + break; + } + for (i = 0; i < subps_count; i++) { + if (subps[i] == NULL || subps[i]->pid != wret) + continue; + e_debug(test_subprocess_event, + "Terminated sub-process [%u]", i); + i_free(subps[i]); + subps_left--; + } + } + + /* Kill disobedient ones with fire */ + for (i = 0; i < subps_count; i++) { + if (subps[i] == NULL || subps[i]->pid == (pid_t)-1) + continue; + e_warning(test_subprocess_event, + "Forcibly killed sub-process [%u]", i); + test_subprocess_kill_forced(subps[i]); + i_assert(subps_left > 0); + i_free(subps[i]); + subps_left--; + } + i_assert(subps_left == 0); + + array_clear(&test_subprocesses); +} + +static void test_subprocess_kill_all_forced(void) +{ + struct test_subprocess **subps; + unsigned int subps_count, i; + + if (!array_is_created(&test_subprocesses)) + return; + + /* This is also called from signal handler context, so no debug logging + here. + */ + + subps = array_get_modifiable(&test_subprocesses, &subps_count); + + if (subps_count == 0) + return; + + for (i = 0; i < subps_count; i++) { + if (subps[i] == NULL || subps[i]->pid == (pid_t)-1) + continue; + test_subprocess_kill_forced(subps[i]); + subps[i]->pid = (pid_t)-1; + } +} + +/* + * Main + */ + +volatile sig_atomic_t terminating = 0; + +static void test_subprocess_cleanup(void) +{ + if (test_subprocess_is_child != 0) { + /* Child processes must not execute the cleanups */ + return; + } + + /* We get here when the test ended normally, badly failed, crashed, + terminated, or executed exit() unexpectedly. The cleanups performed + here are important and must be executed at all times. */ + + /* While any unfreed memory will be handled by the system, lingering + child processes will not be handled so well. So, we need to make sure + here that we don't leave any pesky child processes alive. */ + test_subprocess_kill_all_forced(); + + /* Perform any additional important cleanup specific to the test. */ + if (test_subprocess_cleanup_callback != NULL) + test_subprocess_cleanup_callback(); +} + +static void +test_subprocess_alarm(const siginfo_t *si ATTR_UNUSED, + void *context ATTR_UNUSED) +{ + /* We use alarm() to implement a simple timeout on waitpid(), which will + exit with EINTR when SIGALRM is received. This handler overrides the + default SIGALRM handler, so that the process is not killed and no + messages are printed to terminal. + */ +} + +static void +test_subprocess_terminate(const siginfo_t *si, void *context ATTR_UNUSED) +{ + int signo = si->si_signo; + + if (terminating != 0) + raise(signo); + terminating = 1; + + /* Perform important cleanups */ + test_subprocess_cleanup(); + + (void)signal(signo, SIG_DFL); + if (signo == SIGTERM) + _exit(0); + else + raise(signo); +} + +static void test_atexit(void) +{ + /* NOTICE: This is also called by children, so be careful. */ + + /* Perform important cleanups */ + test_subprocess_cleanup(); +} + +void test_subprocess_set_cleanup_callback(void (*callback)(void)) +{ + test_subprocess_cleanup_callback = callback; +} + +void test_subprocess_notify_signal_send(int signo, pid_t pid) +{ + if (kill(pid, signo) < 0) + i_fatal("kill(%ld, SIGHUP) failed: %m", (long)pid); +} + +void test_subprocess_notify_signal_send_parent(int signo) +{ + test_subprocess_notify_signal_send(signo, getppid()); +} + +void test_subprocess_notify_signal_reset(int signo) +{ + i_assert(signo >= 0 && + (unsigned int)signo < N_ELEMENTS(test_subprocess_notification_signal_received)); + test_subprocess_notification_signal_received[signo] = FALSE; +} + +void test_subprocess_notify_signal_wait(int signo, unsigned int timeout_msecs) +{ + unsigned int i, count = timeout_msecs / 10; + + for (i = 0; i < count; i++) { + if (test_subprocess_notification_signal_received[signo]) + return; + i_sleep_msecs(10); + } + i_fatal("Didn't receive wait notification signal from server"); +} + +static void +test_subprocess_notification_signal(const siginfo_t *si, + void *context ATTR_UNUSED) +{ + int signo = si->si_signo; + + i_assert(signo >= 0 && + (unsigned int)signo < N_ELEMENTS(test_subprocess_notification_signal_received)); + test_subprocess_notification_signal_received[signo] = TRUE; +} + +void test_subprocesses_init(bool debug) +{ + if (!lib_is_initialized()) { + lib_init(); + test_subprocess_lib_init = TRUE; + } + lib_signals_init(); + + atexit(test_atexit); + lib_signals_ignore(SIGPIPE, TRUE); + lib_signals_set_handler(SIGALRM, 0, test_subprocess_alarm, NULL); + lib_signals_set_handler(SIGTERM, 0, test_subprocess_terminate, NULL); + lib_signals_set_handler(SIGQUIT, 0, test_subprocess_terminate, NULL); + lib_signals_set_handler(SIGINT, 0, test_subprocess_terminate, NULL); + lib_signals_set_handler(SIGSEGV, 0, test_subprocess_terminate, NULL); + lib_signals_set_handler(SIGABRT, 0, test_subprocess_terminate, NULL); + lib_signals_set_handler(SIGHUP, LIBSIG_FLAG_RESTART, + test_subprocess_notification_signal, NULL); + lib_signals_set_handler(SIGUSR1, LIBSIG_FLAG_RESTART, + test_subprocess_notification_signal, NULL); + + i_array_init(&test_subprocesses, 8); + + test_subprocess_event = event_create(NULL); + event_set_forced_debug(test_subprocess_event, debug); + event_set_append_log_prefix(test_subprocess_event, "test: "); +} + +void test_subprocesses_deinit(void) +{ + test_subprocess_cleanup(); + test_subprocess_free_all(); + array_free(&test_subprocesses); + + event_unref(&test_subprocess_event); + lib_signals_deinit(); + + if (test_subprocess_lib_init) + lib_deinit(); +} diff --git a/src/lib-test/test-subprocess.h b/src/lib-test/test-subprocess.h new file mode 100644 index 0000000..1a8d174 --- /dev/null +++ b/src/lib-test/test-subprocess.h @@ -0,0 +1,41 @@ +#ifndef TEST_SUBPROCESS_H +#define TEST_SUBPROCESS_H + +struct test_subprocess; + +/* Fork a sub-process for this test. The func is the main function for the + forked sub-process. The provided context is passed to the provided function. + When continue_test=FALSE, the test is ended immediately in the sub-process, + otherwise, the test continues and its result is used to set the exit code + when the process ends gracefully. */ +void test_subprocess_fork(int (*func)(void *), void *context, + bool continue_test); +#define test_subprocess_fork(func, context, continue_test) \ + test_subprocess_fork( \ + (int(*)(void*))func, \ + (TRUE ? context : \ + CALLBACK_TYPECHECK(func, int(*)(typeof(context)))), \ + continue_test) + +void test_subprocess_kill_all(unsigned int timeout_secs); + +/* Set a cleanup callback that is executed even when the test program crashes or + exit()s unexpectedly. Note that this may be run in signal context. */ +void test_subprocess_set_cleanup_callback(void (*callback)(void)); + +/* Send a notification signal (SIGHUP) to the given PID */ +void test_subprocess_notify_signal_send(int signo, pid_t pid); +/* Send a notificatino signal to the parent process. */ +void test_subprocess_notify_signal_send_parent(int signo); +/* Reset any previously sent notification signals. */ +void test_subprocess_notify_signal_reset(int signo); +/* Wait until a notification signal is sent, or return immediately if it was + already sent. test_subprocess_notify_signal_reset() should be called before + this to make sure it's not returning due to a previously sent signal. + If the timeout is reached, i_fatal() is called. */ +void test_subprocess_notify_signal_wait(int signo, unsigned int timeout_msecs); + +void test_subprocesses_init(bool debug); +void test_subprocesses_deinit(void); + +#endif |