brintos

brintos / linux-shallow public Read only

0
0
Text · 2.3 KiB · ac6e5a6 Raw
167 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.03 4ALL_TESTS="5	settime6	adjtime7	adjfreq8"9DEV=$110 11##############################################################################12# Sanity checks13 14if [[ "$(id -u)" -ne 0 ]]; then15	echo "SKIP: need root privileges"16	exit 017fi18 19if [[ "$DEV" == "" ]]; then20	echo "SKIP: PTP device not provided"21	exit 022fi23 24require_command()25{26	local cmd=$1; shift27 28	if [[ ! -x "$(command -v "$cmd")" ]]; then29		echo "SKIP: $cmd not installed"30		exit 131	fi32}33 34phc_sanity()35{36	phc_ctl $DEV get &> /dev/null37 38	if [ $? != 0 ]; then39		echo "SKIP: unknown clock $DEV: No such device"40		exit 141	fi42}43 44require_command phc_ctl45phc_sanity46 47##############################################################################48# Helpers49 50# Exit status to return at the end. Set in case one of the tests fails.51EXIT_STATUS=052# Per-test return value. Clear at the beginning of each test.53RET=054 55check_err()56{57	local err=$158 59	if [[ $RET -eq 0 && $err -ne 0 ]]; then60		RET=$err61	fi62}63 64log_test()65{66	local test_name=$167 68	if [[ $RET -ne 0 ]]; then69		EXIT_STATUS=170		printf "TEST: %-60s  [FAIL]\n" "$test_name"71		return 172	fi73 74	printf "TEST: %-60s  [ OK ]\n" "$test_name"75	return 076}77 78tests_run()79{80	local current_test81 82	for current_test in ${TESTS:-$ALL_TESTS}; do83		$current_test84	done85}86 87##############################################################################88# Tests89 90settime_do()91{92	local res93 94	res=$(phc_ctl $DEV set 0 wait 120.5 get 2> /dev/null \95		| awk '/clock time is/{print $5}' \96		| awk -F. '{print $1}')97 98	(( res == 120 ))99}100 101adjtime_do()102{103	local res104 105	res=$(phc_ctl $DEV set 0 adj 10 get 2> /dev/null \106		| awk '/clock time is/{print $5}' \107		| awk -F. '{print $1}')108 109	(( res == 10 ))110}111 112adjfreq_do()113{114	local res115 116	# Set the clock to be 1% faster117	res=$(phc_ctl $DEV freq 10000000 set 0 wait 100.5 get 2> /dev/null \118		| awk '/clock time is/{print $5}' \119		| awk -F. '{print $1}')120 121	(( res == 101 ))122}123 124##############################################################################125 126cleanup()127{128	phc_ctl $DEV freq 0.0 &> /dev/null129	phc_ctl $DEV set &> /dev/null130}131 132settime()133{134	RET=0135 136	settime_do137	check_err $?138	log_test "settime"139	cleanup140}141 142adjtime()143{144	RET=0145 146	adjtime_do147	check_err $?148	log_test "adjtime"149	cleanup150}151 152adjfreq()153{154	RET=0155 156	adjfreq_do157	check_err $?158	log_test "adjfreq"159	cleanup160}161 162trap cleanup EXIT163 164tests_run165 166exit $EXIT_STATUS167