1# 2# This file and its contents are supplied under the terms of the 3# Common Development and Distribution License ("CDDL"), version 1.0. 4# You may only use this file in accordance with the terms of version 5# 1.0 of the CDDL. 6# 7# A full copy of the text of the CDDL should have accompanied this 8# source. A copy of the CDDL is also available via the Internet at 9# http://www.illumos.org/license/CDDL. 10# 11 12# 13# Copyright 2025 Oxide Computer Company 14# 15 16# 17# Common utility functions and data for I2C related tests, setup, and clean up. 18# 19 20# 21# Common environment and behavior settings. 22# 23export LANG=C.UTF-8 24export LD_PRELOAD=libumem.so 25export UMEM_DEBUG=default 26unalias -a 27set -o errexit 28 29# 30# Access to our program. 31# 32I2CADM=${ISCADM:-/usr/sbin/i2cadm} 33 34# 35# List of our known controllers. 36# 37I2C_CTRLS=(i2csim0 smbussim1) 38 39# 40# Common exit status to use. 41# 42i2c_exit=0 43 44function fatal 45{ 46 typeset msg="$*" 47 echo "TEST FAILED: $msg" >&2 48 exit 1 49} 50 51function warn 52{ 53 typeset msg="$*" 54 echo "TEST FAILED: $msg" >&2 55 i2c_exit=1 56} 57 58# 59# Given a path, clean up all I2C devices that match that path. 60# 61function i2c_cleanup_path 62{ 63 typeset path="$1" 64 65 for dev in $(i2cadm device list -Hpo path $path | sort -r); do 66 if ! i2cadm device remove $dev; then 67 fatal "failed to remove $dev" 68 fi 69 done 70} 71 72# 73# Clean up all devices that may have been created in the kernel on our simulated 74# controllers. 75# 76function i2c_cleanup_devs 77{ 78 for i in ${!I2C_CTRLS[*]}; do 79 i2c_cleanup_path "${I2C_CTRLS[$i]}" 80 done 81} 82 83function i2cadm_fail 84{ 85 typeset ret= 86 "$I2CADM" $@ 1>/dev/null 2>/dev/null 87 ret=$? 88 89 if (( ret == 0 )); then 90 warn "should have failed with args $@, but passed" 91 elif (( ret != 1 && ret != 2 )); then 92 warn "args $@ failed with status $ret, expected 1 or 2" 93 else 94 printf "TEST PASSED: program failed (exited %u): %s\n" "$ret" \ 95 "$*" 96 fi 97} 98 99function i2cadm_pass 100{ 101 typeset ret= 102 "$I2CADM" $@ 1>/dev/null 2>/dev/null 103 ret=$? 104 if (( ret != 0 )); then 105 warn "$@ failed with status $?, but expected success" 106 return 107 fi 108 109 printf "TEST PASSED: %s ran successfully\n" "$*" 110} 111 112# 113# Checks to see if the output of a command matches what we expect. This is 114# expected to be used with the parseable ofmt output. 115# 116function i2cadm_check_output 117{ 118 typeset exp="$1" 119 typeset out= 120 typeset ret= 121 shift 122 123 out=$("$I2CADM" $@ 2>/dev/null) 124 ret=$? 125 if (( ret != 0 )); then 126 warn "$@ failed with status $ret, but expected success" 127 return 128 fi 129 130 if [[ "$out" != "$exp" ]]; then 131 warn "$@ had unexpected output ($out), expected: $exp" 132 return 133 fi 134 135 printf "TEST PASSED: output from %s matched expected value: %s\n" "$*" \ 136 "$exp" 137} 138