1#!/usr/bin/ksh 2# 3# 4# This file and its contents are supplied under the terms of the 5# Common Development and Distribution License ("CDDL"), version 1.0. 6# You may only use this file in accordance with the terms of version 7# 1.0 of the CDDL. 8# 9# A full copy of the text of the CDDL should have accompanied this 10# source. A copy of the CDDL is also available via the Internet at 11# http://www.illumos.org/license/CDDL. 12# 13 14# 15# Copyright 2024 Oxide Computer Company 16# 17 18unalias -a 19set -o pipefail 20 21pcidb_arg0="$(basename $0)" 22pcidb_prog="${PCIDB:-/usr/lib/pci/pcidb}" 23pcidb_exit=0 24 25function warn 26{ 27 typeset msg="$*" 28 [[ -z "$msg" ]] && msg="failed" 29 echo "TEST FAILED: $pcidb_arg0: $msg" >&2 30} 31 32# 33# The following is intended to catch bad filters. 34# 35function pcidb_bad_filter 36{ 37 typeset filt="$1" 38 39 if $pcidb_prog $filt 2>/dev/null; then 40 warn "invalid filter $filt erroneously worked" 41 pcidb_exit=1 42 return 43 fi 44 45 printf "TEST PASSED: invalid filter %s\n" "$filt" 46 47} 48 49function pcidb_bad_args 50{ 51 if $pcidb_prog $@ 2>/dev/null 1>/dev/null; then 52 warn "should have failed with args "$@", but passed" 53 pcidb_exit=1 54 return 55 fi 56 57 printf "TEST PASSED: invalid arguments %s\n" "$*" 58} 59 60function pcidb_no_match 61{ 62 if $pcidb_prog $@ 2>/dev/null 1>/dev/null; then 63 warn "should have failed with args "$@", but passed" 64 pcidb_exit=1 65 return 66 fi 67 68 printf "TEST PASSED: no match %s\n" "$*" 69} 70 71function pcidb_pass 72{ 73 if ! $pcidb_prog $@ 2>/dev/null 1>/dev/null; then 74 warn "should have passed with args "$@", but failed" 75 pcidb_exit=1 76 return 77 fi 78 79 printf "TEST PASSED: pcidb %s\n" "$*" 80} 81 82function pcidb_match 83{ 84 typeset output 85 typeset match="$1" 86 shift 87 88 output=$($pcidb_prog $@) 89 if (( $? != 0)); then 90 warn "failed to run pcidb with args: $@" 91 pcidb_exit=1 92 return 93 fi 94 95 if [[ "$output" != "$match" ]]; then 96 warn "output mismatch with args: $@\n found: $output\n" \ 97 "expected: $match" 98 pcidb_exit=1 99 return 100 fi 101 102 printf "TEST PASSED: successfully matched against %s\n" "$*" 103} 104 105if [[ -n $PCIDB ]]; then 106 pcidb_prog=$PCIDB 107fi 108 109# 110# Before we begin execution, set up the environment such that we have a 111# standard locale and that umem will help us catch mistakes. 112# 113export LC_ALL=C.UTF-8 114export LD_PRELOAD=libumem.so 115export UMEM_DEBUG=default 116 117# 118# Validate that filters match either exactly one or at least one line of 119# output using parsable mode. When we match more than one entry, we 120# don't try to assert the count because we expect that it will actually 121# change over time. 122# 123exp="1de 1248086" 125pcidb_match "$exp" -v -p -o vid pci8086 pci1de 126pcidb_match "Advanced Micro Devices, Inc. [AMD]" -v -p -o vendor pci1022 127pcidb_match "1af4:1044:Virtio 1.0 RNG" -p -o vid,did,device pci1af4,1044 128pcidb_match "Dell:HBA330 Adapter" -s -p -o subvendor,subsystem \ 129 pci1000,97.1028,1f45 130pcidb_match "c:3:30:XHCI" -i -p -o bcc,scc,pi,interface pciclass,0c0330 131pcidb_match "I2O" -S -p -o subclass pciexclass,0e 132pcidb_match "Ethernet 1Gb 2-port 368i Adapter" -s -p -o subsystem pci1590,216,s 133 134# 135# Validate that multiple filters all match something. We don't look for 136# the exact table output due to the breadth of these filters. 137# 138pcidb_pass "pci8086" "pci8086,10d3.8086,a01f" 139pcidb_pass "pci8086,10d3.8086,a01f" "pci8086" 140pcidb_pass "pci8086" "pci8086,10d3.8086,a01f" "pci8086" 141pcidb_pass "pciclass,0c" "pciclass,0b" 142pcidb_pass "pciclass,06" "pciclass,0604" 143 144# 145# Verify valid filters that don't match anything cause use to exit zero 146# 147pcidb_no_match "pci8086" "pci8086,10d3.8086,ffff" 148pcidb_no_match "pci8086,10d3.8086,ffff" 149pcidb_no_match "pciclass,fffefd" 150 151# 152# Run through filter parsing 153# 154pcidb_bad_filter "foo" 155pcidb_bad_filter ";ffvi" 156pcidb_bad_filter "12345" 157pcidb_bad_filter "pc8086" 158pcidb_bad_filter "pciqwer" 159pcidb_bad_filter "pci12345" 160pcidb_bad_filter "pci8086," 161pcidb_bad_filter "pci8086,locke" 162pcidb_bad_filter "pci8086sigh" 163pcidb_bad_filter "pci8086,p" 164pcidb_bad_filter "pci8086,12345" 165pcidb_bad_filter "pci8086,1234zz" 166pcidb_bad_filter "pci8086,1234." 167pcidb_bad_filter "pci8086,1234," 168pcidb_bad_filter "pci8086,1234,b" 169pcidb_bad_filter "pci8086,1234,8" 170pcidb_bad_filter "pci8086,1234,wat" 171pcidb_bad_filter "pci8086,1234.terra" 172pcidb_bad_filter "pci8086,1234.terra,celes" 173pcidb_bad_filter "pci8086,1234.fffff" 174pcidb_bad_filter "pci8086,1234.abcd," 175pcidb_bad_filter "pci8086,1234.abcd." 176pcidb_bad_filter "pci8086,1234.abcdqr" 177pcidb_bad_filter "pci8086,1234.abcd,2,p" 178pcidb_bad_filter "pci8086,1234.abcd,2000000000" 179pcidb_bad_filter "pci8086,1234.abcd,kefka" 180pcidb_bad_filter "pci8086,1234.abcd,34ultros" 181pcidb_bad_filter "pciexqwer" 182pcidb_bad_filter "pciex12345" 183pcidb_bad_filter "pciex8086," 184pcidb_bad_filter "pciex8086,locke" 185pcidb_bad_filter "pciex8086sigh" 186pcidb_bad_filter "pciex8086,p" 187pcidb_bad_filter "pciex8086,12345" 188pcidb_bad_filter "pciex8086,1234zz" 189pcidb_bad_filter "pciex8086,1234." 190pcidb_bad_filter "pciex8086,1234," 191pcidb_bad_filter "pciex8086,1234,b" 192pcidb_bad_filter "pciex8086,1234,8" 193pcidb_bad_filter "pciex8086,1234,wat" 194pcidb_bad_filter "pciex8086,1234.terra" 195pcidb_bad_filter "pciex8086,1234.terra,celes" 196pcidb_bad_filter "pciex8086,1234.fffff" 197pcidb_bad_filter "pciex8086,1234.abcd," 198pcidb_bad_filter "pciex8086,1234.abcd." 199pcidb_bad_filter "pciex8086,1234.abcdqr" 200pcidb_bad_filter "pciex8086,1234.abcd,2,p" 201pcidb_bad_filter "pciex8086,1234.abcd,2000000000" 202pcidb_bad_filter "pciex8086,1234.abcd,kefka" 203pcidb_bad_filter "pciex8086,1234.abcd,34ultros" 204pcidb_bad_filter "pciclas" 205pcidb_bad_filter "pciclassedgar" 206pcidb_bad_filter "pciclass,sabin" 207pcidb_bad_filter "pciclass,0" 208pcidb_bad_filter "pciclass,013" 209pcidb_bad_filter "pciclass,01345" 210pcidb_bad_filter "pciclass,0134567" 211pcidb_bad_filter "pciclass,01," 212pcidb_bad_filter "pciclass,010," 213pcidb_bad_filter "pciclass,010aa," 214pcidb_bad_filter "pciclass,0102as" 215pcidb_bad_filter "pciclass,0102.as" 216pcidb_bad_filter "pciclass,0102@as" 217pcidb_bad_filter "pciclass,010298aa" 218pcidb_bad_filter "pciclass,010298," 219pcidb_bad_filter "pciclass,010298!" 220pcidb_bad_filter "pciclass,010298!shadow" 221pcidb_bad_filter "pciexclas" 222pcidb_bad_filter "pciexclassedgar" 223pcidb_bad_filter "pciexclass,sabin" 224pcidb_bad_filter "pciexclass,0" 225pcidb_bad_filter "pciexclass,013" 226pcidb_bad_filter "pciexclass,01345" 227pcidb_bad_filter "pciexclass,0134567" 228pcidb_bad_filter "pciexclass,01," 229pcidb_bad_filter "pciexclass,010," 230pcidb_bad_filter "pciexclass,010aa," 231pcidb_bad_filter "pciexclass,0102as" 232pcidb_bad_filter "pciexclass,0102.as" 233pcidb_bad_filter "pciexclass,0102@as" 234pcidb_bad_filter "pciexclass,010298aa" 235pcidb_bad_filter "pciexclass,010298," 236pcidb_bad_filter "pciexclass,010298!" 237pcidb_bad_filter "pciexclass,010298!shadow" 238 239# 240# Verify that if we ask for bad columns we error 241# 242pcidb_bad_args -p 243pcidb_bad_args -o 244pcidb_bad_args -o -p 245pcidb_bad_args -p -o terra 246pcidb_bad_args -p -o subclass -v 247pcidb_bad_args -v -d -c 248 249# 250# Bad table / filter combinations. This covers filters that don't agree 251# with one another, tables and filters that are opposite types, and 252# filters and tables that aren't specific enough. 253# 254pcidb_bad_args "pci8086" "pciclass,0b" 255pcidb_bad_args "pci8086,10d3" "pciclass,0b" 256pcidb_bad_args "pci8086,10d3,p" "pciclass,0b" 257pcidb_bad_args "pci8086,a01f,s" "pciclass,0b" 258pcidb_bad_args "pci8086,10d3.8086" "pciclass,0b" 259pcidb_bad_args "pci8086,10d3.8086.a01f" "pciclass,0b" 260pcidb_bad_args "pci8086" "pciclass,0604" 261pcidb_bad_args "pci8086,10d3" "pciclass,0604" 262pcidb_bad_args "pci8086,10d3,p" "pciclass,0604" 263pcidb_bad_args "pci8086,a01f,s" "pciclass,0604" 264pcidb_bad_args "pci8086,10d3.8086" "pciclass,0604" 265pcidb_bad_args "pci8086,10d3.8086.a01f" "pciclass,0604" 266pcidb_bad_args "pci8086" "pciclass,0c0330" 267pcidb_bad_args "pci8086,10d3" "pciclass,0c0330" 268pcidb_bad_args "pci8086,10d3,p" "pciclass,0c0330" 269pcidb_bad_args "pci8086,a01f,s" "pciclass,0c0330" 270pcidb_bad_args "pci8086,10d3.8086" "pciclass,0c0330" 271pcidb_bad_args "pci8086,10d3.8086.a01f" "pciclass,0c0330" 272 273pcidb_bad_args -c "pci1022" 274pcidb_bad_args -S "pci1022,1650" 275pcidb_bad_args -i "pci1022,1483.01de,fff9" 276pcidb_bad_args -v "pciclass02" 277 278pcidb_bad_args -d pciclass,03 279pcidb_bad_args -S pci1000 280pcidb_bad_args -v pci8086,1234 281pcidb_bad_args -c pciclass,010802 282 283 284if (( pcidb_exit == 0 )); then 285 printf "All tests passed successfully!\n" 286fi 287 288exit $pcidb_exit 289