1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4# Kselftest framework requirement - SKIP code is 4.
5ksft_skip=4
6
7TESTNAME=bpftool_metadata
8BPF_FS=$(awk '$3 == "bpf" {print $2; exit}' /proc/mounts)
9BPF_DIR=$BPF_FS/test_$TESTNAME
10
11_cleanup()
12{
13	set +e
14	rm -rf $BPF_DIR 2> /dev/null
15}
16
17cleanup_skip()
18{
19	echo "selftests: $TESTNAME [SKIP]"
20	_cleanup
21
22	exit $ksft_skip
23}
24
25cleanup()
26{
27	if [ "$?" = 0 ]; then
28		echo "selftests: $TESTNAME [PASS]"
29	else
30		echo "selftests: $TESTNAME [FAILED]"
31	fi
32	_cleanup
33}
34
35if [ $(id -u) -ne 0 ]; then
36	echo "selftests: $TESTNAME [SKIP] Need root privileges"
37	exit $ksft_skip
38fi
39
40if [ -z "$BPF_FS" ]; then
41	echo "selftests: $TESTNAME [SKIP] Could not run test without bpffs mounted"
42	exit $ksft_skip
43fi
44
45if ! bpftool version > /dev/null 2>&1; then
46	echo "selftests: $TESTNAME [SKIP] Could not run test without bpftool"
47	exit $ksft_skip
48fi
49
50set -e
51
52trap cleanup_skip EXIT
53
54mkdir $BPF_DIR
55
56trap cleanup EXIT
57
58bpftool prog load metadata_unused.o $BPF_DIR/unused
59
60METADATA_PLAIN="$(bpftool prog)"
61echo "$METADATA_PLAIN" | grep 'a = "foo"' > /dev/null
62echo "$METADATA_PLAIN" | grep 'b = 1' > /dev/null
63
64bpftool prog --json | grep '"metadata":{"a":"foo","b":1}' > /dev/null
65
66bpftool map | grep 'metadata.rodata' > /dev/null
67
68rm $BPF_DIR/unused
69
70bpftool prog load metadata_used.o $BPF_DIR/used
71
72METADATA_PLAIN="$(bpftool prog)"
73echo "$METADATA_PLAIN" | grep 'a = "bar"' > /dev/null
74echo "$METADATA_PLAIN" | grep 'b = 2' > /dev/null
75
76bpftool prog --json | grep '"metadata":{"a":"bar","b":2}' > /dev/null
77
78bpftool map | grep 'metadata.rodata' > /dev/null
79
80rm $BPF_DIR/used
81
82exit 0
83