diff options
Diffstat (limited to 'src/hooks/dhcp/flex_option/tests')
-rw-r--r-- | src/hooks/dhcp/flex_option/tests/Makefile.am | 63 | ||||
-rw-r--r-- | src/hooks/dhcp/flex_option/tests/Makefile.in | 1079 | ||||
-rw-r--r-- | src/hooks/dhcp/flex_option/tests/flex_option_unittests.cc | 1586 | ||||
-rw-r--r-- | src/hooks/dhcp/flex_option/tests/run_unittests.cc | 19 | ||||
-rw-r--r-- | src/hooks/dhcp/flex_option/tests/sub_option_unittests.cc | 3173 | ||||
-rw-r--r-- | src/hooks/dhcp/flex_option/tests/test_flex_option.h | 82 |
6 files changed, 6002 insertions, 0 deletions
diff --git a/src/hooks/dhcp/flex_option/tests/Makefile.am b/src/hooks/dhcp/flex_option/tests/Makefile.am new file mode 100644 index 0000000..5089125 --- /dev/null +++ b/src/hooks/dhcp/flex_option/tests/Makefile.am @@ -0,0 +1,63 @@ +SUBDIRS = . + +AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib +AM_CPPFLAGS += -I$(top_builddir)/src/hooks/dhcp/flex_option -I$(top_srcdir)/src/hooks/dhcp/flex_option +AM_CPPFLAGS += $(BOOST_INCLUDES) +AM_CPPFLAGS += -DFLEX_OPTION_LIB_SO=\"$(abs_top_builddir)/src/hooks/dhcp/flex_option/.libs/libdhcp_flex_option.so\" +AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\" + +AM_CXXFLAGS = $(KEA_CXXFLAGS) + +if USE_STATIC_LINK +AM_LDFLAGS = -static +endif + +# Unit test data files need to get installed. +EXTRA_DIST = + +CLEANFILES = *.gcno *.gcda + +TESTS_ENVIRONMENT = $(LIBTOOL) --mode=execute $(VALGRIND_COMMAND) + +LOG_COMPILER = $(LIBTOOL) +AM_LOG_FLAGS = --mode=execute + +TESTS = +if HAVE_GTEST +TESTS += flex_option_unittests + +flex_option_unittests_SOURCES = run_unittests.cc +flex_option_unittests_SOURCES += test_flex_option.h +flex_option_unittests_SOURCES += flex_option_unittests.cc +flex_option_unittests_SOURCES += sub_option_unittests.cc + +flex_option_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES) + +flex_option_unittests_LDFLAGS = $(AM_LDFLAGS) $(CRYPTO_LDFLAGS) $(GTEST_LDFLAGS) + +flex_option_unittests_CXXFLAGS = $(AM_CXXFLAGS) + +flex_option_unittests_LDADD = $(top_builddir)/src/hooks/dhcp/flex_option/libflex_option.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/dhcpsrv/libkea-dhcpsrv.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/process/libkea-process.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/eval/libkea-eval.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/dhcp_ddns/libkea-dhcp_ddns.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/stats/libkea-stats.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/config/libkea-cfgclient.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/http/libkea-http.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/database/libkea-database.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/dns/libkea-dns++.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/cryptolink/libkea-cryptolink.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/log/libkea-log.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/util/libkea-util.la +flex_option_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la +flex_option_unittests_LDADD += $(LOG4CPLUS_LIBS) +flex_option_unittests_LDADD += $(CRYPTO_LIBS) +flex_option_unittests_LDADD += $(BOOST_LIBS) +flex_option_unittests_LDADD += $(GTEST_LDADD) +endif +noinst_PROGRAMS = $(TESTS) diff --git a/src/hooks/dhcp/flex_option/tests/Makefile.in b/src/hooks/dhcp/flex_option/tests/Makefile.in new file mode 100644 index 0000000..8eee23d --- /dev/null +++ b/src/hooks/dhcp/flex_option/tests/Makefile.in @@ -0,0 +1,1079 @@ +# Makefile.in generated by automake 1.16.5 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2021 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@ +TESTS = $(am__EXEEXT_1) +@HAVE_GTEST_TRUE@am__append_1 = flex_option_unittests +noinst_PROGRAMS = $(am__EXEEXT_2) +subdir = src/hooks/dhcp/flex_option/tests +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4macros/ax_boost_for_kea.m4 \ + $(top_srcdir)/m4macros/ax_cpp14.m4 \ + $(top_srcdir)/m4macros/ax_cpp20.m4 \ + $(top_srcdir)/m4macros/ax_crypto.m4 \ + $(top_srcdir)/m4macros/ax_find_library.m4 \ + $(top_srcdir)/m4macros/ax_gssapi.m4 \ + $(top_srcdir)/m4macros/ax_gtest.m4 \ + $(top_srcdir)/m4macros/ax_isc_rpath.m4 \ + $(top_srcdir)/m4macros/ax_netconf.m4 \ + $(top_srcdir)/m4macros/libtool.m4 \ + $(top_srcdir)/m4macros/ltoptions.m4 \ + $(top_srcdir)/m4macros/ltsugar.m4 \ + $(top_srcdir)/m4macros/ltversion.m4 \ + $(top_srcdir)/m4macros/lt~obsolete.m4 \ + $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +@HAVE_GTEST_TRUE@am__EXEEXT_1 = flex_option_unittests$(EXEEXT) +am__EXEEXT_2 = $(am__EXEEXT_1) +PROGRAMS = $(noinst_PROGRAMS) +am__flex_option_unittests_SOURCES_DIST = run_unittests.cc \ + test_flex_option.h flex_option_unittests.cc \ + sub_option_unittests.cc +@HAVE_GTEST_TRUE@am_flex_option_unittests_OBJECTS = flex_option_unittests-run_unittests.$(OBJEXT) \ +@HAVE_GTEST_TRUE@ flex_option_unittests-flex_option_unittests.$(OBJEXT) \ +@HAVE_GTEST_TRUE@ flex_option_unittests-sub_option_unittests.$(OBJEXT) +flex_option_unittests_OBJECTS = $(am_flex_option_unittests_OBJECTS) +am__DEPENDENCIES_1 = +@HAVE_GTEST_TRUE@flex_option_unittests_DEPENDENCIES = $(top_builddir)/src/hooks/dhcp/flex_option/libflex_option.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcpsrv/libkea-dhcpsrv.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/process/libkea-process.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/eval/libkea-eval.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcp_ddns/libkea-dhcp_ddns.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/stats/libkea-stats.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/config/libkea-cfgclient.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/http/libkea-http.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/hooks/libkea-hooks.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/database/libkea-database.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/cc/libkea-cc.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/asiolink/libkea-asiolink.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dns/libkea-dns++.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/cryptolink/libkea-cryptolink.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/log/libkea-log.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/util/libkea-util.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/exceptions/libkea-exceptions.la \ +@HAVE_GTEST_TRUE@ $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ +@HAVE_GTEST_TRUE@ $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) +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 = +flex_option_unittests_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \ + $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \ + $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) \ + $(flex_option_unittests_LDFLAGS) $(LDFLAGS) -o $@ +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)/flex_option_unittests-flex_option_unittests.Po \ + ./$(DEPDIR)/flex_option_unittests-run_unittests.Po \ + ./$(DEPDIR)/flex_option_unittests-sub_option_unittests.Po +am__mv = mv -f +CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) +LTCXXCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CXXFLAGS) $(CXXFLAGS) +AM_V_CXX = $(am__v_CXX_@AM_V@) +am__v_CXX_ = $(am__v_CXX_@AM_DEFAULT_V@) +am__v_CXX_0 = @echo " CXX " $@; +am__v_CXX_1 = +CXXLD = $(CXX) +CXXLINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CXXLD) $(AM_CXXFLAGS) \ + $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CXXLD = $(am__v_CXXLD_@AM_V@) +am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) +am__v_CXXLD_0 = @echo " CXXLD " $@; +am__v_CXXLD_1 = +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 = $(flex_option_unittests_SOURCES) +DIST_SOURCES = $(am__flex_option_unittests_SOURCES_DIST) +RECURSIVE_TARGETS = all-recursive check-recursive cscopelist-recursive \ + ctags-recursive dvi-recursive html-recursive info-recursive \ + install-data-recursive install-dvi-recursive \ + install-exec-recursive install-html-recursive \ + install-info-recursive install-pdf-recursive \ + install-ps-recursive install-recursive installcheck-recursive \ + installdirs-recursive pdf-recursive ps-recursive \ + tags-recursive uninstall-recursive +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +RECURSIVE_CLEAN_TARGETS = mostlyclean-recursive clean-recursive \ + distclean-recursive maintainer-clean-recursive +am__recursive_targets = \ + $(RECURSIVE_TARGETS) \ + $(RECURSIVE_CLEAN_TARGETS) \ + $(am__extra_recursive_targets) +AM_RECURSIVE_TARGETS = $(am__recursive_targets:-recursive=) TAGS CTAGS \ + distdir distdir-am +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)` +am__tty_colors_dummy = \ + mgn= red= grn= lgn= blu= brg= std=; \ + am__color_tests=no +am__tty_colors = { \ + $(am__tty_colors_dummy); \ + if test "X$(AM_COLOR_TESTS)" = Xno; then \ + am__color_tests=no; \ + elif test "X$(AM_COLOR_TESTS)" = Xalways; then \ + am__color_tests=yes; \ + elif test "X$$TERM" != Xdumb && { test -t 1; } 2>/dev/null; then \ + am__color_tests=yes; \ + fi; \ + if test $$am__color_tests = yes; then \ + red='[0;31m'; \ + grn='[0;32m'; \ + lgn='[1;32m'; \ + blu='[1;34m'; \ + mgn='[0;35m'; \ + brg='[1m'; \ + std='[m'; \ + fi; \ +} +DIST_SUBDIRS = $(SUBDIRS) +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +am__relativize = \ + dir0=`pwd`; \ + sed_first='s,^\([^/]*\)/.*$$,\1,'; \ + sed_rest='s,^[^/]*/*,,'; \ + sed_last='s,^.*/\([^/]*\)$$,\1,'; \ + sed_butlast='s,/*[^/]*$$,,'; \ + while test -n "$$dir1"; do \ + first=`echo "$$dir1" | sed -e "$$sed_first"`; \ + if test "$$first" != "."; then \ + if test "$$first" = ".."; then \ + dir2=`echo "$$dir0" | sed -e "$$sed_last"`/"$$dir2"; \ + dir0=`echo "$$dir0" | sed -e "$$sed_butlast"`; \ + else \ + first2=`echo "$$dir2" | sed -e "$$sed_first"`; \ + if test "$$first2" = "$$first"; then \ + dir2=`echo "$$dir2" | sed -e "$$sed_rest"`; \ + else \ + dir2="../$$dir2"; \ + fi; \ + dir0="$$dir0"/"$$first"; \ + fi; \ + fi; \ + dir1=`echo "$$dir1" | sed -e "$$sed_rest"`; \ + done; \ + reldir="$$dir2" +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +ASCIIDOC = @ASCIIDOC@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +BOOST_INCLUDES = @BOOST_INCLUDES@ +BOOST_LIBS = @BOOST_LIBS@ +BOTAN_TOOL = @BOTAN_TOOL@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CONTRIB_DIR = @CONTRIB_DIR@ +CPPFLAGS = @CPPFLAGS@ +CRYPTO_CFLAGS = @CRYPTO_CFLAGS@ +CRYPTO_INCLUDES = @CRYPTO_INCLUDES@ +CRYPTO_LDFLAGS = @CRYPTO_LDFLAGS@ +CRYPTO_LIBS = @CRYPTO_LIBS@ +CRYPTO_PACKAGE = @CRYPTO_PACKAGE@ +CRYPTO_RPATH = @CRYPTO_RPATH@ +CSCOPE = @CSCOPE@ +CTAGS = @CTAGS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DISTCHECK_BOOST_CONFIGURE_FLAG = @DISTCHECK_BOOST_CONFIGURE_FLAG@ +DISTCHECK_CONTRIB_CONFIGURE_FLAG = @DISTCHECK_CONTRIB_CONFIGURE_FLAG@ +DISTCHECK_CRYPTO_CONFIGURE_FLAG = @DISTCHECK_CRYPTO_CONFIGURE_FLAG@ +DISTCHECK_GSSAPI_CONFIGURE_FLAG = @DISTCHECK_GSSAPI_CONFIGURE_FLAG@ +DISTCHECK_GTEST_CONFIGURE_FLAG = @DISTCHECK_GTEST_CONFIGURE_FLAG@ +DISTCHECK_KEA_SHELL_CONFIGURE_FLAG = @DISTCHECK_KEA_SHELL_CONFIGURE_FLAG@ +DISTCHECK_LIBYANGCPP_CONFIGURE_FLAG = @DISTCHECK_LIBYANGCPP_CONFIGURE_FLAG@ +DISTCHECK_LIBYANG_CONFIGURE_FLAG = @DISTCHECK_LIBYANG_CONFIGURE_FLAG@ +DISTCHECK_LOG4CPLUS_CONFIGURE_FLAG = @DISTCHECK_LOG4CPLUS_CONFIGURE_FLAG@ +DISTCHECK_MYSQL_CONFIGURE_FLAG = @DISTCHECK_MYSQL_CONFIGURE_FLAG@ +DISTCHECK_PERFDHCP_CONFIGURE_FLAG = @DISTCHECK_PERFDHCP_CONFIGURE_FLAG@ +DISTCHECK_PGSQL_CONFIGURE_FLAG = @DISTCHECK_PGSQL_CONFIGURE_FLAG@ +DISTCHECK_PREMIUM_CONFIGURE_FLAG = @DISTCHECK_PREMIUM_CONFIGURE_FLAG@ +DISTCHECK_SYSREPOCPP_CONFIGURE_FLAG = @DISTCHECK_SYSREPOCPP_CONFIGURE_FLAG@ +DISTCHECK_SYSREPO_CONFIGURE_FLAG = @DISTCHECK_SYSREPO_CONFIGURE_FLAG@ +DLLTOOL = @DLLTOOL@ +DPKG = @DPKG@ +DPKGQUERY = @DPKGQUERY@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +ETAGS = @ETAGS@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +FILECMD = @FILECMD@ +GENHTML = @GENHTML@ +GREP = @GREP@ +GSSAPI_CFLAGS = @GSSAPI_CFLAGS@ +GSSAPI_LIBS = @GSSAPI_LIBS@ +GTEST_CONFIG = @GTEST_CONFIG@ +GTEST_INCLUDES = @GTEST_INCLUDES@ +GTEST_LDADD = @GTEST_LDADD@ +GTEST_LDFLAGS = @GTEST_LDFLAGS@ +GTEST_SOURCE = @GTEST_SOURCE@ +HAVE_NETCONF = @HAVE_NETCONF@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +KEA_CXXFLAGS = @KEA_CXXFLAGS@ +KEA_SRCID = @KEA_SRCID@ +KRB5_CONFIG = @KRB5_CONFIG@ +LCOV = @LCOV@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LEX = @LEX@ +LEXLIB = @LEXLIB@ +LEX_OUTPUT_ROOT = @LEX_OUTPUT_ROOT@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIBYANGCPP_CPPFLAGS = @LIBYANGCPP_CPPFLAGS@ +LIBYANGCPP_INCLUDEDIR = @LIBYANGCPP_INCLUDEDIR@ +LIBYANGCPP_LIBS = @LIBYANGCPP_LIBS@ +LIBYANGCPP_PREFIX = @LIBYANGCPP_PREFIX@ +LIBYANGCPP_VERSION = @LIBYANGCPP_VERSION@ +LIBYANG_CPPFLAGS = @LIBYANG_CPPFLAGS@ +LIBYANG_INCLUDEDIR = @LIBYANG_INCLUDEDIR@ +LIBYANG_LIBS = @LIBYANG_LIBS@ +LIBYANG_PREFIX = @LIBYANG_PREFIX@ +LIBYANG_VERSION = @LIBYANG_VERSION@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LOG4CPLUS_INCLUDES = @LOG4CPLUS_INCLUDES@ +LOG4CPLUS_LIBS = @LOG4CPLUS_LIBS@ +LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +MYSQL_CPPFLAGS = @MYSQL_CPPFLAGS@ +MYSQL_LIBS = @MYSQL_LIBS@ +NM = @NM@ +NMEDIT = @NMEDIT@ +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@ +PACKAGE_VERSION_TYPE = @PACKAGE_VERSION_TYPE@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +PDFLATEX = @PDFLATEX@ +PERL = @PERL@ +PGSQL_CPPFLAGS = @PGSQL_CPPFLAGS@ +PGSQL_LIBS = @PGSQL_LIBS@ +PKGPYTHONDIR = @PKGPYTHONDIR@ +PKG_CONFIG = @PKG_CONFIG@ +PLANTUML = @PLANTUML@ +PREMIUM_DIR = @PREMIUM_DIR@ +PYTHON = @PYTHON@ +PYTHON_EXEC_PREFIX = @PYTHON_EXEC_PREFIX@ +PYTHON_PLATFORM = @PYTHON_PLATFORM@ +PYTHON_PREFIX = @PYTHON_PREFIX@ +PYTHON_VERSION = @PYTHON_VERSION@ +RANLIB = @RANLIB@ +SED = @SED@ +SEP = @SEP@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +SPHINXBUILD = @SPHINXBUILD@ +SRPD_PLUGINS_PATH = @SRPD_PLUGINS_PATH@ +SR_PLUGINS_PATH = @SR_PLUGINS_PATH@ +SR_REPO_PATH = @SR_REPO_PATH@ +STRIP = @STRIP@ +SYSREPOCPP_CPPFLAGS = @SYSREPOCPP_CPPFLAGS@ +SYSREPOCPP_INCLUDEDIR = @SYSREPOCPP_INCLUDEDIR@ +SYSREPOCPP_LIBS = @SYSREPOCPP_LIBS@ +SYSREPOCPP_PREFIX = @SYSREPOCPP_PREFIX@ +SYSREPOCPP_VERSION = @SYSREPOCPP_VERSION@ +SYSREPO_CPPFLAGS = @SYSREPO_CPPFLAGS@ +SYSREPO_INCLUDEDIR = @SYSREPO_INCLUDEDIR@ +SYSREPO_LIBS = @SYSREPO_LIBS@ +SYSREPO_PREFIX = @SYSREPO_PREFIX@ +SYSREPO_VERSION = @SYSREPO_VERSION@ +USE_LCOV = @USE_LCOV@ +VALGRIND = @VALGRIND@ +VERSION = @VERSION@ +WARNING_GCC_44_STRICT_ALIASING_CFLAG = @WARNING_GCC_44_STRICT_ALIASING_CFLAG@ +XMLLINT = @XMLLINT@ +YACC = @YACC@ +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@ +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@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +pkgpyexecdir = @pkgpyexecdir@ +pkgpythondir = @pkgpythondir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +pyexecdir = @pyexecdir@ +pythondir = @pythondir@ +runstatedir = @runstatedir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +SUBDIRS = . +AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib \ + -I$(top_builddir)/src/hooks/dhcp/flex_option \ + -I$(top_srcdir)/src/hooks/dhcp/flex_option $(BOOST_INCLUDES) \ + -DFLEX_OPTION_LIB_SO=\"$(abs_top_builddir)/src/hooks/dhcp/flex_option/.libs/libdhcp_flex_option.so\" \ + -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\" +AM_CXXFLAGS = $(KEA_CXXFLAGS) +@USE_STATIC_LINK_TRUE@AM_LDFLAGS = -static + +# Unit test data files need to get installed. +EXTRA_DIST = +CLEANFILES = *.gcno *.gcda +TESTS_ENVIRONMENT = $(LIBTOOL) --mode=execute $(VALGRIND_COMMAND) +LOG_COMPILER = $(LIBTOOL) +AM_LOG_FLAGS = --mode=execute +@HAVE_GTEST_TRUE@flex_option_unittests_SOURCES = run_unittests.cc \ +@HAVE_GTEST_TRUE@ test_flex_option.h flex_option_unittests.cc \ +@HAVE_GTEST_TRUE@ sub_option_unittests.cc +@HAVE_GTEST_TRUE@flex_option_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES) +@HAVE_GTEST_TRUE@flex_option_unittests_LDFLAGS = $(AM_LDFLAGS) $(CRYPTO_LDFLAGS) $(GTEST_LDFLAGS) +@HAVE_GTEST_TRUE@flex_option_unittests_CXXFLAGS = $(AM_CXXFLAGS) +@HAVE_GTEST_TRUE@flex_option_unittests_LDADD = $(top_builddir)/src/hooks/dhcp/flex_option/libflex_option.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcpsrv/libkea-dhcpsrv.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/process/libkea-process.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/eval/libkea-eval.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcp_ddns/libkea-dhcp_ddns.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/stats/libkea-stats.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/config/libkea-cfgclient.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/http/libkea-http.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/hooks/libkea-hooks.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/database/libkea-database.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/cc/libkea-cc.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/asiolink/libkea-asiolink.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dns/libkea-dns++.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/cryptolink/libkea-cryptolink.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/log/libkea-log.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/util/libkea-util.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/exceptions/libkea-exceptions.la \ +@HAVE_GTEST_TRUE@ $(LOG4CPLUS_LIBS) $(CRYPTO_LIBS) \ +@HAVE_GTEST_TRUE@ $(BOOST_LIBS) $(GTEST_LDADD) +all: all-recursive + +.SUFFIXES: +.SUFFIXES: .cc .lo .o .obj +$(srcdir)/Makefile.in: $(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/hooks/dhcp/flex_option/tests/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/hooks/dhcp/flex_option/tests/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: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-noinstPROGRAMS: + @list='$(noinst_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list + +flex_option_unittests$(EXEEXT): $(flex_option_unittests_OBJECTS) $(flex_option_unittests_DEPENDENCIES) $(EXTRA_flex_option_unittests_DEPENDENCIES) + @rm -f flex_option_unittests$(EXEEXT) + $(AM_V_CXXLD)$(flex_option_unittests_LINK) $(flex_option_unittests_OBJECTS) $(flex_option_unittests_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/flex_option_unittests-flex_option_unittests.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/flex_option_unittests-run_unittests.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/flex_option_unittests-sub_option_unittests.Po@am__quote@ # am--include-marker + +$(am__depfiles_remade): + @$(MKDIR_P) $(@D) + @echo '# dummy' >$@-t && $(am__mv) $@-t $@ + +am--depfiles: $(am__depfiles_remade) + +.cc.o: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ $< + +.cc.obj: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.cc.lo: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(LTCXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(LTCXXCOMPILE) -c -o $@ $< + +flex_option_unittests-run_unittests.o: run_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -MT flex_option_unittests-run_unittests.o -MD -MP -MF $(DEPDIR)/flex_option_unittests-run_unittests.Tpo -c -o flex_option_unittests-run_unittests.o `test -f 'run_unittests.cc' || echo '$(srcdir)/'`run_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/flex_option_unittests-run_unittests.Tpo $(DEPDIR)/flex_option_unittests-run_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='run_unittests.cc' object='flex_option_unittests-run_unittests.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -c -o flex_option_unittests-run_unittests.o `test -f 'run_unittests.cc' || echo '$(srcdir)/'`run_unittests.cc + +flex_option_unittests-run_unittests.obj: run_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -MT flex_option_unittests-run_unittests.obj -MD -MP -MF $(DEPDIR)/flex_option_unittests-run_unittests.Tpo -c -o flex_option_unittests-run_unittests.obj `if test -f 'run_unittests.cc'; then $(CYGPATH_W) 'run_unittests.cc'; else $(CYGPATH_W) '$(srcdir)/run_unittests.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/flex_option_unittests-run_unittests.Tpo $(DEPDIR)/flex_option_unittests-run_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='run_unittests.cc' object='flex_option_unittests-run_unittests.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -c -o flex_option_unittests-run_unittests.obj `if test -f 'run_unittests.cc'; then $(CYGPATH_W) 'run_unittests.cc'; else $(CYGPATH_W) '$(srcdir)/run_unittests.cc'; fi` + +flex_option_unittests-flex_option_unittests.o: flex_option_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -MT flex_option_unittests-flex_option_unittests.o -MD -MP -MF $(DEPDIR)/flex_option_unittests-flex_option_unittests.Tpo -c -o flex_option_unittests-flex_option_unittests.o `test -f 'flex_option_unittests.cc' || echo '$(srcdir)/'`flex_option_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/flex_option_unittests-flex_option_unittests.Tpo $(DEPDIR)/flex_option_unittests-flex_option_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='flex_option_unittests.cc' object='flex_option_unittests-flex_option_unittests.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -c -o flex_option_unittests-flex_option_unittests.o `test -f 'flex_option_unittests.cc' || echo '$(srcdir)/'`flex_option_unittests.cc + +flex_option_unittests-flex_option_unittests.obj: flex_option_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -MT flex_option_unittests-flex_option_unittests.obj -MD -MP -MF $(DEPDIR)/flex_option_unittests-flex_option_unittests.Tpo -c -o flex_option_unittests-flex_option_unittests.obj `if test -f 'flex_option_unittests.cc'; then $(CYGPATH_W) 'flex_option_unittests.cc'; else $(CYGPATH_W) '$(srcdir)/flex_option_unittests.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/flex_option_unittests-flex_option_unittests.Tpo $(DEPDIR)/flex_option_unittests-flex_option_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='flex_option_unittests.cc' object='flex_option_unittests-flex_option_unittests.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -c -o flex_option_unittests-flex_option_unittests.obj `if test -f 'flex_option_unittests.cc'; then $(CYGPATH_W) 'flex_option_unittests.cc'; else $(CYGPATH_W) '$(srcdir)/flex_option_unittests.cc'; fi` + +flex_option_unittests-sub_option_unittests.o: sub_option_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -MT flex_option_unittests-sub_option_unittests.o -MD -MP -MF $(DEPDIR)/flex_option_unittests-sub_option_unittests.Tpo -c -o flex_option_unittests-sub_option_unittests.o `test -f 'sub_option_unittests.cc' || echo '$(srcdir)/'`sub_option_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/flex_option_unittests-sub_option_unittests.Tpo $(DEPDIR)/flex_option_unittests-sub_option_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='sub_option_unittests.cc' object='flex_option_unittests-sub_option_unittests.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -c -o flex_option_unittests-sub_option_unittests.o `test -f 'sub_option_unittests.cc' || echo '$(srcdir)/'`sub_option_unittests.cc + +flex_option_unittests-sub_option_unittests.obj: sub_option_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -MT flex_option_unittests-sub_option_unittests.obj -MD -MP -MF $(DEPDIR)/flex_option_unittests-sub_option_unittests.Tpo -c -o flex_option_unittests-sub_option_unittests.obj `if test -f 'sub_option_unittests.cc'; then $(CYGPATH_W) 'sub_option_unittests.cc'; else $(CYGPATH_W) '$(srcdir)/sub_option_unittests.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/flex_option_unittests-sub_option_unittests.Tpo $(DEPDIR)/flex_option_unittests-sub_option_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='sub_option_unittests.cc' object='flex_option_unittests-sub_option_unittests.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(flex_option_unittests_CPPFLAGS) $(CPPFLAGS) $(flex_option_unittests_CXXFLAGS) $(CXXFLAGS) -c -o flex_option_unittests-sub_option_unittests.obj `if test -f 'sub_option_unittests.cc'; then $(CYGPATH_W) 'sub_option_unittests.cc'; else $(CYGPATH_W) '$(srcdir)/sub_option_unittests.cc'; fi` + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +# This directory's subdirectories are mostly independent; you can cd +# into them and run 'make' without going through this Makefile. +# To change the values of 'make' variables: instead of editing Makefiles, +# (1) if the variable is set in 'config.status', edit 'config.status' +# (which will cause the Makefiles to be regenerated when you run 'make'); +# (2) otherwise, pass the desired values on the 'make' command line. +$(am__recursive_targets): + @fail=; \ + if $(am__make_keepgoing); then \ + failcom='fail=yes'; \ + else \ + failcom='exit 1'; \ + fi; \ + dot_seen=no; \ + target=`echo $@ | sed s/-recursive//`; \ + case "$@" in \ + distclean-* | maintainer-clean-*) list='$(DIST_SUBDIRS)' ;; \ + *) list='$(SUBDIRS)' ;; \ + esac; \ + for subdir in $$list; do \ + echo "Making $$target in $$subdir"; \ + if test "$$subdir" = "."; then \ + dot_seen=yes; \ + local_target="$$target-am"; \ + else \ + local_target="$$target"; \ + fi; \ + ($(am__cd) $$subdir && $(MAKE) $(AM_MAKEFLAGS) $$local_target) \ + || eval $$failcom; \ + done; \ + if test "$$dot_seen" = "no"; then \ + $(MAKE) $(AM_MAKEFLAGS) "$$target-am" || exit 1; \ + fi; test -z "$$fail" + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-recursive +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + if ($(ETAGS) --etags-include --version) >/dev/null 2>&1; then \ + include_option=--etags-include; \ + empty_fix=.; \ + else \ + include_option=--include; \ + empty_fix=; \ + fi; \ + list='$(SUBDIRS)'; for subdir in $$list; do \ + if test "$$subdir" = .; then :; else \ + test ! -f $$subdir/TAGS || \ + set "$$@" "$$include_option=$$here/$$subdir/TAGS"; \ + fi; \ + done; \ + $(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-recursive + +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-recursive + +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 + +check-TESTS: $(TESTS) + @failed=0; all=0; xfail=0; xpass=0; skip=0; \ + srcdir=$(srcdir); export srcdir; \ + list=' $(TESTS) '; \ + $(am__tty_colors); \ + if test -n "$$list"; then \ + for tst in $$list; do \ + if test -f ./$$tst; then dir=./; \ + elif test -f $$tst; then dir=; \ + else dir="$(srcdir)/"; fi; \ + if $(TESTS_ENVIRONMENT) $${dir}$$tst $(AM_TESTS_FD_REDIRECT); then \ + all=`expr $$all + 1`; \ + case " $(XFAIL_TESTS) " in \ + *[\ \ ]$$tst[\ \ ]*) \ + xpass=`expr $$xpass + 1`; \ + failed=`expr $$failed + 1`; \ + col=$$red; res=XPASS; \ + ;; \ + *) \ + col=$$grn; res=PASS; \ + ;; \ + esac; \ + elif test $$? -ne 77; then \ + all=`expr $$all + 1`; \ + case " $(XFAIL_TESTS) " in \ + *[\ \ ]$$tst[\ \ ]*) \ + xfail=`expr $$xfail + 1`; \ + col=$$lgn; res=XFAIL; \ + ;; \ + *) \ + failed=`expr $$failed + 1`; \ + col=$$red; res=FAIL; \ + ;; \ + esac; \ + else \ + skip=`expr $$skip + 1`; \ + col=$$blu; res=SKIP; \ + fi; \ + echo "$${col}$$res$${std}: $$tst"; \ + done; \ + if test "$$all" -eq 1; then \ + tests="test"; \ + All=""; \ + else \ + tests="tests"; \ + All="All "; \ + fi; \ + if test "$$failed" -eq 0; then \ + if test "$$xfail" -eq 0; then \ + banner="$$All$$all $$tests passed"; \ + else \ + if test "$$xfail" -eq 1; then failures=failure; else failures=failures; fi; \ + banner="$$All$$all $$tests behaved as expected ($$xfail expected $$failures)"; \ + fi; \ + else \ + if test "$$xpass" -eq 0; then \ + banner="$$failed of $$all $$tests failed"; \ + else \ + if test "$$xpass" -eq 1; then passes=pass; else passes=passes; fi; \ + banner="$$failed of $$all $$tests did not behave as expected ($$xpass unexpected $$passes)"; \ + fi; \ + fi; \ + dashes="$$banner"; \ + skipped=""; \ + if test "$$skip" -ne 0; then \ + if test "$$skip" -eq 1; then \ + skipped="($$skip test was not run)"; \ + else \ + skipped="($$skip tests were not run)"; \ + fi; \ + test `echo "$$skipped" | wc -c` -le `echo "$$banner" | wc -c` || \ + dashes="$$skipped"; \ + fi; \ + report=""; \ + if test "$$failed" -ne 0 && test -n "$(PACKAGE_BUGREPORT)"; then \ + report="Please report to $(PACKAGE_BUGREPORT)"; \ + test `echo "$$report" | wc -c` -le `echo "$$banner" | wc -c` || \ + dashes="$$report"; \ + fi; \ + dashes=`echo "$$dashes" | sed s/./=/g`; \ + if test "$$failed" -eq 0; then \ + col="$$grn"; \ + else \ + col="$$red"; \ + fi; \ + echo "$${col}$$dashes$${std}"; \ + echo "$${col}$$banner$${std}"; \ + test -z "$$skipped" || echo "$${col}$$skipped$${std}"; \ + test -z "$$report" || echo "$${col}$$report$${std}"; \ + echo "$${col}$$dashes$${std}"; \ + test "$$failed" -eq 0; \ + else :; fi +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 + @list='$(DIST_SUBDIRS)'; for subdir in $$list; do \ + if test "$$subdir" = .; then :; else \ + $(am__make_dryrun) \ + || test -d "$(distdir)/$$subdir" \ + || $(MKDIR_P) "$(distdir)/$$subdir" \ + || exit 1; \ + dir1=$$subdir; dir2="$(distdir)/$$subdir"; \ + $(am__relativize); \ + new_distdir=$$reldir; \ + dir1=$$subdir; dir2="$(top_distdir)"; \ + $(am__relativize); \ + new_top_distdir=$$reldir; \ + echo " (cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) top_distdir="$$new_top_distdir" distdir="$$new_distdir" \\"; \ + echo " am__remove_distdir=: am__skip_length_check=: am__skip_mode_fix=: distdir)"; \ + ($(am__cd) $$subdir && \ + $(MAKE) $(AM_MAKEFLAGS) \ + top_distdir="$$new_top_distdir" \ + distdir="$$new_distdir" \ + am__remove_distdir=: \ + am__skip_length_check=: \ + am__skip_mode_fix=: \ + distdir) \ + || exit 1; \ + fi; \ + done +check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) check-TESTS +check: check-recursive +all-am: Makefile $(PROGRAMS) +installdirs: installdirs-recursive +installdirs-am: +install: install-recursive +install-exec: install-exec-recursive +install-data: install-data-recursive +uninstall: uninstall-recursive + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-recursive +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: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) + +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-recursive + +clean-am: clean-generic clean-libtool clean-noinstPROGRAMS \ + mostlyclean-am + +distclean: distclean-recursive + -rm -f ./$(DEPDIR)/flex_option_unittests-flex_option_unittests.Po + -rm -f ./$(DEPDIR)/flex_option_unittests-run_unittests.Po + -rm -f ./$(DEPDIR)/flex_option_unittests-sub_option_unittests.Po + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-recursive + +dvi-am: + +html: html-recursive + +html-am: + +info: info-recursive + +info-am: + +install-data-am: + +install-dvi: install-dvi-recursive + +install-dvi-am: + +install-exec-am: + +install-html: install-html-recursive + +install-html-am: + +install-info: install-info-recursive + +install-info-am: + +install-man: + +install-pdf: install-pdf-recursive + +install-pdf-am: + +install-ps: install-ps-recursive + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-recursive + -rm -f ./$(DEPDIR)/flex_option_unittests-flex_option_unittests.Po + -rm -f ./$(DEPDIR)/flex_option_unittests-run_unittests.Po + -rm -f ./$(DEPDIR)/flex_option_unittests-sub_option_unittests.Po + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-recursive + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-recursive + +pdf-am: + +ps: ps-recursive + +ps-am: + +uninstall-am: + +.MAKE: $(am__recursive_targets) check-am install-am install-strip + +.PHONY: $(am__recursive_targets) CTAGS GTAGS TAGS all all-am \ + am--depfiles check check-TESTS check-am clean clean-generic \ + clean-libtool clean-noinstPROGRAMS 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-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs installdirs-am maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am + +.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/hooks/dhcp/flex_option/tests/flex_option_unittests.cc b/src/hooks/dhcp/flex_option/tests/flex_option_unittests.cc new file mode 100644 index 0000000..c5149ba --- /dev/null +++ b/src/hooks/dhcp/flex_option/tests/flex_option_unittests.cc @@ -0,0 +1,1586 @@ +// Copyright (C) 2019-2022 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +/// @file This file contains tests which verify flexible option. + +#include <config.h> +#include <flex_option.h> +#include <flex_option_log.h> +#include <dhcp/option_custom.h> +#include <dhcp/option_string.h> +#include <dhcp/libdhcp++.h> +#include <dhcpsrv/cfgmgr.h> +#include <eval/eval_context.h> +#include <hooks/callout_manager.h> +#include <hooks/hooks.h> + +#include <tests/test_flex_option.h> +#include <gtest/gtest.h> +#include <sstream> + +using namespace std; +using namespace isc; +using namespace isc::data; +using namespace isc::dhcp; +using namespace isc::eval; +using namespace isc::hooks; +using namespace isc::flex_option; +using namespace isc::flex_option::test; + +namespace { + +/// @brief Test fixture for testing the Flex Option library. +class FlexOptionTest : public BaseFlexOptionTest { }; + +// Verify that the configuration must exist. +TEST_F(FlexOptionTest, noConfig) { + ElementPtr options; + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'options' parameter is mandatory", impl_->getErrMsg()); +} + +// Verify that the configuration must be a list. +TEST_F(FlexOptionTest, configNotList) { + ElementPtr options = Element::createMap(); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'options' parameter must be a list", impl_->getErrMsg()); +} + +// Verify that the configuration can be the empty list. +TEST_F(FlexOptionTest, configEmpty) { + ElementPtr options = Element::createList(); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + EXPECT_TRUE(impl_->getOptionConfigMap().empty()); +} + +// Verify that an option configuration must exist. +TEST_F(FlexOptionTest, noOptionConfig) { + ElementPtr options = Element::createList(); + ElementPtr option; + options->add(option); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("null option element", impl_->getErrMsg()); +} + +// Verify that an option configuration must be a map. +TEST_F(FlexOptionTest, optionConfigNotMap) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createList(); + options->add(option); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("option element is not a map", impl_->getErrMsg()); +} + +// Verify that an unknown option keyword is rejected. +TEST_F(FlexOptionTest, optionConfigUnknown) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr unknown = Element::create(string("'ab'")); + // The right keyword is remove... + option->set("delete", unknown); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("unknown parameter 'delete'", impl_->getErrMsg()); +} + +// Verify that an option configuration must have code or name. +TEST_F(FlexOptionTest, optionConfigNoCodeName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + ostringstream errmsg; + errmsg << "'code' or 'name' must be specified: " << option->str(); + EXPECT_EQ(errmsg.str(), impl_->getErrMsg()); +} + +// Verify that the v4 option code must be in [1..254]. +TEST_F(FlexOptionTest, optionConfigBadCode4) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(false); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'code' must be an integer: false", impl_->getErrMsg()); + + code = Element::create(-1); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value -1 not in [0..255]", impl_->getErrMsg()); + + code = Element::create(DHO_PAD); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("invalid 'code' value 0: reserved for PAD", impl_->getErrMsg()); + + code = Element::create(DHO_END); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("invalid 'code' value 255: reserved for END", impl_->getErrMsg()); + + code = Element::create(256); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value 256 not in [0..255]", impl_->getErrMsg()); + + code = Element::create(1); + option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + code = Element::create(254); + option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); +} + +// Verify that the v6 option code must be in [1..65535]. +TEST_F(FlexOptionTest, optionConfigBadCode6) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(false); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'code' must be an integer: false", impl_->getErrMsg()); + + code = Element::create(-1); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value -1 not in [0..65535]", impl_->getErrMsg()); + + code = Element::create(0); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("invalid 'code' value 0: reserved", impl_->getErrMsg()); + + code = Element::create(65536); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value 65536 not in [0..65535]", impl_->getErrMsg()); + + code = Element::create(1); + option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + code = Element::create(65535); + option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); +} + +// Verify that the space must be a string. +TEST_F(FlexOptionTest, optionConfigBadSpace) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr space = Element::create(true); + option->set("space", space); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'space' must be a string: true", impl_->getErrMsg()); +} + +// Verify that the space must be valid. +TEST_F(FlexOptionTest, optionConfigInvalidSpace) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr space = Element::create(string("-bad-")); + option->set("space", space); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'-bad-' is not a valid space name", impl_->getErrMsg()); +} + +// Verify that the name must be a string. +TEST_F(FlexOptionTest, optionConfigBadName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr name = Element::create(true); + option->set("name", name); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'name' must be a string: true", impl_->getErrMsg()); +} + +// Verify that the name must not be empty. +TEST_F(FlexOptionTest, optionConfigEmptyName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr name = Element::create(string()); + option->set("name", name); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'name' must not be empty", impl_->getErrMsg()); +} + +// Verify that the name must be a known option. +TEST_F(FlexOptionTest, optionConfigUnknownName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr name = Element::create(string("foobar")); + option->set("name", name); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("no known 'foobar' option in 'dhcp4' space", impl_->getErrMsg()); +} + +// Verify that the space must be a known space. +TEST_F(FlexOptionTest, optionConfigUnknownSpace) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr name = Element::create(string("host-name")); + option->set("name", name); + ElementPtr space = Element::create(string("foobar")); + option->set("space", space); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("no known 'host-name' option in 'foobar' space", + impl_->getErrMsg()); +} + +// Verify that the definition is not required when csv-format is not specified. +TEST_F(FlexOptionTest, optionConfigUnknownCodeNoCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(109); + option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + EXPECT_EQ(1, map.count(109)); + auto opt_lst = map[109]; + EXPECT_EQ(1, opt_lst.size()); +} + +// Verify that the definition is not required when csv-format is false. +TEST_F(FlexOptionTest, optionConfigUnknownCodeDisableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(109); + option->set("code", code); + // Disable csv-format. + option->set("csv-format", Element::create(false)); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + EXPECT_EQ(1, map.count(109)); + auto opt_lst = map[109]; + EXPECT_EQ(1, opt_lst.size()); +} + +// Verify that the code must be a known option when csv-format is true. +TEST_F(FlexOptionTest, optionConfigUnknownCodeEnableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(109); + option->set("code", code); + // Enable csv-format. + option->set("csv-format", Element::create(true)); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("no known option with code '109' in 'dhcp4' space", impl_->getErrMsg()); +} + +// Verify that the name can be a standard option. +TEST_F(FlexOptionTest, optionConfigStandardName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr name = Element::create(string("host-name")); + option->set("name", name); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + EXPECT_EQ(1, map.count(DHO_HOST_NAME)); + auto opt_lst = map[DHO_HOST_NAME]; + EXPECT_EQ(1, opt_lst.size()); +} + +// Verify that the name can be an user defined option. +TEST_F(FlexOptionTest, optionConfigDefinedName) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-option", 222, + DHCP4_OPTION_SPACE, "string")); + defs.addItem(def); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + option->set("name", name); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + EXPECT_EQ(1, map.count(222)); + auto opt_lst = map[222]; + EXPECT_EQ(1, opt_lst.size()); +} + +// Verify that the name can be an user defined option in a custom space. +TEST_F(FlexOptionTest, optionConfigDefinedSpace) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-option", 222, + "my-space", "string")); + defs.addItem(def); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + option->set("name", name); + ElementPtr space = Element::create(string("my-space")); + option->set("space", space); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + EXPECT_EQ(1, map.count(222)); + auto opt_lst = map[222]; + EXPECT_EQ(1, opt_lst.size()); +} + +// Last resort is only option 43... + +// Verify that the name must match the code. +TEST_F(FlexOptionTest, optionConfigCodeNameMismatch) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(DHO_HOST_NAME + 1); + option->set("code", code); + ElementPtr name = Element::create(string("host-name")); + option->set("name", name); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "option 'host-name' is defined as code: 12, "; + expected += "not the specified code: 13"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that the csv-format must be a boolean. +TEST_F(FlexOptionTest, optionConfigBadCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr csv_format = Element::create(123); + option->set("csv-format", csv_format); + ElementPtr code = Element::create(12); + option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'csv-format' must be a boolean: 123", impl_->getErrMsg()); +} + +// Verify that an option can be configured more than once. +TEST_F(FlexOptionTest, optionConfigTwice) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr add = Element::create(string("'ab'")); + option->set("add", add); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + + // Add it a second time. + options->add(option); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + EXPECT_EQ(1, map.count(DHO_HOST_NAME)); + auto opt_lst = map[DHO_HOST_NAME]; + EXPECT_EQ(2, opt_lst.size()); +} + +// Verify that the add value must be a string. +TEST_F(FlexOptionTest, optionConfigAddNotString) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr add = Element::create(true); + option->set("add", add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'add' must be a string: true", impl_->getErrMsg()); +} + +// Verify that the add value must not be empty. +TEST_F(FlexOptionTest, optionConfigEmptyAdd) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr add = Element::create(string()); + option->set("add", add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'add' must not be empty", impl_->getErrMsg()); +} + +// Verify that the add value must parse. +TEST_F(FlexOptionTest, optionConfigBadAdd) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr add = Element::create(string("ifelse('a','b','c')")); + option->set("add", add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "can't parse add expression [ifelse('a','b','c')] "; + expected += "error: <string>:1.11: syntax error, "; + expected += "unexpected \",\", expecting == or +"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that a valid v4 add value is accepted. +TEST_F(FlexOptionTest, optionConfigAdd4) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr add = Element::create(string("'abc'")); + option->set("add", add); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + FlexOptionImpl::OptionConfigList opt_lst; + ASSERT_NO_THROW(opt_lst = map.at(DHO_HOST_NAME)); + ASSERT_FALSE(opt_lst.empty()); + EXPECT_EQ(1, opt_lst.size()); + FlexOptionImpl::OptionConfigPtr opt_cfg; + ASSERT_NO_THROW(opt_cfg = opt_lst.front()); + + ASSERT_TRUE(opt_cfg); + EXPECT_EQ(DHO_HOST_NAME, opt_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::ADD, opt_cfg->getAction()); + EXPECT_EQ("'abc'", opt_cfg->getText()); + + ExpressionPtr expr = opt_cfg->getExpr(); + ASSERT_TRUE(expr); + ASSERT_EQ(1, expr->size()); + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + EXPECT_NO_THROW(expr->at(0)->evaluate(*pkt4, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("abc", values.top()); +} + +// Verify that a valid v6 add value is accepted. +TEST_F(FlexOptionTest, optionConfigAdd6) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr add = Element::create(string("'abc'")); + option->set("add", add); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + FlexOptionImpl::OptionConfigList opt_lst; + ASSERT_NO_THROW(opt_lst = map.at(D6O_BOOTFILE_URL)); + ASSERT_FALSE(opt_lst.empty()); + EXPECT_EQ(1, opt_lst.size()); + FlexOptionImpl::OptionConfigPtr opt_cfg; + ASSERT_NO_THROW(opt_cfg = opt_lst.front()); + + ASSERT_TRUE(opt_cfg); + EXPECT_EQ(D6O_BOOTFILE_URL, opt_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::ADD, opt_cfg->getAction()); + EXPECT_EQ("'abc'", opt_cfg->getText()); + + ExpressionPtr expr = opt_cfg->getExpr(); + ASSERT_TRUE(expr); + ASSERT_EQ(1, expr->size()); + Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, 12345)); + ValueStack values; + EXPECT_NO_THROW(expr->at(0)->evaluate(*pkt6, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("abc", values.top()); +} + +// Verify that the supersede value must be a string. +TEST_F(FlexOptionTest, optionConfigSupersedeNotString) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr supersede = Element::create(123); + option->set("supersede", supersede); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'supersede' must be a string: 123", impl_->getErrMsg()); +} + +// Verify that the supersede value must not be empty. +TEST_F(FlexOptionTest, optionConfigEmptySupersede) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr supersede = Element::create(string()); + option->set("supersede", supersede); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'supersede' must not be empty", impl_->getErrMsg()); +} + +// Verify that the supersede value must parse. +TEST_F(FlexOptionTest, optionConfigBadSupersede) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr supersede = Element::create(string("ifelse('a','b','c')")); + option->set("supersede", supersede); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "can't parse supersede expression [ifelse('a','b','c')] "; + expected += "error: <string>:1.11: syntax error, "; + expected += "unexpected \",\", expecting == or +"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that a valid v4 supersede value is accepted. +TEST_F(FlexOptionTest, optionConfigSupersede4) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr supersede = Element::create(string("'abc'")); + option->set("supersede", supersede); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + FlexOptionImpl::OptionConfigList opt_lst; + ASSERT_NO_THROW(opt_lst = map.at(DHO_HOST_NAME)); + ASSERT_FALSE(opt_lst.empty()); + EXPECT_EQ(1, opt_lst.size()); + FlexOptionImpl::OptionConfigPtr opt_cfg; + ASSERT_NO_THROW(opt_cfg = opt_lst.front()); + + ASSERT_TRUE(opt_cfg); + EXPECT_EQ(DHO_HOST_NAME, opt_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::SUPERSEDE, opt_cfg->getAction()); + EXPECT_EQ("'abc'", opt_cfg->getText()); + + ExpressionPtr expr = opt_cfg->getExpr(); + ASSERT_TRUE(expr); + ASSERT_EQ(1, expr->size()); + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + EXPECT_NO_THROW(expr->at(0)->evaluate(*pkt4, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("abc", values.top()); +} + +// Verify that a valid v6 supersede value is accepted. +TEST_F(FlexOptionTest, optionConfigSupersede6) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr supersede = Element::create(string("'abc'")); + option->set("supersede", supersede); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + FlexOptionImpl::OptionConfigList opt_lst; + ASSERT_NO_THROW(opt_lst = map.at(D6O_BOOTFILE_URL)); + ASSERT_FALSE(opt_lst.empty()); + EXPECT_EQ(1, opt_lst.size()); + FlexOptionImpl::OptionConfigPtr opt_cfg; + ASSERT_NO_THROW(opt_cfg = opt_lst.front()); + + ASSERT_TRUE(opt_cfg); + EXPECT_EQ(D6O_BOOTFILE_URL, opt_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::SUPERSEDE, opt_cfg->getAction()); + EXPECT_EQ("'abc'", opt_cfg->getText()); + + ExpressionPtr expr = opt_cfg->getExpr(); + ASSERT_TRUE(expr); + ASSERT_EQ(1, expr->size()); + Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, 12345)); + ValueStack values; + EXPECT_NO_THROW(expr->at(0)->evaluate(*pkt6, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("abc", values.top()); +} + +// Verify that the remove value must be a string. +TEST_F(FlexOptionTest, optionConfigRemoveNotString) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::createMap(); + option->set("remove", remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'remove' must be a string: { }", impl_->getErrMsg()); +} + +// Verify that the remove value must not be empty. +TEST_F(FlexOptionTest, optionConfigEmptyRemove) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::create(string()); + option->set("remove", remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'remove' must not be empty", impl_->getErrMsg()); +} + +// Verify that the remove value must parse. +TEST_F(FlexOptionTest, optionConfigBadRemove) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc'")); + option->set("remove", remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "can't parse remove expression ['abc'] error: "; + expected += "<string>:1.6: syntax error, unexpected end of file, "; + expected += "expecting == or +"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that a valid v4 remove value is accepted. +TEST_F(FlexOptionTest, optionConfigRemove4) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + FlexOptionImpl::OptionConfigList opt_lst; + ASSERT_NO_THROW(opt_lst = map.at(DHO_HOST_NAME)); + ASSERT_FALSE(opt_lst.empty()); + EXPECT_EQ(1, opt_lst.size()); + FlexOptionImpl::OptionConfigPtr opt_cfg; + ASSERT_NO_THROW(opt_cfg = opt_lst.front()); + + ASSERT_TRUE(opt_cfg); + EXPECT_EQ(DHO_HOST_NAME, opt_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::REMOVE, opt_cfg->getAction()); + EXPECT_EQ("'abc' == 'abc'", opt_cfg->getText()); + + ExpressionPtr expr = opt_cfg->getExpr(); + ASSERT_TRUE(expr); + ASSERT_EQ(3, expr->size()); + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + EXPECT_NO_THROW(expr->at(0)->evaluate(*pkt4, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("abc", values.top()); + EXPECT_NO_THROW(expr->at(1)->evaluate(*pkt4, values)); + ASSERT_EQ(2, values.size()); + EXPECT_NO_THROW(expr->at(2)->evaluate(*pkt4, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("true", values.top()); +} + +// Verify that a valid v6 remove value is accepted. +TEST_F(FlexOptionTest, optionConfigRemove6) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + FlexOptionImpl::OptionConfigList opt_lst; + ASSERT_NO_THROW(opt_lst = map.at(D6O_BOOTFILE_URL)); + ASSERT_FALSE(opt_lst.empty()); + EXPECT_EQ(1, opt_lst.size()); + FlexOptionImpl::OptionConfigPtr opt_cfg; + ASSERT_NO_THROW(opt_cfg = opt_lst.front()); + + ASSERT_TRUE(opt_cfg); + EXPECT_EQ(D6O_BOOTFILE_URL, opt_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::REMOVE, opt_cfg->getAction()); + EXPECT_EQ("'abc' == 'abc'", opt_cfg->getText()); + + ExpressionPtr expr = opt_cfg->getExpr(); + ASSERT_TRUE(expr); + ASSERT_EQ(3, expr->size()); + Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, 12345)); + ValueStack values; + EXPECT_NO_THROW(expr->at(0)->evaluate(*pkt6, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("abc", values.top()); + EXPECT_NO_THROW(expr->at(1)->evaluate(*pkt6, values)); + ASSERT_EQ(2, values.size()); + EXPECT_NO_THROW(expr->at(2)->evaluate(*pkt6, values)); + ASSERT_EQ(1, values.size()); + EXPECT_EQ("true", values.top()); +} + +// Verify that multiple actions are not accepted. +TEST_F(FlexOptionTest, optionConfigMultipleAction) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + + // add and supersede. + ElementPtr add = Element::create(string("'abc'")); + option->set("add", add); + ElementPtr supersede = Element::create(string("'abc'")); + option->set("supersede", supersede); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + ostringstream errmsg; + errmsg << "multiple actions: " << option->str(); + EXPECT_EQ(errmsg.str(), impl_->getErrMsg()); + + // supersede and remove. + option->remove("add"); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + errmsg.str(""); + errmsg << "multiple actions: " << option->str(); + EXPECT_EQ(errmsg.str(), impl_->getErrMsg()); + + // add and remove. + option->remove("supersede"); + option->set("add", add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + errmsg.str(""); + errmsg << "multiple actions: " << option->str(); + EXPECT_EQ(errmsg.str(), impl_->getErrMsg()); +} + +// Verify that multiple options are accepted. +TEST_F(FlexOptionTest, optionConfigList) { + ElementPtr options = Element::createList(); + + ElementPtr option1 = Element::createMap(); + options->add(option1); + ElementPtr code1 = Element::create(DHO_HOST_NAME); + option1->set("code", code1); + ElementPtr add1 = Element::create(string("'abc'")); + option1->set("add", add1); + + ElementPtr option2 = Element::createMap(); + options->add(option2); + ElementPtr code2 = Element::create(DHO_ROOT_PATH); + option2->set("code", code2); + ElementPtr supersede2 = Element::create(string("'/'")); + option2->set("supersede", supersede2); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + EXPECT_EQ(2, map.size()); + + FlexOptionImpl::OptionConfigList opt1_lst; + ASSERT_NO_THROW(opt1_lst = map.at(DHO_HOST_NAME)); + ASSERT_FALSE(opt1_lst.empty()); + EXPECT_EQ(1, opt1_lst.size()); + FlexOptionImpl::OptionConfigPtr opt1_cfg; + ASSERT_NO_THROW(opt1_cfg = opt1_lst.front()); + + ASSERT_TRUE(opt1_cfg); + EXPECT_EQ(DHO_HOST_NAME, opt1_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::ADD, opt1_cfg->getAction()); + EXPECT_EQ("'abc'", opt1_cfg->getText()); + + FlexOptionImpl::OptionConfigList opt2_lst; + ASSERT_NO_THROW(opt2_lst = map.at(DHO_ROOT_PATH)); + ASSERT_FALSE(opt2_lst.empty()); + EXPECT_EQ(1, opt2_lst.size()); + FlexOptionImpl::OptionConfigPtr opt2_cfg; + ASSERT_NO_THROW(opt2_cfg = opt2_lst.front()); + + ASSERT_TRUE(opt2_cfg); + EXPECT_EQ(DHO_ROOT_PATH, opt2_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::SUPERSEDE, opt2_cfg->getAction()); + EXPECT_EQ("'/'", opt2_cfg->getText()); +} + +// Verify that empty option config list does nothing. +TEST_F(FlexOptionTest, processEmpty) { + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); +} + +// Verify that NONE action really does nothing. +TEST_F(FlexOptionTest, processNone) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefinitionPtr def = LibDHCP::getOptionDef(DHCP6_OPTION_SPACE, D6O_BOOTFILE_URL); + FlexOptionImpl::OptionConfigPtr + opt_cfg(new FlexOptionImpl::OptionConfig(D6O_BOOTFILE_URL, def)); + EXPECT_EQ(FlexOptionImpl::NONE, opt_cfg->getAction()); + auto map = impl_->getMutableOptionConfigMap(); + auto& opt_lst = map[DHO_HOST_NAME]; + opt_lst.push_back(opt_cfg); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); +} + +// Verify that ADD action adds the specified option in csv format. +TEST_F(FlexOptionTest, processAddEnableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr add = Element::create(string("'abc'")); + option->set("add", add); + + option = Element::createMap(); + options->add(option); + code = Element::create(DHO_DOMAIN_SEARCH); + option->set("code", code); + add = Element::create(string("'example.com'")); + option->set("add", add); + // fqdn option data is parsed using option definition in csv format. + option->set("csv-format", Element::create(true)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + EXPECT_FALSE(response->getOption(DHO_DOMAIN_SEARCH)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_HOST_NAME); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_HOST_NAME, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); + + opt = response->getOption(DHO_DOMAIN_SEARCH); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_DOMAIN_SEARCH, opt->getType()); + const OptionBuffer& buffer_fqdn = opt->getData(); + ASSERT_EQ(13, buffer_fqdn.size()); + EXPECT_EQ(7, buffer_fqdn[0]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[1], "example", 7)); + EXPECT_EQ(3, buffer_fqdn[8]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[9], "com", 3)); + EXPECT_EQ(0, buffer_fqdn[12]); +} + +// Verify that ADD action adds the specified option in raw format. +TEST_F(FlexOptionTest, processAddDisableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr add = Element::create(string("'abc'")); + option->set("add", add); + + option = Element::createMap(); + options->add(option); + code = Element::create(DHO_DOMAIN_SEARCH); + option->set("code", code); + add = Element::create(string("0x076578616d706c6503636f6d00")); + option->set("add", add); + // fqdn option data is specified in raw format. + option->set("csv-format", Element::create(false)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + EXPECT_FALSE(response->getOption(DHO_DOMAIN_SEARCH)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_HOST_NAME); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_HOST_NAME, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); + + opt = response->getOption(DHO_DOMAIN_SEARCH); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_DOMAIN_SEARCH, opt->getType()); + const OptionBuffer& buffer_fqdn = opt->getData(); + ASSERT_EQ(13, buffer_fqdn.size()); + EXPECT_EQ(7, buffer_fqdn[0]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[1], "example", 7)); + EXPECT_EQ(3, buffer_fqdn[8]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[9], "com", 3)); + EXPECT_EQ(0, buffer_fqdn[12]); +} + +// Verify that ADD action does not add an already existing option. +TEST_F(FlexOptionTest, processAddExisting) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr add = Element::create(string("'abc'")); + option->set("add", add); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionStringPtr str(new OptionString(Option::V6, D6O_BOOTFILE_URL, "http")); + response->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_BOOTFILE_URL); + ASSERT_TRUE(opt); + EXPECT_EQ(D6O_BOOTFILE_URL, opt->getType()); + EXPECT_EQ("http", opt->toString()); +} + +// Verify that ADD action does not add an empty value. +TEST_F(FlexOptionTest, processAddEmpty) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr add = Element::create(string("''")); + option->set("add", add); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); +} + +// Verify that SUPERSEDE action supersedes the specified option in csv format. +TEST_F(FlexOptionTest, processSupersedeEnableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr supersede = Element::create(string("'abc'")); + option->set("supersede", supersede); + + option = Element::createMap(); + options->add(option); + code = Element::create(DHO_DOMAIN_SEARCH); + option->set("code", code); + supersede = Element::create(string("'example.com'")); + option->set("supersede", supersede); + // fqdn option data is parsed using option definition in csv format. + option->set("csv-format", Element::create(true)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + EXPECT_FALSE(response->getOption(DHO_DOMAIN_SEARCH)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_HOST_NAME); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_HOST_NAME, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); + + opt = response->getOption(DHO_DOMAIN_SEARCH); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_DOMAIN_SEARCH, opt->getType()); + const OptionBuffer& buffer_fqdn = opt->getData(); + ASSERT_EQ(13, buffer_fqdn.size()); + EXPECT_EQ(7, buffer_fqdn[0]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[1], "example", 7)); + EXPECT_EQ(3, buffer_fqdn[8]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[9], "com", 3)); + EXPECT_EQ(0, buffer_fqdn[12]); +} + +// Verify that SUPERSEDE action supersedes the specified option in raw format. +TEST_F(FlexOptionTest, processSupersedeDisableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr supersede = Element::create(string("'abc'")); + option->set("supersede", supersede); + + option = Element::createMap(); + options->add(option); + code = Element::create(DHO_DOMAIN_SEARCH); + option->set("code", code); + supersede = Element::create(string("0x076578616d706c6503636f6d00")); + option->set("supersede", supersede); + // fqdn option data is specified in raw format. + option->set("csv-format", Element::create(false)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + EXPECT_FALSE(response->getOption(DHO_DOMAIN_SEARCH)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_HOST_NAME); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_HOST_NAME, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); + + opt = response->getOption(DHO_DOMAIN_SEARCH); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_DOMAIN_SEARCH, opt->getType()); + const OptionBuffer& buffer_fqdn = opt->getData(); + ASSERT_EQ(13, buffer_fqdn.size()); + EXPECT_EQ(7, buffer_fqdn[0]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[1], "example", 7)); + EXPECT_EQ(3, buffer_fqdn[8]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[9], "com", 3)); + EXPECT_EQ(0, buffer_fqdn[12]); +} + +// Verify that SUPERSEDE action supersedes an already existing option. +TEST_F(FlexOptionTest, processSupersedeExisting) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr supersede = Element::create(string("0xabcdef")); + option->set("supersede", supersede); + + option = Element::createMap(); + options->add(option); + code = Element::create(D6O_DOMAIN_SEARCH); + option->set("code", code); + supersede = Element::create(string("'example.com'")); + option->set("supersede", supersede); + // fqdn option data is parsed using option definition in csv format. + option->set("csv-format", Element::create(true)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionStringPtr str(new OptionString(Option::V6, D6O_BOOTFILE_URL, "http")); + response->addOption(str); + OptionDefinition def("domain-name", D6O_DOMAIN_SEARCH, DHCP6_OPTION_SPACE, + OPT_FQDN_TYPE); + OptionCustomPtr option_domain_name(new OptionCustom(def, Option::V6)); + option_domain_name->writeFqdn("old.example.com"); + response->addOption(option_domain_name); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_BOOTFILE_URL); + ASSERT_TRUE(opt); + EXPECT_EQ(D6O_BOOTFILE_URL, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(3, buffer.size()); + uint8_t expected[] = { 0xab, 0xcd, 0xef }; + EXPECT_EQ(0, memcmp(&buffer[0], expected, 3)); + + opt = response->getOption(D6O_DOMAIN_SEARCH); + ASSERT_TRUE(opt); + EXPECT_EQ(D6O_DOMAIN_SEARCH, opt->getType()); + const OptionBuffer& buffer_fqdn = opt->getData(); + ASSERT_EQ(13, buffer_fqdn.size()); + EXPECT_EQ(7, buffer_fqdn[0]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[1], "example", 7)); + EXPECT_EQ(3, buffer_fqdn[8]); + EXPECT_EQ(0, memcmp(&buffer_fqdn[9], "com", 3)); + EXPECT_EQ(0, buffer_fqdn[12]); +} + +// Verify that SUPERSEDE action does not supersede an empty value. +TEST_F(FlexOptionTest, processSupersedeEmpty) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr supersede = Element::create(string("''")); + option->set("supersede", supersede); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + + // Empty value does not remove existing values. + OptionStringPtr str(new OptionString(Option::V4, DHO_HOST_NAME, "abc")); + response->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_HOST_NAME); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_HOST_NAME, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); +} + +// Verify that SUPERSEDE if exists + ADD adds a not yet existing option. +TEST_F(FlexOptionTest, processSupersedeAddNotExisting) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option1 = Element::createMap(); + options->add(option1); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option1->set("code", code); + string action = "ifelse(option[bootfile-url].exists,'supersede','')"; + ElementPtr supersede = Element::create(action); + option1->set("supersede", supersede); + ElementPtr option2 = Element::createMap(); + options->add(option2); + option2->set("code", code); + ElementPtr add = Element::create(string("'add'")); + option2->set("add", add); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + EXPECT_FALSE(response->getOption(D6O_BOOTFILE_URL)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_BOOTFILE_URL); + ASSERT_TRUE(opt); + EXPECT_EQ(D6O_BOOTFILE_URL, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "add", 3)); +} + +// Verify that SUPERSEDE if exists + ADD supersedes an existing option. +TEST_F(FlexOptionTest, processSupersedeAddExisting) { + ElementPtr options = Element::createList(); + ElementPtr option1 = Element::createMap(); + options->add(option1); + ElementPtr code = Element::create(DHO_HOST_NAME); + option1->set("code", code); + string action = "ifelse(option[host-name].exists,'supersede','')"; + ElementPtr supersede = Element::create(action); + option1->set("supersede", supersede); + ElementPtr option2 = Element::createMap(); + options->add(option2); + option2->set("code", code); + ElementPtr add = Element::create(string("'add'")); + option2->set("add", add); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionStringPtr str(new OptionString(Option::V4, DHO_HOST_NAME, "foobar")); + // Be careful here: the expression is related to the query. + query->addOption(str); + response->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_HOST_NAME); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_HOST_NAME, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(9, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "supersede", 9)); +} + +// Verify that REMOVE action removes an already existing option. +TEST_F(FlexOptionTest, processRemove) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionStringPtr str(new OptionString(Option::V6, D6O_BOOTFILE_URL, "http")); + response->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_FALSE(response->getOption(D6O_BOOTFILE_URL)); +} + +// Verify that REMOVE action does nothing if the option is not present. +TEST_F(FlexOptionTest, processRemoveNoOption) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(DHO_HOST_NAME)); +} + +// Verify that REMOVE action does nothing when the expression evaluates to false. +TEST_F(FlexOptionTest, processRemoveFalse) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'xyz'")); + option->set("remove", remove); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionStringPtr str(new OptionString(Option::V6, D6O_BOOTFILE_URL, "http")); + response->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_TRUE(response->getOption(D6O_BOOTFILE_URL)); +} + +// A more complex check... +TEST_F(FlexOptionTest, processFullTest) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_BOOT_FILE_NAME); + option->set("code", code); + string expr = "ifelse(option[host-name].exists,"; + expr += "concat(option[host-name].text,'.boot'),'')"; + ElementPtr add = Element::create(expr); + option->set("add", add); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionStringPtr str(new OptionString(Option::V4, DHO_HOST_NAME, "foo")); + query->addOption(str); + EXPECT_FALSE(response->getOption(DHO_BOOT_FILE_NAME)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_BOOT_FILE_NAME); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_BOOT_FILE_NAME, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + ASSERT_EQ(8, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foo.boot", 8)); +} + +// Verify that complex strings with escaped characters are properly parsed on add. +TEST_F(FlexOptionTest, processFullAddWithComplexString) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_NEW_POSIX_TIMEZONE); + option->set("code", code); + string expr = "ifelse(option[39].exists,'EST5EDT4\\,M3.2.0/02:00\\,M11.1.0/02:00','')"; + ElementPtr add = Element::create(expr); + option->set("add", add); + // strings with escape characters are parsed in csv format. + option->set("csv-format", Element::create(true)); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionDefinitionPtr def = isc::dhcp::LibDHCP::getOptionDef(DHCP6_OPTION_SPACE, D6O_CLIENT_FQDN); + OptionCustomPtr str(new OptionCustom(*def, Option::V6)); + query->addOption(str); + EXPECT_FALSE(response->getOption(D6O_NEW_POSIX_TIMEZONE)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_NEW_POSIX_TIMEZONE); + ASSERT_TRUE(opt); + EXPECT_EQ(D6O_NEW_POSIX_TIMEZONE, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + EXPECT_EQ(35, buffer.size()); + std::string data("EST5EDT4,M3.2.0/02:00,M11.1.0/02:00"); + EXPECT_EQ(0, memcmp(&buffer[0], &data[0], buffer.size())); +} + +// Verify that complex strings with escaped characters are properly parsed on supersede. +TEST_F(FlexOptionTest, processFullSupersedeWithComplexString) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_NEW_POSIX_TIMEZONE); + option->set("code", code); + string expr = "ifelse(option[39].exists,'EST5EDT4\\,M3.2.0/02:00\\,M11.1.0/02:00','')"; + ElementPtr supersede = Element::create(expr); + option->set("supersede", supersede); + // strings with escape characters are parsed in csv format. + option->set("csv-format", Element::create(true)); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionDefinitionPtr def = isc::dhcp::LibDHCP::getOptionDef(DHCP6_OPTION_SPACE, D6O_CLIENT_FQDN); + OptionCustomPtr str(new OptionCustom(*def, Option::V6)); + query->addOption(str); + EXPECT_FALSE(response->getOption(D6O_NEW_POSIX_TIMEZONE)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_NEW_POSIX_TIMEZONE); + ASSERT_TRUE(opt); + EXPECT_EQ(D6O_NEW_POSIX_TIMEZONE, opt->getType()); + const OptionBuffer& buffer = opt->getData(); + EXPECT_EQ(35, buffer.size()); + std::string data("EST5EDT4,M3.2.0/02:00,M11.1.0/02:00"); + EXPECT_EQ(0, memcmp(&buffer[0], &data[0], buffer.size())); +} + +// Verify that the client class must be a string. +TEST_F(FlexOptionTest, optionConfigBadClass) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + option->set("client-class", Element::create(true)); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'client-class' must be a string: true", impl_->getErrMsg()); +} + +// Verify that a valid client class is accepted. +TEST_F(FlexOptionTest, optionConfigGuardValid) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + option->set("client-class", Element::create(string("foobar"))); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getOptionConfigMap(); + FlexOptionImpl::OptionConfigList opt_lst; + ASSERT_NO_THROW(opt_lst = map.at(DHO_HOST_NAME)); + ASSERT_FALSE(opt_lst.empty()); + EXPECT_EQ(1, opt_lst.size()); + FlexOptionImpl::OptionConfigPtr opt_cfg; + ASSERT_NO_THROW(opt_cfg = opt_lst.front()); + + ASSERT_TRUE(opt_cfg); + EXPECT_EQ(DHO_HOST_NAME, opt_cfg->getCode()); + EXPECT_EQ("foobar", opt_cfg->getClass()); +} + +// Verify that a guarded action is skipped when query does not belong to the +// client class. +TEST_F(FlexOptionTest, optionConfigGuardNoMatch) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + option->set("client-class", Element::create(string("foobar"))); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionStringPtr str(new OptionString(Option::V4, DHO_HOST_NAME, "foo")); + response->addOption(str); + EXPECT_TRUE(response->getOption(DHO_HOST_NAME)); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_TRUE(response->getOption(DHO_HOST_NAME)); +} + +// Verify that a guarded action is applied when query belongs to the class. +TEST_F(FlexOptionTest, optionConfigGuardMatch) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_BOOTFILE_URL); + option->set("code", code); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + option->set("remove", remove); + option->set("client-class", Element::create(string("foobar"))); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + query->addClass("foobar"); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + string response_txt = response->toText(); + OptionStringPtr str(new OptionString(Option::V6, D6O_BOOTFILE_URL, "http")); + response->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(D6O_BOOTFILE_URL)); +} + +} // end of anonymous namespace diff --git a/src/hooks/dhcp/flex_option/tests/run_unittests.cc b/src/hooks/dhcp/flex_option/tests/run_unittests.cc new file mode 100644 index 0000000..5805b42 --- /dev/null +++ b/src/hooks/dhcp/flex_option/tests/run_unittests.cc @@ -0,0 +1,19 @@ +// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include <config.h> + +#include <log/logger_support.h> +#include <gtest/gtest.h> + +int +main(int argc, char* argv[]) { + ::testing::InitGoogleTest(&argc, argv); + isc::log::initLogger(); + int result = RUN_ALL_TESTS(); + + return (result); +} diff --git a/src/hooks/dhcp/flex_option/tests/sub_option_unittests.cc b/src/hooks/dhcp/flex_option/tests/sub_option_unittests.cc new file mode 100644 index 0000000..e593239 --- /dev/null +++ b/src/hooks/dhcp/flex_option/tests/sub_option_unittests.cc @@ -0,0 +1,3173 @@ +// Copyright (C) 2019-2022 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +/// @file This file contains tests which verify flexible sub-option. + +#include <config.h> +#include <flex_option.h> +#include <flex_option_log.h> +#include <dhcp/docsis3_option_defs.h> +#include <dhcp/libdhcp++.h> +#include <dhcp/option4_addrlst.h> +#include <dhcp/option6_addrlst.h> +#include <dhcp/option_custom.h> +#include <dhcp/option_string.h> +#include <dhcpsrv/cfgmgr.h> +#include <eval/eval_context.h> +#include <hooks/callout_manager.h> +#include <hooks/hooks.h> + +#include <tests/test_flex_option.h> +#include <gtest/gtest.h> +#include <sstream> + +using namespace std; +using namespace isc; +using namespace isc::asiolink; +using namespace isc::data; +using namespace isc::dhcp; +using namespace isc::eval; +using namespace isc::hooks; +using namespace isc::flex_option; +using namespace isc::flex_option::test; + +namespace { + +/// @brief Test fixture for testing the Flex Option library. +class FlexSubOptionTest : public BaseFlexOptionTest { }; + +// Verify that the sub-options configuration must be a list. +TEST_F(FlexSubOptionTest, configNotList) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createMap(); + option->set("sub-options", sub_options); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'sub-options' must be a list: { }", + impl_->getErrMsg()); +} + +// Verify that the sub-options configuration can be the empty list. +TEST_F(FlexSubOptionTest, configEmpty) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + EXPECT_TRUE(impl_->getOptionConfigMap().empty()); + EXPECT_TRUE(impl_->getSubOptionConfigMap().empty()); +} + +// Verify that a sub-option configuration must exist. +TEST_F(FlexSubOptionTest, noSubOptionConfig) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option; + sub_options->add(sub_option); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("null sub-option element", impl_->getErrMsg()); +} + +// Verify that a sub-option configuration must be a map. +TEST_F(FlexSubOptionTest, subOptionConfigNotMap) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createList(); + sub_options->add(sub_option); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("sub-option element is not a map", impl_->getErrMsg()); +} + +// Verify that multiple actions are not accepted. +TEST_F(FlexSubOptionTest, configMultipleAction) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_HOST_NAME); + option->set("code", code); + + // add and sub-options. + ElementPtr add = Element::create(string("'abc'")); + option->set("add", add); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + + EXPECT_THROW(impl_->testConfigure(options), BadValue); + ostringstream errmsg; + errmsg << "'sub-options' and 'add' are incompatible in the same entry"; + EXPECT_EQ(errmsg.str(), impl_->getErrMsg()); +} + +// Verify that an unknown option keyword is rejected. +TEST_F(FlexSubOptionTest, subOptionConfigUnknown) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr unknown = Element::create(string("'ab'")); + // The right keyword is remove... + sub_option->set("delete", unknown); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("unknown parameter 'delete'", impl_->getErrMsg()); +} + +// Verify that a sub-option configuration must have code or name. +TEST_F(FlexSubOptionTest, subOptionConfigNoCodeName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + ostringstream errmsg; + errmsg << "'code' or 'name' must be specified: " << sub_option->str(); + EXPECT_EQ(errmsg.str(), impl_->getErrMsg()); +} + +// Verify that a sub-option configuration must retrieve a space. +TEST_F(FlexSubOptionTest, subOptionConfigNoSpace) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("container is not defined: can't get space", impl_->getErrMsg()); +} + +// Verify that the v4 sub-option code must be an integer in [0..255]. +TEST_F(FlexSubOptionTest, subOptionConfigBadCode4) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + code = Element::create(false); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'code' must be an integer: false", impl_->getErrMsg()); + + code = Element::create(-1); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value -1 not in [0..255]", impl_->getErrMsg()); + + code = Element::create(256); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value 256 not in [0..255]", impl_->getErrMsg()); + + code = Element::create(1); + sub_option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + code = Element::create(254); + sub_option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); +} + +// Verify that the v6 option code must be an integer in [0..65535]. +TEST_F(FlexSubOptionTest, subOptionConfigBadCode6) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + code = Element::create(false); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'code' must be an integer: false", impl_->getErrMsg()); + + code = Element::create(-1); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value -1 not in [0..65535]", impl_->getErrMsg()); + + code = Element::create(65536); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), OutOfRange); + EXPECT_EQ("invalid 'code' value 65536 not in [0..65535]", impl_->getErrMsg()); + + code = Element::create(1); + sub_option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + code = Element::create(65535); + sub_option->set("code", code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); +} + +// Verify that the space must be a string. +TEST_F(FlexSubOptionTest, subOptionConfigBadSpace) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(true); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'space' must be a string: true", impl_->getErrMsg()); +} + +// Verify that the space must be valid. +TEST_F(FlexSubOptionTest, subOptionConfigInvalidSpace) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("-bad-")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + sub_option->set("code", code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'-bad-' is not a valid space name", impl_->getErrMsg()); +} + +// Verify that the name must be a string. +TEST_F(FlexSubOptionTest, subOptionConfigBadName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr name = Element::create(true); + sub_option->set("name", name); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'name' must be a string: true", impl_->getErrMsg()); +} + +// Verify that the name must not be empty. +TEST_F(FlexSubOptionTest, subOptionConfigEmptyName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string()); + sub_option->set("name", name); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'name' must not be empty", impl_->getErrMsg()); +} + +// Verify that the name must be a known option. +TEST_F(FlexSubOptionTest, subOptionConfigUnknownName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("foobar")); + sub_option->set("name", name); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("no known 'foobar' sub-option in 'my-space' space", + impl_->getErrMsg()); +} + +// Verify that the definition is not required when csv-format is not specified. +TEST_F(FlexSubOptionTest, subOptionConfigUnknownCodeNoCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(109)); + auto smap = map[109]; + EXPECT_EQ(1, smap.count(222)); +} + +// Verify that the definition is not required when csv-format is false. +TEST_F(FlexSubOptionTest, subOptionConfigUnknownCodeDisableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + // Disable csv-format. + sub_option->set("csv-format", Element::create(false)); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(109)); + auto smap = map[109]; + EXPECT_EQ(1, smap.count(222)); +} + +// Verify that the code must be a known sub-option when csv-format is true. +TEST_F(FlexSubOptionTest, subOptionConfigUnknownCodeEnableCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + // Enable csv-format. + sub_option->set("csv-format", Element::create(true)); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("no known sub-option with code '222' in 'my-space' space", + impl_->getErrMsg()); +} + +// Verify that the name can be an user defined sub-option. +TEST_F(FlexSubOptionTest, subOptionConfigDefinedName) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-option", 222, + "my-space", "string")); + defs.addItem(def); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(109)); + auto smap = map[109]; + EXPECT_EQ(1, smap.count(222)); +} + +// Verify that the name can be a last resort space defined sub-option. +TEST_F(FlexSubOptionTest, subOptionConfigLastResortName) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-option", 222, + VENDOR_ENCAPSULATED_OPTION_SPACE, + "string")); + defs.addItem(def); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VENDOR_ENCAPSULATED_OPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(DHO_VENDOR_ENCAPSULATED_OPTIONS)); + auto smap = map[DHO_VENDOR_ENCAPSULATED_OPTIONS]; + EXPECT_EQ(1, smap.count(222)); +} + +// Verify that the sub-option definition can be fetched from the last +// resort space. +TEST_F(FlexSubOptionTest, subOptionConfigLastResortCode) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VENDOR_ENCAPSULATED_OPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + // Enable csv-format. + sub_option->set("csv-format", Element::create(true)); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string msg = "no known sub-option with code '222' in '"; + msg += VENDOR_ENCAPSULATED_OPTION_SPACE; + msg += "' space"; + EXPECT_EQ(msg, impl_->getErrMsg()); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-option", 222, + VENDOR_ENCAPSULATED_OPTION_SPACE, + "string")); + defs.addItem(def); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(DHO_VENDOR_ENCAPSULATED_OPTIONS)); + auto smap = map[DHO_VENDOR_ENCAPSULATED_OPTIONS]; + EXPECT_EQ(1, smap.count(222)); +} + +// Verify that the name can be a vendor defined sub-option. +TEST_F(FlexSubOptionTest, subOptionConfigVendorName) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-option", 222, + "vendor-1234", "string")); + defs.addItem(def); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("vendor-1234")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(D6O_VENDOR_OPTS)); + auto smap = map[D6O_VENDOR_OPTS]; + EXPECT_EQ(1, smap.count(222)); +} + +// Verify that the sub-option definition can be fetched from a custom +// vendor space. +TEST_F(FlexSubOptionTest, subOptionConfigVendorCode) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("vendor-1234")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + // Enable csv-format. + sub_option->set("csv-format", Element::create(true)); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("no known sub-option with code '222' in 'vendor-1234' space", + impl_->getErrMsg()); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-option", 222, + "vendor-1234", "string")); + defs.addItem(def); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(D6O_VENDOR_OPTS)); + auto smap = map[D6O_VENDOR_OPTS]; + EXPECT_EQ(1, smap.count(222)); +} + +// Verify that the name can be a vendor standard sub-option. +TEST_F(FlexSubOptionTest, subOptionConfigDosSISName) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + // VENDOR_ID_CABLE_LABS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("tftp-servers")); + sub_option->set("name", name); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(DHO_VIVSO_SUBOPTIONS)); + auto smap = map[DHO_VIVSO_SUBOPTIONS]; + // DOCSIS3_V4_TFTP_SERVERS is 2 + EXPECT_EQ(1, smap.count(2)); +} + +// Verify that the sub-option definition can be fetched from a standard +// vendor space. +TEST_F(FlexSubOptionTest, subOptionConfigDosSISCode) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + // VENDOR_ID_CABLE_LABS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(DOCSIS3_V4_TFTP_SERVERS); + sub_option->set("code", sub_code); + // Enable csv-format. + sub_option->set("csv-format", Element::create(true)); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(DHO_VIVSO_SUBOPTIONS)); + auto smap = map[DHO_VIVSO_SUBOPTIONS]; + // DOCSIS3_V4_TFTP_SERVERS is 2 + EXPECT_EQ(1, smap.count(2)); +} + +// Verify that the name must match the code. +TEST_F(FlexSubOptionTest, subOptionConfigCodeNameMismatch) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + // VENDOR_ID_CABLE_LABS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("tftp-servers")); + sub_option->set("name", name); + // DOCSIS3_V4_TFTP_SERVERS is 2 + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "sub-option 'tftp-servers' is defined as code: 2, "; + expected += "not the specified code: 222"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that the csv-format must be a boolean. +TEST_F(FlexSubOptionTest, subOptionConfigBadCSVFormat) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr csv_format = Element::create(123); + sub_option->set("csv-format", csv_format); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'csv-format' must be a boolean: 123", impl_->getErrMsg()); +} + +// Verify that the container-add must be a boolean. +TEST_F(FlexSubOptionTest, subOptionConfigBadContainerAdd) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr container_add = Element::create(123); + sub_option->set("container-add", container_add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'container-add' must be a boolean: 123", impl_->getErrMsg()); +} + +// Verify that the container-remove must be a boolean. +TEST_F(FlexSubOptionTest, subOptionConfigBadContainerRemove) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr container_remove = Element::create(123); + sub_option->set("container-remove", container_remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'container-remove' must be a boolean: 123", impl_->getErrMsg()); +} + +// Verify that multiple sub-option actions are not accepted. +TEST_F(FlexSubOptionTest, subOptionConfigMultipleAction) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + + // Add it a second time. + sub_options->add(sub_option); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("sub-option 222 of option 109 was already specified", + impl_->getErrMsg()); +} + +// Verify that the add value must be a string. +TEST_F(FlexSubOptionTest, subOptionConfigAddNotString) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr add = Element::create(true); + sub_option->set("add", add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'add' must be a string: true", impl_->getErrMsg()); +} + +// Verify that the add value must not be empty. +TEST_F(FlexSubOptionTest, subOptionConfigEmptyAdd) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr add = Element::create(string()); + sub_option->set("add", add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'add' must not be empty", impl_->getErrMsg()); +} + +// Verify that the add value must parse. +TEST_F(FlexSubOptionTest, subOptionConfigBadAdd) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr add = Element::create(string("ifelse('a','b','c')")); + sub_option->set("add", add); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "can't parse add expression [ifelse('a','b','c')] "; + expected += "error: <string>:1.11: syntax error, "; + expected += "unexpected \",\", expecting == or +"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that the supersede value must be a string. +TEST_F(FlexSubOptionTest, subOptionConfigSupersedeNotString) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr supersede = Element::create(true); + sub_option->set("supersede", supersede); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'supersede' must be a string: true", impl_->getErrMsg()); +} + +// Verify that the supersede value must not be empty. +TEST_F(FlexSubOptionTest, subOptionConfigEmptySupersede) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr supersede = Element::create(string()); + sub_option->set("supersede", supersede); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'supersede' must not be empty", impl_->getErrMsg()); +} + +// Verify that the supersede value must parse. +TEST_F(FlexSubOptionTest, subOptionConfigBadSupersede) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr supersede = Element::create(string("ifelse('a','b','c')")); + sub_option->set("supersede", supersede); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "can't parse supersede expression [ifelse('a','b','c')] "; + expected += "error: <string>:1.11: syntax error, "; + expected += "unexpected \",\", expecting == or +"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that the remove value must be a string. +TEST_F(FlexSubOptionTest, subOptionConfigRemoveNotString) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr remove = Element::createMap(); + sub_option->set("remove", remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'remove' must be a string: { }", impl_->getErrMsg()); +} + +// Verify that the remove value must not be empty. +TEST_F(FlexSubOptionTest, subOptionConfigEmptyRemove) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr remove = Element::create(string()); + sub_option->set("remove", remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'remove' must not be empty", impl_->getErrMsg()); +} + +// Verify that the remove value must parse. +TEST_F(FlexSubOptionTest, subOptionConfigBadRemove) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + ElementPtr remove = Element::create(string("'abc'")); + sub_option->set("remove", remove); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + string expected = "can't parse remove expression ['abc'] error: "; + expected += "<string>:1.6: syntax error, unexpected end of file, "; + expected += "expecting == or +"; + EXPECT_EQ(expected, impl_->getErrMsg()); +} + +// Verify that a complex example must parse. +TEST_F(FlexSubOptionTest, subOptionConfigComplex) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + + ElementPtr sub_option1 = Element::createMap(); + sub_options->add(sub_option1); + ElementPtr space = Element::create(string("my-space")); + sub_option1->set("space", space); + ElementPtr sub_code = Element::create(1); + sub_option1->set("code", sub_code); + ElementPtr add = Element::create(string("'abc'")); + sub_option1->set("add", add); + + ElementPtr sub_option2 = Element::createMap(); + sub_options->add(sub_option2); + sub_option2->set("space", space); + sub_code = Element::create(2); + sub_option2->set("code", sub_code); + ElementPtr supersede = Element::create(string("'def'")); + sub_option2->set("supersede", supersede); + sub_option2->set("container-add", Element::create(false)); + + ElementPtr sub_option3 = Element::createMap(); + sub_options->add(sub_option3); + sub_option3->set("space", space); + sub_code = Element::create(3); + sub_option3->set("code", sub_code); + ElementPtr remove = Element::create(string("'a' == 'b'")); + sub_option3->set("remove", remove); + + ElementPtr sub_option4 = Element::createMap(); + sub_options->add(sub_option4); + sub_option4->set("space", space); + sub_code = Element::create(4); + sub_option4->set("code", sub_code); + remove = Element::create(string("'b' == 'a'")); + sub_option4->set("remove", remove); + sub_option4->set("container-remove", Element::create(false)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(109)); + auto smap = map[109]; + FlexOptionImpl::SubOptionConfigPtr sub_cfg; + ASSERT_NO_THROW(sub_cfg = smap.at(1)); + ASSERT_TRUE(sub_cfg); + EXPECT_EQ(1, sub_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::ADD, sub_cfg->getAction()); + EXPECT_EQ(FlexOptionImpl::ADD, sub_cfg->getContainerAction()); + EXPECT_EQ("'abc'", sub_cfg->getText()); + EXPECT_EQ(109, sub_cfg->getContainerCode()); + + ASSERT_NO_THROW(sub_cfg = smap.at(2)); + ASSERT_TRUE(sub_cfg); + EXPECT_EQ(2, sub_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::SUPERSEDE, sub_cfg->getAction()); + EXPECT_EQ(FlexOptionImpl::NONE, sub_cfg->getContainerAction()); + EXPECT_EQ("'def'", sub_cfg->getText()); + EXPECT_EQ(109, sub_cfg->getContainerCode()); + + ASSERT_NO_THROW(sub_cfg = smap.at(3)); + ASSERT_TRUE(sub_cfg); + EXPECT_EQ(3, sub_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::REMOVE, sub_cfg->getAction()); + EXPECT_EQ(FlexOptionImpl::REMOVE, sub_cfg->getContainerAction()); + EXPECT_EQ("'a' == 'b'", sub_cfg->getText()); + EXPECT_EQ(109, sub_cfg->getContainerCode()); + + ASSERT_NO_THROW(sub_cfg = smap.at(4)); + ASSERT_TRUE(sub_cfg); + EXPECT_EQ(4, sub_cfg->getCode()); + EXPECT_EQ(FlexOptionImpl::REMOVE, sub_cfg->getAction()); + EXPECT_EQ(FlexOptionImpl::NONE, sub_cfg->getContainerAction()); + EXPECT_EQ("'b' == 'a'", sub_cfg->getText()); + EXPECT_EQ(109, sub_cfg->getContainerCode()); +} + +// Empty sub-option config list doing nothing is the same as empty option list. + +// Verify that NONE action really does nothing. +TEST_F(FlexSubOptionTest, subProcessNone) { + auto rai_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE, + DHO_DHCP_AGENT_OPTIONS); + FlexOptionImpl::OptionConfigPtr + opt_cfg(new FlexOptionImpl::OptionConfig(DHO_DHCP_AGENT_OPTIONS, + rai_def)); + // RAI_OPTION_AGENT_CIRCUIT_ID is 1 but has no definition. + OptionDefinitionPtr def; + FlexOptionImpl::SubOptionConfigPtr + sub_cfg(new FlexOptionImpl::SubOptionConfig(1, def, opt_cfg)); + auto& map = impl_->getMutableSubOptionConfigMap(); + auto& smap = map[DHO_DHCP_AGENT_OPTIONS]; + smap[1] = sub_cfg; + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); +} + +// Verify that ADD action adds the specified sub-option in csv format. +TEST_F(FlexSubOptionTest, subProcessAddEnableCSVFormat) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "fqdn", true)); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'example.com'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("csv-format", Element::create(true)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + // The fqdn array is the most complex encoding of one element... + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(13, buffer.size()); + EXPECT_EQ(7, buffer[0]); + EXPECT_EQ(0, memcmp(&buffer[1], "example", 7)); + EXPECT_EQ(3, buffer[8]); + EXPECT_EQ(0, memcmp(&buffer[9], "com", 3)); + EXPECT_EQ(0, buffer[12]); +} + +// Verify that ADD action does nothing when the container does not exist and +// container-add is false. +TEST_F(FlexSubOptionTest, subProcessAddNoContainer) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'abc'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("container-add", Element::create(false)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(222)); +} + +// Verify that ADD action adds the specified sub-option in raw format. +TEST_F(FlexSubOptionTest, subProcessAddDisableCSVFormat) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "fqdn", true)); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("0x076578616d706c6503636f6d00")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + // csv-format is disabled by default. + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(13, buffer.size()); + EXPECT_EQ(7, buffer[0]); + EXPECT_EQ(0, memcmp(&buffer[1], "example", 7)); + EXPECT_EQ(3, buffer[8]); + EXPECT_EQ(0, memcmp(&buffer[9], "com", 3)); + EXPECT_EQ(0, buffer[12]); +} + +// Verify that ADD action adds the specified sub-option in an already +// existing container option. +TEST_F(FlexSubOptionTest, subProcessAdd) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'abc'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + // Only one option with code 222. + EXPECT_EQ(1, response->options_.count(222)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); + + // Only one sub-option. + auto const& opts = opt->getOptions(); + EXPECT_EQ(1, opts.size()); +} + +// Verify that ADD action does not add an already existing sub-option. +TEST_F(FlexSubOptionTest, subProcessAddExisting) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'abc'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + OptionStringPtr str(new OptionString(Option::V4, 1, "xyzt")); + container->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "xyzt", 4)); + + // Only one sub-option. + auto const& opts = opt->getOptions(); + EXPECT_EQ(1, opts.size()); +} + +// Verify that ADD action does not add an empty value. +TEST_F(FlexSubOptionTest, subProcessAddEmpty) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("''")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(222)); +} + +// Verify that ADD action can handle vendor-encapsulated-options 43. +TEST_F(FlexSubOptionTest, subProcessAdd43) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + VENDOR_ENCAPSULATED_OPTION_SPACE, + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VENDOR_ENCAPSULATED_OPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'foobar'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that ADD action can handle DocSIS Vivso. +TEST_F(FlexSubOptionTest, subProcessAddDocSISVIVSO) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'10.1.2.3'")); + sub_option->set("add", add); + // DocSIS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("tftp-servers")); + sub_option->set("name", name); + sub_option->set("csv-format", Element::create(true)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_VIVSO_SUBOPTIONS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(VENDOR_ID_CABLE_LABS, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(DOCSIS3_V4_TFTP_SERVERS); + ASSERT_TRUE(sub); + Option4AddrLstPtr addr = boost::dynamic_pointer_cast<Option4AddrLst>(sub); + ASSERT_TRUE(addr); + auto const& addrs = addr->getAddresses(); + ASSERT_EQ(1, addrs.size()); + EXPECT_EQ("10.1.2.3", addrs[0].toText()); +} + +// Verify that ADD action can handle DocSIS vendor-opts. +TEST_F(FlexSubOptionTest, subProcessAddDocSISVendorOps) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'foobar'")); + sub_option->set("add", add); + // DocSIS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + code = Element::create(DOCSIS3_V6_VENDOR_NAME); + sub_option->set("code", code); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_VENDOR_OPTS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(VENDOR_ID_CABLE_LABS, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(DOCSIS3_V6_VENDOR_NAME); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that ADD action can handle the Vivso option. +TEST_F(FlexSubOptionTest, subProcessAddVivso) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'foobar'")); + sub_option->set("add", add); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_VIVSO_SUBOPTIONS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(123456, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that ADD action can handle the Vivso option with vendor mismatch. +TEST_F(FlexSubOptionTest, subProcessAddVivsoMismatch) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'foobar'")); + sub_option->set("add", add); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionVendorPtr vendor(new OptionVendor(Option::V4, 67890)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V4, 2, "xyzt")); + vendor->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(DHO_VIVSO_SUBOPTIONS); + ASSERT_TRUE(opt); + vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(67890, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + EXPECT_FALSE(sub); + sub = vendor->getOption(2); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "xyzt", 4)); +} + +// Verify that ADD action can handle the vendor-opts option. +TEST_F(FlexSubOptionTest, subProcessAddVendorOpts) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'foobar'")); + sub_option->set("add", add); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_VENDOR_OPTS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(123456, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that ADD action can handle the vendor-opts option with vendor mismatch. +TEST_F(FlexSubOptionTest, subProcessAddVendorOptsMismatch) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr add = Element::create(string("'foobar'")); + sub_option->set("add", add); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionVendorPtr vendor(new OptionVendor(Option::V6, 67890)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V6, 2, "xyzt")); + vendor->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(D6O_VENDOR_OPTS); + ASSERT_TRUE(opt); + vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(67890, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + EXPECT_FALSE(sub); + sub = vendor->getOption(2); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "xyzt", 4)); +} + +// Verify that SUPERSEDE action adds the specified sub-option in csv format. +TEST_F(FlexSubOptionTest, subProcessSupersedeEnableCSVFormat) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "fqdn", true)); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr supersede = Element::create(string("'example.com'")); + sub_option->set("supersede", supersede); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("csv-format", Element::create(true)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + // The fqdn array is the most complex encoding of one element... + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(13, buffer.size()); + EXPECT_EQ(7, buffer[0]); + EXPECT_EQ(0, memcmp(&buffer[1], "example", 7)); + EXPECT_EQ(3, buffer[8]); + EXPECT_EQ(0, memcmp(&buffer[9], "com", 3)); + EXPECT_EQ(0, buffer[12]); +} + +// Verify that SUPERSEDE action does nothing when the container does not exist +// and container-add is false. +TEST_F(FlexSubOptionTest, subProcessSupersedeNoContainer) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr supersede = Element::create(string("'abc'")); + sub_option->set("supersede", supersede); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("container-add", Element::create(false)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(222)); +} + +// Verify that SUPERSEDE action adds the specified sub-option in raw format. +TEST_F(FlexSubOptionTest, subProcessSupersedeDisableCSVFormat) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "fqdn", true)); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr supersede = Element::create(string("0x076578616d706c6503636f6d00")); + sub_option->set("supersede", supersede); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + // csv-format is disabled by default. + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(13, buffer.size()); + EXPECT_EQ(7, buffer[0]); + EXPECT_EQ(0, memcmp(&buffer[1], "example", 7)); + EXPECT_EQ(3, buffer[8]); + EXPECT_EQ(0, memcmp(&buffer[9], "com", 3)); + EXPECT_EQ(0, buffer[12]); +} + +// Verify that SUPERSEDE action adds the specified sub-option in an already +// existing container option. +TEST_F(FlexSubOptionTest, subProcessSupersede) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr supersede = Element::create(string("'abc'")); + sub_option->set("supersede", supersede); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + // Only one option with code 222. + EXPECT_EQ(1, response->options_.count(222)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); + + // Only one sub-option. + auto const& opts = opt->getOptions(); + EXPECT_EQ(1, opts.size()); +} + +// Verify that SUPERSEDE action replaces an already existing sub-option. +TEST_F(FlexSubOptionTest, subProcessSupersedeExisting) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr supersede = Element::create(string("'abc'")); + sub_option->set("supersede", supersede); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + OptionStringPtr str(new OptionString(Option::V4, 1, "xyzt")); + container->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + // Only one option with code 222. + EXPECT_EQ(1, response->options_.count(222)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); + + // Only one sub-option. + auto const& opts = opt->getOptions(); + EXPECT_EQ(1, opts.size()); +} + +// Verify that SUPERSEDE action does not add an empty value. +TEST_F(FlexSubOptionTest, subProcessSupersedeEmpty) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr supersede = Element::create(string("''")); + sub_option->set("supersede", supersede); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(222)); +} + +// Verify that SUPERSEDE action can handle vendor-encapsulated-options 43. +TEST_F(FlexSubOptionTest, subProcessSupersede43) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + VENDOR_ENCAPSULATED_OPTION_SPACE, + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VENDOR_ENCAPSULATED_OPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr supersede = Element::create(string("'foobar'")); + sub_option->set("supersede", supersede); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS); + ASSERT_TRUE(opt); + EXPECT_EQ(DHO_VENDOR_ENCAPSULATED_OPTIONS, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that SUPERSEDE action can handle DocSIS Vivso. +TEST_F(FlexSubOptionTest, subProcessSupersedeDocSISVIVSO) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr supersede = Element::create(string("10.1.2.3")); + sub_option->set("supersede", supersede); + // DocSIS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("tftp-servers")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_VIVSO_SUBOPTIONS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(VENDOR_ID_CABLE_LABS, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(DOCSIS3_V4_TFTP_SERVERS); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + uint8_t expected[] = { 10, 1, 2, 3 }; + EXPECT_EQ(0, memcmp(&buffer[0], expected, 4)); +} + +// Verify that SUPERSEDE action can handle DocSIS vendor-opts. +TEST_F(FlexSubOptionTest, subProcessSupersedeDocSISVendorOps) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr supersede = Element::create(string("'foobar'")); + sub_option->set("supersede", supersede); + // DocSIS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + code = Element::create(DOCSIS3_V6_VENDOR_NAME); + sub_option->set("code", code); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_VENDOR_OPTS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(VENDOR_ID_CABLE_LABS, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(DOCSIS3_V6_VENDOR_NAME); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that SUPERSEDE action can handle the Vivso option. +TEST_F(FlexSubOptionTest, subProcessSupersedeVivso) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr supersede = Element::create(string("'foobar'")); + sub_option->set("supersede", supersede); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(DHO_VIVSO_SUBOPTIONS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(123456, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that SUPERSEDE action can handle the Vivso option with vendor mismatch. +TEST_F(FlexSubOptionTest, subProcessSupersedeVivsoMismatch) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr supersede = Element::create(string("'foobar'")); + sub_option->set("supersede", supersede); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionVendorPtr vendor(new OptionVendor(Option::V4, 67890)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V4, 2, "xyzt")); + vendor->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(DHO_VIVSO_SUBOPTIONS); + ASSERT_TRUE(opt); + vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(67890, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + EXPECT_FALSE(sub); + sub = vendor->getOption(2); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "xyzt", 4)); +} + +// Verify that SUPERSEDE action can handle the vendor-opts option. +TEST_F(FlexSubOptionTest, subProcessSupersedeVendorOpts) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr supersede = Element::create(string("'foobar'")); + sub_option->set("supersede", supersede); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + OptionPtr opt = response->getOption(D6O_VENDOR_OPTS); + ASSERT_TRUE(opt); + OptionVendorPtr vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(123456, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(6, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "foobar", 6)); +} + +// Verify that SUPERSEDE action can handle the vendor-opts option with vendor mismatch. +TEST_F(FlexSubOptionTest, subProcessSupersedeVendorOptsMismatch) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr supersede = Element::create(string("'foobar'")); + sub_option->set("supersede", supersede); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionVendorPtr vendor(new OptionVendor(Option::V6, 67890)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V6, 2, "xyzt")); + vendor->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(D6O_VENDOR_OPTS); + ASSERT_TRUE(opt); + vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(67890, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + EXPECT_FALSE(sub); + sub = vendor->getOption(2); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "xyzt", 4)); +} + +// Verify that REMOVE action removes an already existing sub-option. +TEST_F(FlexSubOptionTest, subProcessRemove) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + OptionStringPtr str(new OptionString(Option::V4, 1, "xyzt")); + container->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(222)); +} + +// Verify that REMOVE action removes an already existing sub-option but +// leaves the container option when container-remove is false. +TEST_F(FlexSubOptionTest, subProcessRemoveLeaveContainer) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("container-remove", Element::create(false)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + string response_txt = response->toText(); + EXPECT_TRUE(response->getOption(222)); + OptionStringPtr str(new OptionString(Option::V4, 1, "xyzt")); + container->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_FALSE(opt->getOption(1)); + EXPECT_TRUE(opt->getOptions().empty()); +} + +// Verify that REMOVE action removes an already existing sub-option but +// leaves the container option when it is not empty. +TEST_F(FlexSubOptionTest, subProcessRemoveContainerNotEmpty) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 2, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + OptionStringPtr str(new OptionString(Option::V4, 1, "abc")); + container->addOption(str); + string response_txt = response->toText(); + str.reset(new OptionString(Option::V4, 2, "xyzt")); + container->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_FALSE(opt->getOption(2)); + EXPECT_EQ(1, opt->getOptions().size()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); +} + +// Verify that REMOVE action does not removes the container option when the +// sub-option does not exist. +TEST_F(FlexSubOptionTest, subProcessRemoveContainerNoSubOption) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("container-remove", Element::create(false)); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_TRUE(response->getOption(222)); +} + +// Verify that REMOVE action does nothing when the expression evaluates to false. +TEST_F(FlexSubOptionTest, subProcessRemoveFalse) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr remove = Element::create(string("'abc' == 'xyz'")); + sub_option->set("remove", remove); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionPtr container(new Option(Option::V4, 222)); + response->addOption(container); + EXPECT_TRUE(response->getOption(222)); + OptionStringPtr str(new OptionString(Option::V4, 1, "abc")); + container->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); +} + +// Verify that REMOVE action can handle vendor-encapsulated-options 43. +TEST_F(FlexSubOptionTest, subProcessRemove43) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + VENDOR_ENCAPSULATED_OPTION_SPACE, + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VENDOR_ENCAPSULATED_OPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + OptionPtr container(new Option(Option::V4, DHO_VENDOR_ENCAPSULATED_OPTIONS)); + response->addOption(container); + EXPECT_TRUE(response->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS)); + OptionStringPtr str(new OptionString(Option::V4, 1, "abc")); + container->addOption(str); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(DHO_VENDOR_ENCAPSULATED_OPTIONS)); +} + +// Verify that REMOVE action can handle DocSIS Vivso. +TEST_F(FlexSubOptionTest, subProcessRemoveDocSISVIVSO) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr remove = Element::create(string("member('ALL')")); + sub_option->set("remove", remove); + // DocSIS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("tftp-servers")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + query->addClass("ALL"); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + OptionVendorPtr vendor(new OptionVendor(Option::V4, 4491)); + response->addOption(vendor); + Option4AddrLstPtr tftp(new Option4AddrLst(DOCSIS3_V4_TFTP_SERVERS, + IOAddress("10.1.2.3"))); + vendor->addOption(tftp); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(DHO_VIVSO_SUBOPTIONS)); +} + +// Verify that REMOVE action can handle DocSIS vendor-opts. +TEST_F(FlexSubOptionTest, subProcessRemoveDocSISVendorOps) { + CfgMgr::instance().setFamily(AF_INET6); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + // DocSIS is 4491 + ElementPtr space = Element::create(string("vendor-4491")); + sub_option->set("space", space); + code = Element::create(DOCSIS3_V6_VENDOR_NAME); + sub_option->set("code", code); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + string response_txt = response->toText(); + OptionVendorPtr vendor(new OptionVendor(Option::V6, 4491)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V6, DOCSIS3_V6_VENDOR_NAME, + "foobar")); + vendor->addOption(str); + EXPECT_TRUE(response->getOption(D6O_VENDOR_OPTS)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(D6O_VENDOR_OPTS)); +} + +// Verify that REMOVE action can handle the Vivso option. +TEST_F(FlexSubOptionTest, subProcessRemoveVivso) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + OptionVendorPtr vendor(new OptionVendor(Option::V4, 123456)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V4, 1, "foobar")); + vendor->addOption(str); + EXPECT_TRUE(response->getOption(DHO_VIVSO_SUBOPTIONS)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(DHO_VIVSO_SUBOPTIONS)); +} + +// Verify that REMOVE action can handle the Vivso option with vendor mismatch. +TEST_F(FlexSubOptionTest, subProcessRemoveVivsoMismatch) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(DHO_VIVSO_SUBOPTIONS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr remove = Element::create(string("member('ALL')")); + sub_option->set("remove", remove); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + query->addClass("ALL"); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + OptionVendorPtr vendor(new OptionVendor(Option::V4, 67890)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V4, 2, "xyzt")); + vendor->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(DHO_VIVSO_SUBOPTIONS); + ASSERT_TRUE(opt); + vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(67890, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + EXPECT_FALSE(sub); + sub = vendor->getOption(2); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "xyzt", 4)); +} + +// Verify that REMOVE action can handle the vendor-opts option. +TEST_F(FlexSubOptionTest, subProcessRemoveVendorOpts) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + string response_txt = response->toText(); + OptionVendorPtr vendor(new OptionVendor(Option::V6, 123456)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V6, 1, "foobar")); + vendor->addOption(str); + EXPECT_TRUE(response->getOption(D6O_VENDOR_OPTS)); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(D6O_VENDOR_OPTS)); +} + +// Verify that REMOVE action can handle the vendor-opts option with vendor mismatch. +TEST_F(FlexSubOptionTest, subProcessRemoveVendorOptsMismatch) { + CfgMgr::instance().setFamily(AF_INET6); + + OptionDefSpaceContainer defs; + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, + "vendor-123456", "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(D6O_VENDOR_OPTS); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr remove = Element::create(string("'abc' == 'abc'")); + sub_option->set("remove", remove); + ElementPtr space = Element::create(string("vendor-123456")); + sub_option->set("space", space); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt6Ptr query(new Pkt6(DHCPV6_SOLICIT, 12345)); + Pkt6Ptr response(new Pkt6(DHCPV6_ADVERTISE, 12345)); + OptionVendorPtr vendor(new OptionVendor(Option::V6, 67890)); + response->addOption(vendor); + OptionStringPtr str(new OptionString(Option::V6, 2, "xyzt")); + vendor->addOption(str); + string response_txt = response->toText(); + + EXPECT_NO_THROW(impl_->process<Pkt6Ptr>(Option::V6, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + OptionPtr opt = response->getOption(D6O_VENDOR_OPTS); + ASSERT_TRUE(opt); + vendor = boost::dynamic_pointer_cast<OptionVendor>(opt); + ASSERT_TRUE(vendor); + EXPECT_EQ(67890, vendor->getVendorId()); + OptionPtr sub = vendor->getOption(1); + EXPECT_FALSE(sub); + sub = vendor->getOption(2); + ASSERT_TRUE(sub); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(4, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "xyzt", 4)); +} + +// Verify that the client class must be a string. +TEST_F(FlexSubOptionTest, subOptionConfigBadClass) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(true); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + sub_option->set("code", code); + sub_option->set("client-class", Element::create(true)); + EXPECT_THROW(impl_->testConfigure(options), BadValue); + EXPECT_EQ("'client-class' must be a string: true", impl_->getErrMsg()); +} + +// Verify that a valid client class is accepted. +TEST_F(FlexSubOptionTest, subOptionConfigGuardValid) { + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(109); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'ab'")); + sub_option->set("add", add); + ElementPtr sub_code = Element::create(222); + sub_option->set("code", sub_code); + sub_option->set("client-class", Element::create(string("foobar"))); + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + auto map = impl_->getSubOptionConfigMap(); + EXPECT_EQ(1, map.count(109)); + auto smap = map[109]; + FlexOptionImpl::SubOptionConfigPtr sub_cfg; + ASSERT_NO_THROW(sub_cfg = smap.at(222)); + ASSERT_TRUE(sub_cfg); + EXPECT_EQ(222, sub_cfg->getCode()); + EXPECT_EQ(109, sub_cfg->getContainerCode()); + EXPECT_EQ("foobar", sub_cfg->getClass()); +} + +// Verify that a guarded action is skipped when query does not belong to the +// client class of the container option. +TEST_F(FlexSubOptionTest, subOptionConfigGuardOptiondNoMatch) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + option->set("client-class", Element::create(string("foobar"))); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'abc'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(222)); +} + +// Verify that a guarded action is applied when query belongs to the class +// class of the container option. +TEST_F(FlexSubOptionTest, subOptionConfigGuardOptiondMatch) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + option->set("client-class", Element::create(string("foobar"))); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'abc'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + query->addClass("foobar"); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); +} + +// Verify that a guarded action is skipped when query does not belong to the +// client class of the sub-option. +TEST_F(FlexSubOptionTest, subOptionConfigGuardSubOptiondNoMatch) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'abc'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("client-class", Element::create(string("foobar"))); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + string response_txt = response->toText(); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + EXPECT_EQ(response_txt, response->toText()); + EXPECT_FALSE(response->getOption(222)); +} + +// Verify that a guarded action is applied when query belongs to the class +// class of the sub-option. +TEST_F(FlexSubOptionTest, subOptionConfigGuardSubOptiondMatch) { + OptionDefSpaceContainer defs; + OptionDefinitionPtr def(new OptionDefinition("my-container", 222, + DHCP4_OPTION_SPACE, "empty", + "my-space")); + defs.addItem(def); + OptionDefinitionPtr sdef(new OptionDefinition("my-option", 1, "my-space", + "string")); + defs.addItem(sdef); + EXPECT_NO_THROW(LibDHCP::setRuntimeOptionDefs(defs)); + + ElementPtr options = Element::createList(); + ElementPtr option = Element::createMap(); + options->add(option); + ElementPtr code = Element::create(222); + option->set("code", code); + ElementPtr sub_options = Element::createList(); + option->set("sub-options", sub_options); + ElementPtr sub_option = Element::createMap(); + sub_options->add(sub_option); + ElementPtr space = Element::create(string("my-space")); + sub_option->set("space", space); + ElementPtr add = Element::create(string("'abc'")); + sub_option->set("add", add); + ElementPtr name = Element::create(string("my-option")); + sub_option->set("name", name); + sub_option->set("client-class", Element::create(string("foobar"))); + + EXPECT_NO_THROW(impl_->testConfigure(options)); + EXPECT_TRUE(impl_->getErrMsg().empty()) << impl_->getErrMsg(); + + Pkt4Ptr query(new Pkt4(DHCPDISCOVER, 12345)); + query->addClass("foobar"); + Pkt4Ptr response(new Pkt4(DHCPOFFER, 12345)); + EXPECT_FALSE(response->getOption(222)); + + EXPECT_NO_THROW(impl_->process<Pkt4Ptr>(Option::V4, query, response)); + + OptionPtr opt = response->getOption(222); + ASSERT_TRUE(opt); + EXPECT_EQ(222, opt->getType()); + OptionPtr sub = opt->getOption(1); + ASSERT_TRUE(sub); + EXPECT_EQ(1, sub->getType()); + const OptionBuffer& buffer = sub->getData(); + ASSERT_EQ(3, buffer.size()); + EXPECT_EQ(0, memcmp(&buffer[0], "abc", 3)); +} + +} // end of anonymous namespace diff --git a/src/hooks/dhcp/flex_option/tests/test_flex_option.h b/src/hooks/dhcp/flex_option/tests/test_flex_option.h new file mode 100644 index 0000000..df0a9bf --- /dev/null +++ b/src/hooks/dhcp/flex_option/tests/test_flex_option.h @@ -0,0 +1,82 @@ +// Copyright (C) 2019-2022 Internet Systems Consortium, Inc. ("ISC") +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +/// @file This file contains common part of flexible option tests. + +#ifndef TEST_FLEX_OPTION_H +#define TEST_FLEX_OPTION_H + +#include <flex_option.h> +#include <dhcp/libdhcp++.h> +#include <dhcpsrv/cfgmgr.h> +#include <gtest/gtest.h> + +namespace isc { +namespace flex_option { +namespace test { + +/// @brief Test class derived from FlexOptionImpl +class TestFlexOptionImpl : public FlexOptionImpl { +public: + /// Export getMutableOptionConfigMap. + using FlexOptionImpl::getMutableOptionConfigMap; + + /// Export getMutableSubOptionConfigMap. + using FlexOptionImpl::getMutableSubOptionConfigMap; + + /// @Brief Configure clone which records the error. + /// + /// @param options The element with option config list. + void testConfigure(isc::data::ConstElementPtr options) { + err_msg_.clear(); + try { + configure(options); + } catch (const std::exception& ex) { + err_msg_ = std::string(ex.what()); + throw; + } + } + + /// @brief Get the last error message. + /// + /// @return The last error message. + const std::string& getErrMsg() const { + return (err_msg_); + } + +private: + /// @brief Last error message. + std::string err_msg_; +}; + +/// @brief The type of shared pointers to TestFlexOptionImpl +typedef boost::shared_ptr<TestFlexOptionImpl> TestFlexOptionImplPtr; + +/// @brief Base test fixture for testing the Flex Option library. +class BaseFlexOptionTest : public ::testing::Test { +public: + /// @brief Constructor. + BaseFlexOptionTest() { + impl_.reset(new TestFlexOptionImpl()); + isc::dhcp::CfgMgr::instance().setFamily(AF_INET); + } + + /// @brief Destructor. + virtual ~BaseFlexOptionTest() { + LibDHCP::clearRuntimeOptionDefs(); + isc::dhcp::CfgMgr::instance().setFamily(AF_INET); + impl_.reset(); + } + + /// @brief Flex Option implementation. + TestFlexOptionImplPtr impl_; +}; + +} // end of namespace test +} // end of namespace flex_option +} // end of namespace isc + +#endif // TEST_FLEX_OPTION_H |