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 2022 Oxide Computer Company
16#
17
18#
19# Additional testing for pcieadm that requires us to actually have
20# privileges rather than relying on existing pieces.
21#
22
23unalias -a
24set -o pipefail
25
26pcieadm_arg0="$(basename $0)"
27pcieadm_prog="/usr/lib/pci/pcieadm"
28pcieadm_tmp="/tmp/pcieadm-priv.$$"
29pcieadm_bdf=""
30pcieadm_dev=""
31pcieadm_path=""
32
33warn()
34{
35	typeset msg="$*"
36	[[ -z "$msg" ]] && msg="failed"
37	echo "TEST FAILED: $pcieadm_arg0: $msg" >&2
38	pcieadm_exit=1
39}
40
41pcieadm_validate_filter()
42{
43	typeset filter="$1"
44
45	if ! $pcieadm_prog show-devs $filter >/dev/null; then
46		warn "failed to show-devs with filter $filter"
47	else
48		printf "TEST PASSED: show-devs $filter\n"
49	fi
50
51	if $pcieadm_prog show-devs $filter 9000/9000/9000; then
52		warn "show-devs $filter 9000/9000/9000, should have failed"
53	else
54		printf "TEST PASSED: show-devs $filter 9000/9000/9000\n"
55	fi
56
57	if ! $pcieadm_prog show-cfgspace -d $filter >/dev/null; then
58		warn "failed to show-cfgspace with filter $filter"
59	else
60		printf "TEST PASSED: show-cfgspace -d $filter\n"
61	fi
62
63	if ! $pcieadm_prog save-cfgspace -d $filter "$pcieadm_tmp/out.bin"; then
64		warn "failed to use save-cfgspace -d $filter"
65	else
66		printf "TEST PASSED: save-cfgspace -d $filter\n"
67	fi
68}
69
70#
71# Before we begin execution, set up the environment such that we have a
72# standard locale and that umem will help us catch mistakes.
73#
74export LC_ALL=C.UTF-8
75export LD_PRELOAD=libumem.so
76export UMEM_DEBUG=default
77
78if [[ -n $PCIEADM ]]; then
79	pcieadm_prog=$PCIEADM
80fi
81
82if ! $pcieadm_prog show-devs >/dev/null; then
83	warn "failed to show devices"
84else
85	printf "successfully listed devices\n"
86fi
87
88if ! mkdir "$pcieadm_tmp"; then
89	warn "failed to create temporary directory"
90	exit $pcieadm_exit
91fi
92
93#
94# Verify that we can grab things based on bdf
95#
96pcieadm_bdf=$($pcieadm_prog show-devs -p -o bdf | \
97    awk '{ print $1; exit 0 }')
98if [[ -z "$pcieadm_bdf" ]]; then
99	warn "failed to obtain bdf based filter"
100else
101	pcieadm_validate_filter "$pcieadm_bdf"
102fi
103
104#
105# Do the same based on the device instance.
106#
107pcieadm_dev=$($pcieadm_prog show-devs -p -o instance | \
108    awk '{ if ($1 != "--") { print $1; exit 0 } }')
109if [[ -z "$pcieadm_dev" ]]; then
110	warn "failed to obtain driver based filter"
111else
112	pcieadm_validate_filter "$pcieadm_dev"
113fi
114
115#
116# Verify based on the /devices path. Note, we use the device name to
117# seed this as if there is no device driver attached, the path may
118# overlap with another device on a PCI-only (non-express) based system.
119#
120pcieadm_path=$($pcieadm_prog show-devs -p -o path $pcieadm_dev | \
121    awk '{ print $1; exit 0 }')
122if [[ -z "$pcieadm_path" ]]; then
123	warn "failed to obtain path based filter"
124else
125	pcieadm_validate_filter "$pcieadm_path"
126fi
127
128#
129# Verify a bad filter doesn't work and results in an error.
130#
131if $pcieadm_prog show-devs /enoent >/dev/null; then
132	warn "pcieadm succeeded with bad filter '/enoent'"
133else
134	printf "TEST PASSED: show-devs /enoent\n"
135fi
136
137if ! $pcieadm_prog save-cfgspace -a "$pcieadm_tmp" > /dev/null; then
138	warn "failed to save all devices"
139else
140	printf "TEST PASSED: save-cfgspace -a\n"
141fi
142
143if (( pcieadm_exit == 0 )); then
144	printf "All tests passed successfully!\n"
145fi
146rm -rf "$pcieadm_tmp"
147exit $pcieadm_exit
148