xref: /illumos-gate/usr/src/test/util-tests/tests/pcidb/pcidbtest.ksh (revision dd72704bd9e794056c558153663c739e2012d721)
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 2021 Oxide Computer Company
16#
17
18unalias -a
19set -o pipefail
20
21pcidb_arg0="$(basename $0)"
22pcidb_prog="/usr/lib/pci/pcidb"
23pcidb_exit=0
24
25warn()
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#
35pcidb_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
49pcidb_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
60pcidb_match()
61{
62	typeset output
63	typeset match="$1"
64	shift
65
66	output=$($pcidb_prog $@)
67	if (( $? != 0)); then
68		warn "failed to run pcidb with args: $@"
69		pcidb_exit=1
70		return
71	fi
72
73	if [[ "$output" != "$match" ]]; then
74		warn "output mismatch with args: $@\n found:    $output\n" \
75		    "expected: $match"
76		pcidb_exit=1
77		return
78	fi
79
80	printf "TEST PASSED: successfully matched against %s\n" "$*"
81}
82
83if [[ -n $PCIDB ]]; then
84	pcidb_prog=$PCIDB
85fi
86
87#
88# Before we begin execution, set up the environment such that we have a
89# standard locale and that umem will help us catch mistakes.
90#
91export LC_ALL=C.UTF-8
92export LD_PRELOAD=libumem.so
93export UMEM_DEBUG=default
94
95#
96# Validate that filters match either exactly one or at least one line of
97# output using parsable mode. When we match more than one entry, we
98# don't try to assert the count because we expect that it will actually
99# change over time.
100#
101exp="1de
1028086"
103pcidb_match "$exp" -v -p -o vid pci8086 pci1de
104pcidb_match "Advanced Micro Devices, Inc. [AMD]" -v -p -o vendor pci1022
105pcidb_match "1af4:1044:Virtio RNG" -p -o vid,did,device pci1af4,1044
106pcidb_match "Dell:HBA330 Adapter" -s -p -o subvendor,subsystem \
107	pci1000,97.1028,1f45
108pcidb_match "c:3:30:XHCI" -i -p -o bcc,scc,pi,interface pciclass,0c0330
109pcidb_match "I2O" -S -p -o subclass pciexclass,0e
110pcidb_match "Ethernet 1Gb 2-port 368i Adapter" -s -p -o subsystem pci1590,216,s
111
112#
113# We should get no output when we specify a class or device filter and
114# use a different table or we have an over specified filter.
115#
116pcidb_match "" -d pciclass,03
117pcidb_match "" -S pci1000
118pcidb_match "" -v pci8086,1234
119pcidb_match "" -c pciclass,010802
120
121#
122# Run through filter parsing
123#
124pcidb_bad_filter "foo"
125pcidb_bad_filter ";ffvi"
126pcidb_bad_filter "12345"
127pcidb_bad_filter "pc8086"
128pcidb_bad_filter "pciqwer"
129pcidb_bad_filter "pci12345"
130pcidb_bad_filter "pci8086,"
131pcidb_bad_filter "pci8086,locke"
132pcidb_bad_filter "pci8086sigh"
133pcidb_bad_filter "pci8086,p"
134pcidb_bad_filter "pci8086,12345"
135pcidb_bad_filter "pci8086,1234zz"
136pcidb_bad_filter "pci8086,1234."
137pcidb_bad_filter "pci8086,1234,"
138pcidb_bad_filter "pci8086,1234,b"
139pcidb_bad_filter "pci8086,1234,8"
140pcidb_bad_filter "pci8086,1234,wat"
141pcidb_bad_filter "pci8086,1234.terra"
142pcidb_bad_filter "pci8086,1234.terra,celes"
143pcidb_bad_filter "pci8086,1234.fffff"
144pcidb_bad_filter "pci8086,1234.abcd,"
145pcidb_bad_filter "pci8086,1234.abcd."
146pcidb_bad_filter "pci8086,1234.abcdqr"
147pcidb_bad_filter "pci8086,1234.abcd,2,p"
148pcidb_bad_filter "pci8086,1234.abcd,2000000000"
149pcidb_bad_filter "pci8086,1234.abcd,kefka"
150pcidb_bad_filter "pci8086,1234.abcd,34ultros"
151pcidb_bad_filter "pciexqwer"
152pcidb_bad_filter "pciex12345"
153pcidb_bad_filter "pciex8086,"
154pcidb_bad_filter "pciex8086,locke"
155pcidb_bad_filter "pciex8086sigh"
156pcidb_bad_filter "pciex8086,p"
157pcidb_bad_filter "pciex8086,12345"
158pcidb_bad_filter "pciex8086,1234zz"
159pcidb_bad_filter "pciex8086,1234."
160pcidb_bad_filter "pciex8086,1234,"
161pcidb_bad_filter "pciex8086,1234,b"
162pcidb_bad_filter "pciex8086,1234,8"
163pcidb_bad_filter "pciex8086,1234,wat"
164pcidb_bad_filter "pciex8086,1234.terra"
165pcidb_bad_filter "pciex8086,1234.terra,celes"
166pcidb_bad_filter "pciex8086,1234.fffff"
167pcidb_bad_filter "pciex8086,1234.abcd,"
168pcidb_bad_filter "pciex8086,1234.abcd."
169pcidb_bad_filter "pciex8086,1234.abcdqr"
170pcidb_bad_filter "pciex8086,1234.abcd,2,p"
171pcidb_bad_filter "pciex8086,1234.abcd,2000000000"
172pcidb_bad_filter "pciex8086,1234.abcd,kefka"
173pcidb_bad_filter "pciex8086,1234.abcd,34ultros"
174pcidb_bad_filter "pciclas"
175pcidb_bad_filter "pciclassedgar"
176pcidb_bad_filter "pciclass,sabin"
177pcidb_bad_filter "pciclass,0"
178pcidb_bad_filter "pciclass,013"
179pcidb_bad_filter "pciclass,01345"
180pcidb_bad_filter "pciclass,0134567"
181pcidb_bad_filter "pciclass,01,"
182pcidb_bad_filter "pciclass,010,"
183pcidb_bad_filter "pciclass,010aa,"
184pcidb_bad_filter "pciclass,0102as"
185pcidb_bad_filter "pciclass,0102.as"
186pcidb_bad_filter "pciclass,0102@as"
187pcidb_bad_filter "pciclass,010298aa"
188pcidb_bad_filter "pciclass,010298,"
189pcidb_bad_filter "pciclass,010298!"
190pcidb_bad_filter "pciclass,010298!shadow"
191pcidb_bad_filter "pciexclas"
192pcidb_bad_filter "pciexclassedgar"
193pcidb_bad_filter "pciexclass,sabin"
194pcidb_bad_filter "pciexclass,0"
195pcidb_bad_filter "pciexclass,013"
196pcidb_bad_filter "pciexclass,01345"
197pcidb_bad_filter "pciexclass,0134567"
198pcidb_bad_filter "pciexclass,01,"
199pcidb_bad_filter "pciexclass,010,"
200pcidb_bad_filter "pciexclass,010aa,"
201pcidb_bad_filter "pciexclass,0102as"
202pcidb_bad_filter "pciexclass,0102.as"
203pcidb_bad_filter "pciexclass,0102@as"
204pcidb_bad_filter "pciexclass,010298aa"
205pcidb_bad_filter "pciexclass,010298,"
206pcidb_bad_filter "pciexclass,010298!"
207pcidb_bad_filter "pciexclass,010298!shadow"
208
209#
210# Verify that if we ask for bad columns we error
211#
212pcidb_bad_args -p
213pcidb_bad_args -o
214pcidb_bad_args -o -p
215pcidb_bad_args -p -o terra
216pcidb_bad_args -p -o subclass -v
217pcidb_bad_args -v -d -c
218
219if (( pcidb_exit == 0 )); then
220	printf "All tests passed successfully!\n"
221fi
222
223exit $pcidb_exit
224