diff options
Diffstat (limited to 'src/lib/eval/tests')
-rw-r--r-- | src/lib/eval/tests/Makefile.am | 47 | ||||
-rw-r--r-- | src/lib/eval/tests/Makefile.in | 1076 | ||||
-rw-r--r-- | src/lib/eval/tests/boolean_unittest.cc | 64 | ||||
-rw-r--r-- | src/lib/eval/tests/context_unittest.cc | 2118 | ||||
-rw-r--r-- | src/lib/eval/tests/dependency_unittest.cc | 103 | ||||
-rw-r--r-- | src/lib/eval/tests/evaluate_unittest.cc | 515 | ||||
-rw-r--r-- | src/lib/eval/tests/run_unittests.cc | 21 | ||||
-rw-r--r-- | src/lib/eval/tests/token_unittest.cc | 3479 |
8 files changed, 7423 insertions, 0 deletions
diff --git a/src/lib/eval/tests/Makefile.am b/src/lib/eval/tests/Makefile.am new file mode 100644 index 0000000..f9d5a9e --- /dev/null +++ b/src/lib/eval/tests/Makefile.am @@ -0,0 +1,47 @@ +SUBDIRS = . + +AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib +AM_CPPFLAGS += $(BOOST_INCLUDES) +AM_CPPFLAGS += -DLOGGING_SPEC_FILE=\"$(abs_top_srcdir)/src/lib/dhcpsrv/logging.spec\" + +AM_CXXFLAGS = $(KEA_CXXFLAGS) + +if USE_STATIC_LINK +AM_LDFLAGS = -static +endif + +CLEANFILES = *.gcno *.gcda + +TESTS_ENVIRONMENT = \ + $(LIBTOOL) --mode=execute $(VALGRIND_COMMAND) + +TESTS = +if HAVE_GTEST + +TESTS += libeval_unittests + +libeval_unittests_SOURCES = boolean_unittest.cc +libeval_unittests_SOURCES += context_unittest.cc +libeval_unittests_SOURCES += dependency_unittest.cc +libeval_unittests_SOURCES += evaluate_unittest.cc +libeval_unittests_SOURCES += token_unittest.cc +libeval_unittests_SOURCES += run_unittests.cc +libeval_unittests_CXXFLAGS = $(AM_CXXFLAGS) +libeval_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) +libeval_unittests_LDFLAGS = $(AM_LDFLAGS) $(CRYPTO_LDFLAGS) $(GTEST_LDFLAGS) +libeval_unittests_LDADD = $(top_builddir)/src/lib/eval/libkea-eval.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/cc/libkea-cc.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/dhcp/libkea-dhcp++.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/asiolink/libkea-asiolink.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/dns/libkea-dns++.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/cryptolink/libkea-cryptolink.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/hooks/libkea-hooks.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/log/libkea-log.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/util/libkea-util.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/exceptions/libkea-exceptions.la +libeval_unittests_LDADD += $(top_builddir)/src/lib/testutils/libkea-testutils.la +libeval_unittests_LDADD += $(CRYPTO_LIBS) $(LOG4CPLUS_LIBS) +libeval_unittests_LDADD += $(BOOST_LIBS) $(GTEST_LDADD) +endif + +noinst_PROGRAMS = $(TESTS) diff --git a/src/lib/eval/tests/Makefile.in b/src/lib/eval/tests/Makefile.in new file mode 100644 index 0000000..490d2dc --- /dev/null +++ b/src/lib/eval/tests/Makefile.in @@ -0,0 +1,1076 @@ +# Makefile.in generated by automake 1.16.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2018 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +TESTS = $(am__EXEEXT_1) +@HAVE_GTEST_TRUE@am__append_1 = libeval_unittests +noinst_PROGRAMS = $(am__EXEEXT_2) +subdir = src/lib/eval/tests +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4macros/ax_boost_for_kea.m4 \ + $(top_srcdir)/m4macros/ax_cpp11.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_sysrepo.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 = libeval_unittests$(EXEEXT) +am__EXEEXT_2 = $(am__EXEEXT_1) +PROGRAMS = $(noinst_PROGRAMS) +am__libeval_unittests_SOURCES_DIST = boolean_unittest.cc \ + context_unittest.cc dependency_unittest.cc \ + evaluate_unittest.cc token_unittest.cc run_unittests.cc +@HAVE_GTEST_TRUE@am_libeval_unittests_OBJECTS = \ +@HAVE_GTEST_TRUE@ libeval_unittests-boolean_unittest.$(OBJEXT) \ +@HAVE_GTEST_TRUE@ libeval_unittests-context_unittest.$(OBJEXT) \ +@HAVE_GTEST_TRUE@ libeval_unittests-dependency_unittest.$(OBJEXT) \ +@HAVE_GTEST_TRUE@ libeval_unittests-evaluate_unittest.$(OBJEXT) \ +@HAVE_GTEST_TRUE@ libeval_unittests-token_unittest.$(OBJEXT) \ +@HAVE_GTEST_TRUE@ libeval_unittests-run_unittests.$(OBJEXT) +libeval_unittests_OBJECTS = $(am_libeval_unittests_OBJECTS) +am__DEPENDENCIES_1 = +@HAVE_GTEST_TRUE@libeval_unittests_DEPENDENCIES = \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/eval/libkea-eval.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/cc/libkea-cc.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcp/libkea-dhcp++.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/hooks/libkea-hooks.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@ $(top_builddir)/src/lib/testutils/libkea-testutils.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 = +libeval_unittests_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \ + $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \ + $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) \ + $(libeval_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)/libeval_unittests-boolean_unittest.Po \ + ./$(DEPDIR)/libeval_unittests-context_unittest.Po \ + ./$(DEPDIR)/libeval_unittests-dependency_unittest.Po \ + ./$(DEPDIR)/libeval_unittests-evaluate_unittest.Po \ + ./$(DEPDIR)/libeval_unittests-run_unittests.Po \ + ./$(DEPDIR)/libeval_unittests-token_unittest.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 = +SOURCES = $(libeval_unittests_SOURCES) +DIST_SOURCES = $(am__libeval_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)` +ETAGS = etags +CTAGS = ctags +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@ +CPP = @CPP@ +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@ +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_GTEST_CONFIGURE_FLAG = @DISTCHECK_GTEST_CONFIGURE_FLAG@ +DISTCHECK_KEA_SHELL_CONFIGURE_FLAG = @DISTCHECK_KEA_SHELL_CONFIGURE_FLAG@ +DISTCHECK_LOG4CPLUS_CONFIGURE_FLAG = @DISTCHECK_LOG4CPLUS_CONFIGURE_FLAG@ +DISTCHECK_PERFDHCP_CONFIGURE_FLAG = @DISTCHECK_PERFDHCP_CONFIGURE_FLAG@ +DISTCHECK_PREMIUM_CONFIGURE_FLAG = @DISTCHECK_PREMIUM_CONFIGURE_FLAG@ +DLLTOOL = @DLLTOOL@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +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_SYSREPO = @HAVE_SYSREPO@ +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@ +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_REPO_PATH = @SR_REPO_PATH@ +STRIP = @STRIP@ +SYSREPOCPP_VERSION = @SYSREPOCPP_VERSION@ +SYSREPO_CPPFLAGS = @SYSREPO_CPPFLAGS@ +SYSREPO_INCLUDEDIR = @SYSREPO_INCLUDEDIR@ +SYSREPO_LIBS = @SYSREPO_LIBS@ +SYSREPO_VERSION = @SYSREPO_VERSION@ +USE_LCOV = @USE_LCOV@ +VALGRIND = @VALGRIND@ +VERSION = @VERSION@ +WARNING_GCC_44_STRICT_ALIASING_CFLAG = @WARNING_GCC_44_STRICT_ALIASING_CFLAG@ +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 \ + $(BOOST_INCLUDES) \ + -DLOGGING_SPEC_FILE=\"$(abs_top_srcdir)/src/lib/dhcpsrv/logging.spec\" +AM_CXXFLAGS = $(KEA_CXXFLAGS) +@USE_STATIC_LINK_TRUE@AM_LDFLAGS = -static +CLEANFILES = *.gcno *.gcda +TESTS_ENVIRONMENT = \ + $(LIBTOOL) --mode=execute $(VALGRIND_COMMAND) + +@HAVE_GTEST_TRUE@libeval_unittests_SOURCES = boolean_unittest.cc \ +@HAVE_GTEST_TRUE@ context_unittest.cc dependency_unittest.cc \ +@HAVE_GTEST_TRUE@ evaluate_unittest.cc token_unittest.cc \ +@HAVE_GTEST_TRUE@ run_unittests.cc +@HAVE_GTEST_TRUE@libeval_unittests_CXXFLAGS = $(AM_CXXFLAGS) +@HAVE_GTEST_TRUE@libeval_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) +@HAVE_GTEST_TRUE@libeval_unittests_LDFLAGS = $(AM_LDFLAGS) $(CRYPTO_LDFLAGS) $(GTEST_LDFLAGS) +@HAVE_GTEST_TRUE@libeval_unittests_LDADD = \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/eval/libkea-eval.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/cc/libkea-cc.la \ +@HAVE_GTEST_TRUE@ $(top_builddir)/src/lib/dhcp/libkea-dhcp++.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/hooks/libkea-hooks.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@ $(top_builddir)/src/lib/testutils/libkea-testutils.la \ +@HAVE_GTEST_TRUE@ $(CRYPTO_LIBS) $(LOG4CPLUS_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/lib/eval/tests/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/lib/eval/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 + +libeval_unittests$(EXEEXT): $(libeval_unittests_OBJECTS) $(libeval_unittests_DEPENDENCIES) $(EXTRA_libeval_unittests_DEPENDENCIES) + @rm -f libeval_unittests$(EXEEXT) + $(AM_V_CXXLD)$(libeval_unittests_LINK) $(libeval_unittests_OBJECTS) $(libeval_unittests_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libeval_unittests-boolean_unittest.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libeval_unittests-context_unittest.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libeval_unittests-dependency_unittest.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libeval_unittests-evaluate_unittest.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libeval_unittests-run_unittests.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libeval_unittests-token_unittest.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 $@ $< + +libeval_unittests-boolean_unittest.o: boolean_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-boolean_unittest.o -MD -MP -MF $(DEPDIR)/libeval_unittests-boolean_unittest.Tpo -c -o libeval_unittests-boolean_unittest.o `test -f 'boolean_unittest.cc' || echo '$(srcdir)/'`boolean_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-boolean_unittest.Tpo $(DEPDIR)/libeval_unittests-boolean_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='boolean_unittest.cc' object='libeval_unittests-boolean_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-boolean_unittest.o `test -f 'boolean_unittest.cc' || echo '$(srcdir)/'`boolean_unittest.cc + +libeval_unittests-boolean_unittest.obj: boolean_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-boolean_unittest.obj -MD -MP -MF $(DEPDIR)/libeval_unittests-boolean_unittest.Tpo -c -o libeval_unittests-boolean_unittest.obj `if test -f 'boolean_unittest.cc'; then $(CYGPATH_W) 'boolean_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/boolean_unittest.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-boolean_unittest.Tpo $(DEPDIR)/libeval_unittests-boolean_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='boolean_unittest.cc' object='libeval_unittests-boolean_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-boolean_unittest.obj `if test -f 'boolean_unittest.cc'; then $(CYGPATH_W) 'boolean_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/boolean_unittest.cc'; fi` + +libeval_unittests-context_unittest.o: context_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-context_unittest.o -MD -MP -MF $(DEPDIR)/libeval_unittests-context_unittest.Tpo -c -o libeval_unittests-context_unittest.o `test -f 'context_unittest.cc' || echo '$(srcdir)/'`context_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-context_unittest.Tpo $(DEPDIR)/libeval_unittests-context_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='context_unittest.cc' object='libeval_unittests-context_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-context_unittest.o `test -f 'context_unittest.cc' || echo '$(srcdir)/'`context_unittest.cc + +libeval_unittests-context_unittest.obj: context_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-context_unittest.obj -MD -MP -MF $(DEPDIR)/libeval_unittests-context_unittest.Tpo -c -o libeval_unittests-context_unittest.obj `if test -f 'context_unittest.cc'; then $(CYGPATH_W) 'context_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/context_unittest.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-context_unittest.Tpo $(DEPDIR)/libeval_unittests-context_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='context_unittest.cc' object='libeval_unittests-context_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-context_unittest.obj `if test -f 'context_unittest.cc'; then $(CYGPATH_W) 'context_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/context_unittest.cc'; fi` + +libeval_unittests-dependency_unittest.o: dependency_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-dependency_unittest.o -MD -MP -MF $(DEPDIR)/libeval_unittests-dependency_unittest.Tpo -c -o libeval_unittests-dependency_unittest.o `test -f 'dependency_unittest.cc' || echo '$(srcdir)/'`dependency_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-dependency_unittest.Tpo $(DEPDIR)/libeval_unittests-dependency_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='dependency_unittest.cc' object='libeval_unittests-dependency_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-dependency_unittest.o `test -f 'dependency_unittest.cc' || echo '$(srcdir)/'`dependency_unittest.cc + +libeval_unittests-dependency_unittest.obj: dependency_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-dependency_unittest.obj -MD -MP -MF $(DEPDIR)/libeval_unittests-dependency_unittest.Tpo -c -o libeval_unittests-dependency_unittest.obj `if test -f 'dependency_unittest.cc'; then $(CYGPATH_W) 'dependency_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/dependency_unittest.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-dependency_unittest.Tpo $(DEPDIR)/libeval_unittests-dependency_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='dependency_unittest.cc' object='libeval_unittests-dependency_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-dependency_unittest.obj `if test -f 'dependency_unittest.cc'; then $(CYGPATH_W) 'dependency_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/dependency_unittest.cc'; fi` + +libeval_unittests-evaluate_unittest.o: evaluate_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-evaluate_unittest.o -MD -MP -MF $(DEPDIR)/libeval_unittests-evaluate_unittest.Tpo -c -o libeval_unittests-evaluate_unittest.o `test -f 'evaluate_unittest.cc' || echo '$(srcdir)/'`evaluate_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-evaluate_unittest.Tpo $(DEPDIR)/libeval_unittests-evaluate_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='evaluate_unittest.cc' object='libeval_unittests-evaluate_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-evaluate_unittest.o `test -f 'evaluate_unittest.cc' || echo '$(srcdir)/'`evaluate_unittest.cc + +libeval_unittests-evaluate_unittest.obj: evaluate_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-evaluate_unittest.obj -MD -MP -MF $(DEPDIR)/libeval_unittests-evaluate_unittest.Tpo -c -o libeval_unittests-evaluate_unittest.obj `if test -f 'evaluate_unittest.cc'; then $(CYGPATH_W) 'evaluate_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/evaluate_unittest.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-evaluate_unittest.Tpo $(DEPDIR)/libeval_unittests-evaluate_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='evaluate_unittest.cc' object='libeval_unittests-evaluate_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-evaluate_unittest.obj `if test -f 'evaluate_unittest.cc'; then $(CYGPATH_W) 'evaluate_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/evaluate_unittest.cc'; fi` + +libeval_unittests-token_unittest.o: token_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-token_unittest.o -MD -MP -MF $(DEPDIR)/libeval_unittests-token_unittest.Tpo -c -o libeval_unittests-token_unittest.o `test -f 'token_unittest.cc' || echo '$(srcdir)/'`token_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-token_unittest.Tpo $(DEPDIR)/libeval_unittests-token_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='token_unittest.cc' object='libeval_unittests-token_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-token_unittest.o `test -f 'token_unittest.cc' || echo '$(srcdir)/'`token_unittest.cc + +libeval_unittests-token_unittest.obj: token_unittest.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-token_unittest.obj -MD -MP -MF $(DEPDIR)/libeval_unittests-token_unittest.Tpo -c -o libeval_unittests-token_unittest.obj `if test -f 'token_unittest.cc'; then $(CYGPATH_W) 'token_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/token_unittest.cc'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-token_unittest.Tpo $(DEPDIR)/libeval_unittests-token_unittest.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='token_unittest.cc' object='libeval_unittests-token_unittest.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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-token_unittest.obj `if test -f 'token_unittest.cc'; then $(CYGPATH_W) 'token_unittest.cc'; else $(CYGPATH_W) '$(srcdir)/token_unittest.cc'; fi` + +libeval_unittests-run_unittests.o: run_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-run_unittests.o -MD -MP -MF $(DEPDIR)/libeval_unittests-run_unittests.Tpo -c -o libeval_unittests-run_unittests.o `test -f 'run_unittests.cc' || echo '$(srcdir)/'`run_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libeval_unittests-run_unittests.Tpo $(DEPDIR)/libeval_unittests-run_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='run_unittests.cc' object='libeval_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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-run_unittests.o `test -f 'run_unittests.cc' || echo '$(srcdir)/'`run_unittests.cc + +libeval_unittests-run_unittests.obj: run_unittests.cc +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -MT libeval_unittests-run_unittests.obj -MD -MP -MF $(DEPDIR)/libeval_unittests-run_unittests.Tpo -c -o libeval_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)/libeval_unittests-run_unittests.Tpo $(DEPDIR)/libeval_unittests-run_unittests.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='run_unittests.cc' object='libeval_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) $(libeval_unittests_CPPFLAGS) $(CPPFLAGS) $(libeval_unittests_CXXFLAGS) $(CXXFLAGS) -c -o libeval_unittests-run_unittests.obj `if test -f 'run_unittests.cc'; then $(CYGPATH_W) 'run_unittests.cc'; else $(CYGPATH_W) '$(srcdir)/run_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)/libeval_unittests-boolean_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-context_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-dependency_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-evaluate_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-run_unittests.Po + -rm -f ./$(DEPDIR)/libeval_unittests-token_unittest.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)/libeval_unittests-boolean_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-context_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-dependency_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-evaluate_unittest.Po + -rm -f ./$(DEPDIR)/libeval_unittests-run_unittests.Po + -rm -f ./$(DEPDIR)/libeval_unittests-token_unittest.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/lib/eval/tests/boolean_unittest.cc b/src/lib/eval/tests/boolean_unittest.cc new file mode 100644 index 0000000..f620dae --- /dev/null +++ b/src/lib/eval/tests/boolean_unittest.cc @@ -0,0 +1,64 @@ +// Copyright (C) 2016-2017 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 <eval/eval_context.h> +#include <eval/evaluate.h> +#include <eval/token.h> +#include <dhcp/pkt4.h> + +#include <boost/shared_ptr.hpp> +#include <boost/scoped_ptr.hpp> +#include <gtest/gtest.h> + +using namespace std; +using namespace isc::dhcp; + +namespace { + +/// @brief Test fixture for testing booleans. +class BooleanTest : public ::testing::Test { +public: + void check(const string& expr, bool expected) { + EvalContext eval(Option::V4); + ASSERT_TRUE(eval.parseString(expr)); + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + if (expected) { + EXPECT_TRUE(evaluateBool(eval.expression, *pkt4)); + } else { + EXPECT_FALSE(evaluateBool(eval.expression, *pkt4)); + } + } +}; + +// A group of tests +TEST_F(BooleanTest, tests) { + // true and (false or false) + check("('a' == 'a') and (('a' == 'b') or ('b' == 'a'))", false); + // (true and false) or false + check("(('a' == 'a') and ('a' == 'b')) or ('b' == 'a')", false); + // not true + check("not ('a' == 'a')", false); + // not false + check("not ('a' == 'b')", true); + // true and true and true and false + check("('a' == 'a') and ('b' == 'b') and ('c' == 'c') and ('a' == 'c')", + false); + // false or false or false or true + check("('a' == 'b') or ('a' == 'c') or ('b' == 'c') or ('b' == 'b')", + true); + // true or false or false or false + check("('a' == 'a') or ('a' == 'b') or ('a' == 'c') or ('b' == 'c')", + true); + // not (true or false) + check("not (('a' == 'a') or ('a' == 'b'))", false); + // not (true and false) + check("not (('a' == 'a') and ('a' == 'b'))", true); + // (not true) and false + check("(not ('a' == 'a')) and ('a' == 'b')",false); +} + +}; diff --git a/src/lib/eval/tests/context_unittest.cc b/src/lib/eval/tests/context_unittest.cc new file mode 100644 index 0000000..26ef533 --- /dev/null +++ b/src/lib/eval/tests/context_unittest.cc @@ -0,0 +1,2118 @@ +// Copyright (C) 2015-2021 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 <eval/eval_context.h> +#include <eval/token.h> +#include <dhcp/option.h> +#include <dhcp/pkt4.h> +#include <asiolink/io_address.h> + +#include <boost/shared_ptr.hpp> +#include <boost/scoped_ptr.hpp> +#include <gtest/gtest.h> + +using namespace std; +using namespace isc::asiolink; +using namespace isc::dhcp; + +namespace { + +/// @brief Test class for testing EvalContext aka class test parsing +class EvalContextTest : public ::testing::Test { +public: + /// @brief constructor to initialize members + EvalContextTest() : ::testing::Test(), + universe_(Option::V4), parsed_(false) + { } + + /// @brief checks if the given token is a string with the expected value + void checkTokenString(const TokenPtr& token, const std::string& expected) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenString> str = + boost::dynamic_pointer_cast<TokenString>(token); + ASSERT_TRUE(str); + + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + + EXPECT_NO_THROW(token->evaluate(*pkt4, values)); + + ASSERT_EQ(1, values.size()); + + EXPECT_EQ(expected, values.top()); + } + + /// @brief checks if the given token is a hex string with the expected value + void checkTokenHexString(const TokenPtr& token, + const std::string& expected) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenHexString> hex = + boost::dynamic_pointer_cast<TokenHexString>(token); + ASSERT_TRUE(hex); + + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + + EXPECT_NO_THROW(token->evaluate(*pkt4, values)); + + ASSERT_EQ(1, values.size()); + + EXPECT_EQ(expected, values.top()); + } + + /// @brief checks if the given token is an IP address with the expected value + void checkTokenIpAddress(const TokenPtr& token, + const std::string& expected) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenIpAddress> ipaddr = + boost::dynamic_pointer_cast<TokenIpAddress>(token); + ASSERT_TRUE(ipaddr); + + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + + EXPECT_NO_THROW(token->evaluate(*pkt4, values)); + + ASSERT_EQ(1, values.size()); + string value = values.top(); + + boost::scoped_ptr<IOAddress> exp_ip; + ASSERT_NO_THROW(exp_ip.reset(new IOAddress(expected))); + vector<uint8_t> exp_addr = exp_ip->toBytes(); + ASSERT_EQ(exp_addr.size(), value.size()); + EXPECT_EQ(0, memcmp(&exp_addr[0], &value[0], value.size())); + } + + /// @brief checks if the given token is an equal operator + void checkTokenEq(const TokenPtr& token) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenEqual> eq = + boost::dynamic_pointer_cast<TokenEqual>(token); + EXPECT_TRUE(eq); + } + + /// @brief Checks if the given token is integer with expected value + /// + /// @param token token to be inspected + /// @param exp_value expected integer value of the token + void checkTokenInteger(const TokenPtr& token, uint32_t exp_value) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenInteger> integer = + boost::dynamic_pointer_cast<TokenInteger>(token); + ASSERT_TRUE(integer); + EXPECT_EQ(exp_value, integer->getInteger()); + } + + /// @brief checks if the given token is an option with the expected code + /// and representation type + /// @param token token to be checked + /// @param expected_code expected option code + /// @param expected_repr expected representation (text, hex, exists) + void checkTokenOption(const TokenPtr& token, + uint16_t expected_code, + TokenOption::RepresentationType expected_repr) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenOption> opt = + boost::dynamic_pointer_cast<TokenOption>(token); + ASSERT_TRUE(opt); + + EXPECT_EQ(expected_code, opt->getCode()); + EXPECT_EQ(expected_repr, opt->getRepresentation()); + } + + /// @brief check if the given token is relay4 with the expected code + /// and representation type + /// @param token token to be checked + /// @param expected_code expected option code + /// @param expected_repr expected representation (text, hex, exists) + void checkTokenRelay4(const TokenPtr& token, + uint16_t expected_code, + TokenOption::RepresentationType expected_repr) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenRelay4Option> relay4 = + boost::dynamic_pointer_cast<TokenRelay4Option>(token); + EXPECT_TRUE(relay4); + + if (relay4) { + EXPECT_EQ(expected_code, relay4->getCode()); + EXPECT_EQ(expected_repr, relay4->getRepresentation()); + } + } + + /// @brief checks if the given token is a TokenRelay6Option with + /// the correct nesting level, option code and representation. + /// @param token token to be checked + /// @param expected_level expected nesting level + /// @param expected_code expected option code + /// @param expected_repr expected representation (text, hex, exists) + void checkTokenRelay6Option(const TokenPtr& token, + int8_t expected_level, + uint16_t expected_code, + TokenOption::RepresentationType expected_repr) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenRelay6Option> opt = + boost::dynamic_pointer_cast<TokenRelay6Option>(token); + ASSERT_TRUE(opt); + + EXPECT_EQ(expected_level, opt->getNest()); + EXPECT_EQ(expected_code, opt->getCode()); + EXPECT_EQ(expected_repr, opt->getRepresentation()); + } + + /// @brief This tests attempts to parse the expression then checks + /// if the number of tokens is correct and the TokenRelay6Option + /// is as expected. + /// + /// @param expr expression to be parsed + /// @param exp_level expected level to be parsed + /// @param exp_code expected option code to be parsed + /// @param exp_repr expected representation to be parsed + /// @param exp_tokens expected number of tokens + void testRelay6Option(const std::string& expr, + int8_t exp_level, + uint16_t exp_code, + TokenOption::RepresentationType exp_repr, + int exp_tokens) { + EvalContext eval(Option::V6); + + // parse the expression + try { + parsed_ = eval.parseString(expr); + } + catch (const EvalParseError& ex) { + FAIL() <<"Exception thrown: " << ex.what(); + return; + } + + // Parsing should succeed and return a token. + EXPECT_TRUE(parsed_); + + // There should be the expected number of tokens. + ASSERT_EQ(exp_tokens, eval.expression.size()); + + // checked that the first token is TokenRelay6Option and that + // is has the correct attributes + checkTokenRelay6Option(eval.expression.at(0), exp_level, exp_code, exp_repr); + } + + /// @brief check if the given token is a Pkt of specified type + /// @param token token to be checked + /// @param type expected type of the Pkt metadata + void checkTokenPkt(const TokenPtr& token, TokenPkt::MetadataType type) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenPkt> pkt = + boost::dynamic_pointer_cast<TokenPkt>(token); + ASSERT_TRUE(pkt); + + EXPECT_EQ(type, pkt->getType()); + } + + /// @brief Test that verifies access to the DHCP packet metadata. + /// + /// This test attempts to parse the expression, will check if the number + /// of tokens is exactly as expected and then will try to verify if the + /// first token represents the expected metadata in DHCP packet. + /// + /// @param expr expression to be parsed + /// @param exp_type expected metadata type to be parsed + /// @param exp_tokens expected number of tokens + void testPktMetadata(const std::string& expr, + TokenPkt::MetadataType exp_type, + int exp_tokens) { + EvalContext eval(Option::V6); + + // Parse the expression. + try { + parsed_ = eval.parseString(expr); + } + catch (const EvalParseError& ex) { + FAIL() << "Exception thrown: " << ex.what(); + return; + } + + // Parsing should succeed and return a token. + EXPECT_TRUE(parsed_); + + // There should be exactly the expected number of tokens. + ASSERT_EQ(exp_tokens, eval.expression.size()); + + // Check that the first token is TokenPkt instance and has correct type. + checkTokenPkt(eval.expression.at(0), exp_type); + } + + /// @brief checks if the given token is Pkt4 of specified type + /// @param token token to be checked + /// @param type expected type of the Pkt4 field + void checkTokenPkt4(const TokenPtr& token, TokenPkt4::FieldType type) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenPkt4> pkt = + boost::dynamic_pointer_cast<TokenPkt4>(token); + ASSERT_TRUE(pkt); + + EXPECT_EQ(type, pkt->getType()); + } + + /// @brief Test that verifies access to the DHCPv4 packet fields. + /// + /// This test attempts to parse the expression, will check if the number + /// of tokens is exactly as expected and then will try to verify if the + /// first token represents the expected field in DHCPv4 packet. + /// + /// @param expr expression to be parsed + /// @param exp_type expected field type to be parsed + /// @param exp_tokens expected number of tokens + void testPkt4Field(const std::string& expr, + TokenPkt4::FieldType exp_type, + int exp_tokens) { + EvalContext eval(Option::V4); + + // Parse the expression. + try { + parsed_ = eval.parseString(expr); + } + catch (const EvalParseError& ex) { + FAIL() << "Exception thrown: " << ex.what(); + return; + } + + // Parsing should succeed and return a token. + EXPECT_TRUE(parsed_); + + // There should be exactly the expected number of tokens. + ASSERT_EQ(exp_tokens, eval.expression.size()); + + // Check that the first token is TokenPkt4 instance and has correct type. + checkTokenPkt4(eval.expression.at(0), exp_type); + } + + /// @brief checks if the given token is Pkt6 of specified type + /// @param token token to be checked + /// @param exp_type expected type of the Pkt6 field + void checkTokenPkt6(const TokenPtr& token, + TokenPkt6::FieldType exp_type) { + ASSERT_TRUE(token); + + boost::shared_ptr<TokenPkt6> pkt = + boost::dynamic_pointer_cast<TokenPkt6>(token); + + ASSERT_TRUE(pkt); + + EXPECT_EQ(exp_type, pkt->getType()); + } + + /// @brief Test that verifies access to the DHCPv6 packet fields. + /// + /// This test attempts to parse the expression, will check if the number + /// of tokens is exactly as planned and then will try to verify if the + /// first token represents expected the field in DHCPv6 packet. + /// + /// @param expr expression to be parsed + /// @param exp_type expected field type to be parsed + /// @param exp_tokens expected number of tokens + void testPkt6Field(const std::string& expr, + TokenPkt6::FieldType exp_type, + int exp_tokens) { + EvalContext eval(Option::V6); + + // Parse the expression. + try { + parsed_ = eval.parseString(expr); + } + catch (const EvalParseError& ex) { + FAIL() << "Exception thrown: " << ex.what(); + return; + } + + // Parsing should succeed and return a token. + EXPECT_TRUE(parsed_); + + // There should be the requested number of tokens + ASSERT_EQ(exp_tokens, eval.expression.size()); + + // Check that the first token is TokenPkt6 instance and has correct type. + checkTokenPkt6(eval.expression.at(0), exp_type); + } + + /// @brief checks if the given token is a TokenRelay with the + /// correct nesting level and field type. + /// @param token token to be checked + /// @param expected_level expected nesting level + /// @param expected_code expected option code + /// @param expected_repr expected representation (text, hex, exists) + void checkTokenRelay6Field(const TokenPtr& token, + int8_t expected_level, + TokenRelay6Field::FieldType expected_type) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenRelay6Field> opt = + boost::dynamic_pointer_cast<TokenRelay6Field>(token); + ASSERT_TRUE(opt); + + EXPECT_EQ(expected_level, opt->getNest()); + EXPECT_EQ(expected_type, opt->getType()); + } + + /// @brief This tests attempts to parse the expression then checks + /// if the number of tokens is correct and the TokenRelay6Field is as + /// expected. + /// + /// @param expr expression to be parsed + /// @param exp_level expected level to be parsed + /// @param exp_type expected field type to be parsed + /// @param exp_tokens expected number of tokens + void testRelay6Field(const std::string& expr, + int8_t exp_level, + TokenRelay6Field::FieldType exp_type, + int exp_tokens) { + EvalContext eval(Option::V6); + + // parse the expression + try { + parsed_ = eval.parseString(expr); + } + catch (const EvalParseError& ex) { + FAIL() <<"Exception thrown: " << ex.what(); + return; + } + + // Parsing should succeed and return a token. + EXPECT_TRUE(parsed_); + + // There should be the expected number of tokens. + ASSERT_EQ(exp_tokens, eval.expression.size()); + + // checked that the first token is TokenRelay6Field and that + // is has the correct attributes + checkTokenRelay6Field(eval.expression.at(0), exp_level, exp_type); + } + + /// @brief checks if the given token is a TokenMember with the + /// correct client class name. + /// @param token token to be checked + /// @param expected_client_class expected client class name + void checkTokenMember(const TokenPtr& token, + const std::string& expected_client_class) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenMember> member = + boost::dynamic_pointer_cast<TokenMember>(token); + ASSERT_TRUE(member); + + EXPECT_EQ(expected_client_class, member->getClientClass()); + } + + /// @brief This tests attempts to parse the expression then checks + /// if the number of tokens is correct and the TokenMember is as + /// expected. + /// + /// @param expr expression to be parsed + /// @param check_defined closure checking if the client class is defined + /// @param exp_client_class expected client class name to be parsed + /// @param exp_tokens expected number of tokens + void testMember(const std::string& expr, + EvalContext::CheckDefined check_defined, + const std::string& exp_client_class, + int exp_tokens) { + EvalContext eval(Option::V6, check_defined); + + // parse the expression + try { + parsed_ = eval.parseString(expr); + } + catch (const EvalParseError& ex) { + FAIL() <<"Exception thrown: " << ex.what(); + return; + } + + // Parsing should succeed and return a token. + EXPECT_TRUE(parsed_); + + // There should be the expected number of tokens. + ASSERT_EQ(exp_tokens, eval.expression.size()); + + // checked that the first token is TokenRelay6Field and that + // is has the correct attributes + checkTokenMember(eval.expression.at(0), exp_client_class); + } + + /// @brief checks if the given token is a substring operator + void checkTokenSubstring(const TokenPtr& token) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenSubstring> sub = + boost::dynamic_pointer_cast<TokenSubstring>(token); + EXPECT_TRUE(sub); + } + + /// @brief checks if the given token is a concat operator + void checkTokenConcat(const TokenPtr& token) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenConcat> conc = + boost::dynamic_pointer_cast<TokenConcat>(token); + EXPECT_TRUE(conc); + } + + /// @brief checks if the given token is an ifelse operator + void checkTokenIfElse(const TokenPtr& token) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenIfElse> alt = + boost::dynamic_pointer_cast<TokenIfElse>(token); + EXPECT_TRUE(alt); + } + + /// @brief checks if the given token is a hexstring operator + void checkTokenToHexString(const TokenPtr& token) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenToHexString> tohex = + boost::dynamic_pointer_cast<TokenToHexString>(token); + EXPECT_TRUE(tohex); + } + + /// @brief checks if the given token is an addrtotext operator + void checkTokenIpAddressToText(const TokenPtr& token, + const std::string& expected) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenIpAddressToText> addrtotext = + boost::dynamic_pointer_cast<TokenIpAddressToText>(token); + EXPECT_TRUE(addrtotext); + + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + + std::vector<uint8_t> bytes = IOAddress(expected).toBytes(); + values.push(std::string(bytes.begin(), bytes.end())); + + EXPECT_NO_THROW(token->evaluate(*pkt4, values)); + + ASSERT_EQ(1, values.size()); + string value = values.top(); + + EXPECT_EQ(value, expected); + } + + /// @brief checks if the given token is a inttotext operator + template <typename IntegerType, typename TokenIntegerType> + void checkTokenIntToText(const TokenPtr& token, + const std::string& expected) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenIntegerType> inttotext = + boost::dynamic_pointer_cast<TokenIntegerType>(token); + EXPECT_TRUE(inttotext); + + Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, 12345)); + ValueStack values; + + IntegerType n; + + try { + if (is_signed<IntegerType>()) { + n = static_cast<IntegerType>(boost::lexical_cast<int32_t>(expected)); + } else { + n = static_cast<IntegerType>(boost::lexical_cast<uint32_t>(expected)); + } + } catch (const boost::bad_lexical_cast& e) { + FAIL() << "invalid value " << expected << ", error: " << e.what(); + } + + values.push(std::string(const_cast<const char*>(reinterpret_cast<char*>(&n)), sizeof(IntegerType))); + + EXPECT_NO_THROW(token->evaluate(*pkt4, values)); + + ASSERT_EQ(1, values.size()); + string value = values.top(); + + EXPECT_EQ(value, expected); + } + + /// @brief checks if the given expression raises the expected message + /// when it is parsed. + void checkError(const string& expr, const string& msg) { + EvalContext eval(universe_); + parsed_ = false; + try { + parsed_ = eval.parseString(expr); + FAIL() << "Expected EvalParseError but nothing was raised"; + } + catch (const EvalParseError& ex) { + EXPECT_EQ(msg, ex.what()); + EXPECT_FALSE(parsed_); + } + catch (...) { + FAIL() << "Expected EvalParseError but something else was raised"; + } + } + + /// @brief sets the universe + /// @note the default universe is DHCPv4 + void setUniverse(const Option::Universe& universe) { + universe_ = universe; + } + + /// @brief Checks if the given token is TokenVendor and has expected characteristics + /// @param token token to be checked + /// @param exp_vendor_id expected vendor-id (aka enterprise number) + /// @param exp_repr expected representation (either 'exists' or 'hex') + /// @param exp_option_code expected option code (ignored if 0) + void checkTokenVendor(const TokenPtr& token, uint32_t exp_vendor_id, + uint16_t exp_option_code, + TokenOption::RepresentationType exp_repr) { + ASSERT_TRUE(token); + + boost::shared_ptr<TokenVendor> vendor = + boost::dynamic_pointer_cast<TokenVendor>(token); + + ASSERT_TRUE(vendor); + + EXPECT_EQ(exp_vendor_id, vendor->getVendorId()); + EXPECT_EQ(exp_repr, vendor->getRepresentation()); + EXPECT_EQ(exp_option_code, vendor->getCode()); + } + + /// @brief Tests if specified token vendor expression can be parsed + /// + /// This test assumes the first token will be token vendor. Any additional + /// tokens are ignored. Tests expressions: + /// vendor[1234].option[234].hex + /// vendor[1234].option[234].exists + /// + /// @param expr expression to be parsed + /// @param u universe (V4 or V6) + /// @param vendor_id expected vendor-id (aka enterprise number) + /// @param option_code expected option code (ignored if 0) + /// @param expected_repr expected representation (either 'exists' or 'hex') + void testVendor(const std::string& expr, Option::Universe u, + uint32_t vendor_id, uint16_t option_code, + TokenOption::RepresentationType expected_repr) { + EvalContext eval(u); + + EXPECT_NO_THROW(parsed_ = eval.parseString(expr)); + EXPECT_TRUE(parsed_); + + // We need at least one token, we will evaluate the first one. + ASSERT_FALSE(eval.expression.empty()); + + checkTokenVendor(eval.expression.at(0), vendor_id, option_code, expected_repr); + } + + /// @brief Checks if token is really a TokenVendor, that the vendor_id was + /// stored properly and that it has expected representation + /// + /// This test is able to handle expressions similar to: + /// vendor[4491].option[1].hex + /// vendor[4491].option[1].exists + /// vendor[4491].exists + /// vendor[*].exists + /// + /// @param expr expression to be parsed + /// @param u universe (V4 or V6) + /// @param vendor_id expected vendor-id (aka enterprise number) + /// @param expected_repr expected representation (either 'exists' or 'hex') + void testVendor(const std::string& expr, Option::Universe u, + uint32_t vendor_id, + TokenOption::RepresentationType expected_repr) { + testVendor(expr, u, vendor_id, 0, expected_repr); + } + + /// @brief Tests if the expression parses into token vendor that returns enterprise-id + /// + /// This test is able to handle expressions similar to: + /// vendor.enterprise + /// + /// @param expr expression to be parsed + /// @param u universe (V4 or V6) + void testVendorEnterprise(const std::string& expr, + Option::Universe u) { + EvalContext eval(u); + + EXPECT_NO_THROW(parsed_ = eval.parseString(expr)); + EXPECT_TRUE(parsed_); + + ASSERT_FALSE(eval.expression.empty()); + + boost::shared_ptr<TokenVendor> vendor = + boost::dynamic_pointer_cast<TokenVendor>(eval.expression.at(0)); + + ASSERT_TRUE(vendor); + EXPECT_EQ(TokenVendor::ENTERPRISE_ID, vendor->getField()); + } + + /// @brief This test checks if vendor-class token is correct + /// + /// This test checks if EXISTS representation is set correctly. + /// It covers cases like: + /// - vendor-class[4491].exists + /// - vendor-class[*].exists + /// + /// @param expr expression to be parsed + /// @param u universe (V4 or V6) + /// @param vendor_id expected vendor-id (aka enterprise number) + void testVendorClass(const std::string& expr, + Option::Universe u, uint32_t vendor_id) { + EvalContext eval(u); + + EXPECT_NO_THROW(parsed_ = eval.parseString(expr)); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(1, eval.expression.size()); + checkTokenVendorClass(eval.expression.at(0), vendor_id, 0, TokenOption::EXISTS, + TokenVendor::EXISTS); + } + + /// @brief Tests if specified token vendor class expression can be parsed + /// + /// This test assumes the first token will be token vendor-class. + /// Any additional tokens are ignored. Tests expressions: + /// - vendor-class[4491].exists + /// - vendor-class[*].exists + /// - vendor-class[4491].data + /// - vendor-class[4491].data[3] + /// + /// @param expr expression to be parsed + /// @param u universe (V4 or V6) + /// @param vendor_id expected vendor-id (aka enterprise number) + /// @param index expected data index + void testVendorClass(const std::string& expr, Option::Universe u, + uint32_t vendor_id, uint16_t index) { + EvalContext eval(u); + + EXPECT_NO_THROW(parsed_ = eval.parseString(expr)); + EXPECT_TRUE(parsed_); + + // Make sure there's at least one token + ASSERT_FALSE(eval.expression.empty()); + + // The first token should be TokenVendorClass, let's take a closer look. + checkTokenVendorClass(eval.expression.at(0), vendor_id, index, + TokenOption::HEXADECIMAL, TokenVendor::DATA); + + } + + /// @brief Tests if the expression parses into vendor class token that + /// returns enterprise-id. + /// + /// This test is able to handle expressions similar to: + /// - vendor-class.enterprise + /// + /// @param expr expression to be parsed + /// @param u universe (V4 or V6) + void testVendorClassEnterprise(const std::string& expr, + Option::Universe u) { + EvalContext eval(u); + + EXPECT_NO_THROW(parsed_ = eval.parseString(expr)); + EXPECT_TRUE(parsed_); + + // Make sure there's at least one token + ASSERT_FALSE(eval.expression.empty()); + + // The first token should be TokenVendorClass, let's take a closer look. + checkTokenVendorClass(eval.expression.at(0), 0, 0, TokenOption::HEXADECIMAL, + TokenVendor::ENTERPRISE_ID); + } + + /// @brief Checks if the given token is TokenVendorClass and has expected characteristics + /// + /// @param token token to be checked + /// @param vendor_id expected vendor-id (aka enterprise number) + /// @param index expected index (used for data field only) + /// @param repr expected representation (either 'exists' or 'hex') + /// @param field expected field (none, enterprise or data) + void checkTokenVendorClass(const TokenPtr& token, uint32_t vendor_id, + uint16_t index, TokenOption::RepresentationType repr, + TokenVendor::FieldType field) { + ASSERT_TRUE(token); + + boost::shared_ptr<TokenVendorClass> vendor = + boost::dynamic_pointer_cast<TokenVendorClass>(token); + + ASSERT_TRUE(vendor); + + EXPECT_EQ(vendor_id, vendor->getVendorId()); + EXPECT_EQ(index, vendor->getDataIndex()); + EXPECT_EQ(repr, vendor->getRepresentation()); + EXPECT_EQ(field, vendor->getField()); + } + + /// @brief checks if the given token is a sub-option with the expected + /// parent option and sub-option codes and representation type + /// @param token token to be checked + /// @param expected_code expected option code + /// @param expected_sub_code expected sub-option code + /// @param expected_repr expected representation (text, hex, exists) + void checkTokenSubOption(const TokenPtr& token, + uint16_t expected_code, + uint16_t expected_sub_code, + TokenOption::RepresentationType expected_repr) { + ASSERT_TRUE(token); + boost::shared_ptr<TokenSubOption> sub = + boost::dynamic_pointer_cast<TokenSubOption>(token); + ASSERT_TRUE(sub); + + EXPECT_EQ(expected_code, sub->getCode()); + EXPECT_EQ(expected_sub_code, sub->getSubCode()); + EXPECT_EQ(expected_repr, sub->getRepresentation()); + } + + Option::Universe universe_; ///< Universe (V4 or V6) + bool parsed_; ///< Parsing status + +}; + +// Test the error method without location +TEST_F(EvalContextTest, error) { + + EvalContext eval(Option::V4); + + EXPECT_THROW(eval.error("an error"), EvalParseError); +} + +// Test the fatal method +TEST_F(EvalContextTest, fatal) { + + EvalContext eval(Option::V4); + + EXPECT_THROW(eval.fatal("a fatal error"), isc::Unexpected); +} + +// Test the convertOptionCode method with an illegal input +TEST_F(EvalContextTest, badOptionCode) { + + EvalContext eval(Option::V4); + + // the option code must be a number + EXPECT_THROW(eval.convertOptionCode("bad", location(position())), + EvalParseError); +} + +// Test the convertNestLevelNumber method with an illegal input +TEST_F(EvalContextTest, badNestLevelNumber) { + + EvalContext eval(Option::V4); + + // the nest level number must be a number + EXPECT_THROW(eval.convertNestLevelNumber("bad", location(position())), + EvalParseError); +} + +// Test the parsing of a basic expression +TEST_F(EvalContextTest, basic) { + + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[123].text == 'MSFT'")); + EXPECT_TRUE(parsed_); +} + +// Test the parsing of a string terminal +TEST_F(EvalContextTest, string) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("'foo' == 'bar'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + + checkTokenString(tmp1, "foo"); + checkTokenString(tmp2, "bar"); +} + +// Test the parsing of a basic expression using integers +TEST_F(EvalContextTest, integer) { + + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("substring(option[123].text, 0, 2) == '42'")); + EXPECT_TRUE(parsed_); +} + +// Test the parsing of a hexstring terminal +TEST_F(EvalContextTest, hexstring) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("0x666f6f == 'foo'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + + checkTokenHexString(tmp, "foo"); +} + +// Test the parsing of a hexstring terminal with an odd number of +// hexadecimal digits +TEST_F(EvalContextTest, oddHexstring) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("0X7 == 'foo'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + + checkTokenHexString(tmp, "\a"); +} + +// Test the parsing of an IPv4 address +TEST_F(EvalContextTest, ipaddress4) { + EvalContext eval(Option::V6); + + EXPECT_NO_THROW(parsed_ = eval.parseString("10.0.0.1 == 'foo'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + + checkTokenIpAddress(tmp, "10.0.0.1"); +} + +// Test the parsing of an IPv6 address +TEST_F(EvalContextTest, ipaddress6) { + EvalContext eval(Option::V6); + + EXPECT_NO_THROW(parsed_ = eval.parseString("2001:db8::1 == 'foo'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + + checkTokenIpAddress(tmp, "2001:db8::1"); +} + +// Test the parsing of an IPv4 compatible IPv6 address +TEST_F(EvalContextTest, ipaddress46) { + EvalContext eval(Option::V6); + + EXPECT_NO_THROW(parsed_ = eval.parseString("::10.0.0.1 == 'foo'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + + checkTokenIpAddress(tmp, "::10.0.0.1"); +} + +// Test the parsing of the unspecified IPv6 address +TEST_F(EvalContextTest, ipaddress6unspec) { + EvalContext eval(Option::V6); + + EXPECT_NO_THROW(parsed_ = eval.parseString(":: == 'foo'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + + checkTokenIpAddress(tmp, "::"); +} + +// Test the parsing of an IPv6 prefix +TEST_F(EvalContextTest, ipaddress6prefix) { + EvalContext eval(Option::V6); + + EXPECT_NO_THROW(parsed_ = eval.parseString("2001:db8:: == 'foo'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + + checkTokenIpAddress(tmp, "2001:db8::"); +} + +// Test the parsing of an equal expression +TEST_F(EvalContextTest, equal) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("'foo' == 'bar'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + + checkTokenString(tmp1, "foo"); + checkTokenString(tmp2, "bar"); + checkTokenEq(tmp3); +} + +// Test the parsing of an option terminal +TEST_F(EvalContextTest, option) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[123].text == 'foo'")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenOption(eval.expression.at(0), 123, TokenOption::TEXTUAL); +} + +// Test parsing of an option identified by name. +TEST_F(EvalContextTest, optionWithName) { + EvalContext eval(Option::V4); + + // Option 'host-name' is a standard DHCPv4 option defined in the libdhcp++. + EXPECT_NO_THROW(parsed_ = eval.parseString("option[host-name].text == 'foo'")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenOption(eval.expression.at(0), 12, TokenOption::TEXTUAL); +} + +// Test parsing of an option existence +TEST_F(EvalContextTest, optionExists) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[100].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(1, eval.expression.size()); + checkTokenOption(eval.expression.at(0), 100, TokenOption::EXISTS); +} + +// Test checking that whitespace can surround option name. +TEST_F(EvalContextTest, optionWithNameAndWhitespace) { + EvalContext eval(Option::V4); + + // Option 'host-name' is a standard DHCPv4 option defined in the libdhcp++. + EXPECT_NO_THROW(parsed_ = eval.parseString("option[ host-name ].text == 'foo'")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenOption(eval.expression.at(0), 12, TokenOption::TEXTUAL); +} + +// Test checking that newlines can surround option name. +TEST_F(EvalContextTest, optionWithNameAndNewline) { + EvalContext eval(Option::V4); + + // Option 'host-name' is a standard DHCPv4 option defined in the libdhcp++. + EXPECT_NO_THROW(parsed_ = + eval.parseString("option[\n host-name \n ].text == \n'foo'")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenOption(eval.expression.at(0), 12, TokenOption::TEXTUAL); +} + +// Test parsing of an option represented as hexadecimal string. +TEST_F(EvalContextTest, optionHex) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[123].hex == 0x666F6F")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenOption(eval.expression.at(0), 123, TokenOption::HEXADECIMAL); +} + +// This test checks that the relay4[code].hex can be used in expressions. +TEST_F(EvalContextTest, relay4Option) { + + EvalContext eval(Option::V4); + EXPECT_NO_THROW(parsed_ = + eval.parseString("relay4[13].hex == 'thirteen'")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + + checkTokenRelay4(tmp1, 13, TokenOption::HEXADECIMAL); + checkTokenString(tmp2, "thirteen"); + checkTokenEq(tmp3); +} + +// This test check the relay4[code].exists is supported. +TEST_F(EvalContextTest, relay4Exists) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("relay4[13].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(1, eval.expression.size()); + checkTokenRelay4(eval.expression.at(0), 13, TokenOption::EXISTS); +} + +// Verify that relay4[13] is not usable in v6 +// There will be a separate relay accessor for v6. +TEST_F(EvalContextTest, relay4Error) { + universe_ = Option::V6; + + checkError("relay4[13].hex == 'thirteen'", + "<string>:1.1-6: relay4 can only be used in DHCPv4."); +} + +// Test the parsing of a relay6 option +TEST_F(EvalContextTest, relay6Option) { + EvalContext eval(Option::V6); + + testRelay6Option("relay6[0].option[123].text == 'foo'", + 0, 123, TokenOption::TEXTUAL, 3); +} + +// Test the parsing of existence for a relay6 option +TEST_F(EvalContextTest, relay6OptionExists) { + EvalContext eval(Option::V6); + + testRelay6Option("relay6[1].option[75].exists", + 1, 75, TokenOption::EXISTS, 1); +} + +// Test the parsing of hex for a relay6 option +TEST_F(EvalContextTest, relay6OptionHex) { + EvalContext eval(Option::V6); + + testRelay6Option("relay6[2].option[85].hex == 'foo'", + 2, 85, TokenOption::HEXADECIMAL, 3); +} + +// Test the parsing of a relay6 option in reverse order +TEST_F(EvalContextTest, relay6OptionReverse) { + EvalContext eval(Option::V6); + + testRelay6Option("relay6[-1].option[123].text == 'foo'", + -1, 123, TokenOption::TEXTUAL, 3); +} + +// Test the nest level of a relay6 option should be in [-32..32[ +TEST_F(EvalContextTest, relay6OptionLimits) { + EvalContext eval(Option::V6); + + // max nest level is hop count limit minus one so 31 + testRelay6Option("relay6[31].option[123].text == 'foo'", + 31, 123, TokenOption::TEXTUAL, 3); + + universe_ = Option::V6; + + checkError("relay6[32].option[123].text == 'foo'", + "<string>:1.8-9: Nest level has invalid value in 32. " + "Allowed range: -32..31"); + + // min nest level is minus hop count limit + testRelay6Option("relay6[-32].option[123].text == 'foo'", + -32, 123, TokenOption::TEXTUAL, 3); + + checkError("relay6[-33].option[123].text == 'foo'", + "<string>:1.8-10: Nest level has invalid value in -33. Allowed range: -32..31"); +} + +// Verify that relay6[13].option is not usable in v4 +TEST_F(EvalContextTest, relay6OptionError) { + universe_ = Option::V4; + + // nest_level is reduced first so raises the error + // (if we'd like to get a relay6 error we have to insert an + // intermediate action to check the universe) + checkError("relay6[0].option[123].text == 'foo'", + "<string>:1.8: Nest level invalid for DHCPv4 packets"); +} + +// Tests whether iface metadata in DHCP can be accessed. +TEST_F(EvalContextTest, pktMetadataIface) { + testPktMetadata("pkt.iface == 'eth0'", TokenPkt::IFACE, 3); +} + +// Tests whether src metadata in DHCP can be accessed. +TEST_F(EvalContextTest, pktMetadataSrc) { + testPktMetadata("pkt.src == fe80::1", TokenPkt::SRC, 3); +} + +// Tests whether dst metadata in DHCP can be accessed. +TEST_F(EvalContextTest, pktMetadataDst) { + testPktMetadata("pkt.dst == fe80::2", TokenPkt::DST, 3); +} + +// Tests whether len metadata in DHCP can be accessed. +TEST_F(EvalContextTest, pktMetadataLen) { + testPktMetadata("pkt.len == 0x00000100", TokenPkt::LEN, 3); +} + +// Tests whether chaddr field in DHCPv4 can be accessed. +TEST_F(EvalContextTest, pkt4FieldChaddr) { + testPkt4Field("pkt4.mac == 0x000102030405", TokenPkt4::CHADDR, 3); +} + +// Tests whether chaddr field in DHCPv4 can be accessed and converted. +TEST_F(EvalContextTest, pkt4FieldChaddrHexa) { + testPkt4Field("hexstring(pkt4.mac, ':') == '00:01:02:03:04:05'", + TokenPkt4::CHADDR, 5); +} + +// Tests whether hlen field in DHCPv4 can be accessed. +TEST_F(EvalContextTest, pkt4FieldHlen) { + testPkt4Field("pkt4.hlen == 0x6", TokenPkt4::HLEN, 3); +} + +// Tests whether htype field in DHCPv4 can be accessed. +TEST_F(EvalContextTest, pkt4FieldHtype) { + testPkt4Field("pkt4.htype == 0x1", TokenPkt4::HTYPE, 3); +} + +// Tests whether ciaddr field in DHCPv4 can be accessed. +TEST_F(EvalContextTest, pkt4FieldCiaddr) { + testPkt4Field("pkt4.ciaddr == 192.0.2.1", TokenPkt4::CIADDR, 3); +} + +// Tests whether giaddr field in DHCPv4 can be accessed. +TEST_F(EvalContextTest, pkt4FieldGiaddr) { + testPkt4Field("pkt4.giaddr == 192.0.2.1", TokenPkt4::GIADDR, 3); +} + +// Tests whether yiaddr field in DHCPv4 can be accessed. +TEST_F(EvalContextTest, pkt4FieldYiaddr) { + testPkt4Field("pkt4.yiaddr == 192.0.2.1", TokenPkt4::YIADDR, 3); +} + +// Tests whether siaddr field in DHCPv4 can be accessed. +TEST_F(EvalContextTest, pkt4FieldSiaddr) { + testPkt4Field("pkt4.siaddr == 192.0.2.1", TokenPkt4::SIADDR, 3); +} + +// Tests whether message type field in DHCPv6 can be accessed. +TEST_F(EvalContextTest, pkt6FieldMsgtype) { + testPkt6Field("pkt6.msgtype == 1", TokenPkt6::MSGTYPE, 3); +} + +// Tests whether transaction id field in DHCPv6 can be accessed. +TEST_F(EvalContextTest, pkt6FieldTransid) { + testPkt6Field("pkt6.transid == 1", TokenPkt6::TRANSID, 3); +} + +// Tests if the linkaddr field in a Relay6 encapsulation can be accessed. +TEST_F(EvalContextTest, relay6FieldLinkAddr) { + testRelay6Field("relay6[0].linkaddr == ::", + 0, TokenRelay6Field::LINKADDR, 3); +} + +// Tests if the peeraddr field in a Relay6 encapsulation can be accessed. +TEST_F(EvalContextTest, relay6FieldPeerAddr) { + testRelay6Field("relay6[1].peeraddr == ::", + 1, TokenRelay6Field::PEERADDR, 3); +} + +// Verify that relay6[0].<field> is not usable in v4 +TEST_F(EvalContextTest, relay6FieldError) { + universe_ = Option::V4; + + // nest_level is reduced first so raises the error + // (if we'd like to get a relay6 error we have to insert an + // intermediate action to check the universe) + checkError("relay6[0].linkaddr == ::", + "<string>:1.8: Nest level invalid for DHCPv4 packets"); +} + +// Tests parsing of member with defined class +TEST_F(EvalContextTest, member) { + auto check_defined = [](const ClientClass& cc) { return (cc == "foo"); }; + testMember("member('foo')", check_defined, "foo", 1); +} + +// Test parsing of member with not defined class +TEST_F(EvalContextTest, memberError) { + auto check_defined = [](const ClientClass& cc) { return (cc == "foo"); }; + EvalContext eval(Option::V6, check_defined); + parsed_ = false; + try { + parsed_ = eval.parseString("member('bar')"); + FAIL() << "Expected EvalParseError but nothing was raised"; + } + catch (const EvalParseError& ex) { + EXPECT_EQ("<string>:1.8-12: Not defined client class 'bar'", + std::string(ex.what())); + EXPECT_FALSE(parsed_); + } + catch (...) { + FAIL() << "Expected EvalParseError but something else was raised"; + } +} + +// Test parsing of logical operators +TEST_F(EvalContextTest, logicalOps) { + // option.exists + EvalContext eval0(Option::V4); + EXPECT_NO_THROW(parsed_ = eval0.parseString("option[123].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(1, eval0.expression.size()); + TokenPtr token = eval0.expression.at(0); + ASSERT_TRUE(token); + boost::shared_ptr<TokenOption> opt = + boost::dynamic_pointer_cast<TokenOption>(token); + EXPECT_TRUE(opt); + + // not + EvalContext evaln(Option::V4); + EXPECT_NO_THROW(parsed_ = evaln.parseString("not option[123].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(2, evaln.expression.size()); + token = evaln.expression.at(1); + ASSERT_TRUE(token); + boost::shared_ptr<TokenNot> tnot = + boost::dynamic_pointer_cast<TokenNot>(token); + EXPECT_TRUE(tnot); + + // and + EvalContext evala(Option::V4); + EXPECT_NO_THROW(parsed_ = + evala.parseString("option[123].exists and option[123].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, evala.expression.size()); + token = evala.expression.at(2); + ASSERT_TRUE(token); + boost::shared_ptr<TokenAnd> tand = + boost::dynamic_pointer_cast<TokenAnd>(token); + EXPECT_TRUE(tand); + + // or + EvalContext evalo(Option::V4); + EXPECT_NO_THROW(parsed_ = + evalo.parseString("option[123].exists or option[123].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, evalo.expression.size()); + token = evalo.expression.at(2); + ASSERT_TRUE(token); + boost::shared_ptr<TokenOr> tor = + boost::dynamic_pointer_cast<TokenOr>(token); + EXPECT_TRUE(tor); +} + +// Test parsing of logical operators with precedence +TEST_F(EvalContextTest, logicalPrecedence) { + // not precedence > and precedence + EvalContext evalna(Option::V4); + EXPECT_NO_THROW(parsed_ = + evalna.parseString("not option[123].exists and option[123].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(4, evalna.expression.size()); + TokenPtr token = evalna.expression.at(3); + ASSERT_TRUE(token); + boost::shared_ptr<TokenAnd> tand = + boost::dynamic_pointer_cast<TokenAnd>(token); + EXPECT_TRUE(tand); + + // and precedence > or precedence + EvalContext evaloa(Option::V4); + EXPECT_NO_THROW(parsed_ = + evaloa.parseString("option[123].exists or option[123].exists " + "and option[123].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(5, evaloa.expression.size()); + token = evaloa.expression.at(4); + ASSERT_TRUE(token); + boost::shared_ptr<TokenOr> tor = + boost::dynamic_pointer_cast<TokenOr>(token); + EXPECT_TRUE(tor); +} + +// Test parsing of logical operators with parentheses (same than +// with precedence but using parentheses to overwrite precedence) +TEST_F(EvalContextTest, logicalParentheses) { + // not precedence > and precedence + EvalContext evalna(Option::V4); + EXPECT_NO_THROW(parsed_ = + evalna.parseString("not (option[123].exists and option[123].exists)")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(4, evalna.expression.size()); + TokenPtr token = evalna.expression.at(3); + ASSERT_TRUE(token); + boost::shared_ptr<TokenNot> tnot = + boost::dynamic_pointer_cast<TokenNot>(token); + EXPECT_TRUE(tnot); + + // and precedence > or precedence + EvalContext evaloa(Option::V4); + EXPECT_NO_THROW(parsed_ = + evaloa.parseString("(option[123].exists or option[123].exists) " + "and option[123].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(5, evaloa.expression.size()); + token = evaloa.expression.at(4); + ASSERT_TRUE(token); + boost::shared_ptr<TokenAnd> tand = + boost::dynamic_pointer_cast<TokenAnd>(token); + EXPECT_TRUE(tand); +} + +// Test the parsing of a substring expression +TEST_F(EvalContextTest, substring) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("substring('foobar',2,all) == 'obar'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(6, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenString(tmp1, "foobar"); + checkTokenString(tmp2, "2"); + checkTokenString(tmp3, "all"); + checkTokenSubstring(tmp4); +} + +// Test the parsing of a concat expression +TEST_F(EvalContextTest, concat) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("concat('foo','bar') == 'foobar'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(5, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + + checkTokenString(tmp1, "foo"); + checkTokenString(tmp2, "bar"); + checkTokenConcat(tmp3); +} + +// Test the parsing of a plus expression +TEST_F(EvalContextTest, plus) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("'foo' + 'bar' == 'foobar'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(5, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + + checkTokenString(tmp1, "foo"); + checkTokenString(tmp2, "bar"); + checkTokenConcat(tmp3); +} + +// Test the parsing of plus expressions +TEST_F(EvalContextTest, assocPlus) { + EvalContext eval(Option::V4); + + // Operator '+' is (left) associative + EXPECT_NO_THROW(parsed_ = + eval.parseString("'a' + 'b' + 'c' == 'abc'")); + + ASSERT_EQ(7, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + TokenPtr tmp5 = eval.expression.at(4); + + checkTokenString(tmp1, "a"); + checkTokenString(tmp2, "b"); + checkTokenConcat(tmp3); + checkTokenString(tmp4, "c"); + checkTokenConcat(tmp5); +} + +// Test the parsing of plus expressions with enforced associativity +TEST_F(EvalContextTest, assocRightPlus) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("'a' + ('b' + 'c') == 'abc'")); + + ASSERT_EQ(7, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + TokenPtr tmp5 = eval.expression.at(4); + + checkTokenString(tmp1, "a"); + checkTokenString(tmp2, "b"); + checkTokenString(tmp3, "c"); + checkTokenConcat(tmp4); + checkTokenConcat(tmp5); +} + +// Test the parsing of an ifelse expression +TEST_F(EvalContextTest, ifElse) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("ifelse('foo' == 'bar', 'us', 'them') == 'you'")); + + ASSERT_EQ(8, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(2); + TokenPtr tmp2 = eval.expression.at(3); + TokenPtr tmp3 = eval.expression.at(4); + TokenPtr tmp4 = eval.expression.at(5); + + checkTokenEq(tmp1); + checkTokenString(tmp2, "us"); + checkTokenString(tmp3, "them"); + checkTokenIfElse(tmp4); +} + +// Test the parsing of a plus operator and ifelse expression +TEST_F(EvalContextTest, plusIfElse) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("'foo' + ifelse('a' == 'a', 'bar', '') == 'foobar'")); + + ASSERT_EQ(10, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + TokenPtr tmp5 = eval.expression.at(4); + TokenPtr tmp6 = eval.expression.at(5); + TokenPtr tmp7 = eval.expression.at(6); + TokenPtr tmp8 = eval.expression.at(7); + + checkTokenString(tmp1, "foo"); + checkTokenString(tmp2, "a"); + checkTokenString(tmp3, "a"); + checkTokenEq(tmp4); + checkTokenString(tmp5, "bar"); + checkTokenString(tmp6, ""); + checkTokenIfElse(tmp7); + checkTokenConcat(tmp8); +} + +// Test the parsing of a hexstring expression +TEST_F(EvalContextTest, toHexString) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = + eval.parseString("hexstring(0x666f,'-') == '66-6f'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(5, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + + checkTokenHexString(tmp1, "fo"); + checkTokenString(tmp2, "-"); + checkTokenToHexString(tmp3); +} + +// Test the parsing of an addrtotext expression +TEST_F(EvalContextTest, addressToText) { + { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("addrtotext(10.0.0.1) == '10.0.0.1'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenIpAddress(tmp1, "10.0.0.1"); + checkTokenIpAddressToText(tmp2, "10.0.0.1"); + checkTokenString(tmp3, "10.0.0.1"); + checkTokenEq(tmp4); + } + + { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("addrtotext(2001:db8::1) == '2001:db8::1'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenIpAddress(tmp1, "2001:db8::1"); + checkTokenIpAddressToText(tmp2, "2001:db8::1"); + checkTokenString(tmp3, "2001:db8::1"); + checkTokenEq(tmp4); + } +} + +// Test the parsing of a int8_t expression +TEST_F(EvalContextTest, int8ToText) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("int8totext(255) == '-1'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenInteger(tmp1, 255); + checkTokenIntToText<int8_t, TokenInt8ToText>(tmp2, "-1"); + checkTokenString(tmp3, "-1"); + checkTokenEq(tmp4); +} + +// Test the parsing of a int16_t expression +TEST_F(EvalContextTest, int16ToText) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("int16totext(65535) == '-1'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenInteger(tmp1, 65535); + checkTokenIntToText<int16_t, TokenInt16ToText>(tmp2, "-1"); + checkTokenString(tmp3, "-1"); + checkTokenEq(tmp4); +} + +// Test the parsing of a int32_t expression +TEST_F(EvalContextTest, int32ToText) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("int32totext(4294967295) == '-1'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenInteger(tmp1, 4294967295); + checkTokenIntToText<int32_t, TokenInt32ToText>(tmp2, "-1"); + checkTokenString(tmp3, "-1"); + checkTokenEq(tmp4); +} + +// Test the parsing of a uint8_t expression +TEST_F(EvalContextTest, uint8ToText) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("uint8totext(255) == '255'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenInteger(tmp1, 255); + checkTokenIntToText<uint8_t, TokenUInt8ToText>(tmp2, "255"); + checkTokenString(tmp3, "255"); + checkTokenEq(tmp4); +} + +// Test the parsing of a uint16_t expression +TEST_F(EvalContextTest, uint16ToText) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("uint16totext(65535) == '65535'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenInteger(tmp1, 65535); + checkTokenIntToText<uint16_t, TokenUInt16ToText>(tmp2, "65535"); + checkTokenString(tmp3, "65535"); + checkTokenEq(tmp4); +} + +// Test the parsing of a uint32_t expression +TEST_F(EvalContextTest, uint32ToText) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("uint32totext(4294967295) == '4294967295'")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(4, eval.expression.size()); + + TokenPtr tmp1 = eval.expression.at(0); + TokenPtr tmp2 = eval.expression.at(1); + TokenPtr tmp3 = eval.expression.at(2); + TokenPtr tmp4 = eval.expression.at(3); + + checkTokenInteger(tmp1, 4294967295); + checkTokenIntToText<uint32_t, TokenUInt32ToText>(tmp2, "4294967295"); + checkTokenString(tmp3, "4294967295"); + checkTokenEq(tmp4); +} + +// +// Test some scanner error cases +TEST_F(EvalContextTest, scanErrors) { + checkError("'", "<string>:1.1: Invalid character: '"); + checkError("'\''", "<string>:1.3: Invalid character: '"); + checkError("'\n'", "<string>:1.1: Invalid character: '"); + checkError("0x123h", "<string>:1.6: Invalid character: h"); + checkError(":1", "<string>:1.1: Invalid character: :"); + checkError("=", "<string>:1.1: Invalid character: ="); + + // Typo should be handled as well. + checkError("subtring", "<string>:1.1: Invalid character: s"); + checkError("foo", "<string>:1.1: Invalid character: f"); + checkError(" bar", "<string>:1.2: Invalid character: b"); + checkError("relay[12].hex == 'foo'", "<string>:1.1: Invalid character: r"); + checkError("pkt4.ziaddr", "<string>:1.6: Invalid character: z"); + checkError("members('foo'", "<string>:1.7: Invalid character: s"); +} + +// Tests some scanner/parser error cases +TEST_F(EvalContextTest, scanParseErrors) { + checkError("", "<string>:1.1: syntax error, unexpected end of file"); + checkError(" ", "<string>:1.2: syntax error, unexpected end of file"); + checkError("0x", "<string>:1.2: Invalid character: x"); + checkError("0abc", + "<string>:1.2: Invalid character: a"); + + // This one is a little bid odd. This is a truncated address, so it's not + // recognized as an address. Instead, the first token (10) is recognized as + // an integer. The only thing we can do with integers right now is to + // apply equality or concat operators, so the only possible next token + // are == and +. There's a dot instead, so an error is reported. + checkError("10.0.1", "<string>:1.3: syntax error, unexpected ., " + "expecting == or +"); + + checkError("10.256.0.1", + "<string>:1.1-10: Failed to convert 10.256.0.1 to " + "an IP address."); + checkError(":::", + "<string>:1.1-3: Failed to convert ::: to an IP address."); + checkError("===", "<string>:1.1-2: syntax error, unexpected =="); + checkError("option[-1].text", + "<string>:1.8-9: Option code has invalid " + "value in -1. Allowed range: 0..255"); + checkError("option[256].text", + "<string>:1.8-10: Option code has invalid " + "value in 256. Allowed range: 0..255"); + setUniverse(Option::V6); + checkError("option[65536].text", + "<string>:1.8-12: Option code has invalid " + "value in 65536. Allowed range: 0..65535"); + setUniverse(Option::V4); + checkError("option[12345678901234567890].text", + "<string>:1.8-27: Failed to convert 12345678901234567890 " + "to an integer."); + checkError("option[123]", + "<string>:1.12: syntax error, unexpected end of file," + " expecting ."); + checkError("option[123].text < 'foo'", "<string>:1.18: Invalid" + " character: <"); + checkError("option[-ab].text", "<string>:1.8: Invalid character: -"); + checkError("option[0ab].text", + "<string>:1.9-10: syntax error, unexpected option name, " + "expecting ]"); + checkError("option[ab_].hex", "<string>:1.8: Invalid character: a"); + checkError("option[\nhost-name\n].hex =\n= 'foo'", + "<string>:3.7: Invalid character: ="); + checkError("substring('foo',12345678901234567890,1)", + "<string>:1.17-36: Failed to convert 12345678901234567890 " + "to an integer."); +} + +// Tests some parser error cases +TEST_F(EvalContextTest, parseErrors) { + checkError("'foo''bar'", + "<string>:1.6-10: syntax error, unexpected constant string, " + "expecting == or +"); + checkError("'foo' (", + "<string>:1.7: syntax error, unexpected (, expecting == or +"); + checkError("== 'ab'", "<string>:1.1-2: syntax error, unexpected =="); + checkError("'foo' ==", + "<string>:1.9: syntax error, unexpected end of file"); + checkError("('foo' == 'bar'", + "<string>:1.16: syntax error, unexpected end of file, " + "expecting ) or and or or"); + checkError("('foo' == 'bar') ''", + "<string>:1.18-19: syntax error, unexpected constant string, " + "expecting end of file"); + checkError("not", + "<string>:1.4: syntax error, unexpected end of file"); + checkError("not 'foo'", + "<string>:1.10: syntax error, unexpected end of file, " + "expecting == or +"); + checkError("not()", + "<string>:1.5: syntax error, unexpected )"); + checkError("(not('foo' 'bar')", + "<string>:1.12-16: syntax error, unexpected constant string, " + "expecting ) or == or +"); + checkError("and", + "<string>:1.1-3: syntax error, unexpected and"); + checkError("'foo' and", + "<string>:1.7-9: syntax error, unexpected and, " + "expecting == or +"); + checkError("'foo' == 'bar' and", + "<string>:1.19: syntax error, unexpected end of file"); + checkError("'foo' == 'bar' and ''", + "<string>:1.22: syntax error, unexpected end of file, " + "expecting == or +"); + checkError("or", + "<string>:1.1-2: syntax error, unexpected or"); + checkError("'foo' or", + "<string>:1.7-8: syntax error, unexpected or, " + "expecting == or +"); + checkError("'foo' == 'bar' or", + "<string>:1.18: syntax error, unexpected end of file"); + checkError("'foo' == 'bar' or ''", + "<string>:1.21: syntax error, unexpected end of file, " + "expecting == or +"); + checkError("option 'ab'", + "<string>:1.8-11: syntax error, unexpected " + "constant string, expecting ["); + checkError("option(10) == 'ab'", + "<string>:1.7: syntax error, " + "unexpected (, expecting ["); + checkError("option['ab'].text == 'foo'", + "<string>:1.8-11: syntax error, " + "unexpected constant string, " + "expecting integer or option name"); + checkError("option[ab].text == 'foo'", + "<string>:1.8-9: option 'ab' is not defined"); + checkError("option[0xa].text == 'ab'", + "<string>:1.8-10: syntax error, " + "unexpected constant hexstring, " + "expecting integer or option name"); + checkError("option[10].bin", "<string>:1.12: Invalid character: b"); + checkError("option[boot-size].bin", "<string>:1.19: Invalid character: b"); + checkError("option[10].exists == 'foo'", + "<string>:1.19-20: syntax error, unexpected ==, " + "expecting end of file"); + checkError("substring('foobar') == 'f'", + "<string>:1.19: syntax error, unexpected ), " + "expecting \",\" or +"); + checkError("substring('foobar',3) == 'bar'", + "<string>:1.21: syntax error, unexpected ), expecting \",\""); + checkError("substring('foobar','3',3) == 'bar'", + "<string>:1.20-22: syntax error, unexpected constant string, " + "expecting integer"); + checkError("substring('foobar',1,a) == 'foo'", + "<string>:1.22: Invalid character: a"); + string long_text = "substring('foobar',1,65535) == "; + for (int i = 0; i < (1 << 16); ++i) { + long_text += "0"; + } + long_text += "'"; + checkError(long_text, + "<string>:1.65568: Invalid character: '"); + checkError("concat('foobar') == 'f'", + "<string>:1.16: syntax error, unexpected ), " + "expecting \",\" or +"); + checkError("concat('foo','bar','') == 'foobar'", + "<string>:1.19: syntax error, unexpected \",\", " + "expecting ) or +"); + checkError("ifelse('foo'=='bar','foo')", + "<string>:1.26: syntax error, unexpected ), " + "expecting \",\" or +"); + checkError("ifelse('foo'=='bar','foo','bar','')", + "<string>:1.32: syntax error, unexpected \",\", " + "expecting ) or +"); + checkError("+ 'a' = 'a'", "<string>:1.1: syntax error, unexpected +"); + checkError("'a' + == 'a'", "<string>:1.7-8: syntax error, unexpected =="); + checkError("'a' ++ 'b' == 'ab'", + "<string>:1.6: syntax error, unexpected +"); + checkError("addrtotext(10.0.0.1, 10.0.0.2)", + "<string>:1.20: syntax error, unexpected \",\", expecting ) or +"); + checkError("addrtotext('cafebabecafebabe')", + "<string>:1.31: syntax error, unexpected end of file, expecting == or +"); + checkError("addrtotext('')", + "<string>:1.15: syntax error, unexpected end of file, expecting == or +"); + checkError("int8totext('01', '01')", + "<string>:1.16: syntax error, unexpected \",\", expecting ) or +"); + checkError("int8totext('0123')", + "<string>:1.19: syntax error, unexpected end of file, expecting == or +"); + checkError("int8totext('')", + "<string>:1.15: syntax error, unexpected end of file, expecting == or +"); + checkError("int16totext('0123', '0123')", + "<string>:1.19: syntax error, unexpected \",\", expecting ) or +"); + checkError("int16totext('01')", + "<string>:1.18: syntax error, unexpected end of file, expecting == or +"); + checkError("int16totext('')", + "<string>:1.16: syntax error, unexpected end of file, expecting == or +"); + checkError("int32totext('01234567', '01234567')", + "<string>:1.23: syntax error, unexpected \",\", expecting ) or +"); + checkError("int32totext('01')", + "<string>:1.18: syntax error, unexpected end of file, expecting == or +"); + checkError("int32totext('')", + "<string>:1.16: syntax error, unexpected end of file, expecting == or +"); + checkError("uint8totext('01', '01')", + "<string>:1.17: syntax error, unexpected \",\", expecting ) or +"); + checkError("uint8totext('0123')", + "<string>:1.20: syntax error, unexpected end of file, expecting == or +"); + checkError("uint8totext('')", + "<string>:1.16: syntax error, unexpected end of file, expecting == or +"); + checkError("uint16totext('0123', '0123')", + "<string>:1.20: syntax error, unexpected \",\", expecting ) or +"); + checkError("uint16totext('01')", + "<string>:1.19: syntax error, unexpected end of file, expecting == or +"); + checkError("uint16totext('')", + "<string>:1.17: syntax error, unexpected end of file, expecting == or +"); + checkError("uint32totext('01234567', '01234567')", + "<string>:1.24: syntax error, unexpected \",\", expecting ) or +"); + checkError("uint32totext('01')", + "<string>:1.19: syntax error, unexpected end of file, expecting == or +"); + checkError("uint32totext('')", + "<string>:1.17: syntax error, unexpected end of file, expecting == or +"); +} + +// Tests some type error cases +TEST_F(EvalContextTest, typeErrors) { + checkError("'foobar'", + "<string>:1.9: syntax error, unexpected end of file, " + "expecting == or +"); + checkError("substring('foobar',all,1) == 'foo'", + "<string>:1.20-22: syntax error, unexpected all, " + "expecting integer"); + checkError("substring('foobar',0x32,1) == 'foo'", + "<string>:1.20-23: syntax error, unexpected constant " + "hexstring, expecting integer"); + + // With the #4483 addition, all integers are treated as 4 byte strings, + // so those checks no longer makes sense. Commenting it out. + // checkError("concat('foo',3) == 'foo3'", + // "<string>:1.14: syntax error, unexpected integer"); + // checkError("concat(3,'foo') == '3foo'", + // "<string>:1.8: syntax error, unexpected integer"); + checkError("('foo' == 'bar') == 'false'", + "<string>:1.18-19: syntax error, unexpected ==, " + "expecting end of file"); + checkError("not 'true'", + "<string>:1.11: syntax error, unexpected end of file, " + "expecting == or +"); + checkError("'true' and 'false'", + "<string>:1.8-10: syntax error, unexpected and, " + "expecting == or +"); + checkError("'true' or 'false'", + "<string>:1.8-9: syntax error, unexpected or, " + "expecting == or +"); + + // Ifelse requires a boolean condition and string branches. + checkError("ifelse('foobar','foo','bar')", + "<string>:1.16: syntax error, unexpected \",\", " + "expecting == or +"); + checkError("ifelse('foo'=='bar','foo'=='foo','bar')", + "<string>:1.26-27: syntax error, unexpected ==, " + "expecting \",\" or +"); + checkError("ifelse('foo'=='bar','foo','bar'=='bar')", + "<string>:1.32-33: syntax error, unexpected ==, " + "expecting ) or +"); + + // Member uses quotes around the client class name. + checkError("member(foo)", "<string>:1.8: Invalid character: f"); + + // sub-option by name is not supported. + checkError("option[123].option[host-name].exists", + "<string>:1.20-28: syntax error, unexpected option name, " + "expecting integer"); + + // Addrtotext requires string storing the binary representation of the address. + checkError("addrtotext('192.100.1.1')", + "<string>:1.26: syntax error, unexpected end of file, expecting == or +"); + + // Int8totext requires string storing the binary representation of the 8 bit integer. + checkError("int8totext('0123')", + "<string>:1.19: syntax error, unexpected end of file, expecting == or +"); + + // Int16totext requires string storing the binary representation of the 16 bit integer. + checkError("int16totext('01')", + "<string>:1.18: syntax error, unexpected end of file, expecting == or +"); + + // Int32totext requires string storing the binary representation of the 32 bit integer. + checkError("int32totext('01')", + "<string>:1.18: syntax error, unexpected end of file, expecting == or +"); + + // Uint8totext requires string storing the binary representation of the 8 bit unsigned integer. + checkError("uint8totext('0123')", + "<string>:1.20: syntax error, unexpected end of file, expecting == or +"); + + // Uint16totext requires string storing the binary representation of the 16 bit unsigned integer. + checkError("uint16totext('01')", + "<string>:1.19: syntax error, unexpected end of file, expecting == or +"); + + // Uint32totext requires string storing the binary representation of the 32 bit unsigned integer. + checkError("uint32totext('01')", + "<string>:1.19: syntax error, unexpected end of file, expecting == or +"); +} + +TEST_F(EvalContextTest, vendor4SpecificVendorExists) { + testVendor("vendor[4491].exists", Option::V4, 4491, TokenOption::EXISTS); +} + +TEST_F(EvalContextTest, vendor6SpecificVendorExists) { + testVendor("vendor[4491].exists", Option::V6, 4491, TokenOption::EXISTS); +} + +TEST_F(EvalContextTest, vendor4AnyVendorExists) { + testVendor("vendor[*].exists", Option::V4, 0, TokenOption::EXISTS); +} + +TEST_F(EvalContextTest, vendor6AnyVendorExists) { + testVendor("vendor[*].exists", Option::V6, 0, TokenOption::EXISTS); +} + +TEST_F(EvalContextTest, vendor4enterprise) { + testVendorEnterprise("vendor.enterprise == 0x1234", Option::V4); +} + +TEST_F(EvalContextTest, vendor6enterprise) { + testVendorEnterprise("vendor.enterprise == 0x1234", Option::V6); +} + +TEST_F(EvalContextTest, vendor4SuboptionExists) { + testVendor("vendor[4491].option[1].exists", Option::V4, 4491, 1, TokenOption::EXISTS); +} + +TEST_F(EvalContextTest, vendor6SuboptionExists) { + testVendor("vendor[4491].option[1].exists", Option::V6, 4491, 1, TokenOption::EXISTS); +} + +TEST_F(EvalContextTest, vendor4SuboptionHex) { + testVendor("vendor[4491].option[1].hex == 0x1234", Option::V4, 4491, 1, + TokenOption::HEXADECIMAL); +} + +TEST_F(EvalContextTest, vendor6SuboptionHex) { + testVendor("vendor[4491].option[1].hex == 0x1234", Option::V6, 4491, 1, + TokenOption::HEXADECIMAL); +} + +TEST_F(EvalContextTest, vendorClass4SpecificVendorExists) { + testVendorClass("vendor-class[4491].exists", Option::V4, 4491); +} + +TEST_F(EvalContextTest, vendorClass6SpecificVendorExists) { + testVendorClass("vendor-class[4491].exists", Option::V6, 4491); +} + +TEST_F(EvalContextTest, vendorClass4AnyVendorExists) { + testVendorClass("vendor-class[*].exists", Option::V4, 0); +} + +TEST_F(EvalContextTest, vendorClass6AnyVendorExists) { + testVendorClass("vendor-class[*].exists", Option::V6, 0); +} + +TEST_F(EvalContextTest, vendorClass4enterprise) { + testVendorClassEnterprise("vendor-class.enterprise == 0x1234", Option::V4); +} + +TEST_F(EvalContextTest, vendorClass6enterprise) { + testVendorClassEnterprise("vendor-class.enterprise == 0x1234", Option::V6); +} + +TEST_F(EvalContextTest, vendorClass4SpecificVendorData) { + testVendorClass("vendor-class[4491].data == 0x1234", Option::V4, 4491, 0); +} + +TEST_F(EvalContextTest, vendorClass6SpecificVendorData) { + testVendorClass("vendor-class[4491].data == 0x1234", Option::V6, 4491, 0); +} + +TEST_F(EvalContextTest, vendorClass4AnyVendorData) { + testVendorClass("vendor-class[*].data == 0x1234", Option::V4, 0, 0); +} + +TEST_F(EvalContextTest, vendorClass6AnyVendorData) { + testVendorClass("vendor-class[*].data == 0x1234", Option::V6, 0, 0); +} + +TEST_F(EvalContextTest, vendorClass4DataIndex) { + testVendorClass("vendor-class[4491].data[3] == 0x1234", Option::V4, 4491, 3); +} + +TEST_F(EvalContextTest, vendorClass6DataIndex) { + testVendorClass("vendor-class[4491].data[3] == 0x1234", Option::V6, 4491, 3); +} + +// Test the parsing of a sub-option with parent by code. +TEST_F(EvalContextTest, subOptionWithCode) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[123].option[234].text == 'foo'")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenSubOption(eval.expression.at(0), 123, 234, TokenOption::TEXTUAL); +} + +// Test the parsing of a sub-option with parent by name. +TEST_F(EvalContextTest, subOptionWithName) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[host-name].option[123].text == 'foo'")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenSubOption(eval.expression.at(0), 12, 123, TokenOption::TEXTUAL); +} + +// Test the parsing of a sub-option existence +TEST_F(EvalContextTest, subOptionExists) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[100].option[200].exists")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(1, eval.expression.size()); + checkTokenSubOption(eval.expression.at(0), 100, 200, TokenOption::EXISTS); +} + +// Test parsing of a sub-option represented as hexadecimal string. +TEST_F(EvalContextTest, subOptionHex) { + EvalContext eval(Option::V4); + + EXPECT_NO_THROW(parsed_ = eval.parseString("option[123].option[234].hex == 0x666F6F")); + EXPECT_TRUE(parsed_); + ASSERT_EQ(3, eval.expression.size()); + checkTokenSubOption(eval.expression.at(0), 123, 234, TokenOption::HEXADECIMAL); +} + +// Checks if integer expressions can be parsed and checked for equality. +TEST_F(EvalContextTest, integer1) { + + EvalContext eval(Option::V6); + + EXPECT_NO_THROW(parsed_ = eval.parseString("1 == 2")); + EXPECT_TRUE(parsed_); + + ASSERT_EQ(3, eval.expression.size()); + + TokenPtr tmp = eval.expression.at(0); + ASSERT_TRUE(tmp); + checkTokenInteger(tmp, 1); + tmp = eval.expression.at(1); + + ASSERT_TRUE(tmp); + checkTokenInteger(tmp, 2); +} + +} diff --git a/src/lib/eval/tests/dependency_unittest.cc b/src/lib/eval/tests/dependency_unittest.cc new file mode 100644 index 0000000..19f9121 --- /dev/null +++ b/src/lib/eval/tests/dependency_unittest.cc @@ -0,0 +1,103 @@ +// Copyright (C) 2018,2021 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 <eval/dependency.h> +#include <eval/eval_context.h> +#include <eval/token.h> +#include <dhcp/pkt4.h> +#include <dhcp/pkt6.h> +#include <dhcp/dhcp4.h> +#include <dhcp/dhcp6.h> +#include <dhcp/option_string.h> + +#include <boost/shared_ptr.hpp> +#include <boost/scoped_ptr.hpp> +#include <gtest/gtest.h> + +using namespace std; +using namespace isc::dhcp; + +namespace { + +/// @brief Test fixture for testing dependency. +/// +/// This class provides several convenience objects to be used during testing +/// of the dependency of classification expressions. +class DependencyTest : public ::testing::Test { +public: + /// @brief Constructor + DependencyTest() : result_(true) { + } + + /// @brief Destructor + /// + /// Reset expression and result. + ~DependencyTest() { + e_.reset(); + result_ = false; + } + + ExpressionPtr e_; ///< An expression + + bool result_; ///< A decision +}; + +// This checks the null expression: it should return false. +TEST_F(DependencyTest, nullExpr) { + TokenPtr token; + ASSERT_NO_THROW(result_ = dependOnClass(token, "foobar")); + EXPECT_FALSE(result_); + ASSERT_NO_THROW(result_ = dependOnClass(e_, "foobar")); + EXPECT_FALSE(result_); +} + +// This checks the empty expression: it should return false. +TEST_F(DependencyTest, emptyExpr) { + e_.reset(new Expression()); + ASSERT_NO_THROW(result_ = dependOnClass(e_, "foobar")); + EXPECT_FALSE(result_); +} + +// This checks the { "true" } expression: it should return false. +TEST_F(DependencyTest, trueExpr) { + TokenPtr ttrue; + ASSERT_NO_THROW(ttrue.reset(new TokenString("true"))); + ASSERT_NO_THROW(result_ = dependOnClass(ttrue, "foobar")); + EXPECT_FALSE(result_); + e_.reset(new Expression()); + e_->push_back(ttrue); + ASSERT_NO_THROW(result_ = dependOnClass(e_, "foobar")); + EXPECT_FALSE(result_); +} + +// This checks the { member('not-matching') } expression: +// it should return false. +TEST_F(DependencyTest, notMatching) { + TokenPtr notmatching; + ASSERT_NO_THROW(notmatching.reset(new TokenMember("not-matching"))); + ASSERT_NO_THROW(result_ = dependOnClass(notmatching, "foobar")); + EXPECT_FALSE(result_); + e_.reset(new Expression()); + e_->push_back(notmatching); + ASSERT_NO_THROW(result_ = dependOnClass(e_, "foobar")); + EXPECT_FALSE(result_); +} + +// This checks the { member('foobar') } expression: it should return true. +TEST_F(DependencyTest, matching) { + TokenPtr matching; + ASSERT_NO_THROW(matching.reset(new TokenMember("foobar"))); + ASSERT_NO_THROW(result_ = dependOnClass(matching, "foobar")); + EXPECT_TRUE(result_); + e_.reset(new Expression()); + e_->push_back(matching); + result_ = false; + ASSERT_NO_THROW(result_ = dependOnClass(e_, "foobar")); + EXPECT_TRUE(result_); +} + +}; diff --git a/src/lib/eval/tests/evaluate_unittest.cc b/src/lib/eval/tests/evaluate_unittest.cc new file mode 100644 index 0000000..37c4f83 --- /dev/null +++ b/src/lib/eval/tests/evaluate_unittest.cc @@ -0,0 +1,515 @@ +// Copyright (C) 2015-2018,2021 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 <eval/evaluate.h> +#include <eval/eval_context.h> +#include <eval/token.h> +#include <dhcp/pkt4.h> +#include <dhcp/pkt6.h> +#include <dhcp/dhcp4.h> +#include <dhcp/dhcp6.h> +#include <dhcp/option_string.h> + +#include <boost/shared_ptr.hpp> +#include <boost/scoped_ptr.hpp> +#include <gtest/gtest.h> + +using namespace std; +using namespace isc::dhcp; + +namespace { + +/// @brief Test fixture for testing evaluation. +/// +/// This class provides several convenience objects to be used during testing +/// of the evaluation of classification expressions. +class EvaluateTest : public ::testing::Test { +public: + + /// @brief Initializes Pkt4,Pkt6 and options that can be useful for + /// evaluation tests. + EvaluateTest() { + e_.clear(); + + result_ = false; + + pkt4_.reset(new Pkt4(DHCPDISCOVER, 12345)); + pkt6_.reset(new Pkt6(DHCPV6_SOLICIT, 12345)); + + // Add options with easily identifiable strings in them + option_str4_.reset(new OptionString(Option::V4, 100, "hundred4")); + option_str6_.reset(new OptionString(Option::V6, 100, "hundred6")); + + pkt4_->addOption(option_str4_); + pkt6_->addOption(option_str6_); + } + + Expression e_; ///< An expression + + bool result_; ///< A decision + + Pkt4Ptr pkt4_; ///< A stub DHCPv4 packet + Pkt6Ptr pkt6_; ///< A stub DHCPv6 packet + + OptionPtr option_str4_; ///< A string option for DHCPv4 + OptionPtr option_str6_; ///< A string option for DHCPv6 + + /// @todo: Add more option types here +}; + +// This checks the empty expression: it should raise EvalBadStack +// when evaluated with a Pkt4. (The actual packet is not used) +TEST_F(EvaluateTest, empty4) { + ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalBadStack); +} + +// This checks the empty expression: it should raise EvalBadStack +// when evaluated with a Pkt6. (The actual packet is not used) +TEST_F(EvaluateTest, empty6) { + ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalBadStack); +} + +// This checks the { "false" } expression: it should return false +// when evaluated with a Pkt4. (The actual packet is not used) +TEST_F(EvaluateTest, false4) { + TokenPtr tfalse; + ASSERT_NO_THROW(tfalse.reset(new TokenString("false"))); + e_.push_back(tfalse); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_FALSE(result_); +} + +// This checks the { "false" } expression: it should return false +// when evaluated with a Pkt6. (The actual packet is not used) +TEST_F(EvaluateTest, false6) { + TokenPtr tfalse; + ASSERT_NO_THROW(tfalse.reset(new TokenString("false"))); + e_.push_back(tfalse); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_FALSE(result_); +} + +// This checks the { "true" } expression: it should return true +// when evaluated with a Pkt4. (The actual packet is not used) +TEST_F(EvaluateTest, true4) { + TokenPtr ttrue; + ASSERT_NO_THROW(ttrue.reset(new TokenString("true"))); + e_.push_back(ttrue); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_TRUE(result_); +} + +// This checks the { "true" } expression: it should return true +// when evaluated with a Pkt6. (The actual packet is not used) +TEST_F(EvaluateTest, true6) { + TokenPtr ttrue; + ASSERT_NO_THROW(ttrue.reset(new TokenString("true"))); + e_.push_back(ttrue); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_TRUE(result_); +} + +// This checks the evaluation must lead to "false" or "true" +// with a Pkt4. (The actual packet is not used) +TEST_F(EvaluateTest, bad4) { + TokenPtr bad; + ASSERT_NO_THROW(bad.reset(new TokenString("bad"))); + e_.push_back(bad); + ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalTypeError); +} + +// This checks the evaluation must lead to "false" or "true" +// with a Pkt6. (The actual packet is not used) +TEST_F(EvaluateTest, bad6) { + TokenPtr bad; + ASSERT_NO_THROW(bad.reset(new TokenString("bad"))); + e_.push_back(bad); + ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalTypeError); +} + +// This checks the evaluation must leave only one value on the stack +// with a Pkt4. (The actual packet is not used) +TEST_F(EvaluateTest, two4) { + TokenPtr ttrue; + ASSERT_NO_THROW(ttrue.reset(new TokenString("true"))); + e_.push_back(ttrue); + e_.push_back(ttrue); + ASSERT_THROW(evaluateBool(e_, *pkt4_), EvalBadStack); +} + +// This checks the evaluation must leave only one value on the stack +// with a Pkt6. (The actual packet is not used) +TEST_F(EvaluateTest, two6) { + TokenPtr ttrue; + ASSERT_NO_THROW(ttrue.reset(new TokenString("true"))); + e_.push_back(ttrue); + e_.push_back(ttrue); + ASSERT_THROW(evaluateBool(e_, *pkt6_), EvalBadStack); +} + +// A more complex test evaluated with a Pkt4. (The actual packet is not used) +TEST_F(EvaluateTest, compare4) { + TokenPtr tfoo; + TokenPtr tbar; + TokenPtr tequal; + + ASSERT_NO_THROW(tfoo.reset(new TokenString("foo"))); + e_.push_back(tfoo); + ASSERT_NO_THROW(tbar.reset(new TokenString("bar"))); + e_.push_back(tbar); + ASSERT_NO_THROW(tequal.reset(new TokenEqual())); + e_.push_back(tequal); + + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_FALSE(result_); +} + +// A more complex test evaluated with a Pkt6. (The actual packet is not used) +TEST_F(EvaluateTest, compare6) { + TokenPtr tfoo; + TokenPtr tbar; + TokenPtr tequal; + + ASSERT_NO_THROW(tfoo.reset(new TokenString("foo"))); + e_.push_back(tfoo); + ASSERT_NO_THROW(tbar.reset(new TokenString("bar"))); + e_.push_back(tbar); + ASSERT_NO_THROW(tequal.reset(new TokenEqual())); + e_.push_back(tequal); + + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_FALSE(result_); +} + +// A test using option existence +TEST_F(EvaluateTest, exists) { + TokenPtr toption; + + ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::EXISTS))); + e_.push_back(toption); + + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_TRUE(result_); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_TRUE(result_); +} + +// A test using option non-existence +TEST_F(EvaluateTest, dontExists) { + TokenPtr toption; + + ASSERT_NO_THROW(toption.reset(new TokenOption(101, TokenOption::EXISTS))); + e_.push_back(toption); + + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_FALSE(result_); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_FALSE(result_); +} + +// A test using packets. +TEST_F(EvaluateTest, packet) { + TokenPtr toption; + TokenPtr tstring; + TokenPtr tequal; + + ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::TEXTUAL))); + e_.push_back(toption); + ASSERT_NO_THROW(tstring.reset(new TokenString("hundred4"))); + e_.push_back(tstring); + ASSERT_NO_THROW(tequal.reset(new TokenEqual())); + e_.push_back(tequal); + + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_TRUE(result_); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_FALSE(result_); +} + +// A test which compares option value represented in hexadecimal format. +TEST_F(EvaluateTest, optionHex) { + TokenPtr toption; + TokenPtr tstring; + TokenPtr tequal; + + ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::HEXADECIMAL))); + e_.push_back(toption); + ASSERT_NO_THROW(tstring.reset(new TokenString("hundred4"))); + e_.push_back(tstring); + ASSERT_NO_THROW(tequal.reset(new TokenEqual())); + e_.push_back(tequal); + + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_TRUE(result_); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_FALSE(result_); +} + +// A test using substring on an option. +TEST_F(EvaluateTest, complex) { + TokenPtr toption; + TokenPtr tstart; + TokenPtr tlength; + TokenPtr tsubstring; + TokenPtr tstring; + TokenPtr tequal; + + // Get the option, i.e., "hundred[46]" + ASSERT_NO_THROW(toption.reset(new TokenOption(100, TokenOption::TEXTUAL))); + e_.push_back(toption); + + // Get substring("hundred[46]", 0, 7), i.e., "hundred" + ASSERT_NO_THROW(tstart.reset(new TokenString("0"))); + e_.push_back(tstart); + ASSERT_NO_THROW(tlength.reset(new TokenString("7"))); + e_.push_back(tlength); + ASSERT_NO_THROW(tsubstring.reset(new TokenSubstring())); + e_.push_back(tsubstring); + + // Compare with "hundred" + ASSERT_NO_THROW(tstring.reset(new TokenString("hundred"))); + e_.push_back(tstring); + ASSERT_NO_THROW(tequal.reset(new TokenEqual())); + e_.push_back(tequal); + + // Should return true for v4 and v6 packets + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt4_)); + EXPECT_TRUE(result_); + ASSERT_NO_THROW(result_ = evaluateBool(e_, *pkt6_)); + EXPECT_TRUE(result_); +} + +/// @brief Generic class for parsing expressions and evaluating them. +/// +/// The main purpose of this class is to provide a generic interface to the +/// eval library, so everything (expression parsing and then evaluation for +/// given packets) can be done in one simple call. +/// +/// These tests may be somewhat redundant to other more specialized tests, but +/// the idea here is to mass produce tests that are trivial to write. +class ExpressionsTest : public EvaluateTest { +public: + + /// @brief Checks if expression can be parsed and evaluated to bool + /// + /// There are skeleton packets created in pkt4_ and pkt6_. Make sure you + /// tweak them as needed before calling this method. + /// + /// @param u universe (V4 or V6) + /// @param expr expression to be parsed + /// @param exp_result expected result (true or false) + void testExpression(const Option::Universe& u, const std::string& expr, + const bool exp_result) { + + EvalContext eval(u); + bool result = false; + bool parsed = false; + + EXPECT_NO_THROW(parsed = eval.parseString(expr)) + << " while parsing expression " << expr; + EXPECT_TRUE(parsed) << " for expression " << expr; + + switch (u) { + case Option::V4: + ASSERT_NO_THROW(result = evaluateBool(eval.expression, *pkt4_)) + << " for expression " << expr; + break; + case Option::V6: + ASSERT_NO_THROW(result = evaluateBool(eval.expression, *pkt6_)) + << " for expression " << expr; + break; + } + + EXPECT_EQ(exp_result, result) << " for expression " << expr; + } + + /// @brief Checks if expression can be parsed and evaluated to string + /// + /// There are skeleton packets created in pkt4_ and pkt6_. Make sure you + /// tweak them as needed before calling this method. + /// + /// @param u universe (V4 or V6) + /// @param expr expression to be parsed + /// @param exp_result expected result (string) + void testExpressionString(const Option::Universe& u, const std::string& expr, + const std::string& exp_result) { + + EvalContext eval(u); + string result; + bool parsed = false; + + EXPECT_NO_THROW(parsed = eval.parseString(expr, EvalContext::PARSER_STRING)) + << " while parsing expression " << expr; + EXPECT_TRUE(parsed) << " for expression " << expr; + + switch (u) { + case Option::V4: + ASSERT_NO_THROW(result = evaluateString(eval.expression, *pkt4_)) + << " for expression " << expr; + break; + case Option::V6: + ASSERT_NO_THROW(result = evaluateString(eval.expression, *pkt6_)) + << " for expression " << expr; + break; + } + + EXPECT_EQ(exp_result, result) << " for expression " << expr; + } + + /// @brief Checks that specified expression throws expected exception. + /// + /// @tparam ex exception type expected to be thrown + /// @param expr expression to be evaluated + template<typename ex> + void testExpressionNegative(const std::string& expr, + const Option::Universe& u = Option::V4, + EvalContext::ParserType type = EvalContext::PARSER_BOOL) { + EvalContext eval(u); + + EXPECT_THROW(eval.parseString(expr, type), ex) << "while parsing expression " + << expr; + } +}; + +// This is a quick way to check if certain expressions are valid or not and +// whether the whole expression makes sense. This particular test checks if +// integers can be used properly in expressions. There are many places where +// integers are used. This particular test checks if pkt6.msgtype returns +// something that can be compared with integers. +// +// For basic things we can take advantage of the skeleton packets created in +// EvaluateTest constructors: The packet type is DISCOVER in DHCPv4 and +// SOLICIT in DHCPv6. There is one option added with code 100 and content +// being either "hundred4" or "hundred6" depending on the universe. + +// Tests if pkt6.msgtype returns something that can be compared with integers. +TEST_F(ExpressionsTest, expressionsInteger1) { + testExpression(Option::V6, "pkt6.msgtype == 1", true); + testExpression(Option::V6, "pkt6.msgtype == 2", false); + + testExpression(Option::V6, "pkt6.msgtype == 0x00000001", true); + testExpression(Option::V6, "pkt6.msgtype == 0x00000002", false); +} + +// Tests if pkt6.transid returns something that can be compared with integers. +TEST_F(ExpressionsTest, expressionsInteger2) { + testExpression(Option::V6, "pkt6.transid == 0", false); + testExpression(Option::V6, "pkt6.transid == 12345", true); + testExpression(Option::V6, "pkt6.transid == 12346", false); +} + +// Tests if pkt4.transid returns something that can be compared with integers. +TEST_F(ExpressionsTest, expressionsInteger3) { + testExpression(Option::V4, "pkt4.transid == 0", false); + testExpression(Option::V4, "pkt4.transid == 12345", true); + testExpression(Option::V4, "pkt4.transid == 12346", false); +} + +// Tests if integers can be compared with integers. +TEST_F(ExpressionsTest, expressionsInteger4) { + testExpression(Option::V6, "0 == 0", true); + testExpression(Option::V6, "2 == 3", false); +} + +// Tests if pkt4.hlen and pkt4.htype return values that can be compared with integers. +TEST_F(ExpressionsTest, expressionsPkt4Hlen) { + + // By default there's no hardware set up. The default Pkt4 constructor + // creates HWAddr(), which has hlen=0 and htype set to HTYPE_ETHER. + testExpression(Option::V4, "pkt4.hlen == 0", true); + testExpression(Option::V4, "pkt4.htype == 1", true); + + // Ok, let's initialize the hardware address to something plausible. + const size_t hwaddr_len = 6; + const uint16_t expected_htype = 123; + std::vector<uint8_t> hw(hwaddr_len,0); + for (int i = 0; i < hwaddr_len; i++) { + hw[i] = i + 1; + } + pkt4_->setHWAddr(expected_htype, hwaddr_len, hw); + + testExpression(Option::V4, "pkt4.hlen == 0", false); + testExpression(Option::V4, "pkt4.hlen == 5", false); + testExpression(Option::V4, "pkt4.hlen == 6", true); + testExpression(Option::V4, "pkt4.hlen == 7", false); + + testExpression(Option::V4, "pkt4.htype == 0", false); + testExpression(Option::V4, "pkt4.htype == 122", false); + testExpression(Option::V4, "pkt4.htype == 123", true); + testExpression(Option::V4, "pkt4.htype == 124", false); + + testExpression(Option::V4, "pkt4.mac == 0x010203040506", true); +} + +// Test if expressions message type can be detected in Pkt4. +// It also doubles as a check for integer comparison here. +TEST_F(ExpressionsTest, expressionsPkt4type) { + + // We can inspect the option content directly, but + // it requires knowledge of the option type and its format. + testExpression(Option::V4, "option[53].hex == 0x0", false); + testExpression(Option::V4, "option[53].hex == 0x1", true); + testExpression(Option::V4, "option[53].hex == 0x2", false); + + // It's easier to simply use the pkt4.msgtype + testExpression(Option::V4, "pkt4.msgtype == 0", false); + testExpression(Option::V4, "pkt4.msgtype == 1", true); + testExpression(Option::V4, "pkt4.msgtype == 2", false); +} + +// This tests if inappropriate values (negative, too large) are +// rejected, but extreme values still allowed for uint32_t are ok. +TEST_F(ExpressionsTest, invalidIntegers) { + + // These are the extreme uint32_t values that still should be accepted. + testExpression(Option::V4, "4294967295 == 0", false); + + // Negative integers should be rejected. + testExpressionNegative<EvalParseError>("4294967295 == -1"); + + // Oops, one too much. + testExpressionNegative<EvalParseError>("4294967296 == 0"); +} + +// Tests whether expressions can be evaluated to a string. +TEST_F(ExpressionsTest, evaluateString) { + + // Check that content of the options is returned properly. + testExpressionString(Option::V4, "option[100].hex", "hundred4"); + testExpressionString(Option::V6, "option[100].hex", "hundred6"); + + // Check that content of non-existing option returns empty string. + testExpressionString(Option::V4, "option[200].hex", ""); + testExpressionString(Option::V6, "option[200].hex", ""); + + testExpressionNegative<EvalParseError>("pkt4.msgtype == 1", Option::V4, + EvalContext::PARSER_STRING); + testExpressionNegative<EvalParseError>("pkt6.msgtype == 1", Option::V6, + EvalContext::PARSER_STRING); + + // Check that ifelse works as expecting (it was added explicitly for + // the string evaluation). + testExpressionString(Option::V4, + "ifelse(option[100].exists,'foo','bar')", "foo"); + testExpressionString(Option::V4, + "ifelse(option[200].exists,'foo','bar')", "bar"); + + // Check that ifelse can be chained. + testExpressionString(Option::V4, + "ifelse(option[200].exists,option[200].hex," + "ifelse(option[100].exists," + "option[100].hex,'none?'))", + "hundred4"); + + // Check that hexstring works as expecting. + testExpressionString(Option::V4, "hexstring(0x1234,':')", "12:34"); + testExpressionString(Option::V4, "hexstring(0x56789a,'-')", "56-78-9a"); + testExpressionString(Option::V4, "hexstring(0xbcde,'')", "bcde"); + testExpressionString(Option::V4, "hexstring(0xf01234,'..')", "f0..12..34"); +} + +}; diff --git a/src/lib/eval/tests/run_unittests.cc b/src/lib/eval/tests/run_unittests.cc new file mode 100644 index 0000000..08f04d1 --- /dev/null +++ b/src/lib/eval/tests/run_unittests.cc @@ -0,0 +1,21 @@ +// Copyright (C) 2015 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/lib/eval/tests/token_unittest.cc b/src/lib/eval/tests/token_unittest.cc new file mode 100644 index 0000000..c186658 --- /dev/null +++ b/src/lib/eval/tests/token_unittest.cc @@ -0,0 +1,3479 @@ +// Copyright (C) 2015-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/. + +#include <config.h> +#include <fstream> +#include <eval/token.h> +#include <eval/eval_context.h> +#include <dhcp/pkt4.h> +#include <dhcp/pkt6.h> +#include <dhcp/dhcp4.h> +#include <dhcp/dhcp6.h> +#include <dhcp/option_string.h> +#include <dhcp/option_vendor.h> +#include <dhcp/option_vendor_class.h> +#include <log/logger_manager.h> +#include <log/logger_name.h> +#include <log/logger_support.h> +#include <testutils/log_utils.h> + +#include <boost/algorithm/string/classification.hpp> +#include <boost/algorithm/string/constants.hpp> +#include <boost/algorithm/string/split.hpp> +#include <boost/shared_ptr.hpp> +#include <boost/scoped_ptr.hpp> +#include <gtest/gtest.h> + +#include <arpa/inet.h> + +using namespace std; +using namespace isc::dhcp; +using namespace isc::asiolink; +using namespace isc::log; +using namespace isc::dhcp::test; + +namespace { + +/// @brief Test fixture for testing Tokens. +/// +/// This class provides several convenience objects to be used during testing +/// of the Token family of classes. + +class TokenTest : public LogContentTest { +public: + + /// @brief Initializes Pkt4, Pkt6 and options that can be useful for + /// evaluation tests. + TokenTest() { + pkt4_.reset(new Pkt4(DHCPDISCOVER, 12345)); + pkt6_.reset(new Pkt6(DHCPV6_SOLICIT, 12345)); + + // Add options with easily identifiable strings in them + option_str4_.reset(new OptionString(Option::V4, 100, "hundred4")); + option_str6_.reset(new OptionString(Option::V6, 100, "hundred6")); + + pkt4_->addOption(option_str4_); + pkt6_->addOption(option_str6_); + + // Change this to true if you need extra information about logging + // checks to be printed. + logCheckVerbose(false); + } + + /// @brief Inserts RAI option with several suboptions + /// + /// The structure inserted is: + /// - RAI (option 82) + /// - option 1 (containing string "one") + /// - option 13 (containing string "thirteen") + void insertRelay4Option() { + + // RAI (Relay Agent Information) option + OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS)); + OptionPtr sub1(new OptionString(Option::V4, 1, "one")); + OptionPtr sub13(new OptionString(Option::V4, 13, "thirteen")); + + rai->addOption(sub1); + rai->addOption(sub13); + pkt4_->addOption(rai); + } + + /// @brief Adds relay encapsulations with some suboptions + /// + /// This will add 2 relay encapsulations all will have + /// msg_type of RELAY_FORW + /// Relay 0 (closest to server) will have + /// linkaddr = peeraddr = 0, hop-count = 1 + /// option 100 "hundred.zero", option 101 "hundredone.zero" + /// Relay 1 (closest to client) will have + /// linkaddr 1::1= peeraddr = 1::2, hop-count = 0 + /// option 100 "hundred.one", option 102 "hundredtwo.one" + void addRelay6Encapsulations() { + // First relay + Pkt6::RelayInfo relay0; + relay0.msg_type_ = DHCPV6_RELAY_FORW; + relay0.hop_count_ = 1; + relay0.linkaddr_ = isc::asiolink::IOAddress("::"); + relay0.peeraddr_ = isc::asiolink::IOAddress("::"); + OptionPtr optRelay01(new OptionString(Option::V6, 100, + "hundred.zero")); + OptionPtr optRelay02(new OptionString(Option::V6, 101, + "hundredone.zero")); + + relay0.options_.insert(make_pair(optRelay01->getType(), optRelay01)); + relay0.options_.insert(make_pair(optRelay02->getType(), optRelay02)); + + pkt6_->addRelayInfo(relay0); + // Second relay + Pkt6::RelayInfo relay1; + relay1.msg_type_ = DHCPV6_RELAY_FORW; + relay1.hop_count_ = 0; + relay1.linkaddr_ = isc::asiolink::IOAddress("1::1"); + relay1.peeraddr_ = isc::asiolink::IOAddress("1::2"); + OptionPtr optRelay11(new OptionString(Option::V6, 100, + "hundred.one")); + OptionPtr optRelay12(new OptionString(Option::V6, 102, + "hundredtwo.one")); + + relay1.options_.insert(make_pair(optRelay11->getType(), optRelay11)); + relay1.options_.insert(make_pair(optRelay12->getType(), optRelay12)); + pkt6_->addRelayInfo(relay1); + } + + /// @brief Verify that the relay6 option evaluations work properly + /// + /// Given the nesting level and option code extract the option + /// and compare it to the expected string. + /// + /// @param test_level The nesting level + /// @param test_code The code of the option to extract + /// @param result_addr The expected result of the address as a string + void verifyRelay6Option(const int8_t test_level, + const uint16_t test_code, + const TokenOption::RepresentationType& test_rep, + const std::string& result_string) { + // Create the token + ASSERT_NO_THROW(t_.reset(new TokenRelay6Option(test_level, + test_code, + test_rep))); + + // We should be able to evaluate it + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + + // We should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // And it should match the expected result + // Invalid nesting levels result in a 0 length string + EXPECT_EQ(result_string, values_.top()); + + // Then we clear the stack + clearStack(); + } + + /// @brief Verify that the relay6 field evaluations work properly + /// + /// Given the nesting level, the field to extract and the expected + /// address create a token and evaluate it then compare the addresses + /// + /// @param test_level The nesting level + /// @param test_field The type of the field to extract + /// @param result_addr The expected result of the address as a string + void verifyRelay6Eval(const int8_t test_level, + const TokenRelay6Field::FieldType test_field, + const int result_len, + const uint8_t result_addr[]) { + // Create the token + ASSERT_NO_THROW(t_.reset(new TokenRelay6Field(test_level, test_field))); + + // We should be able to evaluate it + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + + // We should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // And it should match the expected result + // Invalid nesting levels result in a 0 length string + EXPECT_EQ(result_len, values_.top().size()); + if (result_len != 0) { + EXPECT_EQ(0, memcmp(result_addr, &values_.top()[0], result_len)); + } + + // Then we clear the stack + clearStack(); + } + + /// @brief Convenience function. Removes token and values stacks. + /// @param token specifies if the convenience token should be removed or not + void clearStack(bool token = true) { + while (!values_.empty()) { + values_.pop(); + } + if (token) { + t_.reset(); + } + } + + /// @brief Aux. function that stores integer values as 4 byte string. + /// + /// @param value integer value to be stored + /// @return 4 byte long string with encoded value. + string encode(uint32_t value) { + return EvalContext::fromUint32(value); + } + + TokenPtr t_; ///< Just a convenience pointer + + ValueStack values_; ///< evaluated values will be stored here + + Pkt4Ptr pkt4_; ///< A stub DHCPv4 packet + Pkt6Ptr pkt6_; ///< A stub DHCPv6 packet + + OptionPtr option_str4_; ///< A string option for DHCPv4 + OptionPtr option_str6_; ///< A string option for DHCPv6 + + OptionVendorPtr vendor_; ///< Vendor option used during tests + OptionVendorClassPtr vendor_class_; ///< Vendor class option used during tests + + /// @brief Verify that the substring eval works properly + /// + /// This function takes the parameters and sets up the value + /// stack then executes the eval and checks the results. + /// + /// @param test_string The string to operate on + /// @param test_start The position to start when getting a substring + /// @param test_length The length of the substring to get + /// @param result_string The expected result of the eval + /// @param should_throw The eval will throw + void verifySubstringEval(const std::string& test_string, + const std::string& test_start, + const std::string& test_length, + const std::string& result_string, + bool should_throw = false) { + + // create the token + ASSERT_NO_THROW(t_.reset(new TokenSubstring())); + + // push values on stack + values_.push(test_string); + values_.push(test_start); + values_.push(test_length); + + // evaluate the token + if (should_throw) { + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + ASSERT_EQ(0, values_.size()); + } else { + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // verify results + ASSERT_EQ(1, values_.size()); + EXPECT_EQ(result_string, values_.top()); + + // remove result + values_.pop(); + } + } + + /// @brief Verify that the split eval works properly + /// + /// This function takes the parameters and sets up the value + /// stack then executes the eval and checks the results. + /// + /// @param test_string The string to operate on + /// @param test_delimiters The string of delimiter characters to split upon + /// @param test_field The field number of the desired field + /// @param result_string The expected result of the eval + /// @param should_throw The eval will throw + void verifySplitEval(const std::string& test_string, + const std::string& test_delimiters, + const std::string& test_field, + const std::string& result_string, + bool should_throw = false) { + // create the token + ASSERT_NO_THROW(t_.reset(new TokenSplit())); + + // push values on stack + values_.push(test_string); + values_.push(test_delimiters); + values_.push(test_field); + + // evaluate the token + if (should_throw) { + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + ASSERT_EQ(0, values_.size()); + } else { + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // verify results + ASSERT_EQ(1, values_.size()); + EXPECT_EQ(result_string, values_.top()); + + // remove result + values_.pop(); + } + } + + /// @brief Creates vendor-option with specified value and adds it to packet + /// + /// This method creates specified vendor option, removes any existing + /// vendor options and adds the new one to v4 or v6 packet. + /// + /// @param u universe (V4 or V6) + /// @param vendor_id specifies enterprise-id value. + void setVendorOption(Option::Universe u, uint32_t vendor_id) { + vendor_.reset(new OptionVendor(u, vendor_id)); + switch (u) { + case Option::V4: + pkt4_->delOption(DHO_VIVSO_SUBOPTIONS); + pkt4_->addOption(vendor_); + break; + case Option::V6: + pkt6_->delOption(D6O_VENDOR_OPTS); + pkt6_->addOption(vendor_); + break; + } + } + + /// @brief Creates vendor-class option with specified values and adds it to packet + /// + /// This method creates specified vendor-class option, removes any existing + /// vendor class options and adds the new one to v4 or v6 packet. + /// It also creates data tuples with greek alphabet names. + /// + /// @param u universe (V4 or V6) + /// @param vendor_id specifies enterprise-id value. + /// @param tuples_size number of data tuples to create. + void setVendorClassOption(Option::Universe u, uint32_t vendor_id, + size_t tuples_size = 0) { + // Create the option first. + vendor_class_.reset(new OptionVendorClass(u, vendor_id)); + + // Now let's add specified number of data tuples + OpaqueDataTuple::LengthFieldType len = (u == Option::V4?OpaqueDataTuple::LENGTH_1_BYTE: + OpaqueDataTuple::LENGTH_2_BYTES); + const char* content[] = { "alpha", "beta", "delta", "gamma", "epsilon", + "zeta", "eta", "theta", "iota", "kappa" }; + const size_t nb_content = sizeof(content) / sizeof(char*); + ASSERT_TRUE(tuples_size < nb_content); + for (size_t i = 0; i < tuples_size; ++i) { + OpaqueDataTuple tuple(len); + tuple.assign(string(content[i])); + if (u == Option::V4 && i == 0) { + // vendor-class for v4 has a peculiar quirk. The first tuple is being + // added, even if there's no data at all. + vendor_class_->setTuple(0, tuple); + } else { + vendor_class_->addTuple(tuple); + } + } + + switch (u) { + case Option::V4: + pkt4_->delOption(DHO_VIVCO_SUBOPTIONS); + pkt4_->addOption(vendor_class_); + break; + case Option::V6: + pkt6_->delOption(D6O_VENDOR_CLASS); + pkt6_->addOption(vendor_class_); + break; + } + } + + /// @brief Auxiliary function that evaluates tokens and checks result + /// + /// Depending on the universe, either pkt4_ or pkt6_ are supposed to have + /// all the necessary values and options set. The result is checked + /// on the values_ stack. + /// + /// @param u universe (V4 or V6) + /// @param expected_result text representation of the expected outcome + void evaluate(Option::Universe u, std::string expected_result) { + switch (u) { + case Option::V4: + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + break; + case Option::V6: + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + break; + default: + ADD_FAILURE() << "Invalid universe specified."; + } + ASSERT_EQ(1, values_.size()); + EXPECT_EQ(expected_result, values_.top()); + } + + /// @brief Tests if vendor token behaves properly. + /// + /// @param u universe (V4 or V6) + /// @param token_vendor_id enterprise-id used in the token + /// @param option_vendor_id enterprise-id used in option (0 means don't + /// create the option) + /// @param expected_result text representation of the expected outcome + void testVendorExists(Option::Universe u, uint32_t token_vendor_id, + uint32_t option_vendor_id, + const std::string& expected_result) { + // Let's clear any old values, so we can run multiple cases in each test + clearStack(); + + // Create the token + ASSERT_NO_THROW(t_.reset(new TokenVendor(u, token_vendor_id, + TokenOption::EXISTS))); + + // If specified option is non-zero, create it. + if (option_vendor_id) { + setVendorOption(u, option_vendor_id); + } + + evaluate(u, expected_result); + } + + /// @brief Tests if vendor token properly returns enterprise-id. + /// + /// @param u universe (V4 or V6) + /// @param option_vendor_id enterprise-id used in option (0 means don't + /// create the option) + /// @param expected_result text representation of the expected outcome + void testVendorEnterprise(Option::Universe u, uint32_t option_vendor_id, + const std::string& expected_result) { + // Let's clear any old values, so we can run multiple cases in each test + clearStack(); + + ASSERT_NO_THROW(t_.reset(new TokenVendor(u, 0, TokenVendor::ENTERPRISE_ID))); + if (option_vendor_id) { + setVendorOption(u, option_vendor_id); + } + + evaluate(u, expected_result); + } + + /// @brief Tests if vendor class token properly returns enterprise-id. + /// + /// @param u universe (V4 or V6) + /// @param option_vendor_id enterprise-id used in option (0 means don't + /// create the option) + /// @param expected_result text representation of the expected outcome + void testVendorClassEnterprise(Option::Universe u, uint32_t option_vendor_id, + const std::string& expected_result) { + // Let's clear any old values, so we can run multiple cases in each test + clearStack(); + + ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, 0, TokenVendor::ENTERPRISE_ID))); + if (option_vendor_id) { + setVendorClassOption(u, option_vendor_id); + } + + evaluate(u, expected_result); + } + + /// @brief Tests if vendor class token can report existence properly. + /// + /// @param u universe (V4 or V6) + /// @param token_vendor_id enterprise-id used in the token + /// @param option_vendor_id enterprise-id used in option (0 means don't + /// create the option) + /// @param expected_result text representation of the expected outcome + void testVendorClassExists(Option::Universe u, uint32_t token_vendor_id, + uint32_t option_vendor_id, + const std::string& expected_result) { + // Let's clear any old values, so we can run multiple cases in each test + clearStack(); + + ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, token_vendor_id, + TokenOption::EXISTS))); + + if (option_vendor_id) { + setVendorClassOption(u, option_vendor_id); + } + + evaluate(u, expected_result); + } + + /// @brief Tests if vendor token can handle sub-options properly. + /// + /// @param u universe (V4 or V6) + /// @param token_vendor_id enterprise-id used in the token + /// @param token_option_code option code in the token + /// @param option_vendor_id enterprise-id used in option (0 means don't + /// create the option) + /// @param option_code sub-option code (0 means don't create suboption) + /// @param repr representation (TokenOption::EXISTS or HEXADECIMAL) + /// @param expected_result text representation of the expected outcome + void testVendorSuboption(Option::Universe u, + uint32_t token_vendor_id, uint16_t token_option_code, + uint32_t option_vendor_id, uint16_t option_code, + TokenOption::RepresentationType repr, + const std::string& expected) { + // Let's clear any old values, so we can run multiple cases in each test + clearStack(); + + ASSERT_NO_THROW(t_.reset(new TokenVendor(u, token_vendor_id, repr, + token_option_code))); + if (option_vendor_id) { + setVendorOption(u, option_vendor_id); + if (option_code) { + ASSERT_TRUE(vendor_); + OptionPtr subopt(new OptionString(u, option_code, "alpha")); + vendor_->addOption(subopt); + } + } + + evaluate(u, expected); + } + + /// @brief Tests if vendor class token can handle data chunks properly. + /// + /// @param u universe (V4 or V6) + /// @param token_vendor_id enterprise-id used in the token + /// @param token_index data index used in the token + /// @param option_vendor_id enterprise-id used in option (0 means don't + /// create the option) + /// @param data_tuples number of data tuples in the option + /// @param expected_result text representation of the expected outcome + void testVendorClassData(Option::Universe u, + uint32_t token_vendor_id, uint16_t token_index, + uint32_t option_vendor_id, uint16_t data_tuples, + const std::string& expected) { + // Let's clear any old values, so we can run multiple cases in each test + clearStack(); + + ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, token_vendor_id, + TokenVendor::DATA, token_index))); + if (option_vendor_id) { + setVendorClassOption(u, option_vendor_id, data_tuples); + } + + evaluate(u, expected); + } + + /// @brief Tests if TokenInteger evaluates to the proper value + /// + /// @param expected expected string representation on stack after evaluation + /// @param value integer value passed to constructor + void testInteger(const std::string& expected, uint32_t value) { + + clearStack(); + + ASSERT_NO_THROW(t_.reset(new TokenInteger(value))); + + // The universe (v4 or v6) shouldn't have any impact on this, + // but let's check it anyway. + evaluate(Option::V4, expected); + + clearStack(false); + evaluate(Option::V6, expected); + + clearStack(true); + } +}; + +// This tests the toBool() conversions +TEST_F(TokenTest, toBool) { + + ASSERT_NO_THROW(Token::toBool("true")); + EXPECT_TRUE(Token::toBool("true")); + ASSERT_NO_THROW(Token::toBool("false")); + EXPECT_FALSE(Token::toBool("false")); + + // Token::toBool() is case-sensitive + EXPECT_THROW(Token::toBool("True"), EvalTypeError); + EXPECT_THROW(Token::toBool("TRUE"), EvalTypeError); + + // Proposed aliases + EXPECT_THROW(Token::toBool("1"), EvalTypeError); + EXPECT_THROW(Token::toBool("0"), EvalTypeError); + EXPECT_THROW(Token::toBool(""), EvalTypeError); +} + +// This simple test checks that a TokenString, representing a constant string, +// can be used in Pkt4 evaluation. (The actual packet is not used) +TEST_F(TokenTest, string4) { + + // Store constant string "foo" in the TokenString object. + ASSERT_NO_THROW(t_.reset(new TokenString("foo"))); + + // Make sure that the token can be evaluated without exceptions. + ASSERT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("foo", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_STRING Pushing text string 'foo'"); + EXPECT_TRUE(checkFile()); +} + +// This simple test checks that a TokenString, representing a constant string, +// can be used in Pkt6 evaluation. (The actual packet is not used) +TEST_F(TokenTest, string6) { + + // Store constant string "foo" in the TokenString object. + ASSERT_NO_THROW(t_.reset(new TokenString("foo"))); + + // Make sure that the token can be evaluated without exceptions. + ASSERT_NO_THROW(t_->evaluate(*pkt6_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("foo", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_STRING Pushing text string 'foo'"); + EXPECT_TRUE(checkFile()); +} + +// This simple test checks that a TokenHexString, representing a constant +// string coded in hexadecimal, can be used in Pkt4 evaluation. +// (The actual packet is not used) +TEST_F(TokenTest, hexstring4) { + TokenPtr empty; + TokenPtr bad; + TokenPtr nodigit; + TokenPtr baddigit; + TokenPtr bell; + TokenPtr foo; + TokenPtr cookie; + + // Store constant empty hexstring "" ("") in the TokenHexString object. + ASSERT_NO_THROW(empty.reset(new TokenHexString(""))); + // Store bad encoded hexstring "0abc" (""). + ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc"))); + // Store hexstring with no digits "0x" (""). + ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x"))); + // Store hexstring with a bad hexdigit "0xxabc" (""). + ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc"))); + // Store hexstring with an odd number of hexdigits "0x7" ("\a"). + ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7"))); + // Store constant hexstring "0x666f6f" ("foo"). + ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f"))); + // Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE). + ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363"))); + + // Make sure that tokens can be evaluated without exceptions, + // and verify the debug output + ASSERT_NO_THROW(empty->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(bad->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(nodigit->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(baddigit->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(bell->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(foo->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(cookie->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(7, values_.size()); + uint32_t expected = htonl(DHCP_OPTIONS_COOKIE); + EXPECT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4)); + values_.pop(); + EXPECT_EQ("foo", values_.top()); + values_.pop(); + EXPECT_EQ("\a", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x07"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x666F6F"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x63825363"); + EXPECT_TRUE(checkFile()); +} + +// This simple test checks that a TokenHexString, representing a constant +// string coded in hexadecimal, can be used in Pkt6 evaluation. +// (The actual packet is not used) +TEST_F(TokenTest, hexstring6) { + TokenPtr empty; + TokenPtr bad; + TokenPtr nodigit; + TokenPtr baddigit; + TokenPtr bell; + TokenPtr foo; + TokenPtr cookie; + + // Store constant empty hexstring "" ("") in the TokenHexString object. + ASSERT_NO_THROW(empty.reset(new TokenHexString(""))); + // Store bad encoded hexstring "0abc" (""). + ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc"))); + // Store hexstring with no digits "0x" (""). + ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x"))); + // Store hexstring with a bad hexdigit "0xxabc" (""). + ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc"))); + // Store hexstring with an odd number of hexdigits "0x7" ("\a"). + ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7"))); + // Store constant hexstring "0x666f6f" ("foo"). + ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f"))); + // Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE). + ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363"))); + + // Make sure that tokens can be evaluated without exceptions. + ASSERT_NO_THROW(empty->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(bad->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(nodigit->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(baddigit->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(bell->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(foo->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(cookie->evaluate(*pkt6_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(7, values_.size()); + uint32_t expected = htonl(DHCP_OPTIONS_COOKIE); + EXPECT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4)); + values_.pop(); + EXPECT_EQ("foo", values_.top()); + values_.pop(); + EXPECT_EQ("\a", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + values_.pop(); + EXPECT_EQ("", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x07"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x666F6F"); + addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x63825363"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that a TokenIpAddress, representing an IP address as +// a constant string, can be used in Pkt4/Pkt6 evaluation. +// (The actual packet is not used) +TEST_F(TokenTest, ipaddress) { + TokenPtr bad4; + TokenPtr bad6; + TokenPtr ip4; + TokenPtr ip6; + + // Bad IP addresses + ASSERT_NO_THROW(bad4.reset(new TokenIpAddress("10.0.0.0.1"))); + ASSERT_NO_THROW(bad6.reset(new TokenIpAddress(":::"))); + + // IP addresses + ASSERT_NO_THROW(ip4.reset(new TokenIpAddress("10.0.0.1"))); + ASSERT_NO_THROW(ip6.reset(new TokenIpAddress("2001:db8::1"))); + + // Make sure that tokens can be evaluated without exceptions. + ASSERT_NO_THROW(ip4->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(ip6->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(bad4->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(bad6->evaluate(*pkt6_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(4, values_.size()); + + // Check bad addresses (they pushed '' on the value stack) + EXPECT_EQ(0, values_.top().size()); + values_.pop(); + EXPECT_EQ(0, values_.top().size()); + values_.pop(); + + // Check IPv6 address + uint8_t expected6[] = { 0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1 }; + EXPECT_EQ(16, values_.top().size()); + EXPECT_EQ(0, memcmp(expected6, &values_.top()[0], 16)); + values_.pop(); + + // Check IPv4 address + uint8_t expected4[] = { 10, 0, 0, 1 }; + EXPECT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(expected4, &values_.top()[0], 4)); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress 0x0A000001"); + addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress " + "0x20010DB8000000000000000000000001"); + addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress 0x"); + addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress 0x"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that a TokenIpAddressToText, representing an IP address as +// a string, can be used in Pkt4/Pkt6 evaluation. +// (The actual packet is not used) +TEST_F(TokenTest, addressToText) { + TokenPtr address((new TokenIpAddressToText())); + std::vector<uint8_t> bytes; + + std::string value = "10.0.0.1"; + values_.push(value); + + // Invalid data size fails. + EXPECT_THROW(address->evaluate(*pkt4_, values_), EvalTypeError); + + bytes = IOAddress(value).toBytes(); + values_.push(std::string(bytes.begin(), bytes.end())); + + EXPECT_NO_THROW(address->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(1, values_.size()); + + value = "2001:db8::1"; + bytes = IOAddress(value).toBytes(); + values_.push(std::string(bytes.begin(), bytes.end())); + + EXPECT_NO_THROW(address->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(2, values_.size()); + + values_.push(std::string()); + EXPECT_NO_THROW(address->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(3, values_.size()); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check IPv6 address + EXPECT_EQ(11, values_.top().size()); + EXPECT_EQ("2001:db8::1", values_.top()); + values_.pop(); + + // Check IPv4 address + EXPECT_EQ(8, values_.top().size()); + EXPECT_EQ("10.0.0.1", values_.top()); + values_.pop(); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_IPADDRESSTOTEXT Pushing IPAddress 10.0.0.1"); + addString("EVAL_DEBUG_IPADDRESSTOTEXT Pushing IPAddress 2001:db8::1"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that a TokenIntToText, representing an integer as a string, +// can be used in Pkt4/Pkt6 evaluation. +// (The actual packet is not used) +TEST_F(TokenTest, integerToText) { + TokenPtr int8token((new TokenInt8ToText())); + TokenPtr int16token((new TokenInt16ToText())); + TokenPtr int32token((new TokenInt32ToText())); + TokenPtr uint8token((new TokenUInt8ToText())); + TokenPtr uint16token((new TokenUInt16ToText())); + TokenPtr uint32token((new TokenUInt32ToText())); + + std::vector<uint8_t> bytes; + std::string value = "0123456789"; + + // Invalid data size fails. + values_.push(value); + EXPECT_THROW(int8token->evaluate(*pkt4_, values_), EvalTypeError); + values_.push(value); + EXPECT_THROW(int16token->evaluate(*pkt4_, values_), EvalTypeError); + values_.push(value); + EXPECT_THROW(int32token->evaluate(*pkt4_, values_), EvalTypeError); + values_.push(value); + EXPECT_THROW(uint8token->evaluate(*pkt4_, values_), EvalTypeError); + values_.push(value); + EXPECT_THROW(uint16token->evaluate(*pkt4_, values_), EvalTypeError); + values_.push(value); + EXPECT_THROW(uint32token->evaluate(*pkt4_, values_), EvalTypeError); + + uint64_t data = -1; + + values_.push(std::string(const_cast<const char *>(reinterpret_cast<char*>(&data)), sizeof(int8_t))); + + EXPECT_NO_THROW(int8token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(1, values_.size()); + + values_.push(std::string(const_cast<const char *>(reinterpret_cast<char*>(&data)), sizeof(int16_t))); + + EXPECT_NO_THROW(int16token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(2, values_.size()); + + values_.push(std::string(const_cast<const char *>(reinterpret_cast<char*>(&data)), sizeof(int32_t))); + + EXPECT_NO_THROW(int32token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(3, values_.size()); + + values_.push(std::string(const_cast<const char *>(reinterpret_cast<char*>(&data)), sizeof(uint8_t))); + + EXPECT_NO_THROW(uint8token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(4, values_.size()); + + values_.push(std::string(const_cast<const char *>(reinterpret_cast<char*>(&data)), sizeof(uint16_t))); + + EXPECT_NO_THROW(uint16token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(5, values_.size()); + + values_.push(std::string(const_cast<const char *>(reinterpret_cast<char*>(&data)), sizeof(uint32_t))); + + EXPECT_NO_THROW(uint32token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(6, values_.size()); + + value = ""; + + values_.push(value); + EXPECT_NO_THROW(int8token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(7, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(int16token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(8, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(int32token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(9, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(uint8token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(10, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(uint16token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(11, values_.size()); + + values_.push(value); + EXPECT_NO_THROW(uint32token->evaluate(*pkt4_, values_)); + + // Check that the evaluation put its value on the values stack. + ASSERT_EQ(12, values_.size()); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check empty data + EXPECT_EQ(0, values_.top().size()); + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Check uint32 + EXPECT_EQ(10, values_.top().size()); + EXPECT_EQ("4294967295", values_.top()); + values_.pop(); + + // Check uint16 + EXPECT_EQ(5, values_.top().size()); + EXPECT_EQ("65535", values_.top()); + values_.pop(); + + // Check uint8 + EXPECT_EQ(3, values_.top().size()); + EXPECT_EQ("255", values_.top()); + values_.pop(); + + // Check int32 + EXPECT_EQ(2, values_.top().size()); + EXPECT_EQ("-1", values_.top()); + values_.pop(); + + // Check int16 + EXPECT_EQ(2, values_.top().size()); + EXPECT_EQ("-1", values_.top()); + values_.pop(); + + // Check int8 + EXPECT_EQ(2, values_.top().size()); + EXPECT_EQ("-1", values_.top()); + values_.pop(); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_INT8TOTEXT Pushing Int8 -1"); + addString("EVAL_DEBUG_INT16TOTEXT Pushing Int16 -1"); + addString("EVAL_DEBUG_INT32TOTEXT Pushing Int32 -1"); + addString("EVAL_DEBUG_UINT8TOTEXT Pushing UInt8 255"); + addString("EVAL_DEBUG_UINT16TOTEXT Pushing UInt16 65535"); + addString("EVAL_DEBUG_UINT32TOTEXT Pushing UInt32 4294967295"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an option value is able to extract +// the option from an IPv4 packet and properly store the option's value. +TEST_F(TokenTest, optionString4) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL))); + + // This should evaluate to the content of the option 100 (i.e. "hundred4") + ASSERT_NO_THROW(found->evaluate(*pkt4_, values_)); + + // This should evaluate to "" as there is no option 101. + ASSERT_NO_THROW(not_found->evaluate(*pkt4_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. We should get the empty + // string first. + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Then the content of the option 100. + EXPECT_EQ("hundred4", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred4'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value ''"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing option value is able to extract +// the option from an IPv4 packet and properly store its value in a +// hexadecimal format. +TEST_F(TokenTest, optionHexString4) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL))); + + // This should evaluate to the content of the option 100 (i.e. "hundred4") + ASSERT_NO_THROW(found->evaluate(*pkt4_, values_)); + + // This should evaluate to "" as there is no option 101. + ASSERT_NO_THROW(not_found->evaluate(*pkt4_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. We should get the empty + // string first. + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Then the content of the option 100. + EXPECT_EQ("hundred4", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 0x68756E6472656434"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value 0x"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an option value is able to check +// the existence of the option from an IPv4 packet. +TEST_F(TokenTest, optionExistsString4) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::EXISTS))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::EXISTS))); + + ASSERT_NO_THROW(found->evaluate(*pkt4_, values_)); + ASSERT_NO_THROW(not_found->evaluate(*pkt4_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. + EXPECT_EQ("false", values_.top()); + values_.pop(); + EXPECT_EQ("true", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'true'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value 'false'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an option value is able to extract +// the option from an IPv6 packet and properly store the option's value. +TEST_F(TokenTest, optionString6) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL))); + + // This should evaluate to the content of the option 100 (i.e. "hundred6") + ASSERT_NO_THROW(found->evaluate(*pkt6_, values_)); + + // This should evaluate to "" as there is no option 101. + ASSERT_NO_THROW(not_found->evaluate(*pkt6_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. We should get the empty + // string first. + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Then the content of the option 100. + EXPECT_EQ("hundred6", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred6'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value ''"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an option value is able to extract +// the option from an IPv6 packet and properly store its value in hexadecimal +// format. +TEST_F(TokenTest, optionHexString6) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL))); + + // This should evaluate to the content of the option 100 (i.e. "hundred6") + ASSERT_NO_THROW(found->evaluate(*pkt6_, values_)); + + // This should evaluate to "" as there is no option 101. + ASSERT_NO_THROW(not_found->evaluate(*pkt6_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. We should get the empty + // string first. + EXPECT_EQ("", values_.top()); + values_.pop(); + + // Then the content of the option 100. + EXPECT_EQ("hundred6", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 0x68756E6472656436"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value 0x"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an option value is able to check +// the existence of the option from an IPv6 packet. +TEST_F(TokenTest, optionExistsString6) { + TokenPtr found; + TokenPtr not_found; + + // The packets we use have option 100 with a string in them. + ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::EXISTS))); + ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::EXISTS))); + + ASSERT_NO_THROW(found->evaluate(*pkt6_, values_)); + ASSERT_NO_THROW(not_found->evaluate(*pkt6_, values_)); + + // There should be 2 values evaluated. + ASSERT_EQ(2, values_.size()); + + // This is a stack, so the pop order is inversed. + EXPECT_EQ("false", values_.top()); + values_.pop(); + EXPECT_EQ("true", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'true'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value 'false'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that the existing relay4 option can be found. +TEST_F(TokenTest, relay4Option) { + + // Insert relay option with sub-options 1 and 13 + insertRelay4Option(); + + // Creating the token should be safe. + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL))); + + // We should be able to evaluate it. + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // The option should be found and relay4[13] should evaluate to the + // content of that sub-option, i.e. "thirteen" + EXPECT_EQ("thirteen", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 13 with value 'thirteen'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that the code properly handles cases when +// there is a RAI option, but there's no requested sub-option. +TEST_F(TokenTest, relay4OptionNoSuboption) { + + // Insert relay option with sub-options 1 and 13 + insertRelay4Option(); + + // Creating the token should be safe. + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(15, TokenOption::TEXTUAL))); + + // We should be able to evaluate it. + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // The option should NOT be found (there is no sub-option 15), + // so the expression should evaluate to "" + EXPECT_EQ("", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 15 with value ''"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that the code properly handles cases when +// there's no RAI option at all. +TEST_F(TokenTest, relay4OptionNoRai) { + + // We didn't call insertRelay4Option(), so there's no RAI option. + + // Creating the token should be safe. + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL))); + + // We should be able to evaluate it. + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // The option should NOT be found (there is no sub-option 13), + // so the expression should evaluate to "" + EXPECT_EQ("", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 13 with value ''"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that only the RAI is searched for the requested +// sub-option. +TEST_F(TokenTest, relay4RAIOnly) { + + // Insert relay option with sub-options 1 and 13 + insertRelay4Option(); + + // Add options 13 and 70 to the packet. + OptionPtr opt13(new OptionString(Option::V4, 13, "THIRTEEN")); + OptionPtr opt70(new OptionString(Option::V4, 70, "SEVENTY")); + pkt4_->addOption(opt13); + pkt4_->addOption(opt70); + + // The situation is as follows: + // Packet: + // - option 13 (containing "THIRTEEN") + // - option 82 (rai) + // - option 1 (containing "one") + // - option 13 (containing "thirteen") + + // Let's try to get option 13. It should get the one from RAI + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("thirteen", values_.top()); + + // Try to get option 1. It should get the one from RAI + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(1, TokenOption::TEXTUAL))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("one", values_.top()); + + // Try to get option 70. It should fail, as there's no such + // sub option in RAI. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(70, TokenOption::TEXTUAL))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("", values_.top()); + + // Try to check option 1. It should return "true" + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(1, TokenOption::EXISTS))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // Try to check option 70. It should return "false" + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(70, TokenOption::EXISTS))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 13 with value 'thirteen'"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'one'"); + addString("EVAL_DEBUG_OPTION Pushing option 70 with value ''"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'true'"); + addString("EVAL_DEBUG_OPTION Pushing option 70 with value 'false'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if we can properly extract an option +// from relay encapsulations. Our packet has two relay +// encapsulations. Both include a common option with the +// original message (option 100) and both include their +// own option (101 and 102). We attempt to extract the +// options and compare them to expected values. We also +// try to extract an option from an encapsulation +// that doesn't exist (level 2), this should result in an empty +// string. +TEST_F(TokenTest, relay6Option) { + // We start by adding a set of relay encapsulations to the + // basic v6 packet. + addRelay6Encapsulations(); + + // Then we work our way through the set of choices + // Level 0 both options it has and the check that + // the checking for an option it doesn't have results + // in an empty string. + verifyRelay6Option(0, 100, TokenOption::TEXTUAL, "hundred.zero"); + verifyRelay6Option(0, 100, TokenOption::EXISTS, "true"); + verifyRelay6Option(0, 101, TokenOption::TEXTUAL, "hundredone.zero"); + verifyRelay6Option(0, 102, TokenOption::TEXTUAL, ""); + verifyRelay6Option(0, 102, TokenOption::EXISTS, "false"); + + // Level 1, again both options it has and the one for level 0 + verifyRelay6Option(1, 100, TokenOption::TEXTUAL, "hundred.one"); + verifyRelay6Option(1, 101, TokenOption::TEXTUAL, ""); + verifyRelay6Option(1, 102, TokenOption::TEXTUAL, "hundredtwo.one"); + + // Level 2, no encapsulation so no options + verifyRelay6Option(2, 100, TokenOption::TEXTUAL, ""); + + // Level -1, the same as level 1 + verifyRelay6Option(-1, 100, TokenOption::TEXTUAL, "hundred.one"); + verifyRelay6Option(-1, 101, TokenOption::TEXTUAL, ""); + verifyRelay6Option(-1, 102, TokenOption::TEXTUAL, "hundredtwo.one"); + + // Level -2, the same as level 0 + verifyRelay6Option(-2, 100, TokenOption::TEXTUAL, "hundred.zero"); + verifyRelay6Option(-2, 100, TokenOption::EXISTS, "true"); + verifyRelay6Option(-2, 101, TokenOption::TEXTUAL, "hundredone.zero"); + verifyRelay6Option(-2, 102, TokenOption::TEXTUAL, ""); + verifyRelay6Option(-2, 102, TokenOption::EXISTS, "false"); + + // Level -3, no encapsulation so no options + verifyRelay6Option(-3, 100, TokenOption::TEXTUAL, ""); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred.zero'"); + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'true'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value 'hundredone.zero'"); + addString("EVAL_DEBUG_OPTION Pushing option 102 with value ''"); + addString("EVAL_DEBUG_OPTION Pushing option 102 with value 'false'"); + + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred.one'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value ''"); + addString("EVAL_DEBUG_OPTION Pushing option 102 with value 'hundredtwo.one'"); + + addString("EVAL_DEBUG_OPTION Pushing option 100 with value ''"); + + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred.one'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value ''"); + addString("EVAL_DEBUG_OPTION Pushing option 102 with value 'hundredtwo.one'"); + + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred.zero'"); + addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'true'"); + addString("EVAL_DEBUG_OPTION Pushing option 101 with value 'hundredone.zero'"); + addString("EVAL_DEBUG_OPTION Pushing option 102 with value ''"); + addString("EVAL_DEBUG_OPTION Pushing option 102 with value 'false'"); + + addString("EVAL_DEBUG_OPTION Pushing option 100 with value ''"); + + EXPECT_TRUE(checkFile()); +} + +// Verifies that relay6 option requires DHCPv6 +TEST_F(TokenTest, relay6OptionError) { + // Create a relay6 option token + ASSERT_NO_THROW(t_.reset(new TokenRelay6Option(0, 13, TokenOption::TEXTUAL))); + + // A DHCPv6 packet is required + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); +} + +// Verifies that DHCPv4 packet metadata can be extracted. +TEST_F(TokenTest, pkt4MetaData) { + pkt4_->setIface("eth0"); + pkt4_->setLocalAddr(IOAddress("10.0.0.1")); + pkt4_->setRemoteAddr(IOAddress("10.0.0.2")); + + // Check interface (expect eth0) + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::IFACE))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + ASSERT_EQ("eth0", values_.top()); + + // Check source (expect 10.0.0.2) + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::SRC))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + vector<uint8_t> a2 = IOAddress("10.0.0.2").toBytes(); + ASSERT_EQ(a2.size(), values_.top().size()); + EXPECT_EQ(0, memcmp(&a2[0], &values_.top()[0], a2.size())); + + // Check destination (expect 10.0.0.1) + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::DST))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + vector<uint8_t> a1 = IOAddress("10.0.0.1").toBytes(); + ASSERT_EQ(a1.size(), values_.top().size()); + EXPECT_EQ(0, memcmp(&a1[0], &values_.top()[0], a1.size())); + + // Check length (expect 249) + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::LEN))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + uint32_t length = htonl(static_cast<uint32_t>(pkt4_->len())); + ASSERT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(&length, &values_.top()[0], 4)); + + // Unknown metadata type fails + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::MetadataType(100)))); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_PKT Pushing PKT meta data iface with value eth0"); + addString("EVAL_DEBUG_PKT Pushing PKT meta data src with value 0x0A000002"); + addString("EVAL_DEBUG_PKT Pushing PKT meta data dst with value 0x0A000001"); + addString("EVAL_DEBUG_PKT Pushing PKT meta data len with value 0x000000F9"); + EXPECT_TRUE(checkFile()); +} + +// Verifies that DHCPv6 packet metadata can be extracted. +TEST_F(TokenTest, pkt6MetaData) { + pkt6_->setIface("eth0"); + pkt6_->setLocalAddr(IOAddress("ff02::1:2")); + pkt6_->setRemoteAddr(IOAddress("fe80::1234")); + + // Check interface (expect eth0) + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::IFACE))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + ASSERT_EQ("eth0", values_.top()); + + // Check source (expect fe80::1234) + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::SRC))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + ASSERT_EQ(16, values_.top().size()); + EXPECT_EQ(0xfe, static_cast<uint8_t>(values_.top()[0])); + EXPECT_EQ(0x80, static_cast<uint8_t>(values_.top()[1])); + for (unsigned i = 2; i < 14; ++i) { + EXPECT_EQ(0, values_.top()[i]); + } + EXPECT_EQ(0x12, values_.top()[14]); + EXPECT_EQ(0x34, values_.top()[15]); + + // Check destination (expect ff02::1:2) + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::DST))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + vector<uint8_t> ma = IOAddress("ff02::1:2").toBytes(); + ASSERT_EQ(ma.size(), values_.top().size()); + EXPECT_EQ(0, memcmp(&ma[0], &values_.top()[0], ma.size())); + + // Check length (expect 16) + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::LEN))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + uint32_t length = htonl(static_cast<uint32_t>(pkt6_->len())); + ASSERT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(&length, &values_.top()[0], 4)); + + // Unknown meta data type fails + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::MetadataType(100)))); + EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_PKT Pushing PKT meta data iface with value eth0"); + addString("EVAL_DEBUG_PKT Pushing PKT meta data src with value " + "0xFE800000000000000000000000001234"); + addString("EVAL_DEBUG_PKT Pushing PKT meta data dst with value " + "0xFF020000000000000000000000010002"); + addString("EVAL_DEBUG_PKT Pushing PKT meta data len with value 0x00000010"); + EXPECT_TRUE(checkFile()); +} + +// Verifies if the DHCPv4 packet fields can be extracted. +TEST_F(TokenTest, pkt4Fields) { + pkt4_->setGiaddr(IOAddress("192.0.2.1")); + pkt4_->setCiaddr(IOAddress("192.0.2.2")); + pkt4_->setYiaddr(IOAddress("192.0.2.3")); + pkt4_->setSiaddr(IOAddress("192.0.2.4")); + + // We're setting hardware address to uncommon (7 bytes rather than 6 and + // hardware type 123) HW address. We'll use it in hlen and htype checks. + HWAddrPtr hw(new HWAddr(HWAddr::fromText("01:02:03:04:05:06:07", 123))); + pkt4_->setHWAddr(hw); + + // Check hardware address field. + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::CHADDR))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + uint8_t expected_hw[] = { 1, 2, 3, 4, 5, 6, 7 }; + ASSERT_EQ(7, values_.top().size()); + EXPECT_EQ(0, memcmp(expected_hw, &values_.top()[0], 7)); + + // Check hlen value field. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HLEN))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + ASSERT_EQ(4, values_.top().size()); + uint32_t expected_hlen = htonl(7); + EXPECT_EQ(0, memcmp(&expected_hlen, &values_.top()[0], 4)); + + // Check htype value. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HTYPE))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + ASSERT_EQ(4, values_.top().size()); + uint32_t expected_htype = htonl(123); + EXPECT_EQ(0, memcmp(&expected_htype, &values_.top()[0], 4)); + + // Check giaddr value. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::GIADDR))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + uint8_t expected_addr[] = { 192, 0, 2, 1 }; + ASSERT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4)); + + // Check ciaddr value. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::CIADDR))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + expected_addr[3] = 2; + ASSERT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4)); + + // Check yiaddr value. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::YIADDR))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + expected_addr[3] = 3; + ASSERT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4)); + + // Check siaddr value. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::SIADDR))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + expected_addr[3] = 4; + ASSERT_EQ(4, values_.top().size()); + EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4)); + + // Check msgtype. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::MSGTYPE))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + ASSERT_EQ(4, values_.top().size()); + string exp_msgtype = encode(DHCPDISCOVER); + EXPECT_EQ(0, memcmp(&exp_msgtype[0], &values_.top()[0], 4)); + + // Check transaction-id + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::TRANSID))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + ASSERT_EQ(4, values_.top().size()); + string exp_transid = encode(12345); + EXPECT_EQ(0, memcmp(&exp_transid[0], &values_.top()[0], 4)); + + // Check a DHCPv6 packet throws. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HLEN))); + EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError); + + // Unknown field fails + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::FieldType(100)))); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field mac with value 0x01020304050607"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field hlen with value 0x00000007"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field htype with value 0x0000007B"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field giaddr with value 0xC0000201"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field ciaddr with value 0xC0000202"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field yiaddr with value 0xC0000203"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field siaddr with value 0xC0000204"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field msgtype with value 0x00000001"); + addString("EVAL_DEBUG_PKT4 Pushing PKT4 field transid with value 0x00003039"); + EXPECT_TRUE(checkFile()); +} + +// Verifies if the DHCPv6 packet fields can be extracted. +TEST_F(TokenTest, pkt6Fields) { + // The default test creates a v6 DHCPV6_SOLICIT packet with a + // transaction id of 12345. + + // Check the message type + ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::MSGTYPE))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + uint32_t expected = htonl(1); + EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4)); + + // Check the transaction id field + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID))); + EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_)); + ASSERT_EQ(1, values_.size()); + expected = htonl(12345); + EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4)); + + // Check that working with a v4 packet generates an error + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID))); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + + // Unknown field fails + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::FieldType(100)))); + EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_PKT6 Pushing PKT6 field msgtype with value 0x00000001"); + addString("EVAL_DEBUG_PKT6 Pushing PKT6 field transid with value 0x00003039"); + + EXPECT_TRUE(checkFile()); +} + +// This test checks if we can properly extract the link and peer +// address fields from relay encapsulations. Our packet has +// two relay encapsulations. We attempt to extract the two +// fields from both of the encapsulations and compare them. +// We also try to extract one of the fields from an encapsulation +// that doesn't exist (level 2), this should result in an empty +// string. +TEST_F(TokenTest, relay6Field) { + // Values for the address results + uint8_t zeroaddr[] = { 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 }; + uint8_t linkaddr[] = { 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1 }; + uint8_t peeraddr[] = { 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 2 }; + + // We start by adding a set of relay encapsulations to the + // basic v6 packet. + addRelay6Encapsulations(); + + // Then we work our way through the set of choices + // Level 0 both link and peer address should be 0::0 + verifyRelay6Eval(0, TokenRelay6Field::LINKADDR, 16, zeroaddr); + verifyRelay6Eval(0, TokenRelay6Field::PEERADDR, 16, zeroaddr); + + // Level 1 link and peer should have different non-zero addresses + verifyRelay6Eval(1, TokenRelay6Field::LINKADDR, 16, linkaddr); + verifyRelay6Eval(1, TokenRelay6Field::PEERADDR, 16, peeraddr); + + // Level 2 has no encapsulation so the address should be zero length + verifyRelay6Eval(2, TokenRelay6Field::LINKADDR, 0, zeroaddr); + + // Level -1 is the same as level 1 + verifyRelay6Eval(-1, TokenRelay6Field::LINKADDR, 16, linkaddr); + verifyRelay6Eval(-1, TokenRelay6Field::PEERADDR, 16, peeraddr); + + // Level -2 is the same as level 0 + verifyRelay6Eval(-2, TokenRelay6Field::LINKADDR, 16, zeroaddr); + verifyRelay6Eval(-2, TokenRelay6Field::PEERADDR, 16, zeroaddr); + + // Level -3 has no encapsulation so the address should be zero length + verifyRelay6Eval(-3, TokenRelay6Field::LINKADDR, 0, zeroaddr); + + // Lets check that the layout of the address returned by the + // token matches that of the TokenIpAddress + TokenPtr trelay; + TokenPtr taddr; + TokenPtr tequal; + ASSERT_NO_THROW(trelay.reset(new TokenRelay6Field(1, TokenRelay6Field::LINKADDR))); + ASSERT_NO_THROW(taddr.reset(new TokenIpAddress("1::1"))); + ASSERT_NO_THROW(tequal.reset(new TokenEqual())); + + EXPECT_NO_THROW(trelay->evaluate(*pkt6_, values_)); + EXPECT_NO_THROW(taddr->evaluate(*pkt6_, values_)); + EXPECT_NO_THROW(tequal->evaluate(*pkt6_, values_)); + + // We should have a single value on the stack and it should be "true" + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // be tidy + clearStack(); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest 0 " + "with value 0x00000000000000000000000000000000"); + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field peeraddr nest 0 " + "with value 0x00000000000000000000000000000000"); + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest 1 " + "with value 0x00010000000000000000000000000001"); + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field peeraddr nest 1 " + "with value 0x00010000000000000000000000000002"); + addString("EVAL_DEBUG_RELAY6_RANGE Pushing PKT6 relay field linkaddr " + "nest 2 with value 0x"); + + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest -1 " + "with value 0x00010000000000000000000000000001"); + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field peeraddr nest -1 " + "with value 0x00010000000000000000000000000002"); + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest -2 " + "with value 0x00000000000000000000000000000000"); + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field peeraddr nest -2 " + "with value 0x00000000000000000000000000000000"); + addString("EVAL_DEBUG_RELAY6_RANGE Pushing PKT6 relay field linkaddr " + "nest -3 with value 0x"); + + addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest 1 " + "with value 0x00010000000000000000000000000001"); + addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress " + "0x00010000000000000000000000000001"); + addString("EVAL_DEBUG_EQUAL Popping 0x00010000000000000000000000000001 " + "and 0x00010000000000000000000000000001 pushing result 'true'"); + + EXPECT_TRUE(checkFile()); +} + +// This test checks some error cases for a relay6 field token +TEST_F(TokenTest, relay6FieldError) { + // Create a valid relay6 field token + ASSERT_NO_THROW(t_.reset(new TokenRelay6Field(0, TokenRelay6Field::LINKADDR))); + + // a DHCPv6 packet is required + ASSERT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + + // No test for unknown field as it is not (yet) checked?! +} + +// This test checks if a token representing an == operator is able to +// compare two values (with incorrectly built stack). +TEST_F(TokenTest, optionEqualInvalid) { + + ASSERT_NO_THROW(t_.reset(new TokenEqual())); + + // CASE 1: There's not enough values on the stack. == is an operator that + // takes two parameters. There are 0 on the stack. + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // CASE 2: One value is still not enough. + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); +} + +// This test checks if a token representing an == operator is able to +// compare two different values. +TEST_F(TokenTest, optionEqualFalse) { + + ASSERT_NO_THROW(t_.reset(new TokenEqual())); + + values_.push("foo"); + values_.push("bar"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // After evaluation there should be a single value that represents + // result of "foo" == "bar" comparison. + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_EQUAL Popping 0x626172 and 0x666F6F " + "pushing result 'false'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an == operator is able to +// compare two identical values. +TEST_F(TokenTest, optionEqualTrue) { + + ASSERT_NO_THROW(t_.reset(new TokenEqual())); + + values_.push("foo"); + values_.push("foo"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // After evaluation there should be a single value that represents + // result of "foo" == "foo" comparison. + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_EQUAL Popping 0x666F6F and 0x666F6F " + "pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing a substring request +// throws an exception if there aren't enough values on the stack. +// The stack from the top is: length, start, string. +// The actual packet is not used. +TEST_F(TokenTest, substringNotEnoughValues) { + ASSERT_NO_THROW(t_.reset(new TokenSubstring())); + + // Substring requires three values on the stack, try + // with 0, 1 and 2 all should throw an exception + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + values_.push(""); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + values_.push("0"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // Three should work + values_.push("0"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // As we had an empty string to start with we should have an empty + // one after the evaluate + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING_EMPTY Popping length 0, start 0, " + "string 0x pushing result 0x"); + EXPECT_TRUE(checkFile()); +} + +// Test getting the whole string in different ways +TEST_F(TokenTest, substringWholeString) { + // Get the whole string + verifySubstringEval("foobar", "0", "6", "foobar"); + + // Get the whole string with "all" + verifySubstringEval("foobar", "0", "all", "foobar"); + + // Get the whole string with an extra long number + verifySubstringEval("foobar", "0", "123456", "foobar"); + + // Get the whole string counting from the back + verifySubstringEval("foobar", "-6", "all", "foobar"); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING Popping length 6, start 0, " + "string 0x666F6F626172 pushing result 0x666F6F626172"); + addString("EVAL_DEBUG_SUBSTRING Popping length all, start 0, " + "string 0x666F6F626172 pushing result 0x666F6F626172"); + addString("EVAL_DEBUG_SUBSTRING Popping length 123456, start 0, " + "string 0x666F6F626172 pushing result 0x666F6F626172"); + addString("EVAL_DEBUG_SUBSTRING Popping length all, start -6, " + "string 0x666F6F626172 pushing result 0x666F6F626172"); + EXPECT_TRUE(checkFile()); +} + +// Test getting a suffix, in this case the last 3 characters +TEST_F(TokenTest, substringTrailer) { + verifySubstringEval("foobar", "3", "3", "bar"); + verifySubstringEval("foobar", "3", "all", "bar"); + verifySubstringEval("foobar", "-3", "all", "bar"); + verifySubstringEval("foobar", "-3", "123", "bar"); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING Popping length 3, start 3, " + "string 0x666F6F626172 pushing result 0x626172"); + addString("EVAL_DEBUG_SUBSTRING Popping length all, start 3, " + "string 0x666F6F626172 pushing result 0x626172"); + addString("EVAL_DEBUG_SUBSTRING Popping length all, start -3, " + "string 0x666F6F626172 pushing result 0x626172"); + addString("EVAL_DEBUG_SUBSTRING Popping length 123, start -3, " + "string 0x666F6F626172 pushing result 0x626172"); + EXPECT_TRUE(checkFile()); +} + +// Test getting the middle of the string in different ways +TEST_F(TokenTest, substringMiddle) { + verifySubstringEval("foobar", "1", "4", "ooba"); + verifySubstringEval("foobar", "-5", "4", "ooba"); + verifySubstringEval("foobar", "-1", "-4", "ooba"); + verifySubstringEval("foobar", "5", "-4", "ooba"); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 1, " + "string 0x666F6F626172 pushing result 0x6F6F6261"); + addString("EVAL_DEBUG_SUBSTRING Popping length 4, start -5, " + "string 0x666F6F626172 pushing result 0x6F6F6261"); + addString("EVAL_DEBUG_SUBSTRING Popping length -4, start -1, " + "string 0x666F6F626172 pushing result 0x6F6F6261"); + addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 5, " + "string 0x666F6F626172 pushing result 0x6F6F6261"); + EXPECT_TRUE(checkFile()); +} + +// Test getting the last letter in different ways +TEST_F(TokenTest, substringLastLetter) { + verifySubstringEval("foobar", "5", "all", "r"); + verifySubstringEval("foobar", "5", "1", "r"); + verifySubstringEval("foobar", "5", "5", "r"); + verifySubstringEval("foobar", "-1", "all", "r"); + verifySubstringEval("foobar", "-1", "1", "r"); + verifySubstringEval("foobar", "-1", "5", "r"); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING Popping length all, start 5, " + "string 0x666F6F626172 pushing result 0x72"); + addString("EVAL_DEBUG_SUBSTRING Popping length 1, start 5, " + "string 0x666F6F626172 pushing result 0x72"); + addString("EVAL_DEBUG_SUBSTRING Popping length 5, start 5, " + "string 0x666F6F626172 pushing result 0x72"); + addString("EVAL_DEBUG_SUBSTRING Popping length all, start -1, " + "string 0x666F6F626172 pushing result 0x72"); + addString("EVAL_DEBUG_SUBSTRING Popping length 1, start -1, " + "string 0x666F6F626172 pushing result 0x72"); + addString("EVAL_DEBUG_SUBSTRING Popping length 5, start -1, " + "string 0x666F6F626172 pushing result 0x72"); + EXPECT_TRUE(checkFile()); +} + +// Test we get only what is available if we ask for a longer string +TEST_F(TokenTest, substringLength) { + // Test off the front + verifySubstringEval("foobar", "0", "-4", ""); + verifySubstringEval("foobar", "1", "-4", "f"); + verifySubstringEval("foobar", "2", "-4", "fo"); + verifySubstringEval("foobar", "3", "-4", "foo"); + + // and the back + verifySubstringEval("foobar", "3", "4", "bar"); + verifySubstringEval("foobar", "4", "4", "ar"); + verifySubstringEval("foobar", "5", "4", "r"); + verifySubstringEval("foobar", "6", "4", ""); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 0, " + "string 0x666F6F626172 pushing result 0x"); + addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 1, " + "string 0x666F6F626172 pushing result 0x66"); + addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 2, " + "string 0x666F6F626172 pushing result 0x666F"); + addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 3, " + "string 0x666F6F626172 pushing result 0x666F6F"); + addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 3, " + "string 0x666F6F626172 pushing result 0x626172"); + addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 4, " + "string 0x666F6F626172 pushing result 0x6172"); + addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 5, " + "string 0x666F6F626172 pushing result 0x72"); + addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length 4, start 6, " + "string 0x666F6F626172 pushing result 0x"); + EXPECT_TRUE(checkFile()); +} + +// Test that we get nothing if the starting position is out of the string +TEST_F(TokenTest, substringStartingPosition) { + // Off the front + verifySubstringEval("foobar", "-7", "1", ""); + verifySubstringEval("foobar", "-7", "-11", ""); + verifySubstringEval("foobar", "-7", "all", ""); + + // and the back + verifySubstringEval("foobar", "6", "1", ""); + verifySubstringEval("foobar", "6", "-11", ""); + verifySubstringEval("foobar", "6", "all", ""); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length 1, start -7, " + "string 0x666F6F626172 pushing result 0x"); + addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length -11, start -7, " + "string 0x666F6F626172 pushing result 0x"); + addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length all, start -7, " + "string 0x666F6F626172 pushing result 0x"); + addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length 1, start 6, " + "string 0x666F6F626172 pushing result 0x"); + addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length -11, start 6, " + "string 0x666F6F626172 pushing result 0x"); + addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length all, start 6, " + "string 0x666F6F626172 pushing result 0x"); + EXPECT_TRUE(checkFile()); +} + +// Check what happens if we use strings that aren't numbers for start or length +// We should return the empty string +TEST_F(TokenTest, substringBadParams) { + verifySubstringEval("foobar", "0ick", "all", "", true); + verifySubstringEval("foobar", "ick0", "all", "", true); + verifySubstringEval("foobar", "ick", "all", "", true); + verifySubstringEval("foobar", "0", "ick", "", true); + verifySubstringEval("foobar", "0", "0ick", "", true); + verifySubstringEval("foobar", "0", "ick0", "", true); + verifySubstringEval("foobar", "0", "allaboard", "", true); + + // These should result in a throw which should generate it's own + // log entry +} + +// lastly check that we don't get anything if the string is empty or +// we don't ask for any characters from it. +TEST_F(TokenTest, substringReturnEmpty) { + verifySubstringEval("", "0", "all", ""); + verifySubstringEval("foobar", "0", "0", ""); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING_EMPTY Popping length all, start 0, " + "string 0x pushing result 0x"); + addString("EVAL_DEBUG_SUBSTRING Popping length 0, start 0, " + "string 0x666F6F626172 pushing result 0x"); + EXPECT_TRUE(checkFile()); +} + +// Check if we can use the substring and equal tokens together +// We put the result on the stack first then the substring values +// then evaluate the substring which should leave the original +// result on the bottom with the substring result on next. +// Evaluating the equals should produce true for the first +// and false for the second. +// throws an exception if there aren't enough values on the stack. +// The stack from the top is: length, start, string. +// The actual packet is not used. +TEST_F(TokenTest, substringEquals) { + TokenPtr tequal; + + ASSERT_NO_THROW(t_.reset(new TokenSubstring())); + ASSERT_NO_THROW(tequal.reset(new TokenEqual())); + + // The final expected value + values_.push("ooba"); + + // The substring values + values_.push("foobar"); + values_.push("1"); + values_.push("4"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have two values on the stack + ASSERT_EQ(2, values_.size()); + + // next the equals eval + EXPECT_NO_THROW(tequal->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // get rid of the result + values_.pop(); + + // and try it again but with a bad final value + // The final expected value + values_.push("foob"); + + // The substring values + values_.push("foobar"); + values_.push("1"); + values_.push("4"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have two values on the stack + ASSERT_EQ(2, values_.size()); + + // next the equals eval + EXPECT_NO_THROW(tequal->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 1, " + "string 0x666F6F626172 pushing result 0x6F6F6261"); + addString("EVAL_DEBUG_EQUAL Popping 0x6F6F6261 and 0x6F6F6261 " + "pushing result 'true'"); + addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 1, " + "string 0x666F6F626172 pushing result 0x6F6F6261"); + addString("EVAL_DEBUG_EQUAL Popping 0x6F6F6261 and 0x666F6F62 " + "pushing result 'false'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing a concat request +// throws an exception if there aren't enough values on the stack. +// The actual packet is not used. +TEST_F(TokenTest, concat) { + ASSERT_NO_THROW(t_.reset(new TokenConcat())); + + // Concat requires two values on the stack, try + // with 0 and 1 both should throw an exception + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // Two should work + values_.push("bar"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // Check the result + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("foobar", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_CONCAT Popping 0x626172 and 0x666F6F " + "pushing 0x666F6F626172"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing a hexstring request +// throws an exception if there aren't enough values on the stack. +// The actual packet is not used. +TEST_F(TokenTest, tohexstring) { + ASSERT_NO_THROW(t_.reset(new TokenToHexString())); + + // Hexstring requires two values on the stack, try + // with 0 and 1 both should throw an exception + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // Two should work + values_.push("-"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // Check the result + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("66-6f-6f", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_TOHEXSTRING Popping binary value 0x666F6F and " + "separator -, pushing result 66-6f-6f"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an ifelse is able +// to select the branch following the condition. +TEST_F(TokenTest, ifElse) { + ASSERT_NO_THROW(t_.reset(new TokenIfElse())); + + // Ifelse requires three values on the stack, try + // with 0, 1 and 2 all should throw an exception + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + values_.push("bar"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // The condition must be a boolean + values_.push("bar"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + + // Check if what it returns + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenIfElse())); + values_.push("true"); + values_.push("foo"); + values_.push("bar"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("foo", values_.top()); + + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenIfElse())); + values_.push("false"); + values_.push("foo"); + values_.push("bar"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("bar", values_.top()); +} + +// This test checks if a token representing a not is able to +// negate a boolean value (with incorrectly built stack). +TEST_F(TokenTest, operatorNotInvalid) { + + ASSERT_NO_THROW(t_.reset(new TokenNot())); + + // CASE 1: The stack is empty. + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // CASE 2: The top value is not a boolean + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); +} + +// This test checks if a token representing a not operator is able to +// negate a boolean value. +TEST_F(TokenTest, operatorNot) { + + ASSERT_NO_THROW(t_.reset(new TokenNot())); + + values_.push("true"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // After evaluation there should be the negation of the value. + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // Double negation is identity. + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_NOT Popping 'true' pushing 'false'"); + addString("EVAL_DEBUG_NOT Popping 'false' pushing 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an and is able to +// conjugate two values (with incorrectly built stack). +TEST_F(TokenTest, operatorAndInvalid) { + + ASSERT_NO_THROW(t_.reset(new TokenAnd())); + + // CASE 1: There's not enough values on the stack. and is an operator that + // takes two parameters. There are 0 on the stack. + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // CASE 2: One value is still not enough. + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // CASE 3: The two values must be logical + values_.push("true"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + + // Swap the 2 values + values_.push("true"); + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); +} + +// This test checks if a token representing an and operator is able to +// conjugate false with another logical +TEST_F(TokenTest, operatorAndFalse) { + + ASSERT_NO_THROW(t_.reset(new TokenAnd())); + + values_.push("true"); + values_.push("false"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // After evaluation there should be a single "false" value + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // After true and false, check false and true + values_.push("true"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // And false and false + values_.push("false"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_AND Popping 'false' and 'true' pushing 'false'"); + addString("EVAL_DEBUG_AND Popping 'true' and 'false' pushing 'false'"); + addString("EVAL_DEBUG_AND Popping 'false' and 'false' pushing 'false'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an and is able to +// conjugate two true values. +TEST_F(TokenTest, operatorAndTrue) { + + ASSERT_NO_THROW(t_.reset(new TokenAnd())); + + values_.push("true"); + values_.push("true"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // After evaluation there should be a single "true" value + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_AND Popping 'true' and 'true' pushing 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an or is able to +// combine two values (with incorrectly built stack). +TEST_F(TokenTest, operatorOrInvalid) { + + ASSERT_NO_THROW(t_.reset(new TokenOr())); + + // CASE 1: There's not enough values on the stack. or is an operator that + // takes two parameters. There are 0 on the stack. + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // CASE 2: One value is still not enough. + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack); + + // CASE 3: The two values must be logical + values_.push("true"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); + + // Swap the 2 values + values_.push("true"); + values_.push("foo"); + EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError); +} + +// This test checks if a token representing an or is able to +// conjugate two false values. +TEST_F(TokenTest, operatorOrFalse) { + + ASSERT_NO_THROW(t_.reset(new TokenOr())); + + values_.push("false"); + values_.push("false"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // After evaluation there should be a single "false" value + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OR Popping 'false' and 'false' pushing 'false'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks if a token representing an == operator is able to +// conjugate true with another logical +TEST_F(TokenTest, operatorOrTrue) { + + ASSERT_NO_THROW(t_.reset(new TokenOr())); + + values_.push("false"); + values_.push("true"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // After evaluation there should be a single "true" value + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // After false or true, checks true or false + values_.push("false"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // And true or true + values_.push("true"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_OR Popping 'true' and 'false' pushing 'true'"); + addString("EVAL_DEBUG_OR Popping 'false' and 'true' pushing 'true'"); + addString("EVAL_DEBUG_OR Popping 'true' and 'true' pushing 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies client class membership +TEST_F(TokenTest, member) { + + ASSERT_NO_THROW(t_.reset(new TokenMember("foo"))); + + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // the packet has no classes so false was left on the stack + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + values_.pop(); + + // Add bar and retry + pkt4_->addClass("bar"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // the packet has a class but it is not foo + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + values_.pop(); + + // Add foo and retry + pkt4_->addClass("foo"); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // Now the packet is in the foo class + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); +} + +// This test verifies if expression vendor[4491].exists works properly in DHCPv4. +TEST_F(TokenTest, vendor4SpecificVendorExists) { + // Case 1: no option, should evaluate to false + testVendorExists(Option::V4, 4491, 0, "false"); + + // Case 2: option present, but uses different enterprise-id, should fail + testVendorExists(Option::V4, 4491, 1234, "false"); + + // Case 3: option present and has matching enterprise-id, should succeed + testVendorExists(Option::V4, 4491, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, " + "pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies if expression vendor[4491].exists works properly in DHCPv6. +TEST_F(TokenTest, vendor6SpecificVendorExists) { + // Case 1: no option, should evaluate to false + testVendorExists(Option::V6, 4491, 0, "false"); + + // Case 2: option present, but uses different enterprise-id, should fail + testVendorExists(Option::V6, 4491, 1234, "false"); + + // Case 3: option present and has matching enterprise-id, should succeed + testVendorExists(Option::V6, 4491, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, " + "pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +/// Test if expression vendor[*].exists works properly for DHCPv4. +TEST_F(TokenTest, vendor4AnyVendorExists) { + // Case 1: no option, should evaluate to false + testVendorExists(Option::V4, 0, 0, "false"); + + // Case 2: option present with vendor-id 1234, should succeed + testVendorExists(Option::V4, 0, 1234, "true"); + + // Case 3: option present with vendor-id 4491, should succeed + testVendorExists(Option::V4, 0, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, " + "pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 1234 " + "found, pushing result 'true'"); + addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// Test if expression vendor[*].exists works properly for DHCPv6. +TEST_F(TokenTest, vendor6AnyVendorExists) { + // Case 1: no option, should evaluate to false + testVendorExists(Option::V6, 0, 0, "false"); + + // Case 2: option present with vendor-id 1234, should succeed + testVendorExists(Option::V6, 0, 1234, "true"); + + // Case 3: option present with vendor-id 4491, should succeed + testVendorExists(Option::V6, 0, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, " + "pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 1234 " + "found, pushing result 'true'"); + addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// Test if expression vendor[*].enterprise works properly for DHCPv4. +TEST_F(TokenTest, vendor4enterprise) { + // Case 1: No option present, should return empty string + testVendorEnterprise(Option::V4, 0, ""); + + // Case 2: Option with vendor-id 1234, should return "1234" + testVendorEnterprise(Option::V4, 1234, encode(1234)); + + // Case 3: Option with vendor-id set to maximum value, should still + // be able to handle it + testVendorEnterprise(Option::V4, 4294967295, encode(4294967295)); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, pushing" + " result ''"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 1234 as " + "result 0x000004D2"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 4294967295" + " as result 0xFFFFFFFF"); + EXPECT_TRUE(checkFile()); +} + +// Test if expression vendor[*].enterprise works properly for DHCPv6. +TEST_F(TokenTest, vendor6enterprise) { + // Case 1: No option present, should return empty string + testVendorEnterprise(Option::V6, 0, ""); + + // Case 2: Option with vendor-id 1234, should return "1234" + testVendorEnterprise(Option::V6, 1234, encode(1234)); + + // Case 3: Option with vendor-id set to maximum value, should still + // be able to handle it + testVendorEnterprise(Option::V6, 4294967295, encode(4294967295)); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, pushing" + " result ''"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 1234 as " + "result 0x000004D2"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 4294967295 " + "as result 0xFFFFFFFF"); + EXPECT_TRUE(checkFile()); +} + +// This one tests "vendor[4491].option[1].exists" expression. There are so many +// wonderful ways in which this could fail: the option could not be there, +// it could have different enterprise-id, may not have suboption 1. Or may +// have the suboption with valid type, but enterprise may be different. +TEST_F(TokenTest, vendor4SuboptionExists) { + // Case 1: expression vendor[4491].option[1].exists, no option present + testVendorSuboption(Option::V4, 4491, 1, 0, 0, TokenOption::EXISTS, "false"); + + // Case 2: expression vendor[4491].option[1].exists, option with vendor-id = 1234, + // no suboptions, expected result "false" + testVendorSuboption(Option::V4, 4491, 1, 1234, 0, TokenOption::EXISTS, "false"); + + // Case 3: expression vendor[4491].option[1].exists, option with vendor-id = 1234, + // suboption 1, expected result "false" + testVendorSuboption(Option::V4, 4491, 1, 1234, 1, TokenOption::EXISTS, "false"); + + // Case 4: expression vendor[4491].option[1].exists, option with vendor-id = 4491, + // suboption 2, expected result "false" + testVendorSuboption(Option::V4, 4491, 1, 4491, 2, TokenOption::EXISTS, "false"); + + // Case 5: expression vendor[4491].option[1].exists, option with vendor-id = 4491, + // suboption 1, expected result "true" + testVendorSuboption(Option::V4, 4491, 1, 4491, 1, TokenOption::EXISTS, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, pushing " + "result 'false'"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'false'"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This is similar to the previous one, but tests vendor[4491].option[1].exists +// for DHCPv6. +TEST_F(TokenTest, vendor6SuboptionExists) { + // Case 1: expression vendor[4491].option[1].exists, no option present + testVendorSuboption(Option::V6, 4491, 1, 0, 0, TokenOption::EXISTS, "false"); + + // Case 2: expression vendor[4491].option[1].exists, option with vendor-id = 1234, + // no suboptions, expected result "false" + testVendorSuboption(Option::V6, 4491, 1, 1234, 0, TokenOption::EXISTS, "false"); + + // Case 3: expression vendor[4491].option[1].exists, option with vendor-id = 1234, + // suboption 1, expected result "false" + testVendorSuboption(Option::V6, 4491, 1, 1234, 1, TokenOption::EXISTS, "false"); + + // Case 4: expression vendor[4491].option[1].exists, option with vendor-id = 4491, + // suboption 2, expected result "false" + testVendorSuboption(Option::V6, 4491, 1, 4491, 2, TokenOption::EXISTS, "false"); + + // Case 5: expression vendor[4491].option[1].exists, option with vendor-id = 4491, + // suboption 1, expected result "true" + testVendorSuboption(Option::V6, 4491, 1, 4491, 1, TokenOption::EXISTS, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, pushing " + "result 'false'"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'false'"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies if vendor[4491].option[1].hex expression properly returns +// value of said sub-option or empty string if desired option is not present. +// This test is for DHCPv4. +TEST_F(TokenTest, vendor4SuboptionHex) { + // Case 1: no option present, should return empty string + testVendorSuboption(Option::V4, 4491, 1, 0, 0, TokenOption::HEXADECIMAL, ""); + + // Case 2: option with vendor-id = 1234, no suboptions, expected result "" + testVendorSuboption(Option::V4, 4491, 1, 1234, 0, TokenOption::HEXADECIMAL, ""); + + // Case 3: option with vendor-id = 1234, suboption 1, expected result "" + testVendorSuboption(Option::V4, 4491, 1, 1234, 1, TokenOption::HEXADECIMAL, ""); + + // Case 4: option with vendor-id = 4491, suboption 2, expected result "" + testVendorSuboption(Option::V4, 4491, 1, 4491, 2, TokenOption::HEXADECIMAL, ""); + + // Case 5: option with vendor-id = 4491, suboption 1, expected result content + // of the option + testVendorSuboption(Option::V4, 4491, 1, 4491, 1, TokenOption::HEXADECIMAL, "alpha"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, pushing " + "result ''"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result ''"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x616C706861"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies if vendor[4491].option[1].hex expression properly returns +// value of said sub-option or empty string if desired option is not present. +// This test is for DHCPv4. +TEST_F(TokenTest, vendor6SuboptionHex) { + // Case 1: no option present, should return empty string + testVendorSuboption(Option::V6, 4491, 1, 0, 0, TokenOption::HEXADECIMAL, ""); + + // Case 2: option with vendor-id = 1234, no suboptions, expected result "" + testVendorSuboption(Option::V6, 4491, 1, 1234, 0, TokenOption::HEXADECIMAL, ""); + + // Case 3: option with vendor-id = 1234, suboption 1, expected result "" + testVendorSuboption(Option::V6, 4491, 1, 1234, 1, TokenOption::HEXADECIMAL, ""); + + // Case 4: option with vendor-id = 4491, suboption 2, expected result "" + testVendorSuboption(Option::V6, 4491, 1, 4491, 2, TokenOption::HEXADECIMAL, ""); + + // Case 5: option with vendor-id = 4491, suboption 1, expected result content + // of the option + testVendorSuboption(Option::V6, 4491, 1, 4491, 1, TokenOption::HEXADECIMAL, "alpha"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, pushing " + "result ''"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, " + "option had 1234, pushing result ''"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x"); + addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x616C706861"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies that "vendor-class[4491].exists" expression can be used +// in DHCPv4. +TEST_F(TokenTest, vendorClass4SpecificVendorExists) { + // Case 1: no option present, should fail + testVendorClassExists(Option::V4, 4491, 0, "false"); + + // Case 2: option exists, but has different vendor-id (1234), should fail + testVendorClassExists(Option::V4, 4491, 1234, "false"); + + // Case 3: option exists and has matching vendor-id, should succeed + testVendorClassExists(Option::V4, 4491, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, " + "pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies that "vendor-class[4491].exists" expression can be used +// in DHCPv6. +TEST_F(TokenTest, vendorClass6SpecificVendorExists) { + // Case 1: no option present, should fail + testVendorClassExists(Option::V6, 4491, 0, "false"); + + // Case 2: option exists, but has different vendor-id (1234), should fail + testVendorClassExists(Option::V6, 4491, 1234, "false"); + + // Case 3: option exists and has matching vendor-id, should succeed + testVendorClassExists(Option::V6, 4491, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, pushing " + "result 'false'"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies that "vendor-class[*].exists" can be used in DHCPv4 +// and it matches a vendor class option with any vendor-id. +TEST_F(TokenTest, vendorClass4AnyVendorExists) { + // Case 1: no option present, should fail + testVendorClassExists(Option::V4, 0, 0, "false"); + + // Case 2: option exists, should succeed, regardless of the vendor-id + testVendorClassExists(Option::V4, 0, 1234, "true"); + + // Case 3: option exists, should succeed, regardless of the vendor-id + testVendorClassExists(Option::V4, 0, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, " + "pushing result 'false'"); + addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 1234 " + "found, pushing result 'true'"); + addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies that "vendor-class[*].exists" can be used in DHCPv6 +// and it matches a vendor class option with any vendor-id. +TEST_F(TokenTest, vendorClass6AnyVendorExists) { + // Case 1: no option present, should fail + testVendorClassExists(Option::V6, 0, 0, "false"); + + // Case 2: option exists, should succeed, regardless of the vendor-id + testVendorClassExists(Option::V6, 0, 1234, "true"); + + // Case 3: option exists, should succeed, regardless of the vendor-id + testVendorClassExists(Option::V6, 0, 4491, "true"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, pushing " + "result 'false'"); + addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 1234 " + "found, pushing result 'true'"); + addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 " + "found, pushing result 'true'"); + EXPECT_TRUE(checkFile()); +} + +// Test if expression "vendor-class.enterprise" works properly for DHCPv4. +TEST_F(TokenTest, vendorClass4enterprise) { + // Case 1: No option present, should return empty string + testVendorClassEnterprise(Option::V4, 0, ""); + + // Case 2: Option with vendor-id 1234, should return "1234" + testVendorClassEnterprise(Option::V4, 1234, encode(1234)); + + // Case 3: Option with vendor-id set to maximum value, should still + // be able to handle it + testVendorClassEnterprise(Option::V4, 4294967295, encode(4294967295)); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, pushing " + "result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id " + "1234 as result 0x000004D2"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id " + "4294967295 as result 0xFFFFFFFF"); + EXPECT_TRUE(checkFile()); +} + +// Test if expression "vendor-class.enterprise" works properly for DHCPv6. +TEST_F(TokenTest, vendorClass6enterprise) { + // Case 1: No option present, should return empty string + testVendorClassEnterprise(Option::V6, 0, ""); + + // Case 2: Option with vendor-id 1234, should return "1234" + testVendorClassEnterprise(Option::V6, 1234, encode(1234)); + + // Case 3: Option with vendor-id set to maximum value, should still + // be able to handle it. + testVendorClassEnterprise(Option::V6, 4294967295, encode(4294967295)); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, pushing " + "result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id " + "1234 as result 0x000004D2"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id " + "4294967295 as result 0xFFFFFFFF"); + EXPECT_TRUE(checkFile()); +} + +// Test that expression "vendor-class[4491].data" is able to retrieve content +// of the first tuple of the vendor-class option in DHCPv4. +TEST_F(TokenTest, vendorClass4SpecificVendorData) { + // Case 1: Expression looks for vendor-id 4491, data[0], there is no + // vendor-class option at all, expected result is empty string. + testVendorClassData(Option::V4, 4491, 0, 0, 0, ""); + + // Case 2: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 1234 and no data, expected result is empty string. + testVendorClassData(Option::V4, 4491, 0, 1234, 0, ""); + + // Case 3: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 4491 and no data, expected result is empty string. + // Note that vendor option in v4 always have at least one data chunk, even though + // it may be empty. The OptionVendor code was told to not create any special + // tuples, but it creates one empty on its own. So the code finds that one + // tuple and extracts its content (an empty string). + testVendorClassData(Option::V4, 4491, 0, 4491, 0, ""); + + // Case 4: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string + testVendorClassData(Option::V4, 4491, 0, 1234, 1, ""); + + // Case 5: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 4491 and 1 data tuple, expected result is + // content of that data ("alpha") + testVendorClassData(Option::V4, 4491, 0, 4491, 1, "alpha"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor " + "class found, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor " + "class found, pushing result 'alpha'"); + EXPECT_TRUE(checkFile()); +} + +// Test that expression "vendor-class[4491].data" is able to retrieve content +// of the first tuple of the vendor-class option in DHCPv6. +TEST_F(TokenTest, vendorClass6SpecificVendorData) { + // Case 1: Expression looks for vendor-id 4491, data[0], there is no + // vendor-class option at all, expected result is empty string. + testVendorClassData(Option::V6, 4491, 0, 0, 0, ""); + + // Case 2: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 1234 and no data, expected result is empty string. + testVendorClassData(Option::V6, 4491, 0, 1234, 0, ""); + + // Case 3: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 4491 and no data, expected result is empty string + testVendorClassData(Option::V6, 4491, 0, 4491, 0, ""); + + // Case 4: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string + testVendorClassData(Option::V6, 4491, 0, 1234, 1, ""); + + // Case 5: Expression looks for vendor-id 4491, data[0], there is + // vendor-class with vendor-id 4491 and 1 data tuple, expected result is + // content of that data ("alpha") + testVendorClassData(Option::V6, 4491, 0, 4491, 1, "alpha"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 0, " + "but option with enterprise-id 4491 has only 0 data tuple(s), " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor " + "class found, pushing result 'alpha'"); + EXPECT_TRUE(checkFile()); +} + +// Test that expression "vendor-class[*].data" is able to retrieve content +// of the first tuple of the vendor-class option in DHCPv4. +TEST_F(TokenTest, vendorClass4AnyVendorData) { + // Case 1: Expression looks for any vendor-id (0), data[0], there is no + // vendor-class option at all, expected result is empty string. + testVendorClassData(Option::V4, 0, 0, 0, 0, ""); + + // Case 2: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 1234 and no data (one empty tuple), expected + // result is empty string. + testVendorClassData(Option::V4, 0, 0, 1234, 0, ""); + + // Case 3: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 4491 and no data (one empty tuple), expected + // result is empty string. + testVendorClassData(Option::V4, 0, 0, 4491, 0, ""); + + // Case 4: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 1234 and 1 data tuple, expected result is + // content of that data ("alpha") + testVendorClassData(Option::V4, 0, 0, 1234, 1, "alpha"); + + // Case 5: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 4491 and 1 data tuple, expected result is + // content of that data ("alpha") + testVendorClassData(Option::V4, 0, 0, 4491, 1, "alpha"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in " + "vendor class found, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in " + "vendor class found, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in " + "vendor class found, pushing result 'alpha'"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in " + "vendor class found, pushing result 'alpha'"); + EXPECT_TRUE(checkFile()); +} + +// Test that expression "vendor-class[*].data" is able to retrieve content +// of the first tuple of the vendor-class option in DHCPv6. +TEST_F(TokenTest, vendorClass6AnyVendorData) { + // Case 1: Expression looks for any vendor-id (0), data[0], there is no + // vendor-class option at all, expected result is empty string. + testVendorClassData(Option::V6, 0, 0, 0, 0, ""); + + // Case 2: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 1234 and no data, expected result is empty string. + testVendorClassData(Option::V6, 0, 0, 1234, 0, ""); + + // Case 3: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 4491 and no data, expected result is empty string + testVendorClassData(Option::V6, 0, 0, 4491, 0, ""); + + // Case 4: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 1234 and 1 data tuple, expected result is + // content of that data ("alpha") + testVendorClassData(Option::V6, 0, 0, 1234, 1, "alpha"); + + // Case 5: Expression looks for any vendor-id (0), data[0], there is + // vendor-class with vendor-id 4491 and 1 data tuple, expected result is + // content of that data ("alpha") + testVendorClassData(Option::V6, 0, 0, 4491, 1, "alpha"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 0, " + "but option with enterprise-id 1234 has only 0 data tuple(s), " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 0, " + "but option with enterprise-id 4491 has only 0 data tuple(s), " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor " + "class found, pushing result 'alpha'"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor " + "class found, pushing result 'alpha'"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies if expression vendor-class[4491].data[3] is able to access +// the tuple specified by index. This is a DHCPv4 test. +TEST_F(TokenTest, vendorClass4DataIndex) { + // Case 1: Expression looks for vendor-id 4491, data[3], there is no + // vendor-class option at all, expected result is empty string. + testVendorClassData(Option::V4, 4491, 3, 0, 0, ""); + + // Case 2: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 1234 and no data, expected result is empty string. + testVendorClassData(Option::V4, 4491, 3, 1234, 0, ""); + + // Case 3: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 4491 and no data, expected result is empty string + testVendorClassData(Option::V4, 4491, 3, 4491, 0, ""); + + // Case 4: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string. + testVendorClassData(Option::V4, 4491, 3, 1234, 1, ""); + + // Case 5: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 4491, but has only 3 data tuples, expected + // result is empty string. + testVendorClassData(Option::V4, 4491, 3, 4491, 3, ""); + + // Case 6: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 4491 and 5 data tuples, expected result is + // content of that tuple ("gamma") + testVendorClassData(Option::V4, 4491, 3, 4491, 5, "gamma"); + + // Case 6: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 1234 and 5 data tuples, expected result is + // empty string, because vendor-id does not match. + testVendorClassData(Option::V4, 4491, 3, 1234, 5, ""); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, " + "but option with enterprise-id 4491 has only 1 data tuple(s), " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, " + "but option with enterprise-id 4491 has only 3 data tuple(s), " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 3 (out of 5 received) in vendor " + "class found, pushing result 'gamma'"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + EXPECT_TRUE(checkFile()); +} + +// This test verifies if expression vendor-class[4491].data[3] is able to access +// the tuple specified by index. This is a DHCPv6 test. +TEST_F(TokenTest, vendorClass6DataIndex) { + // Case 1: Expression looks for vendor-id 4491, data[3], there is no + // vendor-class option at all, expected result is empty string. + testVendorClassData(Option::V6, 4491, 3, 0, 0, ""); + + // Case 2: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 1234 and no data, expected result is empty string. + testVendorClassData(Option::V6, 4491, 3, 1234, 0, ""); + + // Case 3: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 4491 and no data, expected result is empty string + testVendorClassData(Option::V6, 4491, 3, 4491, 0, ""); + + // Case 4: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 1234 and 5 data tuples, expected result is empty string. + testVendorClassData(Option::V6, 4491, 3, 1234, 5, ""); + + // Case 5: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 4491, but has only 3 data tuples, expected + // result is empty string. + testVendorClassData(Option::V6, 4491, 3, 4491, 3, ""); + + // Case 6: Expression looks for vendor-id 4491, data[3], there is + // vendor-class with vendor-id 4491 and 5 data tuples, expected result is + // content of that tuple ("gamma") + testVendorClassData(Option::V6, 4491, 3, 4491, 5, "gamma"); + + // Check if the logged messages are correct. + addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, " + "but option with enterprise-id 4491 has only 0 data tuple(s), " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for " + "4491, option had 1234, pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, " + "but option with enterprise-id 4491 has only 3 data tuple(s), " + "pushing result ''"); + addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 3 (out of 5 received) in vendor" + " class found, pushing result 'gamma'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that the existing RAI -sunoption can be found. +TEST_F(TokenTest, subOption) { + + // Insert relay option with sub-options 1 and 13 + insertRelay4Option(); + + // Creating the token should be safe. + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 13, TokenOption::TEXTUAL))); + + // We should be able to evaluate it. + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // The option should be found and option[82].option[13] should evaluate + // to thecontent of that sub-option, i.e. "thirteen" + EXPECT_EQ("thirteen", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUB_OPTION Pushing option 82 " + "sub-option 13 with value 'thirteen'"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that the code properly handles cases when +// there is a RAI option, but there's no requested sub-option. +TEST_F(TokenTest, subOptionNoSubOption) { + + // Insert relay option with sub-options 1 and 13 + insertRelay4Option(); + + + // Creating the token should be safe. + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 15, TokenOption::TEXTUAL))); + + // We should be able to evaluate it. + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // The option should NOT be found (there is no sub-option 15), + // so the expression should evaluate to "" + EXPECT_EQ("", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUB_OPTION Pushing option 82 " + "sub-option 15 with value ''"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that the code properly handles cases when +// there's no RAI option at all. +TEST_F(TokenTest, subOptionNoOption) { + + // We didn't call insertRelay4Option(), so there's no RAI option. + + // Creating the token should be safe. + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 13, TokenOption::TEXTUAL))); + + // We should be able to evaluate it. + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + + // we should have one value on the stack + ASSERT_EQ(1, values_.size()); + + // The option should NOT be found (there is no option 82), + // so the expression should evaluate to "" + EXPECT_EQ("", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUB_OPTION_NO_OPTION Requested option 82 " + "sub-option 13, but the parent option is not present, " + "pushing result ''"); + EXPECT_TRUE(checkFile()); +} + +// This test checks that only the requested parent is searched for +// the requested sub-option. +TEST_F(TokenTest, subOptionOptionOnly) { + + // Insert relay option with sub-options 1 and 13 + insertRelay4Option(); + + // Add options 13 and 70 to the packet. + OptionPtr opt13(new OptionString(Option::V4, 13, "THIRTEEN")); + OptionPtr opt70(new OptionString(Option::V4, 70, "SEVENTY")); + pkt4_->addOption(opt13); + pkt4_->addOption(opt70); + + // The situation is as follows: + // Packet: + // - option 13 (containing "THIRTEEN") + // - option 82 (rai) + // - option 1 (containing "one") + // - option 13 (containing "thirteen") + + // Let's try to get option 13. It should get the one from RAI + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 13, TokenOption::TEXTUAL))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("thirteen", values_.top()); + + // Try to get option 1. It should get the one from RAI + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 1, TokenOption::TEXTUAL))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("one", values_.top()); + + // Try to get option 70. It should fail, as there's no such + // sub option in RAI. + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 70, TokenOption::TEXTUAL))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("", values_.top()); + + // Try to check option 1. It should return "true" + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 1, TokenOption::EXISTS))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("true", values_.top()); + + // Try to check option 70. It should return "false" + clearStack(); + ASSERT_NO_THROW(t_.reset(new TokenSubOption(DHO_DHCP_AGENT_OPTIONS, + 70, TokenOption::EXISTS))); + EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_)); + ASSERT_EQ(1, values_.size()); + EXPECT_EQ("false", values_.top()); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SUB_OPTION Pushing option 82 " + "sub-option 13 with value 'thirteen'"); + addString("EVAL_DEBUG_SUB_OPTION Pushing option 82 " + "sub-option 1 with value 'one'"); + addString("EVAL_DEBUG_SUB_OPTION Pushing option 82 " + "sub-option 70 with value ''"); + addString("EVAL_DEBUG_SUB_OPTION Pushing option 82 " + "sub-option 1 with value 'true'"); + addString("EVAL_DEBUG_SUB_OPTION Pushing option 82 " + "sub-option 70 with value 'false'"); + EXPECT_TRUE(checkFile()); +} + +// Checks if various values can be represented as integer tokens +TEST_F(TokenTest, integer) { + testInteger(encode(0), 0); + testInteger(encode(6), 6); + testInteger(encode(255), 255); + testInteger(encode(256), 256); + testInteger(encode(1410), 1410); + testInteger(encode(4294967295), 4294967295); +} + +// Verify TokenSplit::eval, single delimiter. +TEST_F(TokenTest, split) { + // Get the whole string + std::string input(".two.three..five."); + std::string delims("."); + + // Empty input string should yield empty result. + verifySplitEval("", delims, "1", ""); + + // Empty delimiters string should yield original string result. + verifySplitEval(input, "", "1", input); + + // Field number less than one should yield empty result. + verifySplitEval(input, delims, "0", ""); + + // Now get each field in succession. + verifySplitEval(input, delims, "1", ""); + verifySplitEval(input, delims, "2", "two"); + verifySplitEval(input, delims, "3", "three"); + verifySplitEval(input, delims, "4", ""); + verifySplitEval(input, delims, "5", "five"); + verifySplitEval(input, delims, "6", ""); + + // Too large of a field should yield empty result. + verifySplitEval(input, delims, "7", ""); + + // A string without delimiters returns as field 1. + verifySplitEval("just_one", delims, "1", "just_one"); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + addString("EVAL_DEBUG_SPLIT_EMPTY Popping field 1, delimiters .," + " string , pushing result 0x"); + addString("EVAL_DEBUG_SPLIT_DELIM_EMPTY Popping field 1, delimiters ," + " string .two.three..five., pushing result 0x2E74776F2E74687265652E2E666976652E"); + addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE Popping field 0, delimiters .," + " string .two.three..five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 1, delimiters .," + " string .two.three..five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 2, delimiters .," + " string .two.three..five., pushing result 0x74776F"); + addString("EVAL_DEBUG_SPLIT Popping field 3, delimiters .," + " string .two.three..five., pushing result 0x7468726565"); + addString("EVAL_DEBUG_SPLIT Popping field 4, delimiters .," + " string .two.three..five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 5, delimiters .," + " string .two.three..five., pushing result 0x66697665"); + addString("EVAL_DEBUG_SPLIT Popping field 6, delimiters .," + " string .two.three..five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE Popping field 7, delimiters .," + " string .two.three..five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 1, delimiters .," + " string just_one, pushing result 0x6A7573745F6F6E65"); + EXPECT_TRUE(checkFile()); +} + +// Verify TokenSplit::eval with more than one delimiter. +TEST_F(TokenTest, splitMultipleDelims) { + // Get the whole string + std::string input(".two:three.:five."); + std::string delims(".:"); + + // Empty input string should yield empty result. + verifySplitEval("", delims, "1", ""); + + // Too small of a field should yield empty result. + verifySplitEval(input, delims, "0", ""); + + // Now get each field in succession. + verifySplitEval(input, delims, "1", ""); + verifySplitEval(input, delims, "2", "two"); + verifySplitEval(input, delims, "3", "three"); + verifySplitEval(input, delims, "4", ""); + verifySplitEval(input, delims, "5", "five"); + verifySplitEval(input, delims, "6", ""); + + // Too large of a field should yield empty result. + verifySplitEval(input, delims, "7", ""); + + // A string without delimiters returns as field 1. + verifySplitEval("just_one", delims, "1", "just_one"); + + // Check that the debug output was correct. Add the strings + // to the test vector in the class and then call checkFile + // for comparison + + addString("EVAL_DEBUG_SPLIT_EMPTY Popping field 1, delimiters .:," + " string , pushing result 0x"); + addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE Popping field 0, delimiters .:," + " string .two:three.:five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 1, delimiters .:," + " string .two:three.:five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 2, delimiters .:," + " string .two:three.:five., pushing result 0x74776F"); + addString("EVAL_DEBUG_SPLIT Popping field 3, delimiters .:," + " string .two:three.:five., pushing result 0x7468726565"); + addString("EVAL_DEBUG_SPLIT Popping field 4, delimiters .:," + " string .two:three.:five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 5, delimiters .:," + " string .two:three.:five., pushing result 0x66697665"); + addString("EVAL_DEBUG_SPLIT Popping field 6, delimiters .:," + " string .two:three.:five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT_FIELD_OUT_OF_RANGE Popping field 7, delimiters .:," + " string .two:three.:five., pushing result 0x"); + addString("EVAL_DEBUG_SPLIT Popping field 1, delimiters .:," + " string just_one, pushing result 0x6A7573745F6F6E65"); + EXPECT_TRUE(checkFile()); +} + +}; |