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