tests: avoid bash in shebang and fix wrong "==" in test
[kamenim/samba-autobuild/.git] / source3 / script / tests / test_smbclient_s3.sh
1 #!/bin/sh
2
3 # this runs the file serving tests that are expected to pass with samba3
4
5 if [ $# -lt 6 ]; then
6 cat <<EOF
7 Usage: test_smbclient_s3.sh SERVER SERVER_IP USERNAME PASSWORD USERID LOCAL_PATH
8 EOF
9 exit 1;
10 fi
11
12 SERVER="$1"
13 SERVER_IP="$2"
14 USERNAME="$3"
15 PASSWORD="$4"
16 USERID="$5"
17 LOCAL_PATH="$6"
18 SMBCLIENT="$VALGRIND ${SMBCLIENT:-$BINDIR/smbclient} $CONFIGURATION"
19 WBINFO="$VALGRIND ${WBINFO:-$BINDIR/wbinfo}"
20 shift 6
21 ADDARGS="$*"
22
23 test x"$TEST_FUNCTIONS_SH" != x"INCLUDED" && {
24 incdir=`dirname $0`
25 . $incdir/test_functions.sh
26 }
27
28 failed=0
29
30 # Test that a noninteractive smbclient does not prompt
31 test_noninteractive_no_prompt()
32 {
33     prompt="smb"
34
35     cmd='echo du | $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS 2>&1'
36     eval echo "$cmd"
37     out=`eval $cmd`
38
39     if [ $? != 0 ] ; then
40         echo "$out"
41         echo "command failed"
42         false
43         return
44     fi
45
46     echo "$out" | grep $prompt >/dev/null 2>&1
47
48     if [ $? = 0 ] ; then
49         # got a prompt .. fail
50         echo matched interactive prompt in non-interactive mode
51         false
52     else
53         true
54     fi
55 }
56
57 # Test that an interactive smbclient prompts to stdout
58 test_interactive_prompt_stdout()
59 {
60     prompt="smb"
61     tmpfile=/tmp/smbclient.in.$$
62
63     cat > $tmpfile <<EOF
64 du
65 quit
66 EOF
67
68     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
69     eval echo "$cmd"
70     out=`eval $cmd`
71     ret=$?
72     rm -f $tmpfile
73
74     if [ $ret != 0 ] ; then
75         echo "$out"
76         echo "command failed"
77         false
78         return
79     fi
80
81     echo "$out" | grep $prompt >/dev/null 2>&1
82
83     if [ $? = 0 ] ; then
84         # got a prompt .. succeed
85         true
86     else
87         echo failed to match interactive prompt on stdout
88         false
89     fi
90 }
91
92 # Test creating a bad symlink and deleting it.
93 test_bad_symlink()
94 {
95     prompt="posix_unlink deleted file /newname"
96     tmpfile=/tmp/smbclient.in.$$
97
98     cat > $tmpfile <<EOF
99 posix
100 posix_unlink newname
101 symlink badname newname
102 posix_unlink newname
103 quit
104 EOF
105
106     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
107     eval echo "$cmd"
108     out=`eval $cmd`
109     ret=$?
110     rm -f $tmpfile
111
112     if [ $ret != 0 ] ; then
113         echo "$out"
114         echo "failed create then delete bad symlink with error $ret"
115         false
116         return
117     fi
118
119     echo "$out" | grep "$prompt" >/dev/null 2>&1
120
121     ret=$?
122     if [ $ret = 0 ] ; then
123         # got the correct prompt .. succeed
124         true
125     else
126         echo "$out"
127         echo "failed create then delete bad symlink - grep failed with $ret"
128         false
129     fi
130 }
131
132 # Test creating a good symlink and deleting it by path.
133 test_good_symlink()
134 {
135     tmpfile=/tmp/smbclient.in.$$
136     slink_name="$LOCAL_PATH/slink"
137     slink_target="$LOCAL_PATH/slink_target"
138
139     touch $slink_target
140     ln -s $slink_target $slink_name
141     cat > $tmpfile <<EOF
142 del slink
143 quit
144 EOF
145
146     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
147     eval echo "$cmd"
148     out=`eval $cmd`
149     ret=$?
150     rm -f $tmpfile
151
152     if [ $ret != 0 ] ; then
153         echo "$out"
154         echo "failed delete good symlink with error $ret"
155         rm $slink_target
156         rm $slink_name
157         false
158         return
159     fi
160
161     if [ ! -e $slink_target ] ; then
162         echo "failed delete good symlink - symlink target deleted !"
163         rm $slink_target
164         rm $slink_name
165         false
166         return
167     fi
168
169     if [ -e $slink_name ] ; then
170         echo "failed delete good symlink - symlink still exists"
171         rm $slink_target
172         rm $slink_name
173         false
174     else
175         # got the correct prompt .. succeed
176         rm $slink_target
177         true
178     fi
179 }
180
181 # Test writing into a read-only directory (logon as guest) fails.
182 test_read_only_dir()
183 {
184     prompt="NT_STATUS_ACCESS_DENIED making remote directory"
185     tmpfile=/tmp/smbclient.in.$$
186
187 ##
188 ## We can't do this as non-root. We always have rights to
189 ## create the directory.
190 ##
191     if [ "$USERID" != 0 ] ; then
192         echo "skipping test_read_only_dir as non-root"
193         true
194         return
195     fi
196
197 ##
198 ## We can't do this with an encrypted connection. No credentials
199 ## to set up the channel.
200 ##
201     if [ "$ADDARGS" = "-e" ] ; then
202         echo "skipping test_read_only_dir with encrypted connection"
203         true
204         return
205     fi
206
207     cat > $tmpfile <<EOF
208 mkdir a_test_dir
209 quit
210 EOF
211
212     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
213     eval echo "$cmd"
214     out=`eval $cmd`
215     ret=$?
216     rm -f $tmpfile
217
218     if [ $ret != 0 ] ; then
219         echo "$out"
220         echo "failed writing into read-only directory with error $ret"
221         false
222         return
223     fi
224
225     echo "$out" | grep "$prompt" >/dev/null 2>&1
226
227     ret=$?
228     if [ $ret = 0 ] ; then
229         # got the correct prompt .. succeed
230         true
231     else
232         echo "$out"
233         echo "failed writing into read-only directory - grep failed with $ret"
234         false
235     fi
236 }
237
238 # Test reading an owner-only file (logon as guest) fails.
239 test_owner_only_file()
240 {
241     prompt="NT_STATUS_ACCESS_DENIED opening remote file"
242     tmpfile=/tmp/smbclient.in.$$
243
244 ##
245 ## We can't do this as non-root. We always have rights to
246 ## read the file.
247 ##
248     if [ "$USERID" != 0 ] ; then
249         echo "skipping test_owner_only_file as non-root"
250         true
251         return
252     fi
253
254 ##
255 ## We can't do this with an encrypted connection. No credentials
256 ## to set up the channel.
257 ##
258     if [ "$ADDARGS" = "-e" ] ; then
259         echo "skipping test_owner_only_file with encrypted connection"
260         true
261         return
262     fi
263
264     cat > $tmpfile <<EOF
265 get unreadable_file
266 quit
267 EOF
268
269     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U% //$SERVER/ro-tmp -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
270     eval echo "$cmd"
271     out=`eval $cmd`
272     ret=$?
273     rm -f $tmpfile
274
275     if [ $ret != 0 ] ; then
276         echo "$out"
277         echo "failed reading owner-only file with error $ret"
278         false
279         return
280     fi
281
282     echo "$out" | grep "$prompt" >/dev/null 2>&1
283
284     ret=$?
285     if [ $ret = 0 ] ; then
286         # got the correct prompt .. succeed
287         true
288     else
289         echo "$out"
290         echo "failed reading owner-only file - grep failed with $ret"
291         false
292     fi
293 }
294
295 # Test accessing an msdfs path.
296 test_msdfs_link()
297 {
298     tmpfile=/tmp/smbclient.in.$$
299     prompt="  msdfs-target  "
300
301     cat > $tmpfile <<EOF
302 ls
303 cd \\msdfs-src1
304 ls msdfs-target
305 quit
306 EOF
307
308     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/msdfs-share -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
309     eval echo "$cmd"
310     out=`eval $cmd`
311     ret=$?
312     rm -f $tmpfile
313
314     if [ $ret != 0 ] ; then
315         echo "$out"
316         echo "failed accessing \\msdfs-src1 link with error $ret"
317         false
318         return
319     fi
320
321     echo "$out" | grep "$prompt" >/dev/null 2>&1
322
323     ret=$?
324     if [ $ret != 0 ] ; then
325         echo "$out"
326         echo "failed listing \\msdfs-src1 - grep failed with $ret"
327         false
328     fi
329
330     cat > $tmpfile <<EOF
331 ls
332 cd \\deeppath\\msdfs-src2
333 ls msdfs-target
334 quit
335 EOF
336
337     cmd='CLI_FORCE_INTERACTIVE=yes $SMBCLIENT $CONFIGURATION "$@" -U$USERNAME%$PASSWORD //$SERVER/msdfs-share -I $SERVER_IP $ADDARGS < $tmpfile 2>&1'
338     eval echo "$cmd"
339     out=`eval $cmd`
340     ret=$?
341     rm -f $tmpfile
342
343     if [ $ret != 0 ] ; then
344         echo "$out"
345         echo "failed accessing \\deeppath\\msdfs-src2 link with error $ret"
346         false
347         return
348     fi
349
350     echo "$out" | grep "$prompt" >/dev/null 2>&1
351
352     ret=$?
353     if [ $ret != 0 ] ; then
354         echo "$out"
355         echo "failed listing \\deeppath\\msdfs-src2 - grep failed with $ret"
356         false
357         return
358     else
359         true
360         return
361     fi
362 }
363
364 # Test authenticating using the winbind ccache
365 test_ccache_access()
366 {
367     $WBINFO --ccache-save="${USERNAME}%${PASSWORD}"
368     $SMBCLIENT $CONFIGURATION //$SERVER_IP/tmp -C -U "${USERNAME}%" \
369         -c quit 2>&1
370     ret=$?
371
372     if [ $ret != 0 ] ; then
373         echo "smbclient failed to use cached credentials"
374         false
375         return
376     fi
377
378     $WBINFO --ccache-save="${USERNAME}%GarBage"
379     $SMBCLIENT $CONFIGURATION //$SERVER_IP/tmp -C -U "${USERNAME}%" \
380         -c quit 2>&1
381     ret=$?
382
383     if [ $ret -eq 0 ] ; then
384         echo "smbclient succeeded with wrong cached credentials"
385         false
386         return
387     fi
388
389     $WBINFO --logoff
390 }
391
392
393 testit "smbclient -L $SERVER_IP" $SMBCLIENT $CONFIGURATION -L $SERVER_IP -N -p 139 || failed=`expr $failed + 1`
394 testit "smbclient -L $SERVER -I $SERVER_IP" $SMBCLIENT $CONFIGURATION -L $SERVER -I $SERVER_IP -N -p 139 || failed=`expr $failed + 1`
395
396 testit "noninteractive smbclient does not prompt" \
397     test_noninteractive_no_prompt || \
398     failed=`expr $failed + 1`
399
400 testit "noninteractive smbclient -l does not prompt" \
401    test_noninteractive_no_prompt -l /tmp || \
402     failed=`expr $failed + 1`
403
404 testit "interactive smbclient prompts on stdout" \
405    test_interactive_prompt_stdout || \
406     failed=`expr $failed + 1`
407
408 testit "interactive smbclient -l prompts on stdout" \
409    test_interactive_prompt_stdout -l /tmp || \
410     failed=`expr $failed + 1`
411
412 testit "creating a bad symlink and deleting it" \
413    test_bad_symlink || \
414    failed=`expr $failed + 1`
415
416 testit "creating a good symlink and deleting it by path" \
417    test_good_symlink || \
418    failed=`expr $failed + 1`
419
420 testit "writing into a read-only directory fails" \
421    test_read_only_dir || \
422    failed=`expr $failed + 1`
423
424 testit "Reading a owner-only file fails" \
425    test_owner_only_file || \
426    failed=`expr $failed + 1`
427
428 testit "Accessing an MS-DFS link" \
429    test_msdfs_link || \
430    failed=`expr $failed + 1`
431
432 testit "ccache access works for smbclient" \
433     test_ccache_access || \
434     failed=`expr $failed + 1`
435
436 testok $0 $failed