From 689352773dbb5cfe336051db2a5e4167cd7479f8 Mon Sep 17 00:00:00 2001 From: Vincent Blut Date: Thu, 2 Dec 2021 14:18:11 +0100 Subject: New upstream version 4.2~pre1 --- test/compilation/002-scanbuild | 3 +- test/compilation/003-sanitizers | 42 ++++--- test/simulation/008-ntpera | 58 +++++---- test/simulation/110-chronyc | 258 +++++++++++++++++++++++++++++++++++++++- test/simulation/122-xleave | 34 ++++++ test/simulation/142-ptpport | 41 +++++++ test/simulation/143-manual | 70 +++++++++++ test/simulation/144-exp1 | 55 +++++++++ test/simulation/test.common | 18 ++- test/system/007-cmdmon | 5 +- test/unit/clientlog.c | 206 +++++++++++++++++++++++++++++++- test/unit/hash.c | 9 +- test/unit/ntp_auth.c | 14 +-- test/unit/ntp_core.c | 93 ++++++++++++++- test/unit/test.c | 2 +- test/unit/util.c | 34 +++++- 16 files changed, 872 insertions(+), 70 deletions(-) create mode 100755 test/simulation/142-ptpport create mode 100755 test/simulation/143-manual create mode 100755 test/simulation/144-exp1 (limited to 'test') diff --git a/test/compilation/002-scanbuild b/test/compilation/002-scanbuild index da87407..35a3faf 100755 --- a/test/compilation/002-scanbuild +++ b/test/compilation/002-scanbuild @@ -8,7 +8,8 @@ for opts in \ "--host-system=FreeBSD" \ "--without-nettle" \ "--without-nettle --without-nss" \ - "--without-nettle --without-nss --without-tomcrypt" + "--without-nettle --without-nss --without-tomcrypt" \ + "--without-nettle --without-nss --without-tomcrypt --without-gnutls" do ./configure $opts scan-build make "$@" || exit 1 diff --git a/test/compilation/003-sanitizers b/test/compilation/003-sanitizers index 1508295..2cf5258 100755 --- a/test/compilation/003-sanitizers +++ b/test/compilation/003-sanitizers @@ -13,16 +13,23 @@ fi if [ "$ID" = "fedora" ]; then echo Checking test dependencies: - rpm -q {valgrind,gcc,clang}.x86_64 {libgcc,clang-libs}.{x86_64,i686} || exit 1 + rpm -q {gcc,clang}.x86_64 {valgrind,libgcc,clang-libs}.{x86_64,i686} || exit 1 rpm -q {libseccomp,nettle,nss-softokn-freebl,libtomcrypt,gnutls}-devel.{x86_64,i686} || exit 1 echo fi touch Makefile -for CC in gcc clang; do - export CC - +for extra_config_opts in \ + "--all-privops" \ + "--disable-ipv6" \ + "--disable-scfilter" \ + "--without-gnutls" \ + "--without-nettle" \ + "--without-nettle --without-nss" \ + "--without-nettle --without-nss --without-tomcrypt" \ + "--without-nettle --without-nss --without-tomcrypt --without-gnutls"; \ +do for arch_opts in "-m32" ""; do pushd test/simulation/clknetsim || exit 1 make clean > /dev/null 2>&1 @@ -31,26 +38,21 @@ for CC in gcc clang; do popd - for extra_config_opts in \ - "--all-privops" \ - "--disable-scfilter" \ - "--without-gnutls" \ - "--without-nettle" \ - "--without-nettle --without-nss" \ - "--without-nettle --without-nss --without-tomcrypt"; \ - do + for CC in gcc clang; do + export CC + for san_options in "" "-fsanitize=address" "-fsanitize=memory"; do export CFLAGS="-O2 -g -fsanitize=undefined -fsanitize=float-divide-by-zero -fno-sanitize-recover=undefined,float-divide-by-zero $san_options $arch_opts" # clang msan doesn't work on i686 and otherwise requires patches echo $CFLAGS | grep -q 'sanitize=memory' && continue - # build fails with clang ubsan on i686 (Fedora only?) - [ "$arch_opts" = "-m32" -a "$CC" = "clang" ] && continue + [ -n "$TEST_NO_M32_CLANG" -a "$arch_opts" = "-m32" -a "$CC" = "clang" ] && continue - [ "$CC" = "gcc" ] && echo $CFLAGS | grep -q 'sanitize=address' && CFLAGS="$CFLAGS -static-libasan" + [ -n "$TEST_GCC_STATIC_ASAN" -a "$CC" = "gcc" ] && + echo $CFLAGS | grep -q 'sanitize=address' && CFLAGS="$CFLAGS -static-libasan" - config_opts="--with-user=chrony --enable-debug --enable-scfilter --enable-ntp-signd $extra_config_opts" + config_opts="--with-user=chrony --with-ntp-era=1000000000 --enable-debug --enable-scfilter --enable-ntp-signd $extra_config_opts" echo ----------------------------------------------------------------------------- echo CC=\"$CC\" CFLAGS=\"$CFLAGS\" ./configure $config_opts @@ -67,10 +69,11 @@ for CC in gcc clang; do make "$@" || exit 1 - [ -n "$BUILD_TEST_ONLY" ] && continue + [ -n "$TEST_BUILD_ONLY" ] && continue echo pushd test/unit || exit 1 + make "$@" || exit 1 if [ "$san_options" = "" ]; then make check TEST_WRAPPER="valgrind --error-exitcode=1" || exit 1 else @@ -78,11 +81,16 @@ for CC in gcc clang; do fi popd + [ -n "$TEST_UNIT_ONLY" ] && continue + echo pushd test/simulation || exit 1 export CLKNETSIM_RANDOM_SEED=101 if [ "$arch_opts" = "" -a "$san_options" = "" ]; then CLKNETSIM_CLIENT_WRAPPER=valgrind ./run -i 1 || exit 1 + elif [ "$CC" = "gcc" ] && ! echo $CFLAGS | grep -q "-static-libasan"; then + libasan=$(ldd ../../chronyd | grep -o '/.*lib.*/libasan.so.[0-9]') + CLKNETSIM_PRELOAD=$libasan ./run -i 1 || exit 1 else ./run -i 1 || exit 1 fi diff --git a/test/simulation/008-ntpera b/test/simulation/008-ntpera index 637ad9e..2a4f332 100755 --- a/test/simulation/008-ntpera +++ b/test/simulation/008-ntpera @@ -3,38 +3,48 @@ . ./test.common test_start "NTP eras" -# Assume NTP_ERA_SPLIT is between years 1960 and 1990 - -# Set date to 500 seconds before NTP second overflows, this should -# work correctly with both 32-bit and 64-bit time_t +if check_config_h 'HAVE_LONG_TIME_T 1'; then + ntp_start=$(awk "BEGIN {print $(grep NTP_ERA_SPLIT ../../config.h | tr -dc '0-9*+-')}") +else + ntp_start="-2147483648" +fi + +# Set the starting test date to 500 seconds before the second NTP era. +# This should work with 32-bit time_t and also with 64-bit time_t if the +# configured NTP interval covers the test interval. export CLKNETSIM_START_DATE=$(date -d 'Feb 7 06:19:56 UTC 2036' +'%s') -run_test || test_fail -check_chronyd_exit || test_fail -check_source_selection || test_fail -check_packet_interval || test_fail -check_sync || test_fail - -# The following tests need 64-bit time_t -check_config_h 'HAVE_LONG_TIME_T 1' || test_skip - -for year in 1990 2090; do - export CLKNETSIM_START_DATE=$(date -d "Jan 1 00:00:00 UTC $year" +'%s') +if awk "BEGIN {exit !($ntp_start <= $CLKNETSIM_START_DATE && \ + $CLKNETSIM_START_DATE + $limit < $ntp_start + 2^32)}"; then run_test || test_fail check_chronyd_exit || test_fail check_source_selection || test_fail check_packet_interval || test_fail check_sync || test_fail -done +fi -for year in 1950 2130; do - export CLKNETSIM_START_DATE=$(date -d "Jan 1 00:00:00 UTC $year" +'%s') - run_test || test_fail - check_chronyd_exit || test_fail - check_source_selection || test_fail - check_packet_interval || test_fail - # This check is expected to fail - check_sync && test_fail +# The following tests need 64-bit time_t and ntp_start not before 1970 +check_config_h 'HAVE_LONG_TIME_T 1' || test_skip +echo "$ntp_start" | grep -q '-' && test_skip + +for time_offset in -1e-1 1e-1; do + for start_offset in 0 "2^32 - $limit"; do + export CLKNETSIM_START_DATE=$(awk "BEGIN {print $ntp_start + $start_offset}") + run_test || test_fail + check_chronyd_exit || test_fail + check_source_selection || test_fail + check_packet_interval || test_fail + check_sync || test_fail + done + + for start_offset in -$limit "2^32"; do + export CLKNETSIM_START_DATE=$(awk "BEGIN {print $ntp_start + $start_offset}") + run_test || test_fail + check_chronyd_exit || test_fail + check_source_selection || test_fail + check_packet_interval || test_fail + check_sync && test_fail + done done test_pass diff --git a/test/simulation/110-chronyc b/test/simulation/110-chronyc index e8d6e21..6693d4d 100755 --- a/test/simulation/110-chronyc +++ b/test/simulation/110-chronyc @@ -14,6 +14,7 @@ server 192.168.123.2" client_conf=" refclock SHM 0 noselect smoothtime 400 0.001 leaponly" +cmdmon_unix=0 chronyc_conf="activity tracking @@ -92,7 +93,7 @@ check_chronyc_output "^C0A87B01,192\.168\.123\.1,2,12623049..\..........,-?0\.00 chronyc_options="" server_strata=0 -chronyc_start=0 +chronyc_start=0.5 client_server_conf="" client_conf="" server_conf="server 192.168.123.1" @@ -101,16 +102,14 @@ limit=1 for chronyc_conf in \ "accheck 1.2.3.4" \ "add peer 10.0.0.0 minpoll 2 maxpoll 6" \ - "add server 10.0.0.0 minpoll 6 maxpoll 10 iburst burst key 1 certset 2 maxdelay 1e-3 maxdelayratio 10.0 maxdelaydevratio 10.0 mindelay 1e-4 asymmetry 0.5 offset 1e-5 minsamples 6 maxsamples 6 filter 3 offline auto_offline prefer noselect trust require xleave polltarget 20 port 123 presend 7 minstratum 3 version 4 nts ntsport 4460 copy" \ + "add server 10.0.0.0 minpoll 6 maxpoll 10 iburst burst key 1 certset 2 maxdelay 1e-3 maxdelayratio 10.0 maxdelaydevratio 10.0 mindelay 1e-4 asymmetry 0.5 offset 1e-5 minsamples 6 maxsamples 6 filter 3 offline auto_offline prefer noselect trust require xleave polltarget 20 port 123 presend 7 minstratum 3 version 4 nts ntsport 4460 copy extfield F323" \ "add server node1.net1.clk" \ "allow 1.2.3.4" \ "allow 1.2" \ "allow 3.4.5" \ "allow 6.7.8/22" \ "allow 6.7.8.9/22" \ - "allow 2001:db8::/32" \ "allow 0/0" \ - "allow ::/0" \ "allow" \ "allow all 10/24" \ "authdata" \ @@ -180,6 +179,257 @@ do check_chronyc_output "501 Not authorised$" || test_fail done +cmdmon_unix=1 + +chronyc_conf=" +authdata +clients -k -p 2 +clients -r +clients +ntpdata +selectdata +serverstats" + +run_test || test_fail +check_chronyd_exit || test_fail + +check_chronyc_output "^Name/IP address Mode KeyID Type KLen Last Atmp NAK Cook CLen +========================================================================= +node1\.net1\.clk - 0 0 0 - 0 0 0 0 +Hostname NTP Drop Int IntL Last NTS-KE Drop Int Last +=============================================================================== +Hostname NTP Drop Int IntL Last Cmd Drop Int Last +=============================================================================== +node1\.net1\.clk 1 0 - - 0 0 0 - - +Hostname NTP Drop Int IntL Last Cmd Drop Int Last +=============================================================================== +node1\.net1\.clk 0 0 - - 0 0 0 - - + +Remote address : 192\.168\.123\.1 \(C0A87B01\) +Remote port : 123 +Local address : 192\.168\.123\.1 \(C0A87B01\) +Leap status : Normal +Version : 4 +Mode : Server +Stratum : 1 +Poll interval : 6 \(64 seconds\) +Precision : -23 \(0\.000000119 seconds\) +Root delay : 0\.000000 seconds +Root dispersion : 0\.000000 seconds +Reference ID : 7F7F0101 \(\) +Reference time : Thu Dec 31 23:59:5[89] 2009 +Offset : [-+]0\.000...... seconds +Peer delay : 0\.00....... seconds +Peer dispersion : 0\.00000.... seconds +Response time : 0\.000000... seconds +Jitter asymmetry: \+0\.00 +NTP tests : 111 111 1110 +Interleaved : No +Authenticated : No +TX timestamping : Kernel +RX timestamping : Kernel +Total TX : 1 +Total RX : 1 +Total valid RX : 1 +S Name/IP Address Auth COpts EOpts Last Score Interval Leap +======================================================================= +M node1\.net1\.clk N ----- ----- 0 1\.0 \+0ns \+0ns N +NTP packets received : 1 +NTP packets dropped : 0 +Command packets received : 12 +Command packets dropped : 0 +Client log records dropped : 0 +NTS-KE connections accepted: 0 +NTS-KE connections dropped : 0 +Authenticated NTP packets : 0 +Interleaved NTP packets : 0 +NTP timestamps held : 0 +NTP timestamp span : 0$" || test_fail + +chronyc_conf=" +deny all +cmdallow all +allow 1.2.3.4 +allow 1.2.3.0/28 +deny 1.2.3.0/27 +allow 1.2.4.5 +deny all 1.2.4.0/27 +cmddeny 5.6.7.8 +cmdallow all 5.6.7.0/28 +accheck 1.2.3.4 +accheck 1.2.3.5 +accheck 1.2.4.5 +cmdaccheck 5.6.7.8" + +run_test || test_fail +check_chronyd_exit || test_fail + +check_chronyc_output "^200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +208 Access allowed +209 Access denied +209 Access denied +208 Access allowed$" || test_fail + +if check_config_h 'FEAT_IPV6 1'; then + chronyc_conf=" + deny all + cmdallow all + allow 2001:db8::1 + allow 2001:db8::/64 + deny 2001:db8::/63 + allow 2001:db8:1::1 + deny all 2001:db8:1::/63 + cmddeny 2001:db9::1 + cmdallow all 2001:db9::/64 + accheck 2001:db8::1 + accheck 2001:db8::2 + accheck 2001:db8:1::1 + cmdaccheck 2001:db9::1" + + run_test || test_fail + check_chronyd_exit || test_fail + + check_chronyc_output "^200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +208 Access allowed +209 Access denied +209 Access denied +208 Access allowed$" || test_fail +fi + +chronyc_conf=" +delete 192.168.123.1 +add server node1.net1.clk minpoll 6 maxpoll 10 iburst +offline 192.168.123.1 +burst 1/1 192.168.123.1 +online 192.168.123.1 +maxdelay 192.168.123.1 1e-2 +maxdelaydevratio 192.168.123.1 5.0 +maxdelayratio 192.168.123.1 3.0 +maxpoll 192.168.123.1 5 +maxupdateskew 192.168.123.1 10.0 +minpoll 192.168.123.1 3 +minstratum 192.168.123.1 1 +polltarget 192.168.123.1 10 +delete 192.168.123.1" + +run_test || test_fail +check_chronyd_exit || test_fail + +check_chronyc_output "^200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK$" || test_fail + +chronyc_conf=" +cyclelogs +dump +dfreq 1.0e-3 +doffset -0.01 +local stratum 5 distance 1.0 orphan +local off +makestep 10.0 3 +makestep +manual on +settime now +manual delete 0 +manual reset +manual off +onoffline +refresh +rekey +reload sources +reselect +reselectdist 1e-3 +reset sources +shutdown" + +run_test || test_fail +check_chronyd_exit || test_fail + +check_chronyc_output "^200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +Clock was .\... seconds fast. Frequency change = 0.00ppm, new frequency = 0.00ppm +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK +200 OK$" || test_fail + +server_conf=" +server 192.168.123.1 +noclientlog" + +commands=( + "add server nosuchnode.net1.clk" "^Invalid host/IP address$" + "allow nosuchnode.net1.clk" "^Could not read address$" + "allow 192.168.123.0/2 4" "^Could not read address$" + "allow 192.168.123.0/2e" "^Could not read address$" + "allow 192.168.12e" "^Could not read address$" + "allow 192.168123" "^Could not read address$" + "allow 192.168.123.2/33" "^507 Bad subnet$" + "clients" "Hostname.*519 Client logging is not active in the daemon$" + "delete 192.168.123.2" "^503 No such source$" + "minpoll 192.168.123.2 5" "^503 No such source$" + "ntpdata 192.168.123.2" "^503 No such source$" + "settime now" "^505 Facility not enabled in daemon$" + "smoothing" "^505 Facility not enabled in daemon$" + "smoothtime activate" "^505 Facility not enabled in daemon$" + "smoothtime reset" "^505 Facility not enabled in daemon$" + "sourcename 192.168.123.2" "^503 No such source$" + "trimrtc" "^513 RTC driver not running$" + "writertc" "^513 RTC driver not running$" +) + +for i in $(seq 0 $[${#commands[*]} / 2]); do + chronyc_conf=${commands[$[i * 2]]} + run_test || test_fail + check_chronyd_exit || test_fail + check_chronyc_output "${commands[$[i * 2 + 1]]}" || test_fail +done + +cmdmon_unix=0 +server_conf="server 192.168.123.1" + chronyc_conf="dns -n dns +n dns -4 diff --git a/test/simulation/122-xleave b/test/simulation/122-xleave index f137f19..bc4c3a7 100755 --- a/test/simulation/122-xleave +++ b/test/simulation/122-xleave @@ -8,6 +8,18 @@ client_conf=" logdir tmp log rawmeasurements" +server_conf="noclientlog" + +run_test || test_fail +check_chronyd_exit || test_fail +check_source_selection || test_fail +check_sync || test_fail + +check_file_messages "111 111 1111.* 4I [DKH] [DKH]\$" 0 0 measurements.log || test_fail +rm -f tmp/measurements.log + +server_conf="" + run_test || test_fail check_chronyd_exit || test_fail check_source_selection || test_fail @@ -52,4 +64,26 @@ for rpoll in 4 5 6; do rm -f tmp/measurements.log done +if check_config_h 'FEAT_CMDMON 1'; then + # test client timestamp selection and server timestamp correction + base_delay="(+ 1.25e-6 (* -1 (equal 0.1 from 5)))" + jitter=1e-9 + wander=1e-12 + client_lpeer_options="xleave minpoll 5 maxpoll 5 noselect" + client_rpeer_options="xleave minpoll 5 maxpoll 5 noselect" + chronyc_conf="doffset -0.1" + chronyc_start=7200 + + run_test || test_fail + check_chronyd_exit || test_fail + check_source_selection || test_fail + check_sync && test_fail + + check_file_messages "\.2 N 2 111 111 .... 5 5 .\... ..\....e-.. 2\....e-06" \ + 290 310 measurements.log || test_fail + check_file_messages "\.2 N 2 111 111 .... 5 5 .\... ..\....e-.. .\....e-0[0123]" \ + 0 0 measurements.log || test_fail + rm -f tmp/measurements.log +fi + test_pass diff --git a/test/simulation/142-ptpport b/test/simulation/142-ptpport new file mode 100755 index 0000000..060932c --- /dev/null +++ b/test/simulation/142-ptpport @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +. ./test.common + +test_start "PTP port" + +# Block communication between 3 and 1 +base_delay="(+ 1e-4 (* -1 (equal 0.1 from 3) (equal 0.1 to 1)))" + +cat > tmp/peer.keys <<-EOF +1 MD5 1234567890 +EOF + +clients=2 +peers=2 +max_sync_time=420 + +server_conf=" +ptpport 319" +client_conf=" +ptpport 319 +authselectmode ignore +keyfile tmp/peer.keys" +client_server_options="minpoll 6 maxpoll 6 port 319" +client_peer_options="minpoll 6 maxpoll 6 port 319 key 1" + +run_test || test_fail +check_chronyd_exit || test_fail +check_source_selection || test_fail +check_sync || test_fail + +check_file_messages " 2 1 .* 319 319 1 96 " 150 160 \ + log.packets || test_fail +check_file_messages " 1 2 .* 319 319 1 96 " 150 160 \ + log.packets || test_fail +check_file_messages " 2 3 .* 319 319 1 116 " 150 160 \ + log.packets || test_fail +check_file_messages " 3 2 .* 319 319 1 116 " 150 160 \ + log.packets || test_fail + +test_pass diff --git a/test/simulation/143-manual b/test/simulation/143-manual new file mode 100755 index 0000000..618cee6 --- /dev/null +++ b/test/simulation/143-manual @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +. ./test.common + +export TZ=UTC + +test_start "manual input" + +check_config_h 'FEAT_CMDMON 1' || test_skip + +limit=$[12 * 3600] +client_server_conf=" " +client_conf="manual" +chronyc_conf="timeout 4000000 +settime 1:00:00 +settime 2:00:00 +settime 3:00:00 +settime 4:00:00 +manual delete 2 +settime 6:00:00 +manual list +settime 8:00:00 +manual reset +settime 10:00:00 +manual list" +chronyc_start=1800 +base_delay=1800 +jitter=1e-6 + +time_max_limit=4e-3 +freq_max_limit=4e-3 +time_rms_limit=2e-3 +freq_rms_limit=2e-5 +min_sync_time=7204 +max_sync_time=7206 + +run_test || test_fail +check_chronyd_exit || test_fail +check_sync || test_fail + +check_chronyc_output "^200 OK +Clock was 0\.4. seconds fast\. Frequency change = 0\.00ppm, new frequency = 0\.00ppm +200 OK +Clock was 0\.3. seconds fast\. Frequency change = (99|100)\...ppm, new frequency = (99|100)\...ppm +200 OK +Clock was \-?0\.0. seconds fast\. Frequency change = \-?0\.[01].ppm, new frequency = (99|100)\...ppm +200 OK +Clock was \-?0\.0. seconds fast\. Frequency change = \-?0\.[01].ppm, new frequency = (99|100)\...ppm +200 OK +200 OK +Clock was \-?0\.0. seconds fast\. Frequency change = \-?0\.[012].ppm, new frequency = (99|100)\...ppm +210 n_samples = 4 +# Date Time\(UTC\) Slewed Original Residual +======================================================= + 0 2010-01-01 (00:59:59|01:00:00) [- ]0\.00 0\.46 [- ]0\.00 + 1 2010-01-01 (01:59:59|02:00:00) [- ]0\.00 0\.36 [- ]0\.00 + 2 2010-01-01 (03:59:59|04:00:00) [- ]0\.00 [- ]0\.00 [- ]0\.00 + 3 2010-01-01 (05:59:59|06:00:00) [- ]0\.00 [- ]0\.00 [- ]0\.00 +200 OK +Clock was \-?0\.0. seconds fast\. Frequency change = \-?0\.[012].ppm, new frequency = (99|100)\...ppm +200 OK +200 OK +Clock was \-?0\.0. seconds fast\. Frequency change = \-?0\.00ppm, new frequency = (99|100)\...ppm +210 n_samples = 1 +# Date Time\(UTC\) Slewed Original Residual +======================================================= + 0 2010-01-01 (09:59:59|10:00:00) [- ]0\.00 [- ]0\.00 [- ]0\.00$" \ + || test_fail + +test_pass diff --git a/test/simulation/144-exp1 b/test/simulation/144-exp1 new file mode 100755 index 0000000..4a2042d --- /dev/null +++ b/test/simulation/144-exp1 @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +. ./test.common + +test_start "experimental extension field" + +check_config_h 'FEAT_CMDMON 1' || test_skip + +primary_time_offset=0.1 +server_strata=4 +min_sync_time=2000 +max_sync_time=2300 +chronyc_conf="doffset 0.1" +chronyc_options="-h /clknetsim/unix/1:1" +chronyc_start=2000 + +for options in "extfield F323" "xleave extfield F323"; do + client_server_options="minpoll 6 maxpoll 6 $options" + server_server_options="$client_server_options" + + run_test || test_fail + check_chronyd_exit || test_fail + check_source_selection || test_fail + check_sync || test_fail +done + +server_server_options="" +server_strata=1 +clients=4 +peers=4 +max_sync_time=2400 +# chain of peers and one enabled chronyc +base_delay=$(cat <<-EOF | tr -d '\n' + (+ 1e-4 -1 + (equal 0.1 from (+ to 1)) + (equal 0.1 from (+ to -1)) + (equal 0.1 from 6) + (equal 0.1 to 6)) +EOF +) + +for lpoll in 5 6 7; do + for options in "minsamples 16 extfield F323" "minsamples 16 xleave extfield F323"; do + client_lpeer_options="minpoll $lpoll maxpoll $lpoll $options" + client_rpeer_options="minpoll 6 maxpoll 6 $options" + client_server_options="$client_rpeer_options" + + run_test || test_fail + check_chronyd_exit || test_fail + check_source_selection || test_fail + check_sync || test_fail + done +done + +test_pass diff --git a/test/simulation/test.common b/test/simulation/test.common index 9d85a5c..70bbde1 100644 --- a/test/simulation/test.common +++ b/test/simulation/test.common @@ -27,6 +27,7 @@ fi # Default test testings default_limit=10000 +default_primary_time_offset=0.0 default_time_offset=1e-1 default_freq_offset=1e-4 default_base_delay=1e-4 @@ -76,6 +77,7 @@ default_max_sync_time=210 default_client_min_mean_out_interval=0.0 default_client_max_min_out_interval=inf +default_cmdmon_unix=1 default_dns=0 # Initialize test settings from their defaults @@ -434,6 +436,7 @@ run_simulation() { test_message 2 0 "running simulation:" start_server $nodes \ + -n 2 \ -o tmp/log.offset -f tmp/log.freq -p tmp/log.packets \ -R $(awk "BEGIN {print $update_interval < 0 ? 2^-($update_interval) : 1}") \ -r $(awk "BEGIN {print $max_sync_time * 2^$update_interval}") \ @@ -449,6 +452,8 @@ run_test() { nodes=$(get_chronyd_nodes) [ -n "$chronyc_conf" ] && nodes=$[$nodes + $clients] + export CLKNETSIM_UNIX_SUBNET=$[$cmdmon_unix != 0 ? 2 : 0] + for i in $(seq 1 $nodes); do echo "node${i}_shift_pll = $shift_pll" for j in $(seq 1 $nodes); do @@ -468,7 +473,8 @@ run_test() { step=$server_step start=$server_start freq="" - [ $i -le $falsetickers ] && offset=$i.0 || offset=0.0 + [ $i -le $falsetickers ] && + offset=$i.0 || offset=$primary_time_offset options=$server_chronyd_options elif [ $stratum -le $server_strata ]; then step=$server_step @@ -503,9 +509,15 @@ run_test() { for i in $(seq 1 $[$nodes - $node + 1]); do test_message 2 0 "starting node $node:" + options=$([ $dns -eq 0 ] && printf "%s" "-n") + if [ $cmdmon_unix -ne 0 ]; then + options+=" -h /clknetsim/unix/$[$node - $clients]:1" + else + options+=" -h $(get_node_name $[$node - $clients])" + fi + echo "node${node}_start = $chronyc_start" >> tmp/conf - start_client $node chronyc "$chronyc_conf" "" \ - "$([ $dns -eq 0 ] && printf "%s" "-n") -h $(get_node_name $[$node - $clients]) $chronyc_options" && \ + start_client $node chronyc "$chronyc_conf" "" "$options $chronyc_options" && \ test_ok || test_error [ $? -ne 0 ] && return 1 diff --git a/test/system/007-cmdmon b/test/system/007-cmdmon index fe2b006..bbcc12f 100755 --- a/test/system/007-cmdmon +++ b/test/system/007-cmdmon @@ -110,7 +110,10 @@ Command packets dropped : 0 Client log records dropped : 0 NTS-KE connections accepted: 0 NTS-KE connections dropped : 0 -Authenticated NTP packets : 0$" || test_fail +Authenticated NTP packets : 0 +Interleaved NTP packets : 0 +NTP timestamps held : 0 +NTP timestamp span : 0$"|| test_fail run_chronyc "manual on" || test_fail check_chronyc_output "^200 OK$" || test_fail diff --git a/test/unit/clientlog.c b/test/unit/clientlog.c index 850cedf..2a9a588 100644 --- a/test/unit/clientlog.c +++ b/test/unit/clientlog.c @@ -25,15 +25,24 @@ #include +static uint64_t +get_random64(void) +{ + return ((uint64_t)random() << 40) ^ ((uint64_t)random() << 20) ^ random(); +} + void test_unit(void) { - int i, j, index; + uint64_t ts64, prev_first_ts64, prev_last_ts64, max_step; + uint32_t index2, prev_first, prev_size; + struct timespec ts, ts2; + int i, j, k, index, shift; CLG_Service s; - struct timespec ts; + NTP_int64 ntp_ts; IPAddr ip; char conf[][100] = { - "clientloglimit 10000", + "clientloglimit 20000", "ratelimit interval 3 burst 4 leak 3", "cmdratelimit interval 3 burst 4 leak 3", "ntsratelimit interval 6 burst 8 leak 3", @@ -43,6 +52,7 @@ test_unit(void) for (i = 0; i < sizeof conf / sizeof conf[0]; i++) CNF_ParseLine(NULL, i + 1, conf[i]); + LCL_Initialise(); CLG_Initialise(); TEST_CHECK(ARR_GetSize(records) == 16); @@ -67,7 +77,7 @@ test_unit(void) } DEBUG_LOG("records %u", ARR_GetSize(records)); - TEST_CHECK(ARR_GetSize(records) == 64); + TEST_CHECK(ARR_GetSize(records) == 128); s = CLG_NTP; @@ -82,7 +92,195 @@ test_unit(void) DEBUG_LOG("requests %d responses %d", i, j); TEST_CHECK(j * 4 < i && j * 6 > i); + TEST_CHECK(!ntp_ts_map.timestamps); + + UTI_ZeroNtp64(&ntp_ts); + CLG_SaveNtpTimestamps(&ntp_ts, NULL); + TEST_CHECK(ntp_ts_map.timestamps); + TEST_CHECK(ntp_ts_map.first == 0); + TEST_CHECK(ntp_ts_map.size == 0); + TEST_CHECK(ntp_ts_map.max_size == 128); + TEST_CHECK(ARR_GetSize(ntp_ts_map.timestamps) == ntp_ts_map.max_size); + + TEST_CHECK(ntp_ts_map.max_size > NTPTS_INSERT_LIMIT); + + for (i = 0; i < 200; i++) { + DEBUG_LOG("iteration %d", i); + + max_step = (1ULL << (i % 50)); + ts64 = 0ULL - 100 * max_step; + + if (i > 150) + ntp_ts_map.max_size = 1U << (i % 8); + assert(ntp_ts_map.max_size <= 128); + ntp_ts_map.first = i % ntp_ts_map.max_size; + ntp_ts_map.size = 0; + ntp_ts_map.cached_rx_ts = 0ULL; + ntp_ts_map.slew_epoch = i * 400; + + for (j = 0; j < 500; j++) { + do { + ts64 += get_random64() % max_step + 1; + } while (ts64 == 0ULL); + + int64_to_ntp64(ts64, &ntp_ts); + + if (random() % 10) { + UTI_Ntp64ToTimespec(&ntp_ts, &ts); + UTI_AddDoubleToTimespec(&ts, TST_GetRandomDouble(-1.999, 1.999), &ts); + } else { + UTI_ZeroTimespec(&ts); + } + + CLG_SaveNtpTimestamps(&ntp_ts, + UTI_IsZeroTimespec(&ts) ? (random() % 2 ? &ts : NULL) : &ts); + + if (j < ntp_ts_map.max_size) { + TEST_CHECK(ntp_ts_map.size == j + 1); + TEST_CHECK(ntp_ts_map.first == i % ntp_ts_map.max_size); + } else { + TEST_CHECK(ntp_ts_map.size == ntp_ts_map.max_size); + TEST_CHECK(ntp_ts_map.first == (i + j + ntp_ts_map.size + 1) % ntp_ts_map.max_size); + } + TEST_CHECK(ntp_ts_map.cached_index == ntp_ts_map.size - 1); + TEST_CHECK(get_ntp_tss(ntp_ts_map.size - 1)->slew_epoch == ntp_ts_map.slew_epoch); + TEST_CHECK(CLG_GetNtpTxTimestamp(&ntp_ts, &ts2)); + TEST_CHECK(UTI_CompareTimespecs(&ts, &ts2) == 0); + + for (k = random() % 4; k > 0; k--) { + index2 = random() % ntp_ts_map.size; + int64_to_ntp64(get_ntp_tss(index2)->rx_ts, &ntp_ts); + if (random() % 2) + TEST_CHECK(CLG_GetNtpTxTimestamp(&ntp_ts, &ts)); + + UTI_Ntp64ToTimespec(&ntp_ts, &ts); + UTI_AddDoubleToTimespec(&ts, TST_GetRandomDouble(-1.999, 1.999), &ts); + + ts2 = ts; + CLG_UndoNtpTxTimestampSlew(&ntp_ts, &ts); + if ((get_ntp_tss(index2)->slew_epoch + 1) % (1U << 16) != ntp_ts_map.slew_epoch) { + TEST_CHECK(UTI_CompareTimespecs(&ts, &ts2) == 0); + } else { + TEST_CHECK(fabs(UTI_DiffTimespecsToDouble(&ts, &ts2) - ntp_ts_map.slew_offset) < + 1.0e-9); + } + + CLG_UpdateNtpTxTimestamp(&ntp_ts, &ts); + + TEST_CHECK(CLG_GetNtpTxTimestamp(&ntp_ts, &ts2)); + TEST_CHECK(UTI_CompareTimespecs(&ts, &ts2) == 0); + + if (random() % 2) { + uint16_t prev_epoch = ntp_ts_map.slew_epoch; + handle_slew(NULL, NULL, 0.0, TST_GetRandomDouble(-1.0e-5, 1.0e-5), + LCL_ChangeAdjust, NULL); + TEST_CHECK((prev_epoch + 1) % (1U << 16) == ntp_ts_map.slew_epoch); + } + + if (ntp_ts_map.size > 1) { + index = random() % (ntp_ts_map.size - 1); + if (get_ntp_tss(index)->rx_ts + 1 != get_ntp_tss(index + 1)->rx_ts) { + int64_to_ntp64(get_ntp_tss(index)->rx_ts + 1, &ntp_ts); + TEST_CHECK(!CLG_GetNtpTxTimestamp(&ntp_ts, &ts)); + int64_to_ntp64(get_ntp_tss(index + 1)->rx_ts - 1, &ntp_ts); + TEST_CHECK(!CLG_GetNtpTxTimestamp(&ntp_ts, &ts)); + CLG_UpdateNtpTxTimestamp(&ntp_ts, &ts); + CLG_UndoNtpTxTimestampSlew(&ntp_ts, &ts); + } + } + + if (random() % 2) { + int64_to_ntp64(get_ntp_tss(0)->rx_ts - 1, &ntp_ts); + TEST_CHECK(!CLG_GetNtpTxTimestamp(&ntp_ts, &ts)); + int64_to_ntp64(get_ntp_tss(ntp_ts_map.size - 1)->rx_ts + 1, &ntp_ts); + TEST_CHECK(!CLG_GetNtpTxTimestamp(&ntp_ts, &ts)); + CLG_UpdateNtpTxTimestamp(&ntp_ts, &ts); + CLG_UndoNtpTxTimestampSlew(&ntp_ts, &ts); + } + } + } + + for (j = 0; j < 500; j++) { + shift = (i % 3) * 26; + + if (i % 7 == 0) { + while (ntp_ts_map.size < ntp_ts_map.max_size) { + ts64 += get_random64() >> (shift + 8); + int64_to_ntp64(ts64, &ntp_ts); + CLG_SaveNtpTimestamps(&ntp_ts, NULL); + if (ntp_ts_map.cached_index + NTPTS_INSERT_LIMIT < ntp_ts_map.size) + ts64 = get_ntp_tss(ntp_ts_map.size - 1)->rx_ts; + } + } + do { + if (ntp_ts_map.size > 1 && random() % 2) { + k = random() % (ntp_ts_map.size - 1); + ts64 = get_ntp_tss(k)->rx_ts + + (get_ntp_tss(k + 1)->rx_ts - get_ntp_tss(k)->rx_ts) / 2; + } else { + ts64 = get_random64() >> shift; + } + } while (ts64 == 0ULL); + + int64_to_ntp64(ts64, &ntp_ts); + + prev_first = ntp_ts_map.first; + prev_size = ntp_ts_map.size; + prev_first_ts64 = get_ntp_tss(0)->rx_ts; + prev_last_ts64 = get_ntp_tss(prev_size - 1)->rx_ts; + CLG_SaveNtpTimestamps(&ntp_ts, NULL); + + TEST_CHECK(find_ntp_rx_ts(ts64, &index2)); + + if (ntp_ts_map.size > 1) { + TEST_CHECK(ntp_ts_map.size > 0 && ntp_ts_map.size <= ntp_ts_map.max_size); + if (get_ntp_tss(index2)->flags & NTPTS_DISABLED) + continue; + + TEST_CHECK(get_ntp_tss(ntp_ts_map.size - 1)->rx_ts - ts64 <= NTPTS_FUTURE_LIMIT); + + if ((int64_t)(prev_last_ts64 - ts64) <= NTPTS_FUTURE_LIMIT) { + TEST_CHECK(prev_size + 1 >= ntp_ts_map.size); + if (index2 + NTPTS_INSERT_LIMIT + 1 >= ntp_ts_map.size && + !(index2 == 0 && NTPTS_INSERT_LIMIT < ntp_ts_map.max_size && + ((NTPTS_INSERT_LIMIT == prev_size && (int64_t)(ts64 - prev_first_ts64) > 0) || + (NTPTS_INSERT_LIMIT + 1 == prev_size && (int64_t)(ts64 - prev_first_ts64) < 0)))) + TEST_CHECK((prev_first + prev_size + 1) % ntp_ts_map.max_size == + (ntp_ts_map.first + ntp_ts_map.size) % ntp_ts_map.max_size); + else + TEST_CHECK(prev_first + prev_size == ntp_ts_map.first + ntp_ts_map.size); + } + + TEST_CHECK((int64_t)(get_ntp_tss(ntp_ts_map.size - 1)->rx_ts - + get_ntp_tss(0)->rx_ts) > 0); + for (k = 0; k + 1 < ntp_ts_map.size; k++) + TEST_CHECK((int64_t)(get_ntp_tss(k + 1)->rx_ts - get_ntp_tss(k)->rx_ts) > 0); + } + + if (random() % 10 == 0) { + CLG_DisableNtpTimestamps(&ntp_ts); + TEST_CHECK(!CLG_GetNtpTxTimestamp(&ntp_ts, &ts)); + } + + for (k = random() % 10; k > 0; k--) { + ts64 = get_random64() >> shift; + int64_to_ntp64(ts64, &ntp_ts); + CLG_GetNtpTxTimestamp(&ntp_ts, &ts); + } + } + + if (random() % 2) { + handle_slew(NULL, NULL, 0.0, TST_GetRandomDouble(-1.0e9, 1.0e9), + LCL_ChangeUnknownStep, NULL); + TEST_CHECK(ntp_ts_map.size == 0); + TEST_CHECK(ntp_ts_map.cached_rx_ts == 0ULL); + TEST_CHECK(!CLG_GetNtpTxTimestamp(&ntp_ts, &ts)); + CLG_UpdateNtpTxTimestamp(&ntp_ts, &ts); + } + } + CLG_Finalise(); + LCL_Finalise(); CNF_Finalise(); } #else diff --git a/test/unit/hash.c b/test/unit/hash.c index d57c915..2f9ffef 100644 --- a/test/unit/hash.c +++ b/test/unit/hash.c @@ -38,6 +38,7 @@ test_unit(void) unsigned char data2[] = "12345678910"; unsigned char out[MAX_HASH_LENGTH]; struct hash_test tests[] = { + { "MD5-NC", "\xfc\x24\x97\x1b\x52\x66\xdc\x46\xef\xe0\xe8\x08\x46\x89\xb6\x88", 16 }, { "MD5", "\xfc\x24\x97\x1b\x52\x66\xdc\x46\xef\xe0\xe8\x08\x46\x89\xb6\x88", 16 }, { "SHA1", "\xd8\x85\xb3\x86\xce\xea\x93\xeb\x92\xcd\x7b\x94\xb9\x8d\xc2\x8e" "\x3e\x31\x13\xdd", 20}, @@ -77,9 +78,15 @@ test_unit(void) for (i = 0; tests[i].name[0] != '\0'; i++) { algorithm = UTI_HashNameToAlgorithm(tests[i].name); - TEST_CHECK(algorithm != 0); + if (strcmp(tests[i].name, "MD5-NC") == 0) { + TEST_CHECK(algorithm == 0); + algorithm = HSH_MD5_NONCRYPTO; + } else { + TEST_CHECK(algorithm != 0); + } hash_id = HSH_GetHashId(algorithm); if (hash_id < 0) { + TEST_CHECK(algorithm != HSH_MD5_NONCRYPTO); TEST_CHECK(algorithm != HSH_MD5); #ifdef FEAT_SECHASH TEST_CHECK(algorithm != HSH_SHA1); diff --git a/test/unit/ntp_auth.c b/test/unit/ntp_auth.c index 5c4b255..5f2a9bc 100644 --- a/test/unit/ntp_auth.c +++ b/test/unit/ntp_auth.c @@ -229,12 +229,8 @@ test_unit(void) if (!inst || !can_auth_req) add_dummy_auth(mode, key_id, &req, &req_info); - TEST_CHECK(req_info.auth.mode == mode); - - memset(&req_info.auth, 0, sizeof (req_info.auth)); - TEST_CHECK(NAU_ParsePacket(&req, &req_info)); - TEST_CHECK(req_info.auth.mode == mode); - TEST_CHECK(req_info.auth.mac.key_id == key_id); + assert(req_info.auth.mode == mode); + assert(req_info.auth.mac.key_id == key_id); kod = 1; TEST_CHECK(NAU_CheckRequestAuth(&req, &req_info, &kod) == can_auth_req); @@ -259,10 +255,8 @@ test_unit(void) if (!can_auth_res) add_dummy_auth(mode, key_id, &res, &res_info); - memset(&res_info.auth, 0, sizeof (res_info.auth)); - TEST_CHECK(NAU_ParsePacket(&res, &res_info)); - TEST_CHECK(res_info.auth.mode == mode); - TEST_CHECK(res_info.auth.mac.key_id == key_id); + assert(res_info.auth.mode == mode); + assert(res_info.auth.mac.key_id == key_id); if (inst) { if (mode == NTP_AUTH_SYMMETRIC) { diff --git a/test/unit/ntp_core.c b/test/unit/ntp_core.c index 9aac5d6..74145b9 100644 --- a/test/unit/ntp_core.c +++ b/test/unit/ntp_core.c @@ -331,6 +331,55 @@ process_replay(NCR_Instance inst, NTP_Packet *packet_queue, advance_time(1e-6); } +static void +add_dummy_auth(NTP_AuthMode auth_mode, uint32_t key_id, NTP_Packet *packet, NTP_PacketInfo *info) +{ + unsigned char buf[64]; + int len, fill; + + info->auth.mode = auth_mode; + + switch (auth_mode) { + case NTP_AUTH_NONE: + break; + case NTP_AUTH_SYMMETRIC: + case NTP_AUTH_MSSNTP: + case NTP_AUTH_MSSNTP_EXT: + switch (auth_mode) { + case NTP_AUTH_SYMMETRIC: + len = 16 + random() % 2 * 4; + fill = 1 + random() % 255; + break; + case NTP_AUTH_MSSNTP: + len = 16; + fill = 0; + break; + case NTP_AUTH_MSSNTP_EXT: + len = 68; + fill = 0; + break; + default: + assert(0); + } + + assert(info->length + 4 + len <= sizeof (*packet)); + + *(uint32_t *)((unsigned char *)packet + info->length) = htonl(key_id); + info->auth.mac.key_id = key_id; + info->length += 4; + + memset((unsigned char *)packet + info->length, fill, len); + info->length += len; + break; + case NTP_AUTH_NTS: + memset(buf, 0, sizeof (buf)); + TEST_CHECK(NEF_AddField(packet, info, NTP_EF_NTS_AUTH_AND_EEF, buf, sizeof (buf))); + break; + default: + assert(0); + } +} + #define PACKET_QUEUE_LENGTH 10 void @@ -347,7 +396,8 @@ test_unit(void) CPS_NTP_Source source; NTP_Remote_Address remote_addr; NCR_Instance inst1, inst2; - NTP_Packet packet_queue[PACKET_QUEUE_LENGTH]; + NTP_Packet packet_queue[PACKET_QUEUE_LENGTH], packet; + NTP_PacketInfo info; CNF_Initialise(0, 0); for (i = 0; i < sizeof conf / sizeof conf[0]; i++) @@ -504,6 +554,47 @@ test_unit(void) NCR_DestroyInstance(inst2); } + memset(&packet, 0, sizeof (packet)); + packet.lvm = NTP_LVM(LEAP_Normal, NTP_VERSION, MODE_CLIENT); + + TEST_CHECK(parse_packet(&packet, NTP_HEADER_LENGTH, &info)); + TEST_CHECK(info.auth.mode == NTP_AUTH_NONE); + + TEST_CHECK(parse_packet(&packet, NTP_HEADER_LENGTH, &info)); + add_dummy_auth(NTP_AUTH_SYMMETRIC, 100, &packet, &info); + memset(&info.auth, 0, sizeof (info.auth)); + TEST_CHECK(parse_packet(&packet, info.length, &info)); + TEST_CHECK(info.auth.mode == NTP_AUTH_SYMMETRIC); + TEST_CHECK(info.auth.mac.start == NTP_HEADER_LENGTH); + TEST_CHECK(info.auth.mac.length == info.length - NTP_HEADER_LENGTH); + TEST_CHECK(info.auth.mac.key_id == 100); + + TEST_CHECK(parse_packet(&packet, NTP_HEADER_LENGTH, &info)); + add_dummy_auth(NTP_AUTH_NTS, 0, &packet, &info); + memset(&info.auth, 0, sizeof (info.auth)); + TEST_CHECK(parse_packet(&packet, info.length, &info)); + TEST_CHECK(info.auth.mode == NTP_AUTH_NTS); + + packet.lvm = NTP_LVM(LEAP_Normal, 3, MODE_CLIENT); + + TEST_CHECK(parse_packet(&packet, NTP_HEADER_LENGTH, &info)); + add_dummy_auth(NTP_AUTH_MSSNTP, 200, &packet, &info); + memset(&info.auth, 0, sizeof (info.auth)); + TEST_CHECK(parse_packet(&packet, info.length, &info)); + TEST_CHECK(info.auth.mode == NTP_AUTH_MSSNTP); + TEST_CHECK(info.auth.mac.start == NTP_HEADER_LENGTH); + TEST_CHECK(info.auth.mac.length == 20); + TEST_CHECK(info.auth.mac.key_id == 200); + + TEST_CHECK(parse_packet(&packet, NTP_HEADER_LENGTH, &info)); + add_dummy_auth(NTP_AUTH_MSSNTP_EXT, 300, &packet, &info); + memset(&info.auth, 0, sizeof (info.auth)); + TEST_CHECK(parse_packet(&packet, info.length, &info)); + TEST_CHECK(info.auth.mode == NTP_AUTH_MSSNTP_EXT); + TEST_CHECK(info.auth.mac.start == NTP_HEADER_LENGTH); + TEST_CHECK(info.auth.mac.length == 72); + TEST_CHECK(info.auth.mac.key_id == 300); + KEY_Finalise(); REF_Finalise(); NCR_Finalise(); diff --git a/test/unit/test.c b/test/unit/test.c index 0f5638d..94619c1 100644 --- a/test/unit/test.c +++ b/test/unit/test.c @@ -90,7 +90,7 @@ main(int argc, char **argv) double TST_GetRandomDouble(double min, double max) { - return min + (double)random() / RAND_MAX * (max - min); + return min + random() / 2147483647.0 * (max - min); } void diff --git a/test/unit/util.c b/test/unit/util.c index 88a623d..112676d 100644 --- a/test/unit/util.c +++ b/test/unit/util.c @@ -34,7 +34,7 @@ test_unit(void) { struct timespec ts, ts2, ts3, ts4; char buf[16], *s, *s2, *words[3]; - NTP_int64 ntp_ts, ntp_fuzz; + NTP_int64 ntp_ts, ntp_ts2, ntp_fuzz; NTP_int32 ntp32_ts; struct timeval tv; double x, y, nan, inf; @@ -114,6 +114,13 @@ test_unit(void) #endif TEST_CHECK(ts.tv_nsec == 999999999); + ntp_ts.hi = htonl(JAN_1970 - 1); + ntp_ts.lo = htonl(0xffffffff); + ntp_ts2.hi = htonl(JAN_1970 + 1); + ntp_ts2.lo = htonl(0x80000000); + TEST_CHECK(fabs(UTI_DiffNtp64ToDouble(&ntp_ts, &ntp_ts2) + 1.5) < 1e-9); + TEST_CHECK(fabs(UTI_DiffNtp64ToDouble(&ntp_ts2, &ntp_ts) - 1.5) < 1e-9); + UTI_AddDoubleToTimespec(&ts, 1e-9, &ts); #if defined(HAVE_LONG_TIME_T) && NTP_ERA_SPLIT > 0 TEST_CHECK(ts.tv_sec == 1 + 0x100000000LL * (1 + (NTP_ERA_SPLIT - 1) / 0x100000000LL)); @@ -472,8 +479,8 @@ test_unit(void) ts2.tv_nsec = 250000000; UTI_AdjustTimespec(&ts, &ts2, &ts3, &x, 2.0, -5.0); TEST_CHECK(fabs(x - 6.5) < 1.0e-15); - TEST_CHECK(ts3.tv_sec == 10); - TEST_CHECK(ts3.tv_nsec == 0); + TEST_CHECK((ts3.tv_sec == 10 && ts3.tv_nsec == 0) || + (ts3.tv_sec == 9 && ts3.tv_nsec == 999999999)); for (i = -32; i <= 32; i++) { for (j = c = 0; j < 1000; j++) { @@ -501,9 +508,20 @@ test_unit(void) TEST_CHECK(UTI_DoubleToNtp32(65536.0) == htonl(0xffffffff)); TEST_CHECK(UTI_DoubleToNtp32(65537.0) == htonl(0xffffffff)); + TEST_CHECK(UTI_DoubleToNtp32f28(-1.0) == htonl(0)); + TEST_CHECK(UTI_DoubleToNtp32f28(0.0) == htonl(0)); + TEST_CHECK(UTI_DoubleToNtp32f28(1e-9) == htonl(1)); + TEST_CHECK(UTI_DoubleToNtp32f28(4e-9) == htonl(2)); + TEST_CHECK(UTI_DoubleToNtp32f28(8.0) == htonl(0x80000000)); + TEST_CHECK(UTI_DoubleToNtp32f28(16.0) == htonl(0xffffffff)); + TEST_CHECK(UTI_DoubleToNtp32f28(16.1) == htonl(0xffffffff)); + TEST_CHECK(UTI_DoubleToNtp32f28(16.1) == htonl(0xffffffff)); + + TEST_CHECK(UTI_Ntp32f28ToDouble(htonl(0xffffffff)) >= 65535.999); for (i = 0; i < 100000; i++) { UTI_GetRandomBytes(&ntp32_ts, sizeof (ntp32_ts)); TEST_CHECK(UTI_DoubleToNtp32(UTI_Ntp32ToDouble(ntp32_ts)) == ntp32_ts); + TEST_CHECK(UTI_DoubleToNtp32f28(UTI_Ntp32f28ToDouble(ntp32_ts)) == ntp32_ts); } ts.tv_nsec = 0; @@ -652,6 +670,10 @@ test_unit(void) UTI_GetRandomBytesUrandom(buf, j); if (j && buf[j - 1] % 2) c++; + if (random() % 10000 == 0) { + UTI_ResetGetRandomFunctions(); + TEST_CHECK(!urandom_file); + } } TEST_CHECK(c > 46000 && c < 48000); @@ -660,6 +682,12 @@ test_unit(void) UTI_GetRandomBytes(buf, j); if (j && buf[j - 1] % 2) c++; + if (random() % 10000 == 0) { + UTI_ResetGetRandomFunctions(); +#if HAVE_GETRANDOM + TEST_CHECK(getrandom_buf_available == 0); +#endif + } } TEST_CHECK(c > 46000 && c < 48000); -- cgit v1.2.3