diff options
Diffstat (limited to 'tests/deckard/contrib/libfaketime/test/functests')
6 files changed, 243 insertions, 0 deletions
diff --git a/tests/deckard/contrib/libfaketime/test/functests/common.inc b/tests/deckard/contrib/libfaketime/test/functests/common.inc new file mode 100644 index 0000000..d184ae2 --- /dev/null +++ b/tests/deckard/contrib/libfaketime/test/functests/common.inc @@ -0,0 +1,63 @@ +# libfaketime-specific common support routines for tests + +# say which *_fakecmd wrapper to use +platform() +{ + # may want to expand the pattern for linuxlike + typeset out=$(uname) + case "$out" in + *Darwin*) echo "mac" ;; + *Linux*) echo "linuxlike" ;; + GNU|GNU/kFreeBSD) echo "linuxlike" ;; + *SunOS*) echo "sunos" ;; + *) echo 1>&2 unsupported platform, uname=\"$out\" ;; + esac +} + +# run faked command on a mac +# UNTESTED +mac_fakecmd() +{ + typeset timestring="$1"; shift + typeset fakelib=../src/libfaketime.1.dylib + export DYLD_INSERT_LIBRARIES=$fakelib + export DYLD_FORCE_FLAT_NAMESPACE=1 + FAKETIME="$timestring" \ + "$@" +} + +sunos_fakecmd() +{ + typeset timestring="$1"; shift + typeset fakelib=../src/libfaketime.so.1 + export LD_PRELOAD=$fakelib + FAKETIME="$timestring" \ + "$@" +} + +# run faked command on linuxlike OS +linuxlike_fakecmd() +{ + typeset timestring="$1"; shift + typeset fakelib=../src/libfaketime.so.1 + export LD_PRELOAD=$fakelib + FAKETIME="$timestring" \ + "$@" +} + +# run a command with libfaketime using the given timestring +fakecmd() +{ + ${PLATFORM}_fakecmd "$@" +} + +# generate a sequence of numbers from a to b +range() +{ + typeset a=$1 b=$2 + typeset i=$a + while ((i <= b)); do + echo $i + ((i = i+1)) + done +} diff --git a/tests/deckard/contrib/libfaketime/test/functests/dont_test_false.sh b/tests/deckard/contrib/libfaketime/test/functests/dont_test_false.sh new file mode 100644 index 0000000..65457ef --- /dev/null +++ b/tests/deckard/contrib/libfaketime/test/functests/dont_test_false.sh @@ -0,0 +1,6 @@ +# a testsuite that will force failure - for testing purposes + +run() +{ + run_testcase false +} diff --git a/tests/deckard/contrib/libfaketime/test/functests/test_exclude_mono.sh b/tests/deckard/contrib/libfaketime/test/functests/test_exclude_mono.sh new file mode 100644 index 0000000..f68fc20 --- /dev/null +++ b/tests/deckard/contrib/libfaketime/test/functests/test_exclude_mono.sh @@ -0,0 +1,87 @@ +# Checks that setting DONT_FAKE_MONOTONIC actually prevent +# libfaketime from faking monotonic clocks. +# +# We do this by freezing time at a specific and arbitrary date with faketime, +# and making sure that if we set DONT_FAKE_MONOTONIC to 1, calling +# clock_gettime(CLOCK_MONOTONIC) returns two different values. +# +# We also make sure that if we don't set DONT_FAKE_MONOTONIC to 1, in other +# words when we use the default behavior, two subsequent calls to +# clock_gettime(CLOCK_MONOTONIC) do return different values. + +init() +{ + typeset testsuite="$1" + PLATFORM=$(platform) + if [ -z "$PLATFORM" ]; then + echo "$testsuite: unknown platform! quitting" + return 1 + fi + echo "# PLATFORM=$PLATFORM" + return 0 +} + +run() +{ + init + + run_testcase dont_fake_mono + run_testcase fake_mono +} + +get_token() +{ + string=$1 + token_index=$2 + separator=$3 + + echo $string | cut -d "$separator" -f $token_index +} + +assert_timestamps_neq() +{ + timestamps=$1 + msg=$2 + + first_timestamp=$(get_token "${timestamps}" 1 ' ') + second_timestamp=$(get_token "${timestamps}" 2 ' ') + + assertneq "${first_timestamp}" "${second_timestamp}" "${msg}" +} + +assert_timestamps_eq() +{ + timestamps=$1 + msg=$2 + + first_timestamp=$(get_token "${timestamps}" 1 ' ') + second_timestamp=$(get_token "${timestamps}" 2 ' ') + + asserteq "${first_timestamp}" "${second_timestamp}" "${msg}" +} + +get_monotonic_time() +{ + dont_fake_mono=$1; shift; + clock_id=$1; shift; + DONT_FAKE_MONOTONIC=${dont_fake_mono} fakecmd "2014-07-21 09:00:00" \ + /bin/bash -c "for i in 1 2; do \ + perl -w -MTime::HiRes=clock_gettime,${clock_id} -E \ + 'say clock_gettime(${clock_id})'; \ + sleep 1; \ + done" +} + +dont_fake_mono() +{ + timestamps=$(get_monotonic_time 1 CLOCK_MONOTONIC) + msg="When not faking monotonic time, timestamps should be different" + assert_timestamps_neq "${timestamps}" "${msg}" +} + +fake_mono() +{ + timestamps=$(get_monotonic_time 0 CLOCK_MONOTONIC) + msg="When faking monotonic, timestamps should be equal" + assert_timestamps_eq "${timestamps}" "${msg}" +} diff --git a/tests/deckard/contrib/libfaketime/test/functests/test_null.sh b/tests/deckard/contrib/libfaketime/test/functests/test_null.sh new file mode 100644 index 0000000..09e7560 --- /dev/null +++ b/tests/deckard/contrib/libfaketime/test/functests/test_null.sh @@ -0,0 +1,13 @@ +# check that the date doesn't happen to be 0. + +run() +{ + run_testcase nulltest +} + +nulltest() +{ + typeset tdate=${I2DATES[0]} + + assertneq 0 "$(date +%s)" "($tdate)" +} diff --git a/tests/deckard/contrib/libfaketime/test/functests/test_true.sh b/tests/deckard/contrib/libfaketime/test/functests/test_true.sh new file mode 100644 index 0000000..0025bcf --- /dev/null +++ b/tests/deckard/contrib/libfaketime/test/functests/test_true.sh @@ -0,0 +1,7 @@ +# test suite that always succeeds - for testing framework + +run() +{ + run_testcase true + return 0 +} diff --git a/tests/deckard/contrib/libfaketime/test/functests/test_walkone.sh b/tests/deckard/contrib/libfaketime/test/functests/test_walkone.sh new file mode 100755 index 0000000..b27df9a --- /dev/null +++ b/tests/deckard/contrib/libfaketime/test/functests/test_walkone.sh @@ -0,0 +1,67 @@ +# walking-1 test. +# sourced in from testframe.sh. +# +# this script defines a suite of functional tests +# that verifies the correct operation of libfaketime +# with the date command. + +run() +{ + init + + for i in $(range 0 30); do + run_testcase test_with_i $i + done +} + +# ----- support routines +init() +{ + typeset testsuite="$1" + PLATFORM=$(platform) + if [ -z "$PLATFORM" ]; then + echo "$testsuite: unknown platform! quitting" + return 1 + fi + echo "# PLATFORM=$PLATFORM" + return 0 +} + + +# run date cmd under faketime, print time in secs +fakedate() +{ + # + # let the time format be raw seconds since Epoch + # for both input to libfaketime, and output of the date cmd. + # + typeset fmt='%s' + export FAKETIME_FMT=$fmt + fakecmd "$1" date +$fmt +} + +# +# compute x**n. +# use only the shell, in case we need to run on machines +# without bc, dc, perl, etc. +# +pow() +{ + typeset x="$1" n="$2" + typeset r=1 + typeset i=0 + while ((i < n)); do + ((r = r*x)) + ((i++)) + done + echo $r +} + +# run a fakedate test with a given time t +test_with_i() +{ + typeset i="$1" + typeset t=$(pow 2 $i) + + asserteq $(fakedate $t) $t "(secs since Epoch)" +} |