f21a131f8407e238682afce436b288757c2d7ba9
[samba.git] / source3 / script / tests / test_smbget.sh
1 #!/bin/bash
2 #
3 # Blackbox test for smbget.
4 #
5
6 if [ $# -lt 7 ]; then
7 cat <<EOF
8 Usage: test_smbget SERVER SERVER_IP DOMAIN USERNAME PASSWORD WORKDIR SMBGET
9 EOF
10 exit 1;
11 fi
12
13 SERVER=${1}
14 SERVER_IP=${2}
15 DOMAIN=${3}
16 USERNAME=${4}
17 PASSWORD=${5}
18 WORKDIR=${6}
19 SMBGET="$VALGRIND ${7}"
20
21 TMPDIR="$SRCDIR_ABS/st/tmp"
22
23 incdir=`dirname $0`/../../../testprogs/blackbox
24 . $incdir/subunit.sh
25
26 create_test_data()
27 {
28         pushd $WORKDIR
29         dd if=/dev/urandom bs=1024 count=128 of=testfile
30         chmod 644 testfile
31         mkdir dir1
32         dd if=/dev/urandom bs=1024 count=128 of=dir1/testfile1
33         mkdir dir2
34         dd if=/dev/urandom bs=1024 count=128 of=dir2/testfile2
35         popd
36 }
37
38 remove_test_data()
39 {
40         rm -rf dir1 dir2 testfile
41         pushd $WORKDIR
42         rm -rf dir1 dir2 testfile
43         popd
44 }
45
46 test_singlefile_guest()
47 {
48         [ -e testfile ] && rm testfile
49         echo "$SMBGET -v -a smb://$SERVER_IP/smbget/testfile"
50         $SMBGET -v -a smb://$SERVER_IP/smbget/testfile
51         if [ $? -ne 0 ]; then
52                 echo 'ERROR: RC does not match, expected: 0'
53                 return 1
54         fi
55         cmp --silent $WORKDIR/testfile ./testfile
56         if [ $? -ne 0 ]; then
57                 echo 'ERROR: file content does not match'
58                 return 1
59         fi
60         return 0
61 }
62
63 test_singlefile_U()
64 {
65         [ -e testfile ] && rm testfile
66         $SMBGET -v -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
67         if [ $? -ne 0 ]; then
68                 echo 'ERROR: RC does not match, expected: 0'
69                 return 1
70         fi
71         cmp --silent $WORKDIR/testfile ./testfile
72         if [ $? -ne 0 ]; then
73                 echo 'ERROR: file content does not match'
74                 return 1
75         fi
76         return 0
77 }
78
79 test_singlefile_smburl()
80 {
81         [ -e testfile ] && rm testfile
82         $SMBGET -w $DOMAIN smb://$USERNAME:$PASSWORD@$SERVER_IP/smbget/testfile
83         if [ $? -ne 0 ]; then
84                 echo 'ERROR: RC does not match, expected: 0'
85                 return 1
86         fi
87         cmp --silent $WORKDIR/testfile ./testfile
88         if [ $? -ne 0 ]; then
89                 echo 'ERROR: file content does not match'
90                 return 1
91         fi
92         return 0
93 }
94
95 test_singlefile_rcfile()
96 {
97         [ -e testfile ] && rm testfile
98         echo "user $USERNAME%$PASSWORD" > $TMPDIR/rcfile
99         $SMBGET -vn -f $TMPDIR/rcfile smb://$SERVER_IP/smbget/testfile
100         rc=$?
101         rm -f $TMPDIR/rcfile
102         if [ $rc -ne 0 ]; then
103                 echo 'ERROR: RC does not match, expected: 0'
104                 return 1
105         fi
106         cmp --silent $WORKDIR/testfile ./testfile
107         if [ $? -ne 0 ]; then
108                 echo 'ERROR: file content does not match'
109                 return 1
110         fi
111         return 0
112 }
113
114 test_recursive_U()
115 {
116         [ -e testfile ] && rm testfile
117         [ -d dir1 ] && rm -rf dir1
118         [ -d dir2 ] && rm -rf dir2
119         $SMBGET -v -R -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/
120         if [ $? -ne 0 ]; then
121                 echo 'ERROR: RC does not match, expected: 0'
122                 return 1
123         fi
124
125         cmp --silent $WORKDIR/testfile ./testfile && \
126         cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 && \
127         cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
128         if [ $? -ne 0 ]; then
129                 echo 'ERROR: file content does not match'
130                 return 1
131         fi
132
133         return 0
134 }
135
136 test_resume()
137 {
138         [ -e testfile ] && rm testfile
139         cp $WORKDIR/testfile .
140         truncate -s 1024 testfile
141         $SMBGET -v -r -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
142         if [ $? -ne 0 ]; then
143                 echo 'ERROR: RC does not match, expected: 0'
144                 return 1
145         fi
146
147         cmp --silent $WORKDIR/testfile ./testfile
148         if [ $? -ne 0 ]; then
149                 echo 'ERROR: file content does not match'
150                 return 1
151         fi
152
153         return 0
154 }
155
156 test_resume_modified()
157 {
158         dd if=/dev/urandom bs=1024 count=2 of=testfile
159         $SMBGET -v -r -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
160         if [ $? -ne 1 ]; then
161                 echo 'ERROR: RC does not match, expected: 1'
162                 return 1
163         fi
164
165         return 0
166 }
167
168 test_update()
169 {
170         [ -e testfile ] && rm testfile
171         $SMBGET -v -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
172         if [ $? -ne 0 ]; then
173                 echo 'ERROR: RC does not match, expected: 0'
174                 return 1
175         fi
176
177         # secondary download should pass
178         $SMBGET -v -u -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
179         if [ $? -ne 0 ]; then
180                 echo 'ERROR: RC does not match, expected: 0'
181                 return 1
182         fi
183
184         echo "modified" >> testfile
185         # touch source to trigger new download
186         sleep 2
187         touch -m $WORKDIR/testfile
188         $SMBGET -v -u -U$USERNAME%$PASSWORD smb://$SERVER_IP/smbget/testfile
189         if [ $? -ne 0 ]; then
190                 echo 'ERROR: RC does not match, expected: 0'
191                 return 1
192         fi
193
194         cmp --silent $WORKDIR/testfile ./testfile
195         if [ $? -ne 0 ]; then
196                 echo 'ERROR: file content does not match'
197                 return 1
198         fi
199
200         return 0
201 }
202
203 create_test_data
204
205 pushd $TMPDIR
206
207 failed=0
208 testit "download single file as guest" test_singlefile_guest \
209         || failed=`expr $failed + 1`
210
211 testit "download single file with -U" test_singlefile_U \
212         || failed=`expr $failed + 1`
213
214 testit "download single file with smb URL" test_singlefile_smburl \
215         || failed=`expr $failed + 1`
216
217 testit "download single file with rcfile" test_singlefile_rcfile \
218         || failed=`expr $failed + 1`
219
220 testit "recursive download" test_recursive_U \
221         || failed=`expr $failed + 1`
222
223 testit "resume download" test_resume \
224         || failed=`expr $failed + 1`
225
226 testit "resume download (modified file)" test_resume_modified \
227         || failed=`expr $failed + 1`
228
229 testit "update" test_update \
230         || failed=`expr $failed + 1`
231
232 popd
233
234 remove_test_data
235
236 exit $failed