^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/bash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) ALL_TESTS="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) settime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) adjtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) adjfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) DEV=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) ##############################################################################
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # Sanity checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) if [[ "$(id -u)" -ne 0 ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) echo "SKIP: need root privileges"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if [[ "$DEV" == "" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) echo "SKIP: PTP device not provided"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) exit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) require_command()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) local cmd=$1; shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if [[ ! -x "$(command -v "$cmd")" ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) echo "SKIP: $cmd not installed"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) phc_sanity()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) phc_ctl $DEV get &> /dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if [ $? != 0 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) echo "SKIP: unknown clock $DEV: No such device"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) require_command phc_ctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) phc_sanity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ##############################################################################
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) # Helpers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) # Exit status to return at the end. Set in case one of the tests fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) EXIT_STATUS=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) # Per-test return value. Clear at the beginning of each test.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) RET=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) check_err()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) local err=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if [[ $RET -eq 0 && $err -ne 0 ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) RET=$err
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) log_test()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) local test_name=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if [[ $RET -ne 0 ]]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) EXIT_STATUS=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) printf "TEST: %-60s [FAIL]\n" "$test_name"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) printf "TEST: %-60s [ OK ]\n" "$test_name"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) tests_run()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) local current_test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) for current_test in ${TESTS:-$ALL_TESTS}; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) $current_test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) ##############################################################################
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) # Tests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) settime_do()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) local res
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) res=$(phc_ctl $DEV set 0 wait 120.5 get 2> /dev/null \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) | awk '/clock time is/{print $5}' \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) | awk -F. '{print $1}')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) (( res == 120 ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) adjtime_do()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) local res
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) res=$(phc_ctl $DEV set 0 adj 10 get 2> /dev/null \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) | awk '/clock time is/{print $5}' \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) | awk -F. '{print $1}')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) (( res == 10 ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) adjfreq_do()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) local res
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) # Set the clock to be 1% faster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) res=$(phc_ctl $DEV freq 10000000 set 0 wait 100.5 get 2> /dev/null \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) | awk '/clock time is/{print $5}' \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) | awk -F. '{print $1}')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) (( res == 101 ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ##############################################################################
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) cleanup()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) phc_ctl $DEV freq 0.0 &> /dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) phc_ctl $DEV set &> /dev/null
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) settime()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) RET=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) settime_do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) check_err $?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) log_test "settime"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) adjtime()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) RET=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) adjtime_do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) check_err $?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) log_test "adjtime"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) adjfreq()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) RET=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) adjfreq_do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) check_err $?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) log_test "adjfreq"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) trap cleanup EXIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) tests_run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) exit $EXIT_STATUS