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
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-cfgspace -d $filter >/dev/null; then
52		warn "failed to show-cfgspace with filter $filter"
53	else
54		printf "TEST PASSED: show-cfgspace -d $filter\n"
55	fi
56
57	if ! $pcieadm_prog save-cfgspace -d $filter "$pcieadm_tmp/out.bin"; then
58		warn "failed to use save-cfgspace -d $filter"
59	else
60		printf "TEST PASSED: save-cfgspace -d $filter\n"
61	fi
62}
63
64#
65# Before we begin execution, set up the environment such that we have a
66# standard locale and that umem will help us catch mistakes.
67#
68export LC_ALL=C.UTF-8
69export LD_PRELOAD=libumem.so
70export UMEM_DEBUG=default
71
72if [[ -n $PCIEADM ]]; then
73	pcieadm_prog=$PCIEADM
74fi
75
76if ! $pcieadm_prog show-devs >/dev/null; then
77	warn "failed to show devices"
78else
79	printf "successfully listed devices\n"
80fi
81
82if ! mkdir "$pcieadm_tmp"; then
83	warn "failed to create temporary directory"
84	exit $pcieadm_exit
85fi
86
87#
88# Verify that we can grab things based on bdf
89#
90pcieadm_bdf=$($pcieadm_prog show-devs -p -o bdf | \
91    awk '{ print $1; exit 0 }')
92if [[ -z "$pcieadm_bdf" ]]; then
93	warn "failed to obtain bdf based filter"
94else
95	pcieadm_validate_filter "$pcieadm_bdf"
96fi
97
98#
99# Verify based on device.
100#
101pcieadm_path=$($pcieadm_prog show-devs -p -o path | \
102    awk '{ print $1; exit 0 }')
103if [[ -z "$pcieadm_path" ]]; then
104	warn "failed to obtain path based filter"
105else
106	pcieadm_validate_filter "$pcieadm_path"
107fi
108
109#
110# Do the same based on the device name
111#
112pcieadm_dev=$($pcieadm_prog show-devs -p -o driver | \
113    awk '{ if ($1 != "--") { print $1; exit 0 } }')
114if [[ -z "$pcieadm_dev" ]]; then
115	warn "failed to obtain driver based filter"
116else
117	pcieadm_validate_filter "$pcieadm_dev"
118fi
119
120if ! $pcieadm_prog save-cfgspace -a "$pcieadm_tmp" > /dev/null; then
121	warn "failed to save all devices"
122else
123	printf "TEST PASSED: save-cfgspace -a\n"
124fi
125
126if (( pcieadm_exit == 0 )); then
127	printf "All tests passed successfully!\n"
128fi
129rm -rf "$pcieadm_tmp"
130exit $pcieadm_exit
131