1#!/bin/sh
2
3# this runs a simple tarmode test
4
5if [ $# -lt 7 ]; then
6cat <<EOF
7Usage: test_smbclient_tarmode.sh SERVER SERVER_IP USERNAME PASSWORD LOCAL_PATH PREFIX SMBCLIENT [create|extract] <smbclient arguments>
8EOF
9exit 1;
10fi
11
12SERVER="$1"
13SERVER_IP="$2"
14USERNAME="$3"
15PASSWORD="$4"
16LOCAL_PATH="$5"
17PREFIX="$6"
18SMBCLIENT="$7"
19SMBCLIENT="$VALGRIND ${SMBCLIENT}"
20shift 7
21ADDARGS="$*"
22
23incdir=`dirname $0`/../../../testprogs/blackbox
24. $incdir/subunit.sh
25
26FAILCOUNT=0
27
28# Check command is available
29have_command() {
30	type "$1" > /dev/null 2>&1
31	return $?
32}
33
34# Create a test corpus
35create_test_data() {
36
37	local DIR="$1"
38	local BS=1024
39	local NUM_FILES=10
40	local NORND_COUNT=25
41
42	# Bomb if dir exists
43	if [ -e "$DIR" ]; then
44		echo "Test data directory '$DIR' already exists!"
45		false
46		return
47	fi
48
49	if ! mkdir -p "$DIR" > /dev/null 2>&1; then
50		echo "Couldn't create test data directory '$DIR'"
51		false
52		return
53	fi
54
55	local I=1
56	if have_command "od"; then # Use random file sizes
57		local RND_COUNT
58		for RND_COUNT in `od -An -N$NUM_FILES -tu1 < /dev/urandom`; do
59			if ! dd if=/dev/urandom of="$DIR/file.$I" bs=$BS count=$RND_COUNT > /dev/null 2>&1; then
60				echo "Couldn't create test file '$DIR/file.$I' (random size)"
61				false
62				return
63			fi
64			I=`expr $I + 1`
65		done
66	else # Fallback to same file sizes
67		while [ $I -le $NUM_FILES ]; do
68			if ! dd if=/dev/urandom of="$DIR/file.$I" bs=$BS count=$NORND_COUNT > /dev/null 2>&1; then
69				echo "Couldn't create test file '$DIR/file.$I' (static size)"
70				false
71				return
72			fi
73			I=`expr $I + 1`
74		done
75	fi
76
77	true
78	return
79
80}
81
82# Check that two directories are equivalent (In Data content)
83validate_data() {
84	local DIR1="$1"
85	local DIR2="$2"
86
87	diff -r "$DIR1" "$DIR2"
88	return $?
89}
90
91# Test tarmode -Tc
92test_tarmode_creation() {
93
94	# Clear temp data
95	rm -rf -- "$PREFIX"/tarmode > /dev/null 2>&1
96	rm -f "$PREFIX"/tarmode.tar > /dev/null 2>&1
97	rm -rf "$LOCAL_PATH" > /dev/null 2>&1
98
99	# Build the test data
100	if ! create_test_data "$LOCAL_PATH"; then
101		echo "Test data creation failed"
102		false
103		return
104	fi
105
106	# Create tarfile with smbclient
107	if ! $SMBCLIENT //$SERVER/tmp $CONFIGURATION -U$USERNAME%$PASSWORD -I $SERVER_IP -p 139 \
108			$ADDARGS -c "tarmode full" -Tc "$PREFIX/tarmode.tar" "/tarmode"; then
109		echo "Couldn't create tar file with tarmode -Tc"
110		false
111		return
112	fi
113
114	# Extract data to verify
115	if ! tar -xf "$PREFIX/tarmode.tar" -C "$PREFIX"; then
116		echo "Couldn't extract data from created tarfile"
117		false
118		return
119	fi
120
121	# Verify data
122	if ! validate_data "$PREFIX/tarmode" "$LOCAL_PATH"; then
123		echo "Data not equivalent"
124		false
125		return
126	fi
127
128	true
129	return
130
131}
132
133# Test tarmode -Tx
134test_tarmode_extraction() {
135
136	# Clear temp data
137	rm -rf -- "$PREFIX"/tarmode > /dev/null 2>&1
138	rm -f "$PREFIX"/tarmode.tar > /dev/null 2>&1
139	rm -rf "$LOCAL_PATH" > /dev/null 2>&1
140
141	# Build the test data
142	if ! create_test_data "$PREFIX/tarmode"; then
143		echo "Test data creation failed"
144		false
145		return
146	fi
147
148	# Create tarfile to extract on client
149	if ! tar -cf "$PREFIX/tarmode.tar" -C "$PREFIX" tarmode; then
150		echo "Couldn't create tar archive"
151		false
152		return
153	fi
154
155	# Extract tarfile with smbclient
156	if ! $SMBCLIENT //$SERVER/tmp $CONFIGURATION -U$USERNAME%$PASSWORD -I $SERVER_IP -p 139 \
157			$ADDARGS -c "tarmode full" -Tx "$PREFIX/tarmode.tar"; then
158		echo "Couldn't extact tar file with tarmode -Tx"
159		false
160		return
161	fi
162
163	# Verify data
164	if ! validate_data "$PREFIX/tarmode" "$LOCAL_PATH"; then
165		echo "Data not equivalent"
166		false
167		return
168	fi
169
170	true
171	return
172
173}
174
175testit "test_tarmode_creation" \
176	test_tarmode_creation || FAILCOUNT=`expr $FAILCOUNT + 1`
177
178testit "test_tarmode_extraction" \
179	test_tarmode_extraction || FAILCOUNT=`expr $FAILCOUNT + 1`
180
181testok $0 $FAILCOUNT
182