diff options
Diffstat (limited to 'tests/libntp')
45 files changed, 5411 insertions, 0 deletions
diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am new file mode 100644 index 0000000..2f19a89 --- /dev/null +++ b/tests/libntp/Makefile.am @@ -0,0 +1,93 @@ +NULL = +BUILT_SOURCES = +CLEANFILES = + +check_PROGRAMS = tests + +LDADD = \ + $(top_builddir)/libntp/libntp.a \ + $(LDADD_LIBNTP) \ + $(PTHREAD_LIBS) \ + $(LDADD_NTP) \ + $(GTEST_LDFLAGS) \ + $(GTEST_LIBS) \ + $(NULL) + +AM_CFLAGS = $(CFLAGS_NTP) +AM_CXXFLAGS = $(GTEST_CXXFLAGS) + +AM_CPPFLAGS = $(NTP_INCS) +AM_CPPFLAGS += -I$(top_srcdir)/sntp +AM_CPPFLAGS += $(GTEST_CPPFLAGS) +AM_CPPFLAGS += $(CPPFLAGS_NTP) + +AM_LDFLAGS = $(LDFLAGS_NTP) + +tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp \ + libntptest.cpp \ + a_md5encrypt.cpp \ + atoint.cpp \ + atouint.cpp \ + authkeys.cpp \ + buftvtots.cpp \ + calendar.cpp \ + caljulian.cpp \ + caltontp.cpp \ + calyearstart.cpp \ + clocktime.cpp \ + decodenetnum.cpp \ + hextoint.cpp \ + hextolfp.cpp \ + humandate.cpp \ + lfpfunc.cpp \ + lfptostr.cpp \ + modetoa.cpp \ + msyslog.cpp \ + netof.cpp \ + numtoa.cpp \ + numtohost.cpp \ + octtoint.cpp \ + prettydate.cpp \ + recvbuff.cpp \ + refnumtoa.cpp \ + sfptostr.cpp \ + socktoa.cpp \ + ssl_init.cpp \ + statestr.cpp \ + strtolfp.cpp \ + timespecops.cpp \ + timestructs.cpp \ + timevalops.cpp \ + tstotv.cpp \ + tvtots.cpp \ + uglydate.cpp \ + vi64ops.cpp \ + ymd2yd.cpp \ + $(NULL) + +noinst_HEADERS = lfptest.h \ + libntptest.h \ + sockaddrtest.h \ + timestructs.h \ + $(NULL) + +TESTS = + +if !NTP_CROSSCOMPILE +TESTS += tests +endif + +## check-libntp.mf - automake fragment +## slightly adapted for deeper directory + +BUILT_SOURCES += check-libntp +CLEANFILES += check-libntp + +check-libntp: ../../libntp/libntp.a + @echo stamp > $@ + +../../libntp/libntp.a: + cd ../../libntp && $(MAKE) $(AM_MAKEFLAGS) libntp.a + +include $(top_srcdir)/depsver.mf +include $(top_srcdir)/includes.mf diff --git a/tests/libntp/Makefile.in b/tests/libntp/Makefile.in new file mode 100644 index 0000000..ff397f0 --- /dev/null +++ b/tests/libntp/Makefile.in @@ -0,0 +1,970 @@ +# Makefile.in generated by automake 1.11.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009 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@ +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@ +check_PROGRAMS = tests$(EXEEXT) +TESTS = $(am__EXEEXT_1) +@NTP_CROSSCOMPILE_FALSE@am__append_1 = tests +DIST_COMMON = $(noinst_HEADERS) $(srcdir)/Makefile.am \ + $(srcdir)/Makefile.in $(top_srcdir)/depsver.mf \ + $(top_srcdir)/includes.mf +subdir = tests/libntp +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/sntp/libopts/m4/libopts.m4 \ + $(top_srcdir)/sntp/libopts/m4/stdnoreturn.m4 \ + $(top_srcdir)/sntp/libevent/m4/openldap-thread-check.m4 \ + $(top_srcdir)/sntp/libevent/m4/openldap.m4 \ + $(top_srcdir)/sntp/m4/define_dir.m4 \ + $(top_srcdir)/sntp/m4/hms_search_lib.m4 \ + $(top_srcdir)/sntp/m4/libtool.m4 \ + $(top_srcdir)/sntp/m4/ltoptions.m4 \ + $(top_srcdir)/sntp/m4/ltsugar.m4 \ + $(top_srcdir)/sntp/m4/ltversion.m4 \ + $(top_srcdir)/sntp/m4/lt~obsolete.m4 \ + $(top_srcdir)/sntp/m4/ntp_cacheversion.m4 \ + $(top_srcdir)/sntp/m4/ntp_compiler.m4 \ + $(top_srcdir)/sntp/m4/ntp_crosscompile.m4 \ + $(top_srcdir)/sntp/m4/ntp_debug.m4 \ + $(top_srcdir)/sntp/m4/ntp_dir_sep.m4 \ + $(top_srcdir)/sntp/m4/ntp_facilitynames.m4 \ + $(top_srcdir)/sntp/m4/ntp_googletest.m4 \ + $(top_srcdir)/sntp/m4/ntp_ipv6.m4 \ + $(top_srcdir)/sntp/m4/ntp_lib_m.m4 \ + $(top_srcdir)/sntp/m4/ntp_libevent.m4 \ + $(top_srcdir)/sntp/m4/ntp_libntp.m4 \ + $(top_srcdir)/sntp/m4/ntp_lineeditlibs.m4 \ + $(top_srcdir)/sntp/m4/ntp_locinfo.m4 \ + $(top_srcdir)/sntp/m4/ntp_openssl.m4 \ + $(top_srcdir)/sntp/m4/ntp_pkg_config.m4 \ + $(top_srcdir)/sntp/m4/ntp_prog_cc.m4 \ + $(top_srcdir)/sntp/m4/ntp_rlimit.m4 \ + $(top_srcdir)/sntp/m4/ntp_sntp.m4 \ + $(top_srcdir)/sntp/m4/ntp_ver_suffix.m4 \ + $(top_srcdir)/sntp/m4/ntp_vpathhack.m4 \ + $(top_srcdir)/sntp/m4/os_cflags.m4 \ + $(top_srcdir)/sntp/m4/snprintf.m4 \ + $(top_srcdir)/sntp/m4/version.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am__objects_1 = +am_tests_OBJECTS = tests_main.$(OBJEXT) libntptest.$(OBJEXT) \ + a_md5encrypt.$(OBJEXT) atoint.$(OBJEXT) atouint.$(OBJEXT) \ + authkeys.$(OBJEXT) buftvtots.$(OBJEXT) calendar.$(OBJEXT) \ + caljulian.$(OBJEXT) caltontp.$(OBJEXT) calyearstart.$(OBJEXT) \ + clocktime.$(OBJEXT) decodenetnum.$(OBJEXT) hextoint.$(OBJEXT) \ + hextolfp.$(OBJEXT) humandate.$(OBJEXT) lfpfunc.$(OBJEXT) \ + lfptostr.$(OBJEXT) modetoa.$(OBJEXT) msyslog.$(OBJEXT) \ + netof.$(OBJEXT) numtoa.$(OBJEXT) numtohost.$(OBJEXT) \ + octtoint.$(OBJEXT) prettydate.$(OBJEXT) recvbuff.$(OBJEXT) \ + refnumtoa.$(OBJEXT) sfptostr.$(OBJEXT) socktoa.$(OBJEXT) \ + ssl_init.$(OBJEXT) statestr.$(OBJEXT) strtolfp.$(OBJEXT) \ + timespecops.$(OBJEXT) timestructs.$(OBJEXT) \ + timevalops.$(OBJEXT) tstotv.$(OBJEXT) tvtots.$(OBJEXT) \ + uglydate.$(OBJEXT) vi64ops.$(OBJEXT) ymd2yd.$(OBJEXT) \ + $(am__objects_1) +tests_OBJECTS = $(am_tests_OBJECTS) +tests_LDADD = $(LDADD) +am__DEPENDENCIES_1 = +tests_DEPENDENCIES = $(top_builddir)/libntp/libntp.a \ + $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ + $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ + $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) +AM_V_lt = $(am__v_lt_$(V)) +am__v_lt_ = $(am__v_lt_$(AM_DEFAULT_VERBOSITY)) +am__v_lt_0 = --silent +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/sntp/libevent/build-aux/depcomp +am__depfiles_maybe = depfiles +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_$(V)) +am__v_CXX_ = $(am__v_CXX_$(AM_DEFAULT_VERBOSITY)) +am__v_CXX_0 = @echo " CXX " $@; +AM_V_at = $(am__v_at_$(V)) +am__v_at_ = $(am__v_at_$(AM_DEFAULT_VERBOSITY)) +am__v_at_0 = @ +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_$(V)) +am__v_CXXLD_ = $(am__v_CXXLD_$(AM_DEFAULT_VERBOSITY)) +am__v_CXXLD_0 = @echo " CXXLD " $@; +AM_V_GEN = $(am__v_GEN_$(V)) +am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY)) +am__v_GEN_0 = @echo " GEN " $@; +SOURCES = $(tests_SOURCES) +DIST_SOURCES = $(tests_SOURCES) +HEADERS = $(noinst_HEADERS) +ETAGS = etags +CTAGS = ctags +am__tty_colors = \ +red=; grn=; lgn=; blu=; std= +@NTP_CROSSCOMPILE_FALSE@am__EXEEXT_1 = tests$(EXEEXT) +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +ALLOCA = @ALLOCA@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CALC_TICKADJ_DB = @CALC_TICKADJ_DB@ +CALC_TICKADJ_DL = @CALC_TICKADJ_DL@ +CALC_TICKADJ_DS = @CALC_TICKADJ_DS@ +CALC_TICKADJ_MS = @CALC_TICKADJ_MS@ +CALC_TICKADJ_NI = @CALC_TICKADJ_NI@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CFLAGS_NTP = @CFLAGS_NTP@ +CHUTEST = @CHUTEST@ +CONFIG_SHELL = @CONFIG_SHELL@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CPPFLAGS_NTP = @CPPFLAGS_NTP@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DCFD = @DCFD@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EDITLINE_LIBS = @EDITLINE_LIBS@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +GREP = @GREP@ +GTEST_CONFIG = @GTEST_CONFIG@ +GTEST_CPPFLAGS = @GTEST_CPPFLAGS@ +GTEST_CXXFLAGS = @GTEST_CXXFLAGS@ +GTEST_LDFLAGS = @GTEST_LDFLAGS@ +GTEST_LIBS = @GTEST_LIBS@ +HAVE_INLINE = @HAVE_INLINE@ +HAVE_RLIMIT_MEMLOCK = @HAVE_RLIMIT_MEMLOCK@ +HAVE_RLIMIT_STACK = @HAVE_RLIMIT_STACK@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDADD_LIBNTP = @LDADD_LIBNTP@ +LDADD_NLIST = @LDADD_NLIST@ +LDADD_NTP = @LDADD_NTP@ +LDFLAGS = @LDFLAGS@ +LDFLAGS_NTP = @LDFLAGS_NTP@ +LIBISC_PTHREADS_NOTHREADS = @LIBISC_PTHREADS_NOTHREADS@ +LIBM = @LIBM@ +LIBOBJS = @LIBOBJS@ +LIBOPTS_CFLAGS = @LIBOPTS_CFLAGS@ +LIBOPTS_DIR = @LIBOPTS_DIR@ +LIBOPTS_LDADD = @LIBOPTS_LDADD@ +LIBPARSE = @LIBPARSE@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIBTOOL_DEPS = @LIBTOOL_DEPS@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LSCF = @LSCF@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MAKE_ADJTIMED = @MAKE_ADJTIMED@ +MAKE_CHECK_LAYOUT = @MAKE_CHECK_LAYOUT@ +MAKE_CHECK_Y2K = @MAKE_CHECK_Y2K@ +MAKE_LIBNTPSIM = @MAKE_LIBNTPSIM@ +MAKE_LIBPARSE = @MAKE_LIBPARSE@ +MAKE_LIBPARSE_KERNEL = @MAKE_LIBPARSE_KERNEL@ +MAKE_NTPDSIM = @MAKE_NTPDSIM@ +MAKE_NTPSNMPD = @MAKE_NTPSNMPD@ +MAKE_NTPTIME = @MAKE_NTPTIME@ +MAKE_PARSEKMODULE = @MAKE_PARSEKMODULE@ +MAKE_TICKADJ = @MAKE_TICKADJ@ +MAKE_TIMETRIM = @MAKE_TIMETRIM@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MANTAGFMT = @MANTAGFMT@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +NTPDATE_DB = @NTPDATE_DB@ +NTPDATE_DL = @NTPDATE_DL@ +NTPDATE_DS = @NTPDATE_DS@ +NTPDATE_MS = @NTPDATE_MS@ +NTPDATE_NI = @NTPDATE_NI@ +NTPDC_DB = @NTPDC_DB@ +NTPDC_DL = @NTPDC_DL@ +NTPDC_DS = @NTPDC_DS@ +NTPDC_MS = @NTPDC_MS@ +NTPDC_NI = @NTPDC_NI@ +NTPDSIM_DB = @NTPDSIM_DB@ +NTPDSIM_DL = @NTPDSIM_DL@ +NTPDSIM_DS = @NTPDSIM_DS@ +NTPDSIM_MS = @NTPDSIM_MS@ +NTPDSIM_NI = @NTPDSIM_NI@ +NTPD_DB = @NTPD_DB@ +NTPD_DL = @NTPD_DL@ +NTPD_DS = @NTPD_DS@ +NTPD_MS = @NTPD_MS@ +NTPD_NI = @NTPD_NI@ +NTPQ_DB = @NTPQ_DB@ +NTPQ_DL = @NTPQ_DL@ +NTPQ_DS = @NTPQ_DS@ +NTPQ_MS = @NTPQ_MS@ +NTPQ_NI = @NTPQ_NI@ +NTPSNMPD_DB = @NTPSNMPD_DB@ +NTPSNMPD_DL = @NTPSNMPD_DL@ +NTPSNMPD_DS = @NTPSNMPD_DS@ +NTPSNMPD_MS = @NTPSNMPD_MS@ +NTPSNMPD_NI = @NTPSNMPD_NI@ +NTPSWEEP_DB = @NTPSWEEP_DB@ +NTPSWEEP_DL = @NTPSWEEP_DL@ +NTPSWEEP_DS = @NTPSWEEP_DS@ +NTPSWEEP_MS = @NTPSWEEP_MS@ +NTPSWEEP_NI = @NTPSWEEP_NI@ +NTPTIME_DB = @NTPTIME_DB@ +NTPTIME_DL = @NTPTIME_DL@ +NTPTIME_DS = @NTPTIME_DS@ +NTPTIME_MS = @NTPTIME_MS@ +NTPTIME_NI = @NTPTIME_NI@ +NTPTRACE_DB = @NTPTRACE_DB@ +NTPTRACE_DL = @NTPTRACE_DL@ +NTPTRACE_DS = @NTPTRACE_DS@ +NTPTRACE_MS = @NTPTRACE_MS@ +NTPTRACE_NI = @NTPTRACE_NI@ +NTP_KEYGEN_DB = @NTP_KEYGEN_DB@ +NTP_KEYGEN_DL = @NTP_KEYGEN_DL@ +NTP_KEYGEN_DS = @NTP_KEYGEN_DS@ +NTP_KEYGEN_MS = @NTP_KEYGEN_MS@ +NTP_KEYGEN_NI = @NTP_KEYGEN_NI@ +NTP_KEYSDIR = @NTP_KEYSDIR@ +NTP_WAIT_DB = @NTP_WAIT_DB@ +NTP_WAIT_DL = @NTP_WAIT_DL@ +NTP_WAIT_DS = @NTP_WAIT_DS@ +NTP_WAIT_MS = @NTP_WAIT_MS@ +NTP_WAIT_NI = @NTP_WAIT_NI@ +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@ +PATH_NET_SNMP_CONFIG = @PATH_NET_SNMP_CONFIG@ +PATH_PERL = @PATH_PERL@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +PATH_TEST = @PATH_TEST@ +PERLLIBDIR = @PERLLIBDIR@ +PKG_CONFIG = @PKG_CONFIG@ +POSIX_SHELL = @POSIX_SHELL@ +PROPDELAY = @PROPDELAY@ +PTHREAD_LIBS = @PTHREAD_LIBS@ +RANLIB = @RANLIB@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +SNMP_CFLAGS = @SNMP_CFLAGS@ +SNMP_CPPFLAGS = @SNMP_CPPFLAGS@ +SNMP_LIBS = @SNMP_LIBS@ +SNTP = @SNTP@ +SNTP_DB = @SNTP_DB@ +SNTP_DL = @SNTP_DL@ +SNTP_DS = @SNTP_DS@ +SNTP_MS = @SNTP_MS@ +SNTP_NI = @SNTP_NI@ +STDNORETURN_H = @STDNORETURN_H@ +STRIP = @STRIP@ +TESTDCF = @TESTDCF@ +TICKADJ_DB = @TICKADJ_DB@ +TICKADJ_DL = @TICKADJ_DL@ +TICKADJ_DS = @TICKADJ_DS@ +TICKADJ_MS = @TICKADJ_MS@ +TICKADJ_NI = @TICKADJ_NI@ +TIMETRIM_DB = @TIMETRIM_DB@ +TIMETRIM_DL = @TIMETRIM_DL@ +TIMETRIM_DS = @TIMETRIM_DS@ +TIMETRIM_MS = @TIMETRIM_MS@ +TIMETRIM_NI = @TIMETRIM_NI@ +VERSION = @VERSION@ +VER_SUFFIX = @VER_SUFFIX@ +YACC = @YACC@ +YFLAGS = @YFLAGS@ +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@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +subdirs = @subdirs@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +NULL = +BUILT_SOURCES = check-libntp .deps-ver +CLEANFILES = check-libntp .deps-ver +LDADD = \ + $(top_builddir)/libntp/libntp.a \ + $(LDADD_LIBNTP) \ + $(PTHREAD_LIBS) \ + $(LDADD_NTP) \ + $(GTEST_LDFLAGS) \ + $(GTEST_LIBS) \ + $(NULL) + +AM_CFLAGS = $(CFLAGS_NTP) +AM_CXXFLAGS = $(GTEST_CXXFLAGS) +AM_CPPFLAGS = $(NTP_INCS) -I$(top_srcdir)/sntp $(GTEST_CPPFLAGS) \ + $(CPPFLAGS_NTP) +AM_LDFLAGS = $(LDFLAGS_NTP) +tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp \ + libntptest.cpp \ + a_md5encrypt.cpp \ + atoint.cpp \ + atouint.cpp \ + authkeys.cpp \ + buftvtots.cpp \ + calendar.cpp \ + caljulian.cpp \ + caltontp.cpp \ + calyearstart.cpp \ + clocktime.cpp \ + decodenetnum.cpp \ + hextoint.cpp \ + hextolfp.cpp \ + humandate.cpp \ + lfpfunc.cpp \ + lfptostr.cpp \ + modetoa.cpp \ + msyslog.cpp \ + netof.cpp \ + numtoa.cpp \ + numtohost.cpp \ + octtoint.cpp \ + prettydate.cpp \ + recvbuff.cpp \ + refnumtoa.cpp \ + sfptostr.cpp \ + socktoa.cpp \ + ssl_init.cpp \ + statestr.cpp \ + strtolfp.cpp \ + timespecops.cpp \ + timestructs.cpp \ + timevalops.cpp \ + tstotv.cpp \ + tvtots.cpp \ + uglydate.cpp \ + vi64ops.cpp \ + ymd2yd.cpp \ + $(NULL) + +noinst_HEADERS = lfptest.h \ + libntptest.h \ + sockaddrtest.h \ + timestructs.h \ + $(NULL) + +NTP_INCS = -I$(top_srcdir)/include -I$(top_srcdir)/lib/isc/include \ + -I$(top_srcdir)/lib/isc/$(LIBISC_PTHREADS_NOTHREADS)/include \ + -I$(top_srcdir)/lib/isc/unix/include +all: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) all-am + +.SUFFIXES: +.SUFFIXES: .cpp .lo .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(top_srcdir)/depsver.mf $(top_srcdir)/includes.mf $(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 tests/libntp/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign tests/libntp/Makefile +.PRECIOUS: 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__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + 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-checkPROGRAMS: + @list='$(check_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 +tests$(EXEEXT): $(tests_OBJECTS) $(tests_DEPENDENCIES) + @rm -f tests$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(tests_OBJECTS) $(tests_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/a_md5encrypt.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/atoint.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/atouint.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/authkeys.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/buftvtots.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/calendar.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/caljulian.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/caltontp.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/calyearstart.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/clocktime.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/decodenetnum.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hextoint.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hextolfp.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/humandate.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lfpfunc.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lfptostr.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libntptest.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/modetoa.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/msyslog.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/netof.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/numtoa.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/numtohost.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/octtoint.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/prettydate.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/recvbuff.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/refnumtoa.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sfptostr.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/socktoa.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ssl_init.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/statestr.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/strtolfp.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tests_main.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/timespecops.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/timestructs.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/timevalops.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tstotv.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tvtots.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/uglydate.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vi64ops.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ymd2yd.Po@am__quote@ + +.cpp.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 +@am__fastdepCXX_FALSE@ $(AM_V_CXX) @AM_BACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ $< + +.cpp.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 +@am__fastdepCXX_FALSE@ $(AM_V_CXX) @AM_BACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.cpp.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 +@am__fastdepCXX_FALSE@ $(AM_V_CXX) @AM_BACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(LTCXXCOMPILE) -c -o $@ $< + +tests_main.o: $(top_srcdir)/sntp/tests_main.cpp +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT tests_main.o -MD -MP -MF $(DEPDIR)/tests_main.Tpo -c -o tests_main.o `test -f '$(top_srcdir)/sntp/tests_main.cpp' || echo '$(srcdir)/'`$(top_srcdir)/sntp/tests_main.cpp +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tests_main.Tpo $(DEPDIR)/tests_main.Po +@am__fastdepCXX_FALSE@ $(AM_V_CXX) @AM_BACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$(top_srcdir)/sntp/tests_main.cpp' object='tests_main.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o tests_main.o `test -f '$(top_srcdir)/sntp/tests_main.cpp' || echo '$(srcdir)/'`$(top_srcdir)/sntp/tests_main.cpp + +tests_main.obj: $(top_srcdir)/sntp/tests_main.cpp +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT tests_main.obj -MD -MP -MF $(DEPDIR)/tests_main.Tpo -c -o tests_main.obj `if test -f '$(top_srcdir)/sntp/tests_main.cpp'; then $(CYGPATH_W) '$(top_srcdir)/sntp/tests_main.cpp'; else $(CYGPATH_W) '$(srcdir)/$(top_srcdir)/sntp/tests_main.cpp'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/tests_main.Tpo $(DEPDIR)/tests_main.Po +@am__fastdepCXX_FALSE@ $(AM_V_CXX) @AM_BACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$(top_srcdir)/sntp/tests_main.cpp' object='tests_main.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o tests_main.obj `if test -f '$(top_srcdir)/sntp/tests_main.cpp'; then $(CYGPATH_W) '$(top_srcdir)/sntp/tests_main.cpp'; else $(CYGPATH_W) '$(srcdir)/$(top_srcdir)/sntp/tests_main.cpp'; fi` + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + set x; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + 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 +CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + 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" + +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; 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 \ + echo "$$grn$$dashes"; \ + else \ + echo "$$red$$dashes"; \ + fi; \ + echo "$$banner"; \ + test -z "$$skipped" || echo "$$skipped"; \ + test -z "$$report" || echo "$$report"; \ + echo "$$dashes$$std"; \ + test "$$failed" -eq 0; \ + else :; fi + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) $(check_PROGRAMS) + $(MAKE) $(AM_MAKEFLAGS) check-TESTS +check: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) check-am +all-am: Makefile $(HEADERS) +installdirs: +install: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +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." + -test -z "$(BUILT_SOURCES)" || rm -f $(BUILT_SOURCES) +clean: clean-am + +clean-am: clean-checkPROGRAMS clean-generic clean-libtool \ + mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: all check check-am install install-am install-strip + +.PHONY: CTAGS GTAGS all all-am check check-TESTS check-am clean \ + clean-checkPROGRAMS clean-generic clean-libtool ctags \ + 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 maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags uninstall uninstall-am + + +check-libntp: ../../libntp/libntp.a + @echo stamp > $@ + +../../libntp/libntp.a: + cd ../../libntp && $(MAKE) $(AM_MAKEFLAGS) libntp.a +$(DEPDIR)/deps-ver: $(top_srcdir)/deps-ver + @[ -f $@ ] || \ + cp $(top_srcdir)/deps-ver $@ + @[ -w $@ ] || \ + chmod ug+w $@ + @cmp $(top_srcdir)/deps-ver $@ > /dev/null || ( \ + $(MAKE) $(AM_MAKEFLAGS) clean && \ + echo -n "Prior $(subdir)/$(DEPDIR) version " && \ + cat $@ && \ + rm -rf $(DEPDIR) && \ + mkdir $(DEPDIR) && \ + case "$(top_builddir)" in \ + .) \ + ./config.status Makefile depfiles \ + ;; \ + *) \ + cd "$(top_builddir)" && \ + ./config.status $(subdir)/Makefile depfiles && \ + cd $(subdir) \ + ;; \ + esac && \ + echo -n "Cleaned $(subdir)/$(DEPDIR) version " && \ + cat $(top_srcdir)/deps-ver \ + ) + cp $(top_srcdir)/deps-ver $@ + +.deps-ver: $(top_srcdir)/deps-ver + @[ ! -d $(DEPDIR) ] || $(MAKE) $(AM_MAKEFLAGS) $(DEPDIR)/deps-ver + @touch $@ + +# +# depsver.mf included in Makefile.am for directories with .deps +# +# When building in the same directory with sources that change over +# time, such as when tracking using bk, the .deps files can become +# stale with respect to moved, deleted, or superceded headers. Most +# commonly, this would exhibit as make reporting a failure to make a +# header file which is no longer in the location given. To address +# this issue, we use a deps-ver file which is updated with each change +# that breaks old .deps files. A copy of deps-ver is made into +# $(DEPDIR) if not already present. If $(DEPDIR)/deps-ver is present +# with different contents than deps-ver, we make clean to ensure all +# .o files built before the incompatible change are rebuilt along with +# their updated .deps files, then remove $(DEPDIR) and recreate it as +# empty stubs. +# +# It is normal when configured with --disable-dependency-tracking for +# the DEPDIR to not have been created. For this reason, we use the +# intermediate target .deps-ver, which invokes make recursively if +# DEPDIR exists. +# +# If you modify depsver.mf, please make the changes to the master +# copy, the one in sntp is copied by the bootstrap script from it. +# +# This comment block follows rather than leads the related code so that +# it stays with it in the generated Makefile.in and 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/tests/libntp/a_md5encrypt.cpp b/tests/libntp/a_md5encrypt.cpp new file mode 100644 index 0000000..f4cea7e --- /dev/null +++ b/tests/libntp/a_md5encrypt.cpp @@ -0,0 +1,89 @@ +#include "libntptest.h" + +extern "C" { +#ifdef OPENSSL +# include "openssl/err.h" +# include "openssl/rand.h" +# include "openssl/evp.h" +#endif +#include "ntp.h" +#include "ntp_stdlib.h" +}; + +class a_md5encryptTest : public libntptest { +protected: +}; + +/* + * Example packet with MD5 hash calculated manually. + */ +const int keytype = KEY_TYPE_MD5; +const char *key = "abcdefgh"; +const u_short keyLength = 8; +const char *packet = "ijklmnopqrstuvwx"; +const int packetLength = 16; +const int keyIdLength = 4; +const int digestLength = 16; +const int totalLength = packetLength + keyIdLength + digestLength; +const char *expectedPacket = "ijklmnopqrstuvwx\0\0\0\0\x0c\x0e\x84\xcf\x0b\xb7\xa8\x68\x8e\x52\x38\xdb\xbc\x1c\x39\x53"; + +TEST_F(a_md5encryptTest, Encrypt) { + char *packetPtr = new char[totalLength]; + memset(packetPtr+packetLength, 0, keyIdLength); + memcpy(packetPtr, packet, packetLength); + + cache_secretsize = keyLength; + + int length = MD5authencrypt(keytype, (u_char*)key, (u_int32*)packetPtr, packetLength); + + EXPECT_TRUE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)packetPtr, packetLength, length)); + + EXPECT_EQ(20, length); + EXPECT_TRUE(memcmp(expectedPacket, packetPtr, totalLength) == 0); + + delete[] packetPtr; +} + +TEST_F(a_md5encryptTest, DecryptValid) { + cache_secretsize = keyLength; + + EXPECT_TRUE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)expectedPacket, packetLength, 20)); +} + +TEST_F(a_md5encryptTest, DecryptInvalid) { + cache_secretsize = keyLength; + + const char *invalidPacket = "ijklmnopqrstuvwx\0\0\0\0\x0c\x0e\x84\xcf\x0b\xb7\xa8\x68\x8e\x52\x38\xdb\xbc\x1c\x39\x54"; + + EXPECT_FALSE(MD5authdecrypt(keytype, (u_char*)key, (u_int32*)invalidPacket, packetLength, 20)); +} + +TEST_F(a_md5encryptTest, IPv4AddressToRefId) { + sockaddr_u addr; + addr.sa4.sin_family = AF_INET; + addr.sa4.sin_port = htons(80); + + u_int32 address = inet_addr("192.0.2.1"); + addr.sa4.sin_addr.s_addr = address; + + EXPECT_EQ(address, addr2refid(&addr)); +} + +TEST_F(a_md5encryptTest, IPv6AddressToRefId) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + + sockaddr_u addr; + addr.sa6.sin6_family = AF_INET6; + + addr.sa6.sin6_addr = address; + + const int expected = 0x75cffd52; + + EXPECT_EQ(expected, addr2refid(&addr)); +} diff --git a/tests/libntp/atoint.cpp b/tests/libntp/atoint.cpp new file mode 100644 index 0000000..371f3fe --- /dev/null +++ b/tests/libntp/atoint.cpp @@ -0,0 +1,48 @@ +#include "libntptest.h" + +class atointTest : public libntptest { +}; + +TEST_F(atointTest, RegularPositive) { + const char *str = "17"; + long val; + + ASSERT_TRUE(atoint(str, &val)); + EXPECT_EQ(17, val); +} + +TEST_F(atointTest, RegularNegative) { + const char *str = "-20"; + long val; + + ASSERT_TRUE(atoint(str, &val)); + EXPECT_EQ(-20, val); +} + +TEST_F(atointTest, PositiveOverflowBoundary) { + const char *str = "2147483648"; + long val; + + EXPECT_FALSE(atoint(str, &val)); +} + +TEST_F(atointTest, NegativeOverflowBoundary) { + const char *str = "-2147483649"; + long val; + + EXPECT_FALSE(atoint(str, &val)); +} + +TEST_F(atointTest, PositiveOverflowBig) { + const char *str = "2300000000"; + long val; + + EXPECT_FALSE(atoint(str, &val)); +} + +TEST_F(atointTest, IllegalCharacter) { + const char *str = "4500l"; + long val; + + EXPECT_FALSE(atoint(str, &val)); +} diff --git a/tests/libntp/atouint.cpp b/tests/libntp/atouint.cpp new file mode 100644 index 0000000..cc8cc39 --- /dev/null +++ b/tests/libntp/atouint.cpp @@ -0,0 +1,40 @@ +#include "libntptest.h" + +class atouintTest : public libntptest { +}; + +TEST_F(atouintTest, RegularPositive) { + const char *str = "305"; + u_long actual; + + ASSERT_TRUE(atouint(str, &actual)); + EXPECT_EQ(305, actual); +} + +TEST_F(atouintTest, PositiveOverflowBoundary) { + const char *str = "4294967296"; + u_long actual; + + ASSERT_FALSE(atouint(str, &actual)); +} + +TEST_F(atouintTest, PositiveOverflowBig) { + const char *str = "8000000000"; + u_long actual; + + ASSERT_FALSE(atouint(str, &actual)); +} + +TEST_F(atouintTest, Negative) { + const char *str = "-1"; + u_long actual; + + ASSERT_FALSE(atouint(str, &actual)); +} + +TEST_F(atouintTest, IllegalChar) { + const char *str = "50c3"; + u_long actual; + + ASSERT_FALSE(atouint(str, &actual)); +} diff --git a/tests/libntp/authkeys.cpp b/tests/libntp/authkeys.cpp new file mode 100644 index 0000000..8dd33a9 --- /dev/null +++ b/tests/libntp/authkeys.cpp @@ -0,0 +1,100 @@ +/* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */ + +#include "libntptest.h" + +extern "C" { +#ifdef OPENSSL +# include "openssl/err.h" +# include "openssl/rand.h" +# include "openssl/evp.h" +#endif +#include "ntp.h" +#include "ntp_stdlib.h" +}; + +class authkeysTest : public libntptest { +protected: + static const int KEYTYPE = KEY_TYPE_MD5; + + virtual void SetUp() { + /* + * init_auth() is called by tests_main.cpp earlier. It + * does not initialize global variables like + * authnumkeys, so let's reset them to zero here. + */ + authnumkeys = 0; + + /* + * Especially, empty the key cache! + */ + cache_keyid = 0; + cache_type = 0; + cache_flags = 0; + cache_secret = NULL; + cache_secretsize = 0; + } + + void AddTrustedKey(keyid_t keyno) { + /* + * We need to add a MD5-key in addition to setting the + * trust, because authhavekey() requires type != 0. + */ + MD5auth_setkey(keyno, KEYTYPE, NULL, 0); + + authtrust(keyno, TRUE); + } + + void AddUntrustedKey(keyid_t keyno) { + authtrust(keyno, FALSE); + } +}; + +TEST_F(authkeysTest, AddTrustedKeys) { + const keyid_t KEYNO1 = 5; + const keyid_t KEYNO2 = 8; + + AddTrustedKey(KEYNO1); + AddTrustedKey(KEYNO2); + + EXPECT_TRUE(authistrusted(KEYNO1)); + EXPECT_TRUE(authistrusted(KEYNO2)); +} + +TEST_F(authkeysTest, AddUntrustedKey) { + const keyid_t KEYNO = 3; + + AddUntrustedKey(KEYNO); + + EXPECT_FALSE(authistrusted(KEYNO)); +} + +TEST_F(authkeysTest, HaveKeyCorrect) { + const keyid_t KEYNO = 3; + + AddTrustedKey(KEYNO); + + EXPECT_TRUE(auth_havekey(KEYNO)); + EXPECT_TRUE(authhavekey(KEYNO)); +} + +TEST_F(authkeysTest, HaveKeyIncorrect) { + const keyid_t KEYNO = 2; + + EXPECT_FALSE(auth_havekey(KEYNO)); + EXPECT_FALSE(authhavekey(KEYNO)); +} + +TEST_F(authkeysTest, AddWithAuthUseKey) { + const keyid_t KEYNO = 5; + const char* KEY = "52a"; + + EXPECT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); +} + +TEST_F(authkeysTest, EmptyKey) { + const keyid_t KEYNO = 3; + const char* KEY = ""; + + + EXPECT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); +} diff --git a/tests/libntp/buftvtots.cpp b/tests/libntp/buftvtots.cpp new file mode 100644 index 0000000..bb8a32d --- /dev/null +++ b/tests/libntp/buftvtots.cpp @@ -0,0 +1,61 @@ +#include "lfptest.h" + +extern "C" { +#include "ntp_unixtime.h" +}; + +// Required for Solaris. +#include <math.h> + +class buftvtotsTest : public lfptest { +}; + +#ifndef SYS_WINNT +TEST_F(buftvtotsTest, ZeroBuffer) { + const timeval input = {0, 0}; + const l_fp expected = {0 + JAN_1970, 0}; + + l_fp actual; + + ASSERT_TRUE(buftvtots((const char*)(&input), &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(buftvtotsTest, IntegerAndFractionalBuffer) { + const timeval input = {5, 500000}; // 5.5 + const l_fp expected = {5 + JAN_1970, HALF}; + + l_fp actual; + + ASSERT_TRUE(buftvtots((const char*)(&input), &actual)); + + // Compare the fractional part with an absolute error given. + EXPECT_EQ(expected.l_ui, actual.l_ui); + + double expectedDouble, actualDouble; + M_LFPTOD(0, expected.l_uf, expectedDouble); + M_LFPTOD(0, actual.l_uf, actualDouble); + + // The error should be less than 0.5 us + EXPECT_NEAR(expectedDouble, actualDouble, 0.0000005); +} + +TEST_F(buftvtotsTest, IllegalMicroseconds) { + const timeval input = {0, 1100000}; // > 999 999 microseconds. + + l_fp actual; + + ASSERT_FALSE(buftvtots((const char*)(&input), &actual)); +} + +#else +TEST_F(buftvtotsTest, AlwaysFalseOnWindows) { + /* + * Under Windows, buftvtots will just return + * 0 (false). + */ + l_fp actual; + ASSERT_FALSE(buftvtots("", &actual)); +} + +#endif diff --git a/tests/libntp/calendar.cpp b/tests/libntp/calendar.cpp new file mode 100644 index 0000000..35b39c7 --- /dev/null +++ b/tests/libntp/calendar.cpp @@ -0,0 +1,337 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_calendar.h" +} + +#include <string> +#include <sstream> + +class calendarTest : public libntptest { +protected: + static int leapdays(int year); + + std::string CalendarToString(const calendar &cal); + std::string CalendarToString(const isodate &iso); + ::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual); + ::testing::AssertionResult IsEqual(const isodate &expected, const isodate &actual); + + std::string DateToString(const calendar &cal); + std::string DateToString(const isodate &iso); + ::testing::AssertionResult IsEqualDate(const calendar &expected, const calendar &actual); + ::testing::AssertionResult IsEqualDate(const isodate &expected, const isodate &actual); +}; + + +// --------------------------------------------------------------------- +// test support stuff +// --------------------------------------------------------------------- +int +calendarTest::leapdays(int year) +{ + if (year % 400 == 0) + return 1; + if (year % 100 == 0) + return 0; + if (year % 4 == 0) + return 1; + return 0; +} + +std::string +calendarTest::CalendarToString(const calendar &cal) { + std::ostringstream ss; + ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday + << " (" << cal.yearday << ") " << (u_int)cal.hour << ":" + << (u_int)cal.minute << ":" << (u_int)cal.second; + return ss.str(); +} + +std::string +calendarTest:: CalendarToString(const isodate &iso) { + std::ostringstream ss; + ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday + << (u_int)iso.hour << ":" << (u_int)iso.minute << ":" << (u_int)iso.second; + return ss.str(); +} + +::testing::AssertionResult +calendarTest:: IsEqual(const calendar &expected, const calendar &actual) { + if (expected.year == actual.year && + (!expected.yearday || expected.yearday == actual.yearday) && + expected.month == actual.month && + expected.monthday == actual.monthday && + expected.hour == actual.hour && + expected.minute == actual.minute && + expected.second == actual.second) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "expected: " << CalendarToString(expected) << " but was " + << CalendarToString(actual); + } +} + +::testing::AssertionResult +calendarTest:: IsEqual(const isodate &expected, const isodate &actual) { + if (expected.year == actual.year && + expected.week == actual.week && + expected.weekday == actual.weekday && + expected.hour == actual.hour && + expected.minute == actual.minute && + expected.second == actual.second) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "expected: " << CalendarToString(expected) << " but was " + << CalendarToString(actual); + } +} + +std::string +calendarTest:: DateToString(const calendar &cal) { + std::ostringstream ss; + ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday + << " (" << cal.yearday << ")"; + return ss.str(); +} + +std::string +calendarTest:: DateToString(const isodate &iso) { + std::ostringstream ss; + ss << iso.year << "-" << (u_int)iso.week << "-" << (u_int)iso.weekday; + return ss.str(); +} + +::testing::AssertionResult +calendarTest:: IsEqualDate(const calendar &expected, const calendar &actual) { + if (expected.year == actual.year && + (!expected.yearday || expected.yearday == actual.yearday) && + expected.month == actual.month && + expected.monthday == actual.monthday) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "expected: " << DateToString(expected) << " but was " + << DateToString(actual); + } +} + +::testing::AssertionResult +calendarTest:: IsEqualDate(const isodate &expected, const isodate &actual) { + if (expected.year == actual.year && + expected.week == actual.week && + expected.weekday == actual.weekday) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "expected: " << DateToString(expected) << " but was " + << DateToString(actual); + } +} + + +// --------------------------------------------------------------------- +// test cases +// --------------------------------------------------------------------- +static const u_short real_month_table[2][13] = { + /* -*- table for regular years -*- */ + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, + /* -*- table for leap years -*- */ + { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } +}; + +// days in month, with one month wrap-around at both ends +static const u_short real_month_days[2][14] = { + /* -*- table for regular years -*- */ + { 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 }, + /* -*- table for leap years -*- */ + { 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 } +}; + +// test the day/sec join & split ops, making sure that 32bit +// intermediate results would definitely overflow and the hi DWORD of +// the 'vint64' is definitely needed. +TEST_F(calendarTest, DaySplitMerge) { + for (int32 day = -1000000; day <= 1000000; day += 100) { + for (int32 sec = -100000; sec <= 186400; sec += 10000) { + vint64 merge = ntpcal_dayjoin(day, sec); + ntpcal_split split = ntpcal_daysplit(&merge); + int32 eday = day; + int32 esec = sec; + + while (esec >= 86400) { + eday += 1; + esec -= 86400; + } + while (esec < 0) { + eday -= 1; + esec += 86400; + } + + EXPECT_EQ(eday, split.hi); + EXPECT_EQ(esec, split.lo); + } + } +} + +TEST_F(calendarTest, SplitYearDays1) { + for (int32 eyd = -1; eyd <= 365; eyd++) { + ntpcal_split split = ntpcal_split_yeardays(eyd, 0); + if (split.lo >= 0 && split.hi >= 0) { + EXPECT_GT(12, split.hi); + EXPECT_GT(real_month_days[0][split.hi+1], split.lo); + int32 tyd = real_month_table[0][split.hi] + split.lo; + EXPECT_EQ(eyd, tyd); + } else + EXPECT_TRUE(eyd < 0 || eyd > 364); + } +} + +TEST_F(calendarTest, SplitYearDays2) { + for (int32 eyd = -1; eyd <= 366; eyd++) { + ntpcal_split split = ntpcal_split_yeardays(eyd, 1); + if (split.lo >= 0 && split.hi >= 0) { + EXPECT_GT(12, split.hi); + EXPECT_GT(real_month_days[1][split.hi+1], split.lo); + int32 tyd = real_month_table[1][split.hi] + split.lo; + EXPECT_EQ(eyd, tyd); + } else + EXPECT_TRUE(eyd < 0 || eyd > 365); + } +} + +TEST_F(calendarTest, RataDie1) { + int32 testDate = 1; // 0001-01-01 (proleptic date) + calendar expected = { 1, 1, 1, 1 }; + calendar actual; + + ntpcal_rd_to_date(&actual, testDate); + EXPECT_TRUE(IsEqualDate(expected, actual)); +} + +// check last day of february for first 10000 years +TEST_F(calendarTest, LeapYears1) { + calendar dateIn, dateOut; + + for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) { + dateIn.month = 2; + dateIn.monthday = 28 + leapdays(dateIn.year); + dateIn.yearday = 31 + dateIn.monthday; + + ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn)); + + EXPECT_TRUE(IsEqualDate(dateIn, dateOut)); + } +} + +// check first day of march for first 10000 years +TEST_F(calendarTest, LeapYears2) { + calendar dateIn, dateOut; + + for (dateIn.year = 1; dateIn.year < 10000; ++dateIn.year) { + dateIn.month = 3; + dateIn.monthday = 1; + dateIn.yearday = 60 + leapdays(dateIn.year); + + ntpcal_rd_to_date(&dateOut, ntpcal_date_to_rd(&dateIn)); + EXPECT_TRUE(IsEqualDate(dateIn, dateOut)); + } +} + +// Full roundtrip for 1601-01-01 to 2400-12-31 +// checks sequence of rata die numbers and validates date output +// (since the input is all nominal days of the calendar in that range +// and the result of the inverse calculation must match the input no +// invalid output can occur.) +TEST_F(calendarTest, RoundTripDate) { + calendar truDate, expDate = { 1600, 0, 12, 31 };; + int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate); + int leaps; + + while (expDate.year < 2400) { + expDate.year++; + expDate.month = 0; + expDate.yearday = 0; + leaps = leapdays(expDate.year); + while (expDate.month < 12) { + expDate.month++; + expDate.monthday = 0; + while (expDate.monthday < real_month_days[leaps][expDate.month]) { + expDate.monthday++; + expDate.yearday++; + expRdn++; + + truRdn = ntpcal_date_to_rd(&expDate); + EXPECT_EQ(expRdn, truRdn); + + ntpcal_rd_to_date(&truDate, truRdn); + EXPECT_TRUE(IsEqualDate(expDate, truDate)); + } + } + } +} + +// Roundtrip testing on calyearstart +TEST_F(calendarTest, RoundTripYearStart) { + static const time_t pivot = 0; + u_int32 ntp, expys, truys; + calendar date; + + for (ntp = 0; ntp < 0xFFFFFFFFu - 30000000u; ntp += 30000000u) { + truys = calyearstart(ntp, &pivot); + ntpcal_ntp_to_date(&date, ntp, &pivot); + date.month = date.monthday = 1; + date.hour = date.minute = date.second = 0; + expys = ntpcal_date_to_ntp(&date); + EXPECT_EQ(expys, truys); + } +} + +// Roundtrip testing on calymonthstart +TEST_F(calendarTest, RoundTripMonthStart) { + static const time_t pivot = 0; + u_int32 ntp, expms, trums; + calendar date; + + for (ntp = 0; ntp < 0xFFFFFFFFu - 2000000u; ntp += 2000000u) { + trums = calmonthstart(ntp, &pivot); + ntpcal_ntp_to_date(&date, ntp, &pivot); + date.monthday = 1; + date.hour = date.minute = date.second = 0; + expms = ntpcal_date_to_ntp(&date); + EXPECT_EQ(expms, trums); + } +} + +// Roundtrip testing on calweekstart +TEST_F(calendarTest, RoundTripWeekStart) { + static const time_t pivot = 0; + u_int32 ntp, expws, truws; + isodate date; + + for (ntp = 0; ntp < 0xFFFFFFFFu - 600000u; ntp += 600000u) { + truws = calweekstart(ntp, &pivot); + isocal_ntp_to_date(&date, ntp, &pivot); + date.hour = date.minute = date.second = 0; + date.weekday = 1; + expws = isocal_date_to_ntp(&date); + EXPECT_EQ(expws, truws); + } +} + +// Roundtrip testing on caldaystart +TEST_F(calendarTest, RoundTripDayStart) { + static const time_t pivot = 0; + u_int32 ntp, expds, truds; + calendar date; + + for (ntp = 0; ntp < 0xFFFFFFFFu - 80000u; ntp += 80000u) { + truds = caldaystart(ntp, &pivot); + ntpcal_ntp_to_date(&date, ntp, &pivot); + date.hour = date.minute = date.second = 0; + expds = ntpcal_date_to_ntp(&date); + EXPECT_EQ(expds, truds); + } +} + diff --git a/tests/libntp/caljulian.cpp b/tests/libntp/caljulian.cpp new file mode 100644 index 0000000..d9cc952 --- /dev/null +++ b/tests/libntp/caljulian.cpp @@ -0,0 +1,94 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_calendar.h" +} + +#include <string> +#include <sstream> + +class caljulianTest : public libntptest { +protected: + virtual void SetUp(); + virtual void TearDown(); + + std::string CalendarToString(const calendar &cal) { + std::ostringstream ss; + ss << cal.year << "-" << (u_int)cal.month << "-" << (u_int)cal.monthday + << " (" << cal.yearday << ") " << (u_int)cal.hour << ":" + << (u_int)cal.minute << ":" << (u_int)cal.second; + return ss.str(); + } + + ::testing::AssertionResult IsEqual(const calendar &expected, const calendar &actual) { + if (expected.year == actual.year && + (expected.yearday == actual.yearday || + (expected.month == actual.month && + expected.monthday == actual.monthday)) && + expected.hour == actual.hour && + expected.minute == actual.minute && + expected.second == actual.second) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "expected: " << CalendarToString(expected) << " but was " + << CalendarToString(actual); + } + } +}; + +void caljulianTest::SetUp() +{ + ntpcal_set_timefunc(timefunc); + settime(1970, 1, 1, 0, 0, 0); +} + +void caljulianTest::TearDown() +{ + ntpcal_set_timefunc(NULL); +} + + +TEST_F(caljulianTest, RegularTime) { + u_long testDate = 3485080800UL; // 2010-06-09 14:00:00 + calendar expected = {2010,160,6,9,14,0,0}; + + calendar actual; + + caljulian(testDate, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(caljulianTest, LeapYear) { + u_long input = 3549902400UL; // 2012-06-28 20:00:00Z + calendar expected = {2012, 179, 6, 28, 20, 0, 0}; + + calendar actual; + + caljulian(input, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(caljulianTest, uLongBoundary) { + u_long time = 4294967295UL; // 2036-02-07 6:28:15 + calendar expected = {2036,0,2,7,6,28,15}; + + calendar actual; + + caljulian(time, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(caljulianTest, uLongWrapped) { + u_long time = 0; + calendar expected = {2036,0,2,7,6,28,16}; + + calendar actual; + + caljulian(time, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); +} diff --git a/tests/libntp/caltontp.cpp b/tests/libntp/caltontp.cpp new file mode 100644 index 0000000..b9732db --- /dev/null +++ b/tests/libntp/caltontp.cpp @@ -0,0 +1,49 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_calendar.h" +} + +class caltontpTest : public libntptest { +}; + +TEST_F(caltontpTest, DateGivenMonthDay) { + // 2010-06-24 12:50:00 + calendar input = {2010, 0, 6, 24, 12, 50, 0}; + + u_long expected = 3486372600UL; // This is the timestamp above. + + EXPECT_EQ(expected, caltontp(&input)); +} + +TEST_F(caltontpTest, DateGivenYearDay) { + // 2010-06-24 12:50:00 + // This is the 175th day of 2010. + calendar input = {2010, 175, 0, 0, 12, 50, 0}; + + u_long expected = 3486372600UL; // This is the timestamp above. + + EXPECT_EQ(expected, caltontp(&input)); +} + +TEST_F(caltontpTest, DateLeapYear) { + // 2012-06-24 12:00:00 + // This is the 176th day of 2012 (since 2012 is a leap year). + calendar inputYd = {2012, 176, 0, 0, 12, 00, 00}; + calendar inputMd = {2012, 0, 6, 24, 12, 00, 00}; + + u_long expected = 3549528000UL; + + EXPECT_EQ(expected, caltontp(&inputYd)); + EXPECT_EQ(expected, caltontp(&inputMd)); +} + +TEST_F(caltontpTest, WraparoundDateIn2036) { + // 2036-02-07 06:28:16 + // This is (one) wrapping boundary where we go from ULONG_MAX to 0. + calendar input = {2036, 0, 2, 7, 6, 28, 16}; + + u_long expected = 0UL; + + EXPECT_EQ(expected, caltontp(&input)); +} diff --git a/tests/libntp/calyearstart.cpp b/tests/libntp/calyearstart.cpp new file mode 100644 index 0000000..774edf3 --- /dev/null +++ b/tests/libntp/calyearstart.cpp @@ -0,0 +1,43 @@ +#include "libntptest.h" + +class calyearstartTest : public libntptest { +protected: + virtual void SetUp(); + virtual void TearDown(); +}; + +void calyearstartTest::SetUp() +{ + ntpcal_set_timefunc(timefunc); + settime(1970, 1, 1, 0, 0, 0); +} + +void calyearstartTest::TearDown() +{ + ntpcal_set_timefunc(NULL); +} + + +TEST_F(calyearstartTest, NoWrapInDateRange) { + const u_int32 input = 3486372600UL; // 2010-06-24 12:50:00. + const u_int32 expected = 3471292800UL; // 2010-01-01 00:00:00 + + EXPECT_EQ(expected, calyearstart(input, &nowtime)); + EXPECT_EQ(expected, calyearstart(input, NULL)); +} + +TEST_F(calyearstartTest, NoWrapInDateRangeLeapYear) { + const u_int32 input = 3549528000UL; // 2012-06-24 12:00:00 + const u_int32 expected = 3534364800UL; // 2012-01-01 00:00:00 + + EXPECT_EQ(expected, calyearstart(input, &nowtime)); + EXPECT_EQ(expected, calyearstart(input, NULL)); +} + +TEST_F(calyearstartTest, WrapInDateRange) { + const u_int32 input = 19904UL; // 2036-02-07 12:00:00 + const u_int32 expected = 4291747200UL; // 2036-01-01 00:00:00 + + EXPECT_EQ(expected, calyearstart(input, &nowtime)); + EXPECT_EQ(expected, calyearstart(input, NULL)); +} diff --git a/tests/libntp/clocktime.cpp b/tests/libntp/clocktime.cpp new file mode 100644 index 0000000..a24df1a --- /dev/null +++ b/tests/libntp/clocktime.cpp @@ -0,0 +1,187 @@ +#include "libntptest.h" + +// --------------------------------------------------------------------- +// test fixture +// +// The clocktimeTest uses the NTP calendar feature to use a mockup +// function for getting the current system time, so the tests are not +// dependent on the actual system time. + +class clocktimeTest : public libntptest { + virtual void SetUp(); + virtual void TearDown(); +}; + +void clocktimeTest::SetUp() +{ + ntpcal_set_timefunc(timefunc); + settime(2000, 1, 1, 0, 0, 0); +} + +void clocktimeTest::TearDown() +{ + ntpcal_set_timefunc(NULL); +} + +// --------------------------------------------------------------------- +// test cases + +TEST_F(clocktimeTest, CurrentYear) { + // Timestamp: 2010-06-24 12:50:00Z + const u_int32 timestamp = 3486372600UL; + const u_int32 expected = timestamp; // exactly the same. + + const int yday=175, hour=12, minute=50, second=0, tzoff=0; + + u_long yearstart=0; + u_int32 actual; + + ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + EXPECT_EQ(expected, actual); +} + +TEST_F(clocktimeTest, CurrentYearFuzz) { + /* + * Timestamp (rec_ui) is: 2010-06-24 12:50:00 + * Time sent into function is 12:00:00. + * + * Since the fuzz is rather small, we should get a NTP + * timestamp for the 12:00:00 time. + */ + + const u_int32 timestamp = 3486372600UL; // 2010-06-24 12:50:00Z + const u_int32 expected = 3486369600UL; // 2010-06-24 12:00:00Z + + const int yday=175, hour=12, minute=0, second=0, tzoff=0; + + u_long yearstart=0; + u_int32 actual; + + ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + EXPECT_EQ(expected, actual); +} + +TEST_F(clocktimeTest, TimeZoneOffset) { + /* + * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800 + * (which is 2010-06-24 04:00:00Z) + * + * Time sent into function is 04:00:00 +0800 + */ + const u_int32 timestamp = 3486369600UL; + const u_int32 expected = timestamp; + + const int yday=175, hour=4, minute=0, second=0, tzoff=8; + + u_long yearstart=0; + u_int32 actual; + + ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + EXPECT_EQ(expected, actual); +} + +TEST_F(clocktimeTest, WrongYearStart) { + /* + * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z + * Time sent into function is 11:00:00. + * Yearstart sent into function is the yearstart of 2009! + */ + const u_int32 timestamp = 3471418800UL; + const u_int32 expected = timestamp; + + const int yday=2, hour=11, minute=0, second=0, tzoff=0; + + u_long yearstart = 302024100UL; // Yearstart of 2009. + u_int32 actual; + + ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + EXPECT_EQ(expected, actual); +} + +TEST_F(clocktimeTest, PreviousYear) { + /* + * Timestamp is: 2010-01-01 01:00:00Z + * Time sent into function is 23:00:00 + * (which is meant to be 2009-12-31 23:00:00Z) + */ + const u_int32 timestamp = 3471296400UL; + const u_int32 expected = 3471289200UL; + + const int yday=365, hour=23, minute=0, second=0, tzoff=0; + + u_long yearstart = 0; + u_int32 actual; + + ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + EXPECT_EQ(expected, actual); +} + +TEST_F(clocktimeTest, NextYear) { + /* + * Timestamp is: 2009-12-31 23:00:00Z + * Time sent into function is 01:00:00 + * (which is meant to be 2010-01-01 01:00:00Z) + */ + const u_int32 timestamp = 3471289200UL; + const u_int32 expected = 3471296400UL; + + const int yday=1, hour=1, minute=0, second=0, tzoff=0; + u_long yearstart = 0; + u_int32 actual; + + ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); + EXPECT_EQ(expected, actual); +} + +TEST_F(clocktimeTest, NoReasonableConversion) { + /* Timestamp is: 2010-01-02 11:00:00Z */ + const u_int32 timestamp = 3471418800UL; + + const int yday=100, hour=12, minute=0, second=0, tzoff=0; + u_long yearstart = 0; + u_int32 actual; + + ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp, + &yearstart, &actual)); +} + +TEST_F(clocktimeTest, AlwaysInLimit) { + /* Timestamp is: 2010-01-02 11:00:00Z */ + const u_int32 timestamp = 3471418800UL; + const u_short prime_incs[] = { 127, 151, 163, 179 }; + int cyc; + int yday; + u_char whichprime; + u_short ydayinc; + int hour; + int minute; + int second; + u_long yearstart; + u_int32 actual; + u_int32 diff; + + yearstart = 0; + for (cyc = 0; cyc < 5; cyc++) { + settime(1900 + cyc * 65, 1, 1, 0, 0, 0); + for (yday = -26000; yday < 26000; yday += ydayinc) { + whichprime = abs(yday) % COUNTOF(prime_incs); + ydayinc = prime_incs[whichprime]; + for (hour = -204; hour < 204; hour += 2) { + for (minute = -60; minute < 60; minute++) { + clocktime(yday, hour, minute, 30, 0, + timestamp, &yearstart, &actual); + diff = actual - timestamp; + if (diff >= 0x80000000UL) + diff = ~diff + 1; + ASSERT_LE(diff, (183u * SECSPERDAY)); + } + } + } + } +} diff --git a/tests/libntp/decodenetnum.cpp b/tests/libntp/decodenetnum.cpp new file mode 100644 index 0000000..c6766de --- /dev/null +++ b/tests/libntp/decodenetnum.cpp @@ -0,0 +1,93 @@ +#include "sockaddrtest.h" + +class decodenetnumTest : public sockaddrtest { +}; + +TEST_F(decodenetnumTest, IPv4AddressOnly) { + const char *str = "192.0.2.1"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); + SET_PORT(&expected, NTP_PORT); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IPv4AddressWithPort) { + const char *str = "192.0.2.2:2000"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2"); + SET_PORT(&expected, 2000); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IPv6AddressOnly) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa6.sin6_family = AF_INET6; + expected.sa6.sin6_addr = address; + SET_PORT(&expected, NTP_PORT); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IPv6AddressWithPort) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa6.sin6_family = AF_INET6; + expected.sa6.sin6_addr = address; + SET_PORT(&expected, 3000); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IllegalAddress) { + const char *str = "192.0.2.270:2000"; + sockaddr_u actual; + + ASSERT_FALSE(decodenetnum(str, &actual)); +} + +TEST_F(decodenetnumTest, IllegalCharInPort) { + /* An illegal port does not make the decodenetnum fail, but instead + * makes it use the standard port. + */ + const char *str = "192.0.2.1:a700"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); + SET_PORT(&expected, NTP_PORT); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} diff --git a/tests/libntp/hextoint.cpp b/tests/libntp/hextoint.cpp new file mode 100644 index 0000000..54c9a67 --- /dev/null +++ b/tests/libntp/hextoint.cpp @@ -0,0 +1,42 @@ +#include "libntptest.h" + +class hextointTest : public libntptest { +}; + +TEST_F(hextointTest, SingleDigit) { + const char *str = "a"; // 10 decimal + u_long actual; + + ASSERT_TRUE(hextoint(str, &actual)); + EXPECT_EQ(10, actual); +} + +TEST_F(hextointTest, MultipleDigits) { + const char *str = "8F3"; // 2291 decimal + u_long actual; + + ASSERT_TRUE(hextoint(str, &actual)); + EXPECT_EQ(2291, actual); +} + +TEST_F(hextointTest, MaxUnsigned) { + const char *str = "ffffffff"; // 4294967295 decimal + u_long actual; + + ASSERT_TRUE(hextoint(str, &actual)); + EXPECT_EQ(4294967295UL, actual); +} + +TEST_F(hextointTest, Overflow) { + const char *str = "100000000"; // Overflow by 1 + u_long actual; + + ASSERT_FALSE(hextoint(str, &actual)); +} + +TEST_F(hextointTest, IllegalChar) { + const char *str = "5gb"; // Illegal character g + u_long actual; + + ASSERT_FALSE(hextoint(str, &actual)); +} diff --git a/tests/libntp/hextolfp.cpp b/tests/libntp/hextolfp.cpp new file mode 100644 index 0000000..2e9f072 --- /dev/null +++ b/tests/libntp/hextolfp.cpp @@ -0,0 +1,58 @@ +#include "lfptest.h" + +class hextolfpTest : public lfptest { +}; + +TEST_F(hextolfpTest, PositiveInteger) { + const char *str = "00001000.00000000"; + l_fp actual; + + l_fp expected = {4096, 0}; // 16^3, no fraction part. + + ASSERT_TRUE(hextolfp(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(hextolfpTest, NegativeInteger) { + const char *str = "ffffffff.00000000"; // -1 decimal + l_fp actual; + + l_fp expected = {-1, 0}; + + ASSERT_TRUE(hextolfp(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(hextolfpTest, PositiveFraction) { + const char *str = "00002000.80000000"; // 8196.5 decimal + l_fp actual; + + l_fp expected = {8192, HALF}; + + ASSERT_TRUE(hextolfp(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(hextolfpTest, NegativeFraction) { + const char *str = "ffffffff.40000000"; // -1 + 0.25 decimal + l_fp actual; + + l_fp expected = {-1, QUARTER}; //-1 + 0.25 + + ASSERT_TRUE(hextolfp(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(hextolfpTest, IllegalNumberOfInteger) { + const char *str = "1000000.00000000"; // Missing one digit in integral part. + l_fp actual; + + ASSERT_FALSE(hextolfp(str, &actual)); +} + +TEST_F(hextolfpTest, IllegalChar) { + const char *str = "10000000.0000h000"; // Illegal character h. + l_fp actual; + + ASSERT_FALSE(hextolfp(str, &actual)); +} diff --git a/tests/libntp/humandate.cpp b/tests/libntp/humandate.cpp new file mode 100644 index 0000000..742aa28 --- /dev/null +++ b/tests/libntp/humandate.cpp @@ -0,0 +1,41 @@ +#include "libntptest.h" + +#include <sstream> +#include <string> + +class humandateTest : public libntptest { +}; + +TEST_F(humandateTest, RegularTime) { + time_t sample = 1276601278; + std::ostringstream expected; + + tm* time; + time = localtime(&sample); + ASSERT_TRUE(time != NULL); + + expected << std::setfill('0') + << std::setw(2) << time->tm_hour << ":" + << std::setw(2) << time->tm_min << ":" + << std::setw(2) << time->tm_sec; + + EXPECT_STREQ(expected.str().c_str(), humantime(sample)); +} + +TEST_F(humandateTest, CurrentTime) { + time_t sample; + std::ostringstream expected; + + time(&sample); + + tm* time; + time = localtime(&sample); + ASSERT_TRUE(time != NULL); + + expected << std::setfill('0') + << std::setw(2) << time->tm_hour << ":" + << std::setw(2) << time->tm_min << ":" + << std::setw(2) << time->tm_sec; + + EXPECT_STREQ(expected.str().c_str(), humantime(sample)); +} diff --git a/tests/libntp/lfpfunc.cpp b/tests/libntp/lfpfunc.cpp new file mode 100644 index 0000000..275918c --- /dev/null +++ b/tests/libntp/lfpfunc.cpp @@ -0,0 +1,547 @@ +#include "libntptest.h" +#include "timestructs.h" + +extern "C" { +#include "ntp_fp.h" +} + +#include <float.h> +#include <math.h> + +#include <string> +#include <sstream> + +class lfpTest : public libntptest +{ + // nothing new right now +}; + +struct lfp_hl { + uint32_t h, l; +}; + +//---------------------------------------------------------------------- +// OO-wrapper for 'l_fp' +//---------------------------------------------------------------------- + +class LFP +{ +public: + ~LFP(); + LFP(); + LFP(const LFP& rhs); + LFP(int32 i, u_int32 f); + + LFP operator+ (const LFP &rhs) const; + LFP& operator+=(const LFP &rhs); + + LFP operator- (const LFP &rhs) const; + LFP& operator-=(const LFP &rhs); + + LFP& operator=(const LFP &rhs); + LFP operator-() const; + + bool operator==(const LFP &rhs) const; + + LFP neg() const; + LFP abs() const; + int signum() const; + + bool l_isgt (const LFP &rhs) const + { return L_ISGT(&_v, &rhs._v); } + bool l_isgtu(const LFP &rhs) const + { return L_ISGTU(&_v, &rhs._v); } + bool l_ishis(const LFP &rhs) const + { return L_ISHIS(&_v, &rhs._v); } + bool l_isgeq(const LFP &rhs) const + { return L_ISGEQ(&_v, &rhs._v); } + bool l_isequ(const LFP &rhs) const + { return L_ISEQU(&_v, &rhs._v); } + + int ucmp(const LFP & rhs) const; + int scmp(const LFP & rhs) const; + + std::string toString() const; + std::ostream& toStream(std::ostream &oo) const; + + operator double() const; + explicit LFP(double); + +protected: + LFP(const l_fp &rhs); + + static int cmp_work(u_int32 a[3], u_int32 b[3]); + + l_fp _v; +}; + +static std::ostream& operator<<(std::ostream &oo, const LFP& rhs) +{ + return rhs.toStream(oo); +} + +//---------------------------------------------------------------------- +// reference comparision +// This is implementad as a full signed MP-subtract in 3 limbs, where +// the operands are zero or sign extended before the subtraction is +// executed. +//---------------------------------------------------------------------- +int LFP::scmp(const LFP & rhs) const +{ + u_int32 a[3], b[3]; + const l_fp &op1(_v), &op2(rhs._v); + + a[0] = op1.l_uf; a[1] = op1.l_ui; a[2] = 0; + b[0] = op2.l_uf; b[1] = op2.l_ui; b[2] = 0; + + a[2] -= (op1.l_i < 0); + b[2] -= (op2.l_i < 0); + + return cmp_work(a,b); +} + +int LFP::ucmp(const LFP & rhs) const +{ + u_int32 a[3], b[3]; + const l_fp &op1(_v), &op2(rhs._v); + + a[0] = op1.l_uf; a[1] = op1.l_ui; a[2] = 0; + b[0] = op2.l_uf; b[1] = op2.l_ui; b[2] = 0; + + return cmp_work(a,b); +} + +int LFP::cmp_work(u_int32 a[3], u_int32 b[3]) +{ + u_int32 cy, idx, tmp; + for (cy = idx = 0; idx < 3; ++idx) { + tmp = a[idx]; cy = (a[idx] -= cy ) > tmp; + tmp = a[idx]; cy |= (a[idx] -= b[idx]) > tmp; + } + if (a[2]) + return -1; + return a[0] || a[1]; +} + +//---------------------------------------------------------------------- +// imlementation of the LFP stuff +// This should be easy enough... +//---------------------------------------------------------------------- + +LFP::~LFP() +{ + // NOP +} + +LFP::LFP() +{ + _v.l_ui = 0; + _v.l_uf = 0; +} + +LFP::LFP(int32 i, u_int32 f) +{ + _v.l_i = i; + _v.l_uf = f; +} + +LFP::LFP(const LFP &rhs) +{ + _v = rhs._v; +} + +LFP::LFP(const l_fp & rhs) +{ + _v = rhs; +} + +LFP& LFP::operator=(const LFP & rhs) +{ + _v = rhs._v; + return *this; +} + +LFP& LFP::operator+=(const LFP & rhs) +{ + L_ADD(&_v, &rhs._v); + return *this; +} + +LFP& LFP::operator-=(const LFP & rhs) +{ + L_SUB(&_v, &rhs._v); + return *this; +} + +LFP LFP::operator+(const LFP &rhs) const +{ + LFP tmp(*this); + return tmp += rhs; +} + +LFP LFP::operator-(const LFP &rhs) const +{ + LFP tmp(*this); + return tmp -= rhs; +} + +LFP LFP::operator-() const +{ + LFP tmp(*this); + L_NEG(&tmp._v); + return tmp; +} + +LFP +LFP::neg() const +{ + LFP tmp(*this); + L_NEG(&tmp._v); + return tmp; +} + +LFP +LFP::abs() const +{ + LFP tmp(*this); + if (L_ISNEG(&tmp._v)) + L_NEG(&tmp._v); + return tmp; +} + +int +LFP::signum() const +{ + if (_v.l_ui & 0x80000000u) + return -1; + return (_v.l_ui || _v.l_uf); +} + +std::string +LFP::toString() const +{ + std::ostringstream oss; + toStream(oss); + return oss.str(); +} + +std::ostream& +LFP::toStream(std::ostream &os) const +{ + return os + << mfptoa(_v.l_ui, _v.l_uf, 9) + << " [$" << std::setw(8) << std::setfill('0') << std::hex << _v.l_ui + << ':' << std::setw(8) << std::setfill('0') << std::hex << _v.l_uf + << ']'; +} + +bool LFP::operator==(const LFP &rhs) const +{ + return L_ISEQU(&_v, &rhs._v); +} + + +LFP::operator double() const +{ + double res; + LFPTOD(&_v, res); + return res; +} + +LFP::LFP(double rhs) +{ + DTOLFP(rhs, &_v); +} + + +//---------------------------------------------------------------------- +// testing the relational macros works better with proper predicate +// formatting functions; it slows down the tests a bit, but makes for +// readable failure messages. +//---------------------------------------------------------------------- + +testing::AssertionResult isgt_p( + const LFP &op1, const LFP &op2) +{ + if (op1.l_isgt(op2)) + return testing::AssertionSuccess() + << "L_ISGT(" << op1 << "," << op2 << ") is true"; + else + return testing::AssertionFailure() + << "L_ISGT(" << op1 << "," << op2 << ") is false"; +} + +testing::AssertionResult isgeq_p( + const LFP &op1, const LFP &op2) +{ + if (op1.l_isgeq(op2)) + return testing::AssertionSuccess() + << "L_ISGEQ(" << op1 << "," << op2 << ") is true"; + else + return testing::AssertionFailure() + << "L_ISGEQ(" << op1 << "," << op2 << ") is false"; +} + +testing::AssertionResult isgtu_p( + const LFP &op1, const LFP &op2) +{ + if (op1.l_isgtu(op2)) + return testing::AssertionSuccess() + << "L_ISGTU(" << op1 << "," << op2 << ") is true"; + else + return testing::AssertionFailure() + << "L_ISGTU(" << op1 << "," << op2 << ") is false"; +} + +testing::AssertionResult ishis_p( + const LFP &op1, const LFP &op2) +{ + if (op1.l_ishis(op2)) + return testing::AssertionSuccess() + << "L_ISHIS(" << op1 << "," << op2 << ") is true"; + else + return testing::AssertionFailure() + << "L_ISHIS(" << op1 << "," << op2 << ") is false"; +} + +testing::AssertionResult isequ_p( + const LFP &op1, const LFP &op2) +{ + if (op1.l_isequ(op2)) + return testing::AssertionSuccess() + << "L_ISEQU(" << op1 << "," << op2 << ") is true"; + else + return testing::AssertionFailure() + << "L_ISEQU(" << op1 << "," << op2 << ") is false"; +} + +//---------------------------------------------------------------------- +// test data table for add/sub and compare +//---------------------------------------------------------------------- + +static const lfp_hl addsub_tab[][3] = { + // trivial idendity: + {{0 ,0 }, { 0,0 }, { 0,0}}, + // with carry from fraction and sign change: + {{-1,0x80000000}, { 0,0x80000000}, { 0,0}}, + // without carry from fraction + {{ 1,0x40000000}, { 1,0x40000000}, { 2,0x80000000}}, + // with carry from fraction: + {{ 1,0xC0000000}, { 1,0xC0000000}, { 3,0x80000000}}, + // with carry from fraction and sign change: + {{0x7FFFFFFF, 0x7FFFFFFF}, {0x7FFFFFFF,0x7FFFFFFF}, {0xFFFFFFFE,0xFFFFFFFE}}, + // two tests w/o carry (used for l_fp<-->double): + {{0x55555555,0xAAAAAAAA}, {0x11111111,0x11111111}, {0x66666666,0xBBBBBBBB}}, + {{0x55555555,0x55555555}, {0x11111111,0x11111111}, {0x66666666,0x66666666}}, + // wide-range test, triggers compare trouble + {{0x80000000,0x00000001}, {0xFFFFFFFF,0xFFFFFFFE}, {0x7FFFFFFF,0xFFFFFFFF}} +}; +static const size_t addsub_cnt(sizeof(addsub_tab)/sizeof(addsub_tab[0])); +static const size_t addsub_tot(sizeof(addsub_tab)/sizeof(addsub_tab[0][0])); + + +//---------------------------------------------------------------------- +// epsilon estimation for the precision of a conversion double --> l_fp +// +// The error estimation limit is as follows: +// * The 'l_fp' fixed point fraction has 32 bits precision, so we allow +// for the LSB to toggle by clamping the epsilon to be at least 2^(-31) +// +// * The double mantissa has a precsion 54 bits, so the other minimum is +// dval * (2^(-53)) +// +// The maximum of those two boundaries is used for the check. +// +// Note: once there are more than 54 bits between the highest and lowest +// '1'-bit of the l_fp value, the roundtrip *will* create truncation +// errors. This is an inherent property caused by the 54-bit mantissa of +// the 'double' type. +double eps(double d) +{ + return std::max<double>(ldexp(1.0, -31), ldexp(fabs(d), -53)); +} + +//---------------------------------------------------------------------- +// test addition +//---------------------------------------------------------------------- +TEST_F(lfpTest, AdditionLR) { + for (size_t idx=0; idx < addsub_cnt; ++idx) { + LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + LFP op2(addsub_tab[idx][1].h, addsub_tab[idx][1].l); + LFP exp(addsub_tab[idx][2].h, addsub_tab[idx][2].l); + LFP res(op1 + op2); + + ASSERT_EQ(exp, res); + } +} + +TEST_F(lfpTest, AdditionRL) { + for (size_t idx=0; idx < addsub_cnt; ++idx) { + LFP op2(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + LFP op1(addsub_tab[idx][1].h, addsub_tab[idx][1].l); + LFP exp(addsub_tab[idx][2].h, addsub_tab[idx][2].l); + LFP res(op1 + op2); + + ASSERT_EQ(exp, res); + } +} + +//---------------------------------------------------------------------- +// test subtraction +//---------------------------------------------------------------------- +TEST_F(lfpTest, SubtractionLR) { + for (size_t idx=0; idx < addsub_cnt; ++idx) { + LFP op2(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + LFP exp(addsub_tab[idx][1].h, addsub_tab[idx][1].l); + LFP op1(addsub_tab[idx][2].h, addsub_tab[idx][2].l); + LFP res(op1 - op2); + + ASSERT_EQ(exp, res); + } +} + +TEST_F(lfpTest, SubtractionRL) { + for (size_t idx=0; idx < addsub_cnt; ++idx) { + LFP exp(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + LFP op2(addsub_tab[idx][1].h, addsub_tab[idx][1].l); + LFP op1(addsub_tab[idx][2].h, addsub_tab[idx][2].l); + LFP res(op1 - op2); + + ASSERT_EQ(exp, res); + } +} + +//---------------------------------------------------------------------- +// test negation +//---------------------------------------------------------------------- +TEST_F(lfpTest, Negation) { + for (size_t idx=0; idx < addsub_cnt; ++idx) { + LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + LFP op2(-op1); + LFP sum(op1 + op2); + + ASSERT_EQ(LFP(0,0), sum); + } +} + +//---------------------------------------------------------------------- +// test absolute value +//---------------------------------------------------------------------- +TEST_F(lfpTest, Absolute) { + for (size_t idx=0; idx < addsub_cnt; ++idx) { + LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + LFP op2(op1.abs()); + + ASSERT_TRUE(op2.signum() >= 0); + + if (op1.signum() >= 0) + op1 -= op2; + else + op1 += op2; + ASSERT_EQ(LFP(0,0), op1); + } + + // There is one special case we have to check: the minimum + // value cannot be negated, or, to be more precise, the + // negation reproduces the original pattern. + LFP minVal(0x80000000, 0x00000000); + LFP minAbs(minVal.abs()); + ASSERT_EQ(-1, minVal.signum()); + ASSERT_EQ(minVal, minAbs); +} + +//---------------------------------------------------------------------- +// fp -> double -> fp rountrip test +//---------------------------------------------------------------------- +TEST_F(lfpTest, FDF_RoundTrip) { + // since a l_fp has 64 bits in it's mantissa and a double has + // only 54 bits available (including the hidden '1') we have to + // make a few concessions on the roundtrip precision. The 'eps()' + // function makes an educated guess about the avilable precision + // and checks the difference in the two 'l_fp' values against + // that limit. + for (size_t idx=0; idx < addsub_cnt; ++idx) { + LFP op1(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + double op2(op1); + LFP op3(op2); + // for manual checks only: + // std::cout << std::setprecision(16) << op2 << std::endl; + ASSERT_LE(fabs(op1-op3), eps(op2)); + } +} + +//---------------------------------------------------------------------- +// test the compare stuff +// +// This uses the local compare and checks if the operations using the +// macros in 'ntp_fp.h' produce mathing results. +// ---------------------------------------------------------------------- +TEST_F(lfpTest, SignedRelOps) { + const lfp_hl * tv(&addsub_tab[0][0]); + for (size_t lc=addsub_tot-1; lc; --lc,++tv) { + LFP op1(tv[0].h,tv[0].l); + LFP op2(tv[1].h,tv[1].l); + int cmp(op1.scmp(op2)); + + switch (cmp) { + case -1: + std::swap(op1, op2); + case 1: + EXPECT_TRUE (isgt_p(op1,op2)); + EXPECT_FALSE(isgt_p(op2,op1)); + + EXPECT_TRUE (isgeq_p(op1,op2)); + EXPECT_FALSE(isgeq_p(op2,op1)); + + EXPECT_FALSE(isequ_p(op1,op2)); + EXPECT_FALSE(isequ_p(op2,op1)); + break; + case 0: + EXPECT_FALSE(isgt_p(op1,op2)); + EXPECT_FALSE(isgt_p(op2,op1)); + + EXPECT_TRUE (isgeq_p(op1,op2)); + EXPECT_TRUE (isgeq_p(op2,op1)); + + EXPECT_TRUE (isequ_p(op1,op2)); + EXPECT_TRUE (isequ_p(op2,op1)); + break; + default: + FAIL() << "unexpected SCMP result: " << cmp; + } + } +} + +TEST_F(lfpTest, UnsignedRelOps) { + const lfp_hl * tv(&addsub_tab[0][0]); + for (size_t lc=addsub_tot-1; lc; --lc,++tv) { + LFP op1(tv[0].h,tv[0].l); + LFP op2(tv[1].h,tv[1].l); + int cmp(op1.ucmp(op2)); + + switch (cmp) { + case -1: + std::swap(op1, op2); + case 1: + EXPECT_TRUE (isgtu_p(op1,op2)); + EXPECT_FALSE(isgtu_p(op2,op1)); + + EXPECT_TRUE (ishis_p(op1,op2)); + EXPECT_FALSE(ishis_p(op2,op1)); + break; + case 0: + EXPECT_FALSE(isgtu_p(op1,op2)); + EXPECT_FALSE(isgtu_p(op2,op1)); + + EXPECT_TRUE (ishis_p(op1,op2)); + EXPECT_TRUE (ishis_p(op2,op1)); + break; + default: + FAIL() << "unexpected UCMP result: " << cmp; + } + } +} + +//---------------------------------------------------------------------- +// that's all folks... but feel free to add things! +//---------------------------------------------------------------------- diff --git a/tests/libntp/lfptest.h b/tests/libntp/lfptest.h new file mode 100644 index 0000000..034eb41 --- /dev/null +++ b/tests/libntp/lfptest.h @@ -0,0 +1,31 @@ +#ifndef NTP_TESTS_LFPTEST_H +#define NTP_TESTS_LFPTEST_H + +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +}; + +class lfptest : public libntptest { +protected: + ::testing::AssertionResult IsEqual(const l_fp &expected, const l_fp &actual) { + if (L_ISEQU(&expected, &actual)) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << " expected: " << lfptoa(&expected, FRACTION_PREC) + << " (" << expected.l_ui << "." << expected.l_uf << ")" + << " but was: " << lfptoa(&actual, FRACTION_PREC) + << " (" << actual.l_ui << "." << actual.l_uf << ")"; + } + } + + static const int32 HALF = -2147483647L - 1L; + static const int32 HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005 + static const int32 HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005 + static const int32 QUARTER = 1073741824L; + static const int32 QUARTER_PROMILLE_APPRX = 1073742L; +}; + +#endif /* NTP_TESTS_LFPTEST_H */ diff --git a/tests/libntp/lfptostr.cpp b/tests/libntp/lfptostr.cpp new file mode 100644 index 0000000..aa5f511 --- /dev/null +++ b/tests/libntp/lfptostr.cpp @@ -0,0 +1,103 @@ +/* + * This file contains test for both mfptoa and mfptoms (which uses dolfptoa), + * since all these functions are very similar. It also tests ulfptoa, which is + * a macro. + */ + +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +}; + +class lfptostrTest : public libntptest { +protected: + static const int LFP_MAX_PRECISION = 10; + static const int LFP_MAX_PRECISION_MS = 7; + + static const int ONE_FOURTH = 1073741824; // (1 << 30) + static const int HALF = (1 << 31); + static const int THREE_FOURTH = -ONE_FOURTH; + static const int HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005 + static const int HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005 +}; + +TEST_F(lfptostrTest, PositiveInteger) { + l_fp test = {200, 0}; // exact 200.0000000000 + + EXPECT_STREQ("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION)); + EXPECT_STREQ("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS)); +} + +TEST_F(lfptostrTest, NegativeInteger) { + l_fp test = {-100, 0}; // -100 + + EXPECT_STREQ("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION)); + EXPECT_STREQ("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); +} + +TEST_F(lfptostrTest, PositiveIntegerWithFraction) { + l_fp test = {200, ONE_FOURTH}; // 200.25 + + EXPECT_STREQ("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION)); + EXPECT_STREQ("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); +} + +TEST_F(lfptostrTest, NegativeIntegerWithFraction) { + l_fp test = {-100, ONE_FOURTH}; // -99.75 + + EXPECT_STREQ("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION)); + EXPECT_STREQ("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS)); +} + +TEST_F(lfptostrTest, RoundingDownToInteger) { + l_fp test = {10, ONE_FOURTH}; // 10.25 + + EXPECT_STREQ("10", lfptoa(&test, 0)); + EXPECT_STREQ("10250", lfptoms(&test, 0)); +} + +TEST_F(lfptostrTest, RoundingMiddleToInteger) { + l_fp test = {10, HALF}; // 10.5 + + EXPECT_STREQ("11", lfptoa(&test, 0)); + EXPECT_STREQ("10500", lfptoms(&test, 0)); +} + +TEST_F(lfptostrTest, RoundingUpToInteger) { + l_fp test = {5, THREE_FOURTH}; // 5.75 + + EXPECT_STREQ("6", lfptoa(&test, 0)); + EXPECT_STREQ("5750", lfptoms(&test, 0)); +} + +TEST_F(lfptostrTest, SingleDecimal) { + l_fp test = {8, ONE_FOURTH}; // 8.25 + + EXPECT_STREQ("8.3", lfptoa(&test, 1)); + EXPECT_STREQ("8250.0", lfptoms(&test, 1)); +} + +TEST_F(lfptostrTest, MillisecondsRoundingUp) { + l_fp test = {1, HALF_PROMILLE_UP}; //slightly more than 1.0005 + + EXPECT_STREQ("1.0", lfptoa(&test, 1)); + + EXPECT_STREQ("1000.5", lfptoms(&test, 1)); + EXPECT_STREQ("1001", lfptoms(&test, 0)); +} + +TEST_F(lfptostrTest, MillisecondsRoundingDown) { + l_fp test = {1, HALF_PROMILLE_DOWN}; // slightly less than 1.0005 + + EXPECT_STREQ("1.0", lfptoa(&test, 1)); + + EXPECT_STREQ("1000.5", lfptoms(&test, 1)); + EXPECT_STREQ("1000", lfptoms(&test, 0)); +} + +TEST_F(lfptostrTest, UnsignedInteger) { + l_fp test = {3000000000UL, 0}; + + EXPECT_STREQ("3000000000.0", ulfptoa(&test, 1)); +} diff --git a/tests/libntp/libntptest.cpp b/tests/libntp/libntptest.cpp new file mode 100644 index 0000000..c3363b0 --- /dev/null +++ b/tests/libntp/libntptest.cpp @@ -0,0 +1,27 @@ +#include "libntptest.h" + +/* This file contains various constants that libntp needs to be set + * and that is normally defined in ntpd/ntpq/... + */ + +u_long current_time = 4; // needed by authkeys. Used only in to calculate lifetime. +const char *progname = "libntptest"; + +time_t libntptest::nowtime = 0; + +time_t libntptest::timefunc(time_t *ptr) +{ + if (ptr) + *ptr = nowtime; + return nowtime; +} + +void libntptest::settime(int y, int m, int d, int H, int M, int S) +{ + + time_t days(ntpcal_edate_to_eradays(y-1, m-1, d-1) + 1 - DAY_UNIX_STARTS); + time_t secs(ntpcal_etime_to_seconds(H, M, S)); + + nowtime = days * SECSPERDAY + secs; +} + diff --git a/tests/libntp/libntptest.h b/tests/libntp/libntptest.h new file mode 100644 index 0000000..ef2daa8 --- /dev/null +++ b/tests/libntp/libntptest.h @@ -0,0 +1,15 @@ +#include "tests_main.h" + +extern "C" { +#include "ntp_stdlib.h" +#include "ntp_calendar.h" +}; + +class libntptest : public ntptest { + +protected: + static time_t timefunc(time_t*); + static time_t nowtime; + static void settime(int y, int m, int d, int H, int M, int S); + +}; diff --git a/tests/libntp/modetoa.cpp b/tests/libntp/modetoa.cpp new file mode 100644 index 0000000..cb7404d --- /dev/null +++ b/tests/libntp/modetoa.cpp @@ -0,0 +1,16 @@ +#include "libntptest.h" + +class modetoaTest : public libntptest { +}; + +TEST_F(modetoaTest, KnownMode) { + const int MODE = 3; // Should be "client" + + EXPECT_STREQ("client", modetoa(MODE)); +} + +TEST_F(modetoaTest, UnknownMode) { + const int MODE = 100; + + EXPECT_STREQ("mode#100", modetoa(MODE)); +} diff --git a/tests/libntp/msyslog.cpp b/tests/libntp/msyslog.cpp new file mode 100644 index 0000000..5b00703 --- /dev/null +++ b/tests/libntp/msyslog.cpp @@ -0,0 +1,137 @@ +#include "libntptest.h" + +extern "C" { +#include <stdio.h> +#include <string.h> +#include <errno.h> +#ifndef VSNPRINTF_PERCENT_M +// format_errmsg() is normally private to msyslog.c +void format_errmsg (char *, size_t, const char *, int); +#endif +}; + +class msyslogTest : public libntptest { +}; + +// msnprintf() +TEST_F(msyslogTest, msnprintf) +{ +#define FMT_PREFIX "msyslog.cpp ENOENT: " + char exp_buf[512]; + char act_buf[512]; + int exp_cnt; + int act_cnt; + + exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s", + strerror(ENOENT)); + errno = ENOENT; + act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m"); + EXPECT_EQ(exp_cnt, act_cnt); + EXPECT_STREQ(exp_buf, act_buf); +} + +TEST_F(msyslogTest, msnprintfLiteralPercentm) +{ + char exp_buf[32]; + char act_buf[32]; + int exp_cnt; + int act_cnt; + + exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m"); + errno = ENOENT; + act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m"); + EXPECT_EQ(exp_cnt, act_cnt); + EXPECT_STREQ(exp_buf, act_buf); +} + +TEST_F(msyslogTest, msnprintfBackslashLiteralPercentm) +{ + char exp_buf[32]; + char act_buf[32]; + int exp_cnt; + int act_cnt; + + exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m"); + errno = ENOENT; + act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m"); + EXPECT_EQ(exp_cnt, act_cnt); + EXPECT_STREQ(exp_buf, act_buf); +} + +TEST_F(msyslogTest, msnprintfBackslashPercent) +{ + char exp_buf[32]; + char act_buf[32]; + int exp_cnt; + int act_cnt; + + exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s", + strerror(ENOENT)); + errno = ENOENT; + act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m"); + EXPECT_EQ(exp_cnt, act_cnt); + EXPECT_STREQ(exp_buf, act_buf); +} + +TEST_F(msyslogTest, msnprintfHangingPercent) +{ + static char fmt[] = "percent then nul term then non-nul %\0oops!"; + char exp_buf[64]; + char act_buf[64]; + int exp_cnt; + int act_cnt; + + ZERO(exp_buf); + ZERO(act_buf); + exp_cnt = snprintf(exp_buf, sizeof(exp_buf), fmt); + act_cnt = msnprintf(act_buf, sizeof(act_buf), fmt); + EXPECT_EQ(exp_cnt, act_cnt); + EXPECT_STREQ(exp_buf, act_buf); + EXPECT_STREQ("", act_buf + 1 + strlen(act_buf)); +} + +#ifndef VSNPRINTF_PERCENT_M +TEST_F(msyslogTest, format_errmsgHangingPercent) +{ + static char fmt[] = "percent then nul term then non-nul %\0oops!"; + char act_buf[64]; + + ZERO(act_buf); + format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT); + EXPECT_STREQ(fmt, act_buf); + EXPECT_STREQ("", act_buf + 1 + strlen(act_buf)); +} +#endif + +TEST_F(msyslogTest, msnprintfNullTarget) +{ + int exp_cnt; + int act_cnt; + + exp_cnt = snprintf(NULL, 0, "%d", 123); + errno = ENOENT; + act_cnt = msnprintf(NULL, 0, "%d", 123); + EXPECT_EQ(exp_cnt, act_cnt); +} + +TEST_F(msyslogTest, msnprintfTruncate) +{ + char undist[] = "undisturbed"; + char exp_buf[512]; + char act_buf[512]; + int exp_cnt; + int act_cnt; + + memcpy(exp_buf + 3, undist, sizeof(undist)); + memcpy(act_buf + 3, undist, sizeof(undist)); + exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT)); + errno = ENOENT; + act_cnt = msnprintf(act_buf, 3, "%m"); + EXPECT_EQ('\0', exp_buf[2]); + EXPECT_EQ('\0', act_buf[2]); + EXPECT_TRUE(act_cnt > 0); + EXPECT_EQ(exp_cnt, act_cnt); + EXPECT_STREQ(exp_buf, act_buf); + EXPECT_STREQ(exp_buf + 3, undist); + EXPECT_STREQ(act_buf + 3, undist); +} diff --git a/tests/libntp/netof.cpp b/tests/libntp/netof.cpp new file mode 100644 index 0000000..67b9df4 --- /dev/null +++ b/tests/libntp/netof.cpp @@ -0,0 +1,69 @@ +#include "sockaddrtest.h" + +class netofTest : public sockaddrtest { +}; + +TEST_F(netofTest, ClassBAddress) { + sockaddr_u input = CreateSockaddr4("172.16.2.1", NTP_PORT); + sockaddr_u expected = CreateSockaddr4("172.16.0.0", NTP_PORT); + + sockaddr_u* actual = netof(&input); + + ASSERT_TRUE(actual != NULL); + EXPECT_TRUE(IsEqual(expected, *actual)); +} + +TEST_F(netofTest, ClassCAddress) { + sockaddr_u input = CreateSockaddr4("192.0.2.255", NTP_PORT); + sockaddr_u expected = CreateSockaddr4("192.0.2.0", NTP_PORT); + + sockaddr_u* actual = netof(&input); + + ASSERT_TRUE(actual != NULL); + EXPECT_TRUE(IsEqual(expected, *actual)); +} + +TEST_F(netofTest, ClassAAddress) { + /* Class A addresses are assumed to be classless, + * thus the same address should be returned. + */ + sockaddr_u input = CreateSockaddr4("10.20.30.40", NTP_PORT); + sockaddr_u expected = CreateSockaddr4("10.20.30.40", NTP_PORT); + + sockaddr_u* actual = netof(&input); + + ASSERT_TRUE(actual != NULL); + EXPECT_TRUE(IsEqual(expected, *actual)); +} + +TEST_F(netofTest, IPv6Address) { + /* IPv6 addresses are assumed to have 64-bit host- and 64-bit network parts. */ + const struct in6_addr input_address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334 + + const struct in6_addr expected_address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; // 2001:0db8:85a3:08d3:0000:0000:0000:0000 + + sockaddr_u input; + input.sa6.sin6_family = AF_INET6; + input.sa6.sin6_addr = input_address; + SET_PORT(&input, 3000); + + sockaddr_u expected; + expected.sa6.sin6_family = AF_INET6; + expected.sa6.sin6_addr = expected_address; + SET_PORT(&expected, 3000); + + sockaddr_u* actual = netof(&input); + + ASSERT_TRUE(actual != NULL); + EXPECT_TRUE(IsEqual(expected, *actual)); +} diff --git a/tests/libntp/numtoa.cpp b/tests/libntp/numtoa.cpp new file mode 100644 index 0000000..58e00c4 --- /dev/null +++ b/tests/libntp/numtoa.cpp @@ -0,0 +1,18 @@ +#include "libntptest.h" + +class numtoaTest : public libntptest { +}; + +TEST_F(numtoaTest, Address) { + u_int32 input = htonl(3221225472UL+512UL+1UL); // 192.0.2.1 + + EXPECT_STREQ("192.0.2.1", numtoa(input)); +} + +TEST_F(numtoaTest, Netmask) { + // 255.255.255.0 + u_int32 hostOrder = 255UL*256UL*256UL*256UL + 255UL*256UL*256UL + 255UL*256UL; + u_int32 input = htonl(hostOrder); + + EXPECT_STREQ("255.255.255.0", numtoa(input)); +} diff --git a/tests/libntp/numtohost.cpp b/tests/libntp/numtohost.cpp new file mode 100644 index 0000000..ffc574e --- /dev/null +++ b/tests/libntp/numtohost.cpp @@ -0,0 +1,15 @@ +#include "libntptest.h" + +class numtohostTest : public libntptest { +}; + +TEST_F(numtohostTest, LoopbackNetNonResolve) { + /* A loopback address in 127.0.0.0/8 is chosen, and + * numtohost() should not try to resolve it unless + * it is 127.0.0.1 + */ + + u_int32 input = 127*256*256*256 + 1*256 + 1; // 127.0.1.1 + + EXPECT_STREQ("127.0.1.1", numtohost(htonl(input))); +} diff --git a/tests/libntp/octtoint.cpp b/tests/libntp/octtoint.cpp new file mode 100644 index 0000000..14a993e --- /dev/null +++ b/tests/libntp/octtoint.cpp @@ -0,0 +1,57 @@ +#include "libntptest.h" + +class octtointTest : public libntptest { +}; + +TEST_F(octtointTest, SingleDigit) { + const char* str = "5"; + u_long actual; + + ASSERT_TRUE(octtoint(str, &actual)); + EXPECT_EQ(5, actual); +} + +TEST_F(octtointTest, MultipleDigits) { + const char* str = "271"; + u_long actual; + + ASSERT_TRUE(octtoint(str, &actual)); + EXPECT_EQ(185, actual); +} + +TEST_F(octtointTest, Zero) { + const char* str = "0"; + u_long actual; + + ASSERT_TRUE(octtoint(str, &actual)); + EXPECT_EQ(0, actual); +} + +TEST_F(octtointTest, MaximumUnsigned32bit) { + const char* str = "37777777777"; + u_long actual; + + ASSERT_TRUE(octtoint(str, &actual)); + EXPECT_EQ(4294967295UL, actual); +} + +TEST_F(octtointTest, Overflow) { + const char* str = "40000000000"; + u_long actual; + + ASSERT_FALSE(octtoint(str, &actual)); +} + +TEST_F(octtointTest, IllegalCharacter) { + const char* str = "5ac2"; + u_long actual; + + ASSERT_FALSE(octtoint(str, &actual)); +} + +TEST_F(octtointTest, IllegalDigit) { + const char* str = "5283"; + u_long actual; + + ASSERT_FALSE(octtoint(str, &actual)); +} diff --git a/tests/libntp/prettydate.cpp b/tests/libntp/prettydate.cpp new file mode 100644 index 0000000..391ac29 --- /dev/null +++ b/tests/libntp/prettydate.cpp @@ -0,0 +1,16 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +}; + +class prettydateTest : public libntptest { +protected: + static const u_int32 HALF = 2147483648UL; +}; + +TEST_F(prettydateTest, ConstantDate) { + l_fp time = {3485080800UL, HALF}; // 2010-06-09 14:00:00.5 + + ASSERT_STREQ("cfba1ce0.80000000 Wed, Jun 9 2010 14:00:00.500", gmprettydate(&time)); +} diff --git a/tests/libntp/recvbuff.cpp b/tests/libntp/recvbuff.cpp new file mode 100644 index 0000000..e9505bd --- /dev/null +++ b/tests/libntp/recvbuff.cpp @@ -0,0 +1,38 @@ +#include "libntptest.h" + +extern "C" { +#include "recvbuff.h" +}; + +class recvbuffTest : public libntptest { +protected: + virtual void SetUp() { + init_recvbuff(RECV_INIT); + } +}; + +TEST_F(recvbuffTest, Initialization) { + EXPECT_EQ(RECV_INIT, free_recvbuffs()); + EXPECT_EQ(0, full_recvbuffs()); + EXPECT_FALSE(has_full_recv_buffer()); + EXPECT_TRUE(get_full_recv_buffer() == NULL); +} + +TEST_F(recvbuffTest, GetAndFree) { + int initial = free_recvbuffs(); + recvbuf_t* buf = get_free_recv_buffer(); + + EXPECT_EQ(initial-1, free_recvbuffs()); + freerecvbuf(buf); + EXPECT_EQ(initial, free_recvbuffs()); +} + +TEST_F(recvbuffTest, GetAndFill) { + int initial = free_recvbuffs(); + recvbuf_t* buf = get_free_recv_buffer(); + + add_full_recv_buffer(buf); + EXPECT_EQ(1, full_recvbuffs()); + EXPECT_TRUE(has_full_recv_buffer()); + EXPECT_EQ(buf, get_full_recv_buffer()); +} diff --git a/tests/libntp/refnumtoa.cpp b/tests/libntp/refnumtoa.cpp new file mode 100644 index 0000000..75460c0 --- /dev/null +++ b/tests/libntp/refnumtoa.cpp @@ -0,0 +1,52 @@ +#include "libntptest.h" + +#include "ntp_net.h" +#include "ntp_refclock.h" + +#include <sstream> + +class refnumtoaTest : public libntptest { +protected: + /* Might need to be updated if a new refclock gets this id. */ + static const int UNUSED_REFCLOCK_ID = 250; +}; + +#ifdef REFCLOCK /* clockname() is useless otherwise */ +TEST_F(refnumtoaTest, LocalClock) { + /* We test with a refclock address of type LOCALCLOCK. + * with id 8 + */ + u_int32 addr = REFCLOCK_ADDR; + addr |= REFCLK_LOCALCLOCK << 8; + addr |= 0x8; + + sockaddr_u address; + address.sa4.sin_family = AF_INET; + address.sa4.sin_addr.s_addr = htonl(addr); + + std::ostringstream expected; + expected << clockname(REFCLK_LOCALCLOCK) + << "(8)"; + + EXPECT_STREQ(expected.str().c_str(), refnumtoa(&address)); +} +#endif /* REFCLOCK */ + +#ifdef REFCLOCK /* refnumtoa() is useless otherwise */ +TEST_F(refnumtoaTest, UnknownId) { + /* We test with a currently unused refclock ID */ + u_int32 addr = REFCLOCK_ADDR; + addr |= UNUSED_REFCLOCK_ID << 8; + addr |= 0x4; + + sockaddr_u address; + address.sa4.sin_family = AF_INET; + address.sa4.sin_addr.s_addr = htonl(addr); + + std::ostringstream expected; + expected << "REFCLK(" << UNUSED_REFCLOCK_ID + << ",4)"; + + EXPECT_STREQ(expected.str().c_str(), refnumtoa(&address)); +} +#endif /* REFCLOCK */ diff --git a/tests/libntp/sfptostr.cpp b/tests/libntp/sfptostr.cpp new file mode 100644 index 0000000..5cef47e --- /dev/null +++ b/tests/libntp/sfptostr.cpp @@ -0,0 +1,71 @@ +/* + * This file contains test for both fptoa and fptoms (which uses dofptoa), + * since all these functions are very similar. + */ + +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +}; + +class sfptostr : public libntptest { +protected: + static const int SFP_MAX_PRECISION = 6; +}; + +TEST_F(sfptostr, PositiveInteger) { + s_fp test = 300 << 16; // exact 300.000000 + + EXPECT_STREQ("300.000000", fptoa(test, SFP_MAX_PRECISION)); + EXPECT_STREQ("300000.000", fptoms(test, SFP_MAX_PRECISION)); +} + +TEST_F(sfptostr, NegativeInteger) { + s_fp test = -200 << 16; // exact -200.000000 + + EXPECT_STREQ("-200.000000", fptoa(test, SFP_MAX_PRECISION)); + EXPECT_STREQ("-200000.000", fptoms(test, SFP_MAX_PRECISION)); +} + +TEST_F(sfptostr, PositiveIntegerPositiveFraction) { + s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5 + + EXPECT_STREQ("300.500000", fptoa(test, SFP_MAX_PRECISION)); + EXPECT_STREQ("300500.000", fptoms(test, SFP_MAX_PRECISION)); +} + +TEST_F(sfptostr, NegativeIntegerNegativeFraction) { + s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5 + + EXPECT_STREQ("-200.500000", fptoa(test, SFP_MAX_PRECISION)); + EXPECT_STREQ("-200500.000", fptoms(test, SFP_MAX_PRECISION)); +} + +TEST_F(sfptostr, PositiveIntegerNegativeFraction) { + s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25 + + EXPECT_STREQ("299.750000", fptoa(test, SFP_MAX_PRECISION)); + EXPECT_STREQ("299750.000", fptoms(test, SFP_MAX_PRECISION)); +} + +TEST_F(sfptostr, NegativeIntegerPositiveFraction) { + s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75 + + EXPECT_STREQ("-199.250000", fptoa(test, SFP_MAX_PRECISION)); + EXPECT_STREQ("-199250.000", fptoms(test, SFP_MAX_PRECISION)); +} + +TEST_F(sfptostr, SingleDecimalInteger) { + s_fp test = 300 << 16; // 300 + + EXPECT_STREQ("300.0", fptoa(test, 1)); + EXPECT_STREQ("300000.0", fptoms(test, 1)); +} + +TEST_F(sfptostr, SingleDecimalRounding) { + s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75 + + EXPECT_STREQ("2.8", fptoa(test, 1)); + EXPECT_STREQ("2750.0", fptoms(test, 1)); +} diff --git a/tests/libntp/sockaddrtest.h b/tests/libntp/sockaddrtest.h new file mode 100644 index 0000000..4baac4d --- /dev/null +++ b/tests/libntp/sockaddrtest.h @@ -0,0 +1,58 @@ +#ifndef TESTS_SOCKADDRTEST_H +#define TESTS_SOCKADDRTEST_H + +#include "libntptest.h" + +extern "C" { +#include "ntp.h" +}; + +class sockaddrtest : public libntptest { +protected: + ::testing::AssertionResult IsEqual(const sockaddr_u &expected, const sockaddr_u &actual) { + if (expected.sa.sa_family != actual.sa.sa_family) { + return ::testing::AssertionFailure() + << "Expected sa_family: " << expected.sa.sa_family + << " but got: " << actual.sa.sa_family; + } + + if (actual.sa.sa_family == AF_INET) { // IPv4 + if (expected.sa4.sin_port == actual.sa4.sin_port && + memcmp(&expected.sa4.sin_addr, &actual.sa4.sin_addr, + sizeof(in_addr)) == 0) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "IPv4 comparision failed, expected: " + << expected.sa4.sin_addr.s_addr + << "(" << socktoa(&expected) << ")" + << " but was: " + << actual.sa4.sin_addr.s_addr + << "(" << socktoa(&actual) << ")"; + } + } else if (actual.sa.sa_family == AF_INET6) { //IPv6 + if (expected.sa6.sin6_port == actual.sa6.sin6_port && + memcmp(&expected.sa6.sin6_addr, &actual.sa6.sin6_addr, + sizeof(in6_addr)) == 0) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "IPv6 comparision failed"; + } + } else { // Unknown family + return ::testing::AssertionFailure() + << "Unknown sa_family: " << actual.sa.sa_family; + } + } + + sockaddr_u CreateSockaddr4(const char* address, unsigned int port) { + sockaddr_u s; + s.sa4.sin_family = AF_INET; + s.sa4.sin_addr.s_addr = inet_addr(address); + SET_PORT(&s, port); + + return s; + } +}; + +#endif // TESTS_SOCKADDRTEST_H diff --git a/tests/libntp/socktoa.cpp b/tests/libntp/socktoa.cpp new file mode 100644 index 0000000..6c70b6d --- /dev/null +++ b/tests/libntp/socktoa.cpp @@ -0,0 +1,100 @@ +#include "sockaddrtest.h" + +class socktoaTest : public sockaddrtest { +}; + +TEST_F(socktoaTest, IPv4AddressWithPort) { + sockaddr_u input = CreateSockaddr4("192.0.2.10", 123); + + EXPECT_STREQ("192.0.2.10", socktoa(&input)); + EXPECT_STREQ("192.0.2.10:123", sockporttoa(&input)); +} + +TEST_F(socktoaTest, IPv6AddressWithPort) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + const char* expected = + "2001:db8:85a3:8d3:1319:8a2e:370:7334"; + const char* expected_port = + "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123"; + + sockaddr_u input; + memset(&input, 0, sizeof(input)); + AF(&input) = AF_INET6; + SET_ADDR6N(&input, address); + SET_PORT(&input, 123); + + EXPECT_STREQ(expected, socktoa(&input)); + EXPECT_STREQ(expected_port, sockporttoa(&input)); +} + +#ifdef ISC_PLATFORM_HAVESCOPEID +TEST_F(socktoaTest, ScopedIPv6AddressWithPort) { + const struct in6_addr address = { + 0xfe, 0x80, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x02, 0x12, 0x3f, 0xff, + 0xfe, 0x29, 0xff, 0xfa + }; + + const char* expected = + "fe80::212:3fff:fe29:fffa%5"; + const char* expected_port = + "[fe80::212:3fff:fe29:fffa%5]:123"; + + sockaddr_u input; + memset(&input, 0, sizeof(input)); + AF(&input) = AF_INET6; + SET_ADDR6N(&input, address); + SET_PORT(&input, 123); + SCOPE_VAR(&input) = 5; + + EXPECT_STREQ(expected, socktoa(&input)); + EXPECT_STREQ(expected_port, sockporttoa(&input)); +} +#endif /* ISC_PLATFORM_HAVESCOPEID */ + +TEST_F(socktoaTest, HashEqual) { + sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123); + sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123); + + ASSERT_TRUE(IsEqual(input1, input2)); + EXPECT_EQ(sock_hash(&input1), sock_hash(&input2)); +} + +TEST_F(socktoaTest, HashNotEqual) { + /* These two addresses should not generate the same hash. */ + sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123); + sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123); + + ASSERT_FALSE(IsEqual(input1, input2)); + EXPECT_NE(sock_hash(&input1), sock_hash(&input2)); +} + +TEST_F(socktoaTest, IgnoreIPv6Fields) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + sockaddr_u input1, input2; + + input1.sa6.sin6_family = AF_INET6; + input1.sa6.sin6_addr = address; + input1.sa6.sin6_flowinfo = 30L; // This value differs from input2. + SET_PORT(&input1, NTP_PORT); + + input2.sa6.sin6_family = AF_INET6; + input2.sa6.sin6_addr = address; + input2.sa6.sin6_flowinfo = 10L; // This value differs from input1. + SET_PORT(&input2, NTP_PORT); + + EXPECT_EQ(sock_hash(&input1), sock_hash(&input2)); +} diff --git a/tests/libntp/ssl_init.cpp b/tests/libntp/ssl_init.cpp new file mode 100644 index 0000000..8812656 --- /dev/null +++ b/tests/libntp/ssl_init.cpp @@ -0,0 +1,50 @@ +#include "libntptest.h" + +extern "C" { +#ifdef OPENSSL +# include "openssl/err.h" +# include "openssl/rand.h" +# include "openssl/evp.h" +#endif +#include "ntp.h" +}; + +class ssl_initTest : public libntptest { +protected: + static const size_t TEST_MD5_DIGEST_LENGTH = 16; + static const size_t TEST_SHA1_DIGEST_LENGTH = 20; +}; + +// keytype_from_text() +TEST_F(ssl_initTest, MD5KeyTypeWithoutDigestLength) { + ASSERT_EQ(KEY_TYPE_MD5, keytype_from_text("MD5", NULL)); +} + +TEST_F(ssl_initTest, MD5KeyTypeWithDigestLength) { + size_t digestLength; + size_t expected = TEST_MD5_DIGEST_LENGTH; + + EXPECT_EQ(KEY_TYPE_MD5, keytype_from_text("MD5", &digestLength)); + EXPECT_EQ(expected, digestLength); +} + +#ifdef OPENSSL +TEST_F(ssl_initTest, SHA1KeyTypeWithDigestLength) { + size_t digestLength; + size_t expected = TEST_SHA1_DIGEST_LENGTH; + + EXPECT_EQ(NID_sha, keytype_from_text("SHA", &digestLength)); + EXPECT_EQ(expected, digestLength); +} +#endif /* OPENSSL */ + +// keytype_name() +TEST_F(ssl_initTest, MD5KeyName) { + EXPECT_STREQ("MD5", keytype_name(KEY_TYPE_MD5)); +} + +#ifdef OPENSSL +TEST_F(ssl_initTest, SHA1KeyName) { + EXPECT_STREQ("SHA", keytype_name(NID_sha)); +} +#endif /* OPENSSL */ diff --git a/tests/libntp/statestr.cpp b/tests/libntp/statestr.cpp new file mode 100644 index 0000000..506b079 --- /dev/null +++ b/tests/libntp/statestr.cpp @@ -0,0 +1,27 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp.h" // Needed for MAX_MAC_LEN used in ntp_control.h +#include "ntp_control.h" +}; + +class statestrTest : public libntptest { +}; + +// eventstr() +TEST_F(statestrTest, PeerRestart) { + EXPECT_STREQ("restart", eventstr(PEVNT_RESTART)); +} + +TEST_F(statestrTest, SysUnspecified) { + EXPECT_STREQ("unspecified", eventstr(EVNT_UNSPEC)); +} + +// ceventstr() +TEST_F(statestrTest, ClockCodeExists) { + EXPECT_STREQ("clk_unspec", ceventstr(CTL_CLK_OKAY)); +} + +TEST_F(statestrTest, ClockCodeUnknown) { + EXPECT_STREQ("clk_-1", ceventstr(-1)); +} diff --git a/tests/libntp/strtolfp.cpp b/tests/libntp/strtolfp.cpp new file mode 100644 index 0000000..729996e --- /dev/null +++ b/tests/libntp/strtolfp.cpp @@ -0,0 +1,107 @@ +#include "lfptest.h" + +/* This class tests both atolfp and mstolfp */ + +class strtolfpTest : public lfptest { +}; + +TEST_F(strtolfpTest, PositiveInteger) { + const char *str = "500"; + const char *str_ms = "500000"; + + l_fp expected = {500,0}; + l_fp actual, actual_ms; + + ASSERT_TRUE(atolfp(str, &actual)); + ASSERT_TRUE(mstolfp(str_ms, &actual_ms)); + + EXPECT_TRUE(IsEqual(expected, actual)); + EXPECT_TRUE(IsEqual(expected, actual_ms)); +} + +TEST_F(strtolfpTest, NegativeInteger) { + const char *str = "-300"; + const char *str_ms = "-300000"; + + l_fp expected; + expected.l_i = -300; + expected.l_uf = 0; + + l_fp actual, actual_ms; + + ASSERT_TRUE(atolfp(str, &actual)); + ASSERT_TRUE(mstolfp(str_ms, &actual_ms)); + + EXPECT_TRUE(IsEqual(expected, actual)); + EXPECT_TRUE(IsEqual(expected, actual_ms)); +} + +TEST_F(strtolfpTest, PositiveFraction) { + const char *str = "+500.5"; + const char *str_ms = "500500.0"; + + l_fp expected = {500, HALF}; + l_fp actual, actual_ms; + + ASSERT_TRUE(atolfp(str, &actual)); + ASSERT_TRUE(mstolfp(str_ms, &actual_ms)); + + EXPECT_TRUE(IsEqual(expected, actual)); + EXPECT_TRUE(IsEqual(expected, actual_ms)); +} + +TEST_F(strtolfpTest, NegativeFraction) { + const char *str = "-300.75"; + const char *str_ms = "-300750"; + + l_fp expected; + expected.l_i = -301; + expected.l_uf = QUARTER; + + l_fp actual, actual_ms; + + ASSERT_TRUE(atolfp(str, &actual)); + ASSERT_TRUE(mstolfp(str_ms, &actual_ms)); + + EXPECT_TRUE(IsEqual(expected, actual)); + EXPECT_TRUE(IsEqual(expected, actual_ms)); +} + +TEST_F(strtolfpTest, PositiveMsFraction) { + const char *str = "300.00025"; + const char *str_ms = "300000.25"; + + l_fp expected = {300, QUARTER_PROMILLE_APPRX}; + l_fp actual, actual_ms; + + ASSERT_TRUE(atolfp(str, &actual)); + ASSERT_TRUE(mstolfp(str_ms, &actual_ms)); + + EXPECT_TRUE(IsEqual(expected, actual)); + EXPECT_TRUE(IsEqual(expected, actual_ms)); +} + +TEST_F(strtolfpTest, NegativeMsFraction) { + const char *str = "-199.99975"; + const char *str_ms = "-199999.75"; + + l_fp expected; + expected.l_i = -200; + expected.l_uf = QUARTER_PROMILLE_APPRX; + + l_fp actual, actual_ms; + + ASSERT_TRUE(atolfp(str, &actual)); + ASSERT_TRUE(mstolfp(str_ms, &actual_ms)); + + EXPECT_TRUE(IsEqual(expected, actual)); + EXPECT_TRUE(IsEqual(expected, actual_ms)); +} + +TEST_F(strtolfpTest, InvalidChars) { + const char *str = "500.4a2"; + l_fp actual, actual_ms; + + ASSERT_FALSE(atolfp(str, &actual)); + ASSERT_FALSE(mstolfp(str, &actual_ms)); +} diff --git a/tests/libntp/timespecops.cpp b/tests/libntp/timespecops.cpp new file mode 100644 index 0000000..392d271 --- /dev/null +++ b/tests/libntp/timespecops.cpp @@ -0,0 +1,475 @@ +#include "libntptest.h" +#include "timestructs.h" + +extern "C" { +#include <math.h> +#include "timespecops.h" +} + +#include <string> +#include <sstream> + +using namespace timeStruct; + +class timespecTest : public libntptest { +protected: + static u_int32 my_tick_to_tsf(u_int32 ticks); + static u_int32 my_tsf_to_tick(u_int32 tsf); + + // that's it... + struct lfpfracdata { + long nsec; + u_int32 frac; + }; + static const lfpfracdata fdata[]; +}; + +u_int32 +timespecTest::my_tick_to_tsf( + u_int32 ticks + ) +{ + // convert nanoseconds to l_fp fractional units, using double + // precision float calculations or, if available, 64bit integer + // arithmetic. This should give the precise fraction, rounded to + // the nearest representation. +#ifdef HAVE_U_INT64 + return u_int32(((u_int64(ticks) << 32) + 500000000) / 1000000000); +#else + return u_int32(double(ticks) * 4.294967296 + 0.5); +#endif + // And before you ask: if ticks >= 1000000000, the result is + // truncated nonsense, so don't use it out-of-bounds. +} + +u_int32 +timespecTest::my_tsf_to_tick( + u_int32 tsf + ) +{ + // Inverse operation: converts fraction to microseconds. +#ifdef HAVE_U_INT64 + return u_int32((u_int64(tsf) * 1000000000 + 0x80000000) >> 32); +#else + return u_int32(double(tsf) / 4.294967296 + 0.5); +#endif + // Beware: The result might be 10^9 due to rounding! +} + +const timespecTest::lfpfracdata timespecTest::fdata [] = { + { 0, 0x00000000 }, { 2218896, 0x00916ae6 }, + { 16408100, 0x0433523d }, { 125000000, 0x20000000 }, + { 250000000, 0x40000000 }, { 287455871, 0x4996b53d }, + { 375000000, 0x60000000 }, { 500000000, 0x80000000 }, + { 518978897, 0x84dbcd0e }, { 563730222, 0x90509fb3 }, + { 563788007, 0x9054692c }, { 583289882, 0x95527c57 }, + { 607074509, 0x9b693c2a }, { 625000000, 0xa0000000 }, + { 645184059, 0xa52ac851 }, { 676497788, 0xad2ef583 }, + { 678910895, 0xadcd1abb }, { 679569625, 0xadf84663 }, + { 690926741, 0xb0e0932d }, { 705656483, 0xb4a5e73d }, + { 723553854, 0xb93ad34c }, { 750000000, 0xc0000000 }, + { 763550253, 0xc3780785 }, { 775284917, 0xc6791284 }, + { 826190764, 0xd3813ce8 }, { 875000000, 0xe0000000 }, + { 956805507, 0xf4f134a9 }, { 982570733, 0xfb89c16c } +}; + + +// --------------------------------------------------------------------- +// test support stuff -- part 1 +// --------------------------------------------------------------------- + +TEST_F(timespecTest, Helpers1) { + timespec_wrap x; + + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) { + x.V.tv_nsec = -1; + ASSERT_FALSE(x.valid()); + x.V.tv_nsec = 0; + ASSERT_TRUE(x.valid()); + x.V.tv_nsec = 999999999; + ASSERT_TRUE(x.valid()); + x.V.tv_nsec = 1000000000; + ASSERT_FALSE(x.valid()); + } +} + +//---------------------------------------------------------------------- +// test normalisation +//---------------------------------------------------------------------- + +TEST_F(timespecTest, Normalise) { + for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) { + timespec_wrap x(0, ns); + + x = normalize_tspec(x); + ASSERT_TRUE(x.valid()); + } +} + +//---------------------------------------------------------------------- +// test classification +//---------------------------------------------------------------------- + +TEST_F(timespecTest, SignNoFrac) { + // sign test, no fraction + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i, 0); + int E = (i > 0) - (i < 0); + int r = test_tspec(a); + + ASSERT_EQ(E, r); + } +} + +TEST_F(timespecTest, SignWithFrac) { + // sign test, with fraction + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i, 10); + int E = (i >= 0) - (i < 0); + int r = test_tspec(a); + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// test compare +//---------------------------------------------------------------------- +TEST_F(timespecTest, CmpFracEQ) { + // fractions are equal + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timespec_wrap a( i , 200); + timespec_wrap b( j , 200); + int E = (i > j) - (i < j); + int r = cmp_tspec_denorm(a, b); + ASSERT_EQ(E, r); + } +} + +TEST_F(timespecTest, CmpFracGT) { + // fraction a bigger fraction b + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timespec_wrap a(i, 999999800); + timespec_wrap b(j, 200); + int E = (i >= j) - (i < j); + int r = cmp_tspec_denorm(a, b); + ASSERT_EQ(E, r); + } +} + +TEST_F(timespecTest, CmpFracLT) { + // fraction a less fraction b + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timespec_wrap a(i, 200); + timespec_wrap b(j, 999999800); + int E = (i > j) - (i <= j); + int r = cmp_tspec_denorm(a, b); + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// Test addition (sum) +//---------------------------------------------------------------------- + +TEST_F(timespecTest, AddFullNorm) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timespec_wrap a(i, 200); + timespec_wrap b(j, 400); + timespec_wrap E(i + j, 200 + 400); + timespec_wrap c; + + c = add_tspec(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timespecTest, AddFullOflow1) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timespec_wrap a(i, 200); + timespec_wrap b(j, 999999900); + timespec_wrap E(i + j + 1, 100); + timespec_wrap c; + + c = add_tspec(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timespecTest, AddNsecNorm) { + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i, 200); + timespec_wrap E(i, 600); + timespec_wrap c; + + c = add_tspec_ns(a, 600 - 200); + ASSERT_EQ(E, c); + } +} + +TEST_F(timespecTest, AddNsecOflow1) { + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i, 200); + timespec_wrap E(i + 1, 100); + timespec_wrap c; + + c = add_tspec_ns(a, NANOSECONDS - 100); + ASSERT_EQ(E, c); + } +} + +//---------------------------------------------------------------------- +// test subtraction (difference) +//---------------------------------------------------------------------- + +TEST_F(timespecTest, SubFullNorm) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timespec_wrap a( i , 600); + timespec_wrap b( j , 400); + timespec_wrap E(i-j, 200); + timespec_wrap c; + + c = sub_tspec(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timespecTest, SubFullOflow) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timespec_wrap a( i , 100); + timespec_wrap b( j , 999999900); + timespec_wrap E(i-j-1, 200); + timespec_wrap c; + + c = sub_tspec(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timespecTest, SubNsecNorm) { + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i, 600); + timespec_wrap E(i, 200); + timespec_wrap c; + + c = sub_tspec_ns(a, 600 - 200); + ASSERT_EQ(E, c); + } +} + +TEST_F(timespecTest, SubNsecOflow) { + for (int i = -4; i <= 4; ++i) { + timespec_wrap a( i , 100); + timespec_wrap E(i-1, 200); + timespec_wrap c; + + c = sub_tspec_ns(a, NANOSECONDS - 100); + ASSERT_EQ(E, c); + } +} + +//---------------------------------------------------------------------- +// test negation +//---------------------------------------------------------------------- + +TEST_F(timespecTest, Neg) { + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i, 100); + timespec_wrap b; + timespec_wrap c; + + b = neg_tspec(a); + c = add_tspec(a, b); + ASSERT_EQ(0, test_tspec(c)); + } +} + +//---------------------------------------------------------------------- +// test abs value +//---------------------------------------------------------------------- + +TEST_F(timespecTest, AbsNoFrac) { + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i , 0); + timespec_wrap b; + + b = abs_tspec(a); + ASSERT_EQ((i != 0), test_tspec(b)); + } +} + +TEST_F(timespecTest, AbsWithFrac) { + for (int i = -4; i <= 4; ++i) { + timespec_wrap a(i, 100); + timespec_wrap b; + + b = abs_tspec(a); + ASSERT_EQ(1, test_tspec(b)); + } +} + +// --------------------------------------------------------------------- +// test support stuff -- part 2 +// --------------------------------------------------------------------- + +TEST_F(timespecTest, Helpers2) { + AssertTimespecClose isClose(0, 2); + timespec_wrap x, y; + + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) + for (x.V.tv_nsec = 1; + x.V.tv_nsec < 1000000000; + x.V.tv_nsec += 499999999) { + for (long i = -4; i < 5; i++) { + y = x; + y.V.tv_nsec += i; + if (i >= -2 && i <= 2) + ASSERT_PRED_FORMAT2(isClose, x, y); + else + ASSERT_PRED_FORMAT2(!isClose, x, y); + } + } +} + +// global predicate instances we're using here +static AssertFpClose FpClose(0, 1); +static AssertTimespecClose TimespecClose(0, 2); + +//---------------------------------------------------------------------- +// conversion to l_fp +//---------------------------------------------------------------------- + +TEST_F(timespecTest, ToLFPbittest) { + for (u_int32 i = 0; i < 1000000000; i+=1000) { + timespec_wrap a(1, i); + l_fp_wrap E(1, my_tick_to_tsf(i)); + l_fp_wrap r; + + r = tspec_intv_to_lfp(a); + ASSERT_PRED_FORMAT2(FpClose, E, r); + } +} + +TEST_F(timespecTest, ToLFPrelPos) { + for (int i = 0; i < COUNTOF(fdata); i++) { + timespec_wrap a(1, fdata[i].nsec); + l_fp_wrap E(1, fdata[i].frac); + l_fp_wrap r; + + r = tspec_intv_to_lfp(a); + ASSERT_EQ(E, r); + } +} + +TEST_F(timespecTest, ToLFPrelNeg) { + for (int i = 0; i < COUNTOF(fdata); i++) { + timespec_wrap a(-1, fdata[i].nsec); + l_fp_wrap E(~0, fdata[i].frac); + l_fp_wrap r; + + r = tspec_intv_to_lfp(a); + ASSERT_EQ(E, r); + } +} + +TEST_F(timespecTest, ToLFPabs) { + for (int i = 0; i < COUNTOF(fdata); i++) { + timespec_wrap a(1, fdata[i].nsec); + l_fp_wrap E(1 + JAN_1970, fdata[i].frac); + l_fp_wrap r; + + r = tspec_stamp_to_lfp(a); + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// conversion from l_fp +//---------------------------------------------------------------------- +TEST_F(timespecTest, FromLFPbittest) { + // Not *exactly* a bittest, because 2**32 tests would take a + // really long time even on very fast machines! So we do test + // every 1000 fractional units. + for (u_int32 tsf = 0; tsf < ~u_int32(1000); tsf += 1000) { + timespec_wrap E(1, my_tsf_to_tick(tsf)); + l_fp_wrap a(1, tsf); + timespec_wrap r; + + r = lfp_intv_to_tspec(a); + // The conversion might be off by one nanosecond when + // comparing to calculated value. + ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + +TEST_F(timespecTest, FromLFPrelPos) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(1, fdata[i].frac); + timespec_wrap E(1, fdata[i].nsec); + timespec_wrap r; + + r = lfp_intv_to_tspec(a); + ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + +TEST_F(timespecTest, FromLFPrelNeg) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(~0, fdata[i].frac); + timespec_wrap E(-1, fdata[i].nsec); + timespec_wrap r; + + r = lfp_intv_to_tspec(a); + ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + + +// nsec -> frac -> nsec roundtrip, using a prime start and increment +TEST_F(timespecTest, LFProundtrip) { + for (int32_t t = -1; t < 2; ++t) + for (u_int32 i = 4999; i < 1000000000; i+=10007) { + timespec_wrap E(t, i); + l_fp_wrap a; + timespec_wrap r; + + a = tspec_intv_to_lfp(E); + r = lfp_intv_to_tspec(a); + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// string formatting +//---------------------------------------------------------------------- + +TEST_F(timespecTest, ToString) { + static const struct { + time_t sec; + long nsec; + const char * repr; + } data [] = { + { 0, 0, "0.000000000" }, + { 2, 0, "2.000000000" }, + {-2, 0, "-2.000000000" }, + { 0, 1, "0.000000001" }, + { 0,-1, "-0.000000001" }, + { 1,-1, "0.999999999" }, + {-1, 1, "-0.999999999" }, + {-1,-1, "-1.000000001" }, + }; + for (int i = 0; i < COUNTOF(data); i++) { + timespec_wrap a(data[i].sec, data[i].nsec); + std::string E(data[i].repr); + std::string r(tspectoa(a)); + ASSERT_EQ(E, r); + } +} + +// -*- EOF -*- diff --git a/tests/libntp/timestructs.cpp b/tests/libntp/timestructs.cpp new file mode 100644 index 0000000..3cd0c54 --- /dev/null +++ b/tests/libntp/timestructs.cpp @@ -0,0 +1,156 @@ +/* + * timestructs.cpp -- test bed adaptors for time structs. + * + * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project. + * The contents of 'html/copyright.html' apply. + */ +#include "libntptest.h" +#include "timestructs.h" + +extern "C" { +#include "timetoa.h" +#include "timevalops.h" +#include "timespecops.h" +} + +namespace timeStruct { + +std::ostream& +operator << (std::ostream& os, const timeStruct::l_fp_wrap& val) +{ + // raw data formatting + os << "0x" << std::hex << val.V.l_ui << ':' + << std::setfill('0') << std::setw(8) << val.V.l_uf + << std::dec; + // human-readable format + os << '[' << lfptoa(&val.V, 10) << ']'; + return os; +} + +std::ostream& +operator << (std::ostream& os, const timeStruct::timeval_wrap& val) +{ + // raw data formatting + os << val.V.tv_sec << ':' << val.V.tv_usec; + // human-readable format + os << '[' + << format_time_fraction(val.V.tv_sec, val.V.tv_usec, 6) + << ']'; + return os; +} + +std::ostream& +operator << (std::ostream& os, const timeStruct::timespec_wrap& val) +{ + // raw data formatting + os << val.V.tv_sec << ':' << val.V.tv_nsec; + // human-readable format + os << '[' + << format_time_fraction(val.V.tv_sec, val.V.tv_nsec, 9) + << ']'; + return os; +} + +// Implementation of the l_fp closeness predicate + +AssertFpClose::AssertFpClose( + u_int32 hi, + u_int32 lo + ) +{ + limit.l_ui = hi; + limit.l_uf = lo; +} + +::testing::AssertionResult +AssertFpClose::operator()( + const char* m_expr, + const char* n_expr, + const l_fp & m, + const l_fp & n + ) +{ + l_fp diff; + + if (L_ISGEQ(&m, &n)) { + diff = m; + L_SUB(&diff, &n); + } else { + diff = n; + L_SUB(&diff, &m); + } + if (L_ISGEQ(&limit, &diff)) + return ::testing::AssertionSuccess(); + + return ::testing::AssertionFailure() + << m_expr << " which is " << l_fp_wrap(m) + << "\nand\n" + << n_expr << " which is " << l_fp_wrap(n) + << "\nare not close; diff=" << l_fp_wrap(diff); +} + +// Implementation of the timeval closeness predicate + +AssertTimevalClose::AssertTimevalClose( + time_t hi, + int32 lo + ) +{ + limit.tv_sec = hi; + limit.tv_usec = lo; +} + +::testing::AssertionResult +AssertTimevalClose::operator()( + const char* m_expr, + const char* n_expr, + const struct timeval & m, + const struct timeval & n + ) +{ + struct timeval diff; + + diff = abs_tval(sub_tval(m, n)); + if (cmp_tval(limit, diff) >= 0) + return ::testing::AssertionSuccess(); + + return ::testing::AssertionFailure() + << m_expr << " which is " << timeval_wrap(m) + << "\nand\n" + << n_expr << " which is " << timeval_wrap(n) + << "\nare not close; diff=" << timeval_wrap(diff); +} + +// Implementation of the timespec closeness predicate + +AssertTimespecClose::AssertTimespecClose( + time_t hi, + int32 lo + ) +{ + limit.tv_sec = hi; + limit.tv_nsec = lo; +} + +::testing::AssertionResult +AssertTimespecClose::operator()( + const char* m_expr, + const char* n_expr, + const struct timespec & m, + const struct timespec & n + ) +{ + struct timespec diff; + + diff = abs_tspec(sub_tspec(m, n)); + if (cmp_tspec(limit, diff) >= 0) + return ::testing::AssertionSuccess(); + + return ::testing::AssertionFailure() + << m_expr << " which is " << timespec_wrap(m) + << "\nand\n" + << n_expr << " which is " << timespec_wrap(n) + << "\nare not close; diff=" << timespec_wrap(diff); +} + +} // namespace timeStruct diff --git a/tests/libntp/timestructs.h b/tests/libntp/timestructs.h new file mode 100644 index 0000000..7bc4ffd --- /dev/null +++ b/tests/libntp/timestructs.h @@ -0,0 +1,185 @@ +/* + * timestructs.h -- test bed adaptors for time structs. + * + * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project. + * The contents of 'html/copyright.html' apply. + * + * Some wrapper classes and a closeness predicate that are used to + * bridge the gap between the goggletest framework and the structs used + * for representing time stamps (l_fp, struct timeval, struct timespec). + * + * Some ostream conversion operators are provided to give diagnostic + * output on errors. The normal string conversion functions will give + * HRVs (human readable values) but we might also be interested in the + * machine representation for diagnostic purposes. + */ +#ifndef TIMESTRUCTS_H +#define TIMESTRUCTS_H + +extern "C" { +#include "ntp_fp.h" +} + +namespace timeStruct { + +// wrap a l_fp struct with common operations +class l_fp_wrap { + public: + l_fp V; + + l_fp_wrap() + { ZERO(V); } + l_fp_wrap(u_int32 hi, u_int32 lo) + { V.l_ui = hi; V.l_uf = lo; } + l_fp_wrap(const l_fp &rhs) + { V = rhs; } + bool operator == (const l_fp_wrap& rhs) const + { return L_ISEQU(&V, &rhs.V); } + operator l_fp* () + { return &V; } + operator l_fp& () + { return V; } + l_fp_wrap & operator = (const l_fp_wrap& rhs) + { V = rhs.V; return *this; } + l_fp_wrap& operator = (const l_fp& rhs) + { V = rhs; return *this; } + }; + +// wrap a 'struct timeval' with common operations +class timeval_wrap { +public: + struct timeval V; + + timeval_wrap() + { ZERO(V); } + timeval_wrap(time_t hi, long lo) + { V.tv_sec = hi; V.tv_usec = lo; } + timeval_wrap(const struct timeval & rhs) + { V = rhs; } + timeval_wrap(const timeval_wrap & rhs) + { V = rhs.V; } + bool operator == (const timeval_wrap& rhs) const + { return V.tv_sec == rhs.V.tv_sec && + V.tv_usec == rhs.V.tv_usec ; } + bool valid() const + { return V.tv_usec >= 0 && V.tv_usec < 1000000; } + operator struct timeval* () + { return &V; } + operator struct timeval& () + { return V; } + timeval_wrap& operator = (const timeval_wrap& rhs) + { V = rhs.V; return *this; } + timeval_wrap& operator = (const struct timeval& rhs) + { V = rhs; return *this; } +}; + +// wrap a 'struct timespec' with common operations +class timespec_wrap { +public: + struct timespec V; + + timespec_wrap() + { ZERO(V); } + timespec_wrap(time_t hi, long lo) + { V.tv_sec = hi; V.tv_nsec = lo; } + timespec_wrap(const struct timespec & rhs) + { V = rhs; } + timespec_wrap(const timespec_wrap & rhs) + { V = rhs.V; } + bool operator == (const timespec_wrap& rhs) const + { return V.tv_sec == rhs.V.tv_sec && + V.tv_nsec == rhs.V.tv_nsec ; } + bool valid() const + { return V.tv_nsec >= 0 && V.tv_nsec < 1000000000; } + operator struct timespec* () + { return &V; } + operator struct timespec& () + { return V; } + timespec_wrap& operator = (const timespec_wrap& rhs) + { V = rhs.V; return *this; } + timespec_wrap& operator = (const struct timespec& rhs) + { V = rhs; return *this; } +}; + +// l_fp closeness testing predicate +// +// This predicate is used for the closeness ('near') testing of l_fp +// values. Once constructed with a limit, it can be used to check the +// absolute difference of two l_fp structs against that limit; if the +// difference is less or equal to this limit, the test passes. +class AssertFpClose { +private: + l_fp limit; + +public: + AssertFpClose(u_int32 hi, u_int32 lo); + + ::testing::AssertionResult + operator()(const char* m_expr, const char* n_expr, + const l_fp & m, const l_fp & n); +}; + + +// timeval closeness testing predicate +// +// CAVEAT: This class uses the timevalops functions +// - sub_tval +// - abs_tval +// - cmp_tval +// +// This creates a dependency loop of sorts. The loop is defused by the +// fact that these basic operations can be tested by exact value tests, +// so once the basic timeval operations passed it's safe to use this +// predicate. +class AssertTimevalClose { +private: + struct timeval limit; + +public: + // note: (hi,lo) should be a positive normalised timeval; + // the constructor does not normalise the values! + AssertTimevalClose(time_t hi, int32 lo); + + ::testing::AssertionResult + operator()(const char* m_expr, const char* n_expr, + const struct timeval & m, const struct timeval & n); +}; + + +// timespec closeness testing predicate +// +// CAVEAT: This class uses the timespecops functions +// - sub_tspec +// - abs_tspec +// - cmp_tspec +// +// See the equivalent timeval helper. +class AssertTimespecClose { +private: + struct timespec limit; + +public: + // note: (hi,lo) should be a positive normalised timespec; + // the constructor does not normalise the values! + AssertTimespecClose(time_t hi, int32 lo); + + ::testing::AssertionResult + operator()(const char* m_expr, const char* n_expr, + const struct timespec & m, const struct timespec & n); +}; + + +// since googletest wants to string format items, we declare the +// necessary operators. Since all adaptors have only public members +// there is need for friend declarations anywhere. + +extern std::ostream& operator << (std::ostream& os, + const timeStruct::l_fp_wrap& val); +extern std::ostream& operator << (std::ostream& os, + const timeStruct::timeval_wrap& val); +extern std::ostream& operator << (std::ostream& os, + const timeStruct::timespec_wrap& val); + +} // namespace timeStruct + +#endif // TIMESTRUCTS_H diff --git a/tests/libntp/timevalops.cpp b/tests/libntp/timevalops.cpp new file mode 100644 index 0000000..7232615 --- /dev/null +++ b/tests/libntp/timevalops.cpp @@ -0,0 +1,480 @@ +#include "libntptest.h" +#include "timestructs.h" + +extern "C" { +#include <math.h> +#include "timevalops.h" +} + +#include <string> +#include <sstream> + +using namespace timeStruct; + +class timevalTest : public libntptest { +protected: + static u_int32 my_tick_to_tsf(u_int32 ticks); + static u_int32 my_tsf_to_tick(u_int32 tsf); + + // that's it... + struct lfpfracdata { + long usec; + u_int32 frac; + }; + static const lfpfracdata fdata[]; +}; + +u_int32 +timevalTest::my_tick_to_tsf( + u_int32 ticks + ) +{ + // convert microseconds to l_fp fractional units, using double + // precision float calculations or, if available, 64bit integer + // arithmetic. This should give the precise fraction, rounded to + // the nearest representation. +#ifdef HAVE_U_INT64 + return u_int32(((u_int64(ticks) << 32) + 500000) / 1000000); +#else + return u_int32(double(ticks) * 4294.967296 + 0.5); +#endif + // And before you ask: if ticks >= 1000000, the result is + // truncated nonsense, so don't use it out-of-bounds. +} + +u_int32 +timevalTest::my_tsf_to_tick( + u_int32 tsf + ) +{ + // Inverse operation: converts fraction to microseconds. +#ifdef HAVE_U_INT64 + return u_int32((u_int64(tsf) * 1000000 + 0x80000000) >> 32); +#else + return u_int32(double(tsf) / 4294.967296 + 0.5); +#endif + // Beware: The result might be 10^6 due to rounding! +} + +const timevalTest::lfpfracdata timevalTest::fdata [] = { + { 0, 0x00000000 }, { 7478, 0x01ea1405 }, + { 22077, 0x05a6d699 }, { 125000, 0x20000000 }, + { 180326, 0x2e29d841 }, { 207979, 0x353e1c9b }, + { 250000, 0x40000000 }, { 269509, 0x44fe8ab5 }, + { 330441, 0x5497c808 }, { 333038, 0x5541fa76 }, + { 375000, 0x60000000 }, { 394734, 0x650d4995 }, + { 446327, 0x72427c7c }, { 500000, 0x80000000 }, + { 517139, 0x846338b4 }, { 571953, 0x926b8306 }, + { 587353, 0x965cc426 }, { 625000, 0xa0000000 }, + { 692136, 0xb12fd32c }, { 750000, 0xc0000000 }, + { 834068, 0xd5857aff }, { 848454, 0xd9344806 }, + { 854222, 0xdaae4b02 }, { 861465, 0xdc88f862 }, + { 875000, 0xe0000000 }, { 910661, 0xe921144d }, + { 922162, 0xec12cf10 }, { 942190, 0xf1335d25 } +}; + + +// --------------------------------------------------------------------- +// test support stuff - part1 +// --------------------------------------------------------------------- + +TEST_F(timevalTest, Helpers1) { + timeval_wrap x; + + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) { + x.V.tv_usec = -1; + ASSERT_FALSE(x.valid()); + x.V.tv_usec = 0; + ASSERT_TRUE(x.valid()); + x.V.tv_usec = 999999; + ASSERT_TRUE(x.valid()); + x.V.tv_usec = 1000000; + ASSERT_FALSE(x.valid()); + } +} + +//---------------------------------------------------------------------- +// test normalisation +//---------------------------------------------------------------------- + +TEST_F(timevalTest, Normalise) { + for (long ns = -2000000000; ns <= 2000000000; ns += 10000000) { + timeval_wrap x(0, ns); + + x = normalize_tval(x); + ASSERT_TRUE(x.valid()); + } +} + +//---------------------------------------------------------------------- +// test classification +//---------------------------------------------------------------------- + +TEST_F(timevalTest, SignNoFrac) { + // sign test, no fraction + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 0); + int E = (i > 0) - (i < 0); + int r = test_tval(a); + + ASSERT_EQ(E, r); + } +} + +TEST_F(timevalTest, SignWithFrac) { + // sign test, with fraction + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 10); + int E = (i >= 0) - (i < 0); + int r = test_tval(a); + + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// test compare +//---------------------------------------------------------------------- +TEST_F(timevalTest, CmpFracEQ) { + // fractions are equal + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timeval_wrap a(i, 200); + timeval_wrap b(j, 200); + int E = (i > j) - (i < j); + int r = cmp_tval_denorm(a, b); + + ASSERT_EQ(E, r); + } +} + +TEST_F(timevalTest, CmpFracGT) { + // fraction a bigger fraction b + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timeval_wrap a( i , 999800); + timeval_wrap b( j , 200); + int E = (i >= j) - (i < j); + int r = cmp_tval_denorm(a, b); + + ASSERT_EQ(E, r); + } +} + +TEST_F(timevalTest, CmpFracLT) { + // fraction a less fraction b + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timeval_wrap a(i, 200); + timeval_wrap b(j, 999800); + int E = (i > j) - (i <= j); + int r = cmp_tval_denorm(a, b); + + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// Test addition (sum) +//---------------------------------------------------------------------- + +TEST_F(timevalTest, AddFullNorm) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timeval_wrap a(i, 200); + timeval_wrap b(j, 400); + timeval_wrap E(i + j, 200 + 400); + timeval_wrap c; + + c = add_tval(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timevalTest, AddFullOflow1) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timeval_wrap a(i, 200); + timeval_wrap b(j, 999900); + timeval_wrap E(i + j + 1, 100); + timeval_wrap c; + + c = add_tval(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timevalTest, AddUsecNorm) { + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 200); + timeval_wrap E(i, 600); + timeval_wrap c; + + c = add_tval_us(a, 600 - 200); + ASSERT_EQ(E, c); + } +} + +TEST_F(timevalTest, AddUsecOflow1) { + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 200); + timeval_wrap E(i + 1, 100); + timeval_wrap c; + + c = add_tval_us(a, MICROSECONDS - 100); + ASSERT_EQ(E, c); + } +} + +//---------------------------------------------------------------------- +// test subtraction (difference) +//---------------------------------------------------------------------- + +TEST_F(timevalTest, SubFullNorm) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timeval_wrap a(i, 600); + timeval_wrap b(j, 400); + timeval_wrap E(i - j, 600 - 400); + timeval_wrap c; + + c = sub_tval(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timevalTest, SubFullOflow) { + for (int i = -4; i <= 4; ++i) + for (int j = -4; j <= 4; ++j) { + timeval_wrap a(i, 100); + timeval_wrap b(j, 999900); + timeval_wrap E(i - j - 1, 200); + timeval_wrap c; + + c = sub_tval(a, b); + ASSERT_EQ(E, c); + } +} + +TEST_F(timevalTest, SubUsecNorm) { + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 600); + timeval_wrap E(i, 200); + timeval_wrap c; + + c = sub_tval_us(a, 600 - 200); + ASSERT_EQ(E, c); + } +} + +TEST_F(timevalTest, SubUsecOflow) { + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 100); + timeval_wrap E(i - 1, 200); + timeval_wrap c; + + c = sub_tval_us(a, MICROSECONDS - 100); + ASSERT_EQ(E, c); + } +} + +//---------------------------------------------------------------------- +// test negation +//---------------------------------------------------------------------- + +TEST_F(timevalTest, Neg) { + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 100); + timeval_wrap b; + timeval_wrap c; + + b = neg_tval(a); + c = add_tval(a, b); + ASSERT_EQ(0, test_tval(c)); + } +} + +//---------------------------------------------------------------------- +// test abs value +//---------------------------------------------------------------------- + +TEST_F(timevalTest, AbsNoFrac) { + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 0); + timeval_wrap b; + + b = abs_tval(a); + ASSERT_EQ((i != 0), test_tval(b)); + } +} + +TEST_F(timevalTest, AbsWithFrac) { + for (int i = -4; i <= 4; ++i) { + timeval_wrap a(i, 100); + timeval_wrap b; + + b = abs_tval(a); + ASSERT_EQ(1, test_tval(b)); + } +} + +// --------------------------------------------------------------------- +// test support stuff -- part 2 +// --------------------------------------------------------------------- + +TEST_F(timevalTest, Helpers2) { + AssertTimevalClose isClose(0, 2); + timeval_wrap x, y; + + for (x.V.tv_sec = -2; x.V.tv_sec < 3; x.V.tv_sec++) + for (x.V.tv_usec = 1; + x.V.tv_usec < 1000000; + x.V.tv_usec += 499999) { + for (long i = -4; i < 5; i++) { + y = x; + y.V.tv_usec += i; + if (i >= -2 && i <= 2) + ASSERT_PRED_FORMAT2(isClose, x, y); + else + ASSERT_PRED_FORMAT2(!isClose, x, y); + } + } +} + +// and the global predicate instances we're using here +static AssertFpClose FpClose(0, 1); +static AssertTimevalClose TimevalClose(0, 1); + +//---------------------------------------------------------------------- +// conversion to l_fp +//---------------------------------------------------------------------- + +TEST_F(timevalTest, ToLFPbittest) { + for (u_int32 i = 0; i < 1000000; i++) { + timeval_wrap a(1, i); + l_fp_wrap E(1, my_tick_to_tsf(i)); + l_fp_wrap r; + + r = tval_intv_to_lfp(a); + ASSERT_PRED_FORMAT2(FpClose, E, r); + } +} + +TEST_F(timevalTest, ToLFPrelPos) { + for (int i = 0; i < COUNTOF(fdata); i++) { + timeval_wrap a(1, fdata[i].usec); + l_fp_wrap E(1, fdata[i].frac); + l_fp_wrap r; + + r = tval_intv_to_lfp(a); + ASSERT_PRED_FORMAT2(FpClose, E, r); + } +} + +TEST_F(timevalTest, ToLFPrelNeg) { + for (int i = 0; i < COUNTOF(fdata); i++) { + timeval_wrap a(-1, fdata[i].usec); + l_fp_wrap E(~0, fdata[i].frac); + l_fp_wrap r; + + r = tval_intv_to_lfp(a); + ASSERT_PRED_FORMAT2(FpClose, E, r); + } +} + +TEST_F(timevalTest, ToLFPabs) { + for (int i = 0; i < COUNTOF(fdata); i++) { + timeval_wrap a(1, fdata[i].usec); + l_fp_wrap E(1 + JAN_1970, fdata[i].frac); + l_fp_wrap r; + + r = tval_stamp_to_lfp(a); + ASSERT_PRED_FORMAT2(FpClose, E, r); + } +} + +//---------------------------------------------------------------------- +// conversion from l_fp +//---------------------------------------------------------------------- + +TEST_F(timevalTest, FromLFPbittest) { + // Not *exactly* a bittest, because 2**32 tests would take a + // really long time even on very fast machines! So we do test + // every 1000 fractional units. + for (u_int32 tsf = 0; tsf < ~u_int32(1000); tsf += 1000) { + timeval_wrap E(1, my_tsf_to_tick(tsf)); + l_fp_wrap a(1, tsf); + timeval_wrap r; + + r = lfp_intv_to_tval(a); + // The conversion might be off by one microsecond when + // comparing to calculated value. + ASSERT_PRED_FORMAT2(TimevalClose, E, r); + } +} + +TEST_F(timevalTest, FromLFPrelPos) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(1, fdata[i].frac); + timeval_wrap E(1, fdata[i].usec); + timeval_wrap r; + + r = lfp_intv_to_tval(a); + ASSERT_PRED_FORMAT2(TimevalClose, E, r); + } +} + +TEST_F(timevalTest, FromLFPrelNeg) { + for (int i = 0; i < COUNTOF(fdata); i++) { + l_fp_wrap a(~0, fdata[i].frac); + timeval_wrap E(-1, fdata[i].usec); + timeval_wrap r; + + r = lfp_intv_to_tval(a); + ASSERT_PRED_FORMAT2(TimevalClose, E, r); + } +} + +// usec -> frac -> usec roundtrip, using a prime start and increment +TEST_F(timevalTest, LFProundtrip) { + for (int32_t t = -1; t < 2; ++t) + for (u_int32 i = 5; i < 1000000; i+=11) { + timeval_wrap E(t, i); + l_fp_wrap a; + timeval_wrap r; + + a = tval_intv_to_lfp(E); + r = lfp_intv_to_tval(a); + ASSERT_EQ(E, r); + } +} + +//---------------------------------------------------------------------- +// string formatting +//---------------------------------------------------------------------- + +TEST_F(timevalTest, ToString) { + static const struct { + time_t sec; + long usec; + const char * repr; + } data [] = { + { 0, 0, "0.000000" }, + { 2, 0, "2.000000" }, + {-2, 0, "-2.000000" }, + { 0, 1, "0.000001" }, + { 0,-1, "-0.000001" }, + { 1,-1, "0.999999" }, + {-1, 1, "-0.999999" }, + {-1,-1, "-1.000001" }, + }; + for (int i = 0; i < COUNTOF(data); ++i) { + timeval_wrap a(data[i].sec, data[i].usec); + std::string E(data[i].repr); + std::string r(tvaltoa(a)); + + ASSERT_EQ(E, r); + } +} + +// -*- EOF -*- diff --git a/tests/libntp/tstotv.cpp b/tests/libntp/tstotv.cpp new file mode 100644 index 0000000..41f44d7 --- /dev/null +++ b/tests/libntp/tstotv.cpp @@ -0,0 +1,57 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +#include "timevalops.h" +}; + +class tstotvTest : public libntptest { +protected: + ::testing::AssertionResult IsEqual(const timeval& expected, + const timeval& actual) { + if (expected.tv_sec == actual.tv_sec && + expected.tv_usec == actual.tv_usec) { + // Success + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "expected: " << expected.tv_sec << "." + << expected.tv_usec + << " but was: " << actual.tv_sec << "." + << actual.tv_usec; + } + } + + static const u_long HALF = 2147483648UL; +}; + +TEST_F(tstotvTest, Seconds) { + const l_fp input = {50, 0}; // 50.0 s + const timeval expected = {50, 0}; + timeval actual; + + TSTOTV(&input, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(tstotvTest, MicrosecondsExact) { + const l_fp input = {50, HALF}; // 10.5 s + const timeval expected = {50, 500000}; + timeval actual; + + TSTOTV(&input, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); + +} + +TEST_F(tstotvTest, MicrosecondsRounding) { + const l_fp input = {50, 3865471UL}; // Should round to 50.0009 + const timeval expected = {50, 900}; + timeval actual; + + TSTOTV(&input, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); +} diff --git a/tests/libntp/tvtots.cpp b/tests/libntp/tvtots.cpp new file mode 100644 index 0000000..c51b7f5 --- /dev/null +++ b/tests/libntp/tvtots.cpp @@ -0,0 +1,54 @@ +#include "lfptest.h" + +extern "C" { +#include "timevalops.h" +}; + +// Required on Solaris for ldexp. +#include <math.h> + +class tvtotsTest : public lfptest { +}; + +TEST_F(tvtotsTest, Seconds) { + timeval input = {500, 0}; // 500.0 s + l_fp expected = {500, 0}; + l_fp actual; + + TVTOTS(&input, &actual); + + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(tvtotsTest, MicrosecondsRounded) { + /* 0.0005 can not be represented exact in a l_fp structure. + * It would equal to 2147483,648. This means that + * HALF_PROMILLE_UP (which is 2147484) should be + * the correct rounding. */ + + timeval input = {0, 500}; // 0.0005 exact + l_fp expected = {0, HALF_PROMILLE_UP}; + l_fp actual; + + TVTOTS(&input, &actual); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(tvtotsTest, MicrosecondsExact) { + // 0.5 can be represented exact in both l_fp and timeval. + const timeval input = {10, 500000}; // 0.5 exact + const l_fp expected = {10, HALF}; // 0.5 exact + l_fp actual; + + TVTOTS(&input, &actual); + + // Compare the fractional part with an absolute error given. + EXPECT_EQ(expected.l_ui, actual.l_ui); + + double expectedDouble, actualDouble; + M_LFPTOD(0, expected.l_uf, expectedDouble); + M_LFPTOD(0, actual.l_uf, actualDouble); + + // The error should be less than 0.5 us + EXPECT_NEAR(expectedDouble, actualDouble, 0.0000005); +} diff --git a/tests/libntp/uglydate.cpp b/tests/libntp/uglydate.cpp new file mode 100644 index 0000000..6d9e6c6 --- /dev/null +++ b/tests/libntp/uglydate.cpp @@ -0,0 +1,18 @@ +#include "libntptest.h" + +extern "C" { +#include "ntp_fp.h" +}; + +class uglydateTest : public libntptest { +protected: + static const u_int32 HALF = 2147483648UL; +}; + +TEST_F(uglydateTest, ConstantDateTime) { + l_fp time = {3485080800UL, HALF}; // 2010-06-09 14:00:00.5 + + EXPECT_STREQ("3485080800.500000 10:159:14:00:00.500", + uglydate(&time)); +} + diff --git a/tests/libntp/vi64ops.cpp b/tests/libntp/vi64ops.cpp new file mode 100644 index 0000000..7b839ab --- /dev/null +++ b/tests/libntp/vi64ops.cpp @@ -0,0 +1,64 @@ +#include "libntptest.h" + +extern "C" { +#include "vint64ops.h" +} + +class vi64Test : public libntptest { +public: + ::testing::AssertionResult IsEqual(const vint64 &expected, const vint64 &actual) { + if (0 == memcmp(&expected, &actual, sizeof(vint64))) { + return ::testing::AssertionSuccess(); + } else { + return ::testing::AssertionFailure() + << "expected: " + << std::hex << expected.D_s.hi << '.' + << std::hex << expected.D_s.lo + << " but was " + << std::hex << actual.D_s.hi << '.' + << std::hex << actual.D_s.lo; + } + } +}; + +// ---------------------------------------------------------------------- +// test number parser +TEST_F(vi64Test, ParseVUI64_pos) { + vint64 act, exp; + const char *sp; + char *ep; + + sp = "1234x"; + exp.D_s.hi = 0; + exp.D_s.lo = 1234; + act = strtouv64(sp, &ep, 0); + EXPECT_TRUE(IsEqual(exp, act)); + EXPECT_EQ(*ep, 'x'); +} + +TEST_F(vi64Test, ParseVUI64_neg) { + vint64 act, exp; + const char *sp; + char *ep; + + sp = "-1234x"; + exp.D_s.hi = ~0; + exp.D_s.lo = -1234; + act = strtouv64(sp, &ep, 0); + EXPECT_TRUE(IsEqual(exp, act)); + EXPECT_EQ(*ep, 'x'); +} + +TEST_F(vi64Test, ParseVUI64_case) { + vint64 act, exp; + const char *sp; + char *ep; + + sp = "0123456789AbCdEf"; + exp.D_s.hi = 0x01234567; + exp.D_s.lo = 0x89ABCDEF; + act = strtouv64(sp, &ep, 16); + EXPECT_TRUE(IsEqual(exp, act)); + EXPECT_EQ(*ep, '\0'); +} + diff --git a/tests/libntp/ymd2yd.cpp b/tests/libntp/ymd2yd.cpp new file mode 100644 index 0000000..e6f46f8 --- /dev/null +++ b/tests/libntp/ymd2yd.cpp @@ -0,0 +1,23 @@ +#include "libntptest.h" + +class ymd2ydTest : public libntptest { +}; + +TEST_F(ymd2ydTest, NonLeapYearFebruary) { + EXPECT_EQ(31+20, ymd2yd(2010,2,20)); //2010-02-20 +} + +TEST_F(ymd2ydTest, NonLeapYearJune) { + int expected = 31+28+31+30+31+18; // 18 June non-leap year + EXPECT_EQ(expected, ymd2yd(2011,6,18)); +} + +TEST_F(ymd2ydTest, LeapYearFebruary) { + EXPECT_EQ(31+20, ymd2yd(2012,2,20)); //2012-02-20 (leap year) +} + +TEST_F(ymd2ydTest, LeapYearDecember) { + // 2012-12-31 + int expected = 31+29+31+30+31+30+31+31+30+31+30+31; + EXPECT_EQ(expected, ymd2yd(2012,12,31)); +} |