summaryrefslogtreecommitdiffstats
path: root/tests/faketime_common.sh
blob: 2fab0824c9d11a3d2b2407bd757694ebc68640f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# addd 2016-03-11 by Thomas D., released under ASL 2.0
# Several tests make use of faketime. They all need to know when
# faketime is missing or the system isn't year-2038 complaint.
# This script can be sourced to prevent duplicated code.

rsyslog_testbench_preload_libfaketime() {
    local missing_requirements=
    if ! hash find 2>/dev/null ; then
        missing_requirements="'find' is missing in PATH; Make sure you have findutils/coreutils installed! Skipping test ..."
    fi

    if ! hash $RS_SORTCMD 2>/dev/null ; then
        missing_requirements="'sort' is missing in PATH; Make sure you have coreutils installed! Skipping test ..."
    fi

    if ! hash $RS_HEADCMD 2>/dev/null ; then
        missing_requirements="'head' is missing in PATH; Make sure you have coreutils installed! Skipping test ..."
    fi

    if [ -n "${missing_requirements}" ]; then
        printf '%s\n' "${missing_requirements}"
        exit 77
    fi

    RSYSLOG_LIBFAKETIME=$(find /usr -name 'libfaketime.so*' -type f | $RS_SORTCMD --reverse | $RS_HEADCMD --lines 1)
    if [ -z "${RSYSLOG_LIBFAKETIME}" ]; then
        echo "Could not determine libfaketime library, skipping test!"
        exit 77
    fi

    echo "Testing '${RSYSLOG_LIBFAKETIME}' library ..."
    local faketime_testtime=$(LD_PRELOAD="${RSYSLOG_LIBFAKETIME}" FAKETIME="1991-08-25 20:57:08" TZ=GMT date +%s 2>/dev/null)
    if [ ${faketime_testtime} -ne 683153828 ] ; then
        echo "'${RSYSLOG_LIBFAKETIME}' failed sanity check, skipping test!"
        exit 77
    else
        echo "Test passed! Will use '${RSYSLOG_LIBFAKETIME}' library!"
        export RSYSLOG_PRELOAD="${RSYSLOG_LIBFAKETIME}"
    fi

    # GMT-1 (POSIX TIME) is GMT+1 in "Human Time"
    faketime_testtime=$(LD_PRELOAD="${RSYSLOG_LIBFAKETIME}" FAKETIME="2040-01-01 16:00:00" TZ=GMT-1 date +%s 2>/dev/null)
    if [ ${faketime_testtime} -eq -1 ]; then
        echo "Note: System is not year-2038 compliant"
        RSYSLOG_TESTBENCH_Y2K38_INCOMPATIBLE="yes"
    else
        echo "Note: System is year-2038 compliant"
    fi
}

rsyslog_testbench_require_y2k38_support() {
    if [ -n "${RSYSLOG_TESTBENCH_Y2K38_INCOMPATIBLE}" ]; then
        echo "Skipping further tests because system doesn't support year 2038 ..."
        exit_test
        exit 0
    fi
}

rsyslog_testbench_preload_libfaketime