1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015 Fujitsu Ltd.
4# Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
5#
6# Test du command with some basic options.
7
8TST_CNT=23
9TST_SETUP=setup
10TST_TESTFUNC=do_test
11TST_NEEDS_TMPDIR=1
12TST_NEEDS_CMDS="dd du stat"
13. tst_test.sh
14
15setup()
16{
17	ROD_SILENT mkdir basedir
18	ROD_SILENT cd basedir
19
20	ROD_SILENT dd if=/dev/zero of=testfile bs=1M count=10
21
22	ROD_SILENT mkdir -p testdir
23
24	ROD_SILENT ln -s ../testfile testdir/testsymlink
25
26	# Display values are in units of the first available SIZE
27	# from --block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and
28	# BLOCKSIZE environment variables. Here we need to
29	# set DU_BLOCK_SIZE to 1024 to ensure the result is expected.
30	export DU_BLOCK_SIZE=1024
31}
32
33du_test()
34{
35	local test_return
36
37	$1 > ../temp 2>&1
38	test_return=$?
39
40	if [ ${test_return} -ne 0 ]; then
41		grep -q -E "unrecognized option|invalid option" ../temp
42		if [ $? -eq 0 ]; then
43			tst_res TCONF "'$1' not supported"
44		else
45			tst_res TFAIL "'$1' failed"
46		fi
47		return
48	fi
49
50	grep -q $2 ../temp
51	if [ $? -eq 0 ]; then
52		tst_res TPASS "'$1' passed"
53	else
54		tst_res TFAIL "'$1' failed"
55		tst_res TINFO "Looking for '$2' in:"
56		cat ../temp
57	fi
58}
59
60block_size=512
61page_size=$(tst_getconf PAGESIZE)
62if [ "$page_size" -lt 1024 ]; then
63	tst_brk TBROK "Page size < 1024"
64fi
65page_size=$((page_size / 1024))
66
67# The output could be different in some systems, if we use du to
68# estimate file space usage with the same filesystem and the same size.
69# So we use the approximate value to check.
70check1="^10[2-3][0-9][0-9][[:space:]]\."
71check2="^10[2-3][0-9][0-9][[:space:]]testfile"
72check3="^\(0\|${page_size}\)[[:space:]]\.\/testdir\/testsymlink"
73check5="^20[4-6][0-9][0-9][[:space:]]\."
74check7="^10[4-5][0-9][0-9]\{4\}[[:space:]]\."
75check9="^10[2-3][0-9][0-9][[:space:]]total"
76check11="^10[2-3][0-9][0-9][[:space:]]testdir\/testsymlink"
77check14="^1[0,1]M[[:space:]]\."
78check16="^10[2-3][0-9][0-9][[:space:]]testdir\/"
79check20="^11M[[:space:]]\."
80check23="^[0-9]\{1,2\}[[:space:]]\."
81
82do_test()
83{
84	case $1 in
85	1) du_test "du" ${check1};;
86	2) du_test "du testfile" ${check2};;
87	3) du_test "du -a" ${check3};;
88	4) du_test "du --all" ${check3};;
89	5) du_test "du -B ${block_size}" ${check5};;
90	6) du_test "du --block-size=${block_size}" ${check5};;
91	7) du_test "du -b" ${check7};;
92	8) du_test "du --bytes" ${check7};;
93	9) du_test "du -c" ${check9};;
94	10) du_test "du --total" ${check9};;
95	11) du_test "du -D testdir/testsymlink" ${check11};;
96	12) du_test "du --dereference-args testdir/testsymlink" ${check11};;
97	13) du_test "du --max-depth=1" ${check1};;
98	14) du_test "du --human-readable" ${check14};;
99	15) du_test "du -k" ${check1};;
100	16) du_test "du -L testdir/" ${check16};;
101	17) du_test "du --dereference testdir/" ${check16};;
102	18) du_test "du -P" ${check1};;
103	19) du_test "du --no-dereference" ${check1};;
104	20) du_test "du --si" ${check20};;
105	21) du_test "du -s" ${check1};;
106	22) du_test "du --summarize" ${check1};;
107	23) du_test "du --exclude=testfile" ${check23};;
108	esac
109}
110
111tst_run
112