summaryrefslogtreecommitdiffstats
path: root/testprogs/blackbox/common_test_fns.inc
blob: df8fee0e032e85b7329509edde04ec671f91fec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Common tests
# Pulled out of existing tests to prevent duplication.
#
test_smbclient()
{
	name="$1"
	cmd="$2"
	unc="$3"
	shift
	shift
	shift
	subunit_start_test "$name"
	output=$($VALGRIND $smbclient $CONFIGURATION "$unc" -c "$cmd" "$@" 2>&1)
	status=$?
	if [ x$status = x0 ]; then
		subunit_pass_test "$name"
	else
		printf '%s' "$output" | subunit_fail_test "$name"
	fi
	return $status
}

test_smbclient_expect_failure()
{
	name="$1"
	cmd="$2"
	unc="$3"
	shift
	shift
	shift
	subunit_start_test "$name"
	output=$($VALGRIND $smbclient $CONFIGURATION "$unc" -c "$cmd" "$@" 2>&1)
	status=$?
	if [ x$status = x0 ]; then
		printf '%s' "$output" | subunit_fail_test "$name"
		return 1
	else
		subunit_pass_test "$name"
		return 0
	fi
}

test_rpcclient_grep()
{
	name="$1"
	cmd="$2"
	srv="$3"
	grep="$4"
	shift
	shift
	shift
	shift
	subunit_start_test "$name"
	output=$($VALGRIND $rpcclient $CONFIGURATION "$srv" -c "$cmd" "$@" 2>&1)
	status=$?
	if [ x$status != x0 ]; then
		printf '%s' "$output" | subunit_fail_test "$name"
		return $status
	fi
	printf '%s' "$output" | grep -q "$grep"
	gstatus=$?
	if [ x$gstatus = x0 ]; then
		subunit_pass_test "$name"
	else
		printf '%s' "$output" | subunit_fail_test "$name"
	fi
	return $gstatus
}

test_rpcclient_expect_failure_grep()
{
	name="$1"
	cmd="$2"
	srv="$3"
	grep="$4"
	shift
	shift
	shift
	shift
	subunit_start_test "$name"
	output=$($VALGRIND $rpcclient $CONFIGURATION "$srv" -c "$cmd" "$@" 2>&1)
	status=$?
	if [ x$status = x0 ]; then
		printf '%s' "$output" | subunit_fail_test "$name"
		return 1
	fi
	printf '%s' "$output" | grep -q "$grep"
	gstatus=$?
	if [ x$gstatus = x0 ]; then
		subunit_pass_test "$name"
	else
		printf '%s' "$output" | subunit_fail_test "$name"
	fi
	return $gstatus
}

kerberos_kinit()
{
	kinit_tool="${1}"
	principal="${2}"
	password="${3}"
	shift 3
	kbase=$(basename ${kinit_tool})
	if [ "${kbase}" = "samba4kinit" ]; then
		kpassfile=$(mktemp)
		echo $password >${kpassfile}
		$kinit_tool -c ${KRB5CCNAME} --password-file=${kpassfile} "$@" $principal
		status=$?
		rm -f ${kpassfile}
	else
		echo $password | $kinit_tool "$@" $principal
		status=$?
	fi
	return $status
}

remove_directory()
{
	local xdir=${1}
	shift

	if [ "$xdir" = "/" ] || [ ! -d "$xdir" ] || [ -z "$(ls -A "$xdir")" ]; then
		return
	fi

	rm -rf "$xdir"
}

# This function should be used to either use a system tool or one of our
# build dir if available.
#
# Examples:
#
#   system_or_builddir_binary ldbsearch ${BUILDDIR}
#   system_or_builddir_binary kinit ${BUILDDIR} samba4kinit
#
system_or_builddir_binary()
{
	_bin_name="${1}"
	_build_dir="${2}"
	_alt_bin_name="${3}"

	_bin="$(command -v ${_bin_name})"
	_bin_build_path="${_build_dir}/${_bin_name}"

	if [ -n "${_alt_bin_name}" ]; then
	    _bin_build_path="${_build_dir}/${_alt_bin_name}"
	fi

	if [ -x "${_bin_build_path}" ]; then
		_bin="${_bin_build_path}"
	fi

	echo "${_bin}"
}