summaryrefslogtreecommitdiffstats
path: root/tests/ts/hwclock
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 19:10:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 19:10:49 +0000
commitcfe5e3905201349e9cf3f95d52ff4bd100bde37d (patch)
treed0baf160cbee3195249d095f85e52d20c21acf02 /tests/ts/hwclock
parentInitial commit. (diff)
downloadutil-linux-cfe5e3905201349e9cf3f95d52ff4bd100bde37d.tar.xz
util-linux-cfe5e3905201349e9cf3f95d52ff4bd100bde37d.zip
Adding upstream version 2.39.3.upstream/2.39.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ts/hwclock')
-rwxr-xr-xtests/ts/hwclock/show38
-rwxr-xr-xtests/ts/hwclock/systohc103
2 files changed, 141 insertions, 0 deletions
diff --git a/tests/ts/hwclock/show b/tests/ts/hwclock/show
new file mode 100755
index 0000000..d8cdc8e
--- /dev/null
+++ b/tests/ts/hwclock/show
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+#
+# Copyright (C) 2023 Thomas Weißschuh <thomas@t-8ch.de>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="show"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_HWCLOCK"
+
+ts_skip_nonroot
+ts_skip_qemu_user
+ts_skip_docker
+
+# RTC interrupts are sometimes broken on GitHub Actions
+if ts_is_virt "microsoft"; then
+ TS_KNOWN_FAIL="yes"
+fi
+
+"$TS_CMD_HWCLOCK" --show > /dev/null 2> "$TS_ERRLOG" || ts_die
+
+ts_finalize
diff --git a/tests/ts/hwclock/systohc b/tests/ts/hwclock/systohc
new file mode 100755
index 0000000..fbd67ee
--- /dev/null
+++ b/tests/ts/hwclock/systohc
@@ -0,0 +1,103 @@
+#!/bin/bash
+
+#
+# Copyright (C) 2007 Karel Zak <kzak@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="system to hw"
+NTP_SERVER="0.fedora.pool.ntp.org"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_HWCLOCK"
+
+ts_skip_nonroot
+if [ "$TRAVIS_DIST" == "precise" ]; then
+ ts_skip "https://github.com/util-linux/util-linux/issues/1082"
+fi
+if [ "$GITHUB_ACTIONS" == "true" ]; then
+ ts_skip "virtual machine"
+fi
+
+ts_check_prog "bc"
+ts_check_prog "sntp"
+
+function get_offset_sys_ntp
+{
+ local ip="$@"
+ local out
+
+ # using hostname instead of IP could give us more than one offset
+ out=$(sntp --timeout 1 "$ip") || return 1
+
+ # sed must deliver a signed float or empty string for sure
+ out=$(echo "$out" | \
+ sed -n 's/.* \(\(+\|-\)[0-9]\{1,\}\.[0-9]\{1,\}\).*/\1/1p')
+
+ [ -n "$out" ] || return 1
+ echo "$out"
+}
+
+function check_diff_offset
+{
+ local a=${1#+}
+ local b=${2#+}
+ local max="$3"
+ local tmp
+
+ tmp=$(echo "$a - $b" | bc | tr -d '-')
+ echo "$tmp"
+
+ tmp=$(echo "$tmp < $max" | bc)
+ [ $tmp -eq 1 ]
+}
+
+
+# we need fixed ntp IP to get comparable offsets
+NTP_IP=$(ts_resolve_host "$NTP_SERVER") \
+ || ts_skip "can't resolve hostname $NTP_SERVER"
+
+OFFSET_A=$(get_offset_sys_ntp "$NTP_IP") \
+ || ts_skip "can't get ntp offset 1st, $NTP_IP"
+OFFSET_B=$(get_offset_sys_ntp "$NTP_IP") \
+ || ts_skip "can't get ntp offset 2nd, $NTP_IP"
+
+diff=$(check_diff_offset $OFFSET_A $OFFSET_B 0.02) \
+ || ts_skip "unreliable ntp or sys clock offsets: $NTP_IP $OFFSET_A $OFFSET_B +/-$diff"
+
+# hwclock --show should work if we have a hw clock
+tmp=$($TS_CMD_HWCLOCK --show 2>&1)
+if [ $? != "0" ]; then
+ echo "$tmp" | grep -q "Cannot access the Hardware Clock via" \
+ && ts_skip "no hardware clock found"
+ ts_failed "hwclock --show"
+fi
+
+# call hwclock
+for i in `seq 1 10`; do
+ # only *skip* on failure for now
+ $TS_CMD_HWCLOCK --systohc || ts_skip "hwclock --systohc failed, $i"
+ $TS_CMD_HWCLOCK --hctosys || ts_skip "hwclock --hctosys failed, $i"
+done
+
+OFFSET_C=$(get_offset_sys_ntp "$NTP_IP") \
+ || ts_skip "can't get ntp offset 3rd, $NTP_IP"
+
+diff=$(check_diff_offset "$OFFSET_B" "$OFFSET_C" 1.0) \
+ || ts_failed "offsets $NTP_IP: $OFFSET_B $OFFSET_C +/-$diff"
+
+ts_ok "offsets $NTP_IP: $OFFSET_B $OFFSET_C +/-$diff"