1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015 Fujitsu Ltd.
4# Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5#
6# Test basic functionality of lsmod command.
7
8TST_TESTFUNC=lsmod_test
9TST_NEEDS_TMPDIR=1
10TST_NEEDS_CMDS="lsmod"
11. tst_test.sh
12
13lsmod_matches_proc_modules()
14{
15	lsmod_output=$(lsmod | awk '!/Module/{print $1, $2, $3}' | sort)
16	if [ -z "$lsmod_output" ]; then
17		tst_brk TBROK "Failed to parse the output from lsmod"
18	fi
19
20	modules_output=$(awk '{print $1, $2, $3}' /proc/modules | sort)
21	if [ -z "$modules_output" ]; then
22		tst_brk TBROK "Failed to parse /proc/modules"
23	fi
24
25	if [ "$lsmod_output" != "$modules_output" ]; then
26		tst_res TINFO "lsmod output different from /proc/modules"
27
28		echo "$lsmod_output" > temp1
29		echo "$modules_output" > temp2
30		if tst_cmd_available diff; then
31			diff temp1 temp2
32		else
33			cat temp1 temp2
34		fi
35
36		return 1
37	fi
38	return 0
39}
40
41lsmod_test()
42{
43	for i in $(seq 1 5); do
44		if lsmod_matches_proc_modules; then
45			tst_res TPASS "'lsmod' passed"
46			return
47		fi
48		tst_res TINFO "Trying again"
49		sleep 1
50	done
51	tst_res TFAIL "'lsmod' doesn't match /proc/modules output"
52}
53
54tst_run
55