libnet: Fix Coverity ID 1634803 Dereference after null check
[samba4-gss.git] / source3 / script / tests / test_smbget.sh
blob8f8513d5b62779f18090a30dc4b2b0c4e61a93b7
1 #!/usr/bin/env bash
3 # Blackbox test for smbget.
6 if [ $# -lt 8 ]; then
7 cat <<EOF
8 Usage: test_smbget SERVER SERVER_IP DOMAIN REALM USERNAME PASSWORD WORKDIR SMBGET
9 EOF
10 exit 1
13 SERVER=${1}
14 SERVER_IP=${2}
15 DOMAIN=${3}
16 REALM=${4}
17 USERNAME=${5}
18 PASSWORD=${6}
19 DOMAIN_USER=${7}
20 DOMAIN_USER_PASSWORD=${8}
21 WORKDIR=${9}
22 SMBGET="$VALGRIND ${10}"
23 shift 10
25 TMPDIR="$SELFTEST_TMPDIR"
27 incdir=$(dirname $0)/../../../testprogs/blackbox
28 . $incdir/subunit.sh
29 . "${incdir}/common_test_fns.inc"
31 samba_kinit=$(system_or_builddir_binary kinit "${BINDIR}" samba4kinit)
32 samba_texpect="${BINDIR}/texpect"
34 create_test_data()
36 pushd $WORKDIR
37 dd if=/dev/urandom bs=1024 count=128 of=testfile
38 chmod 644 testfile
39 mkdir dir1
40 dd if=/dev/urandom bs=1024 count=128 of=dir1/testfile1
41 mkdir dir2
42 dd if=/dev/urandom bs=1024 count=128 of=dir2/testfile2
43 popd
46 remove_test_data()
48 pushd $WORKDIR
49 rm -rf dir1 dir2 testfile
50 popd
53 clear_download_area()
55 rm -rf dir1 dir2 testfile dir001 dir004 readable_file
58 test_singlefile_guest()
60 clear_download_area
61 echo "$SMBGET --verbose --guest smb://$SERVER_IP/smbget_guest/testfile"
62 $SMBGET --verbose --guest smb://$SERVER_IP/smbget_guest/testfile
63 if [ $? -ne 0 ]; then
64 echo 'ERROR: RC does not match, expected: 0'
65 return 1
67 cmp --silent $WORKDIR/testfile ./testfile
68 if [ $? -ne 0 ]; then
69 echo 'ERROR: file content does not match'
70 return 1
72 return 0
75 test_singlefile_U()
77 clear_download_area
78 $SMBGET --verbose -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
79 if [ $? -ne 0 ]; then
80 echo 'ERROR: RC does not match, expected: 0'
81 return 1
83 cmp --silent $WORKDIR/testfile ./testfile
84 if [ $? -ne 0 ]; then
85 echo 'ERROR: file content does not match'
86 return 1
88 return 0
91 test_singlefile_U_UPN()
93 clear_download_area
95 ${SMBGET} --verbose -U"${DOMAIN_USER}@${REALM}%${DOMAIN_USER_PASSWORD}" \
96 "smb://${SERVER_IP}/smbget/testfile"
97 ret=${?}
98 if [ ${ret} -ne 0 ]; then
99 echo 'ERROR: RC does not match, expected: 0'
100 return 1
103 cmp --silent "${WORKDIR}/testfile" ./testfile
104 ret=${?}
105 if [ ${ret} -ne 0 ]; then
106 echo 'ERROR: file content does not match'
107 return 1
110 return 0
113 test_singlefile_U_domain()
115 clear_download_area
117 ${SMBGET} --verbose -U"${DOMAIN}/${DOMAIN_USER}%${DOMAIN_USER_PASSWORD}" \
118 "smb://${SERVER_IP}/smbget/testfile"
119 ret=${?}
120 if [ ${ret} -ne 0 ]; then
121 echo 'ERROR: RC does not match, expected: 0'
122 return 1
125 cmp --silent "${WORKDIR}/testfile" ./testfile
126 ret=${?}
127 if [ ${ret} -ne 0 ]; then
128 echo 'ERROR: file content does not match'
129 return 1
132 return 0
135 test_singlefile_smburl()
137 clear_download_area
138 $SMBGET --workgroup $DOMAIN smb://${DOMAIN_USER}:$DOMAIN_USER_PASSWORD@$SERVER_IP/smbget/testfile
139 if [ $? -ne 0 ]; then
140 echo 'ERROR: RC does not match, expected: 0'
141 return 1
143 cmp --silent $WORKDIR/testfile ./testfile
144 if [ $? -ne 0 ]; then
145 echo 'ERROR: file content does not match'
146 return 1
148 return 0
151 test_singlefile_smburl2()
153 clear_download_area
154 $SMBGET "smb://$DOMAIN;${DOMAIN_USER}:$DOMAIN_USER_PASSWORD@$SERVER_IP/smbget/testfile"
155 if [ $? -ne 0 ]; then
156 echo 'ERROR: RC does not match, expected: 0'
157 return 1
159 cmp --silent $WORKDIR/testfile ./testfile
160 if [ $? -ne 0 ]; then
161 echo 'ERROR: file content does not match'
162 return 1
164 return 0
167 test_singlefile_smburl_interactive()
169 clear_download_area
171 tmpfile="$(mktemp --tmpdir="${TMPDIR}" expect_XXXXXXXXXX)"
173 cat >"${tmpfile}" <<EOF
174 expect Password for
175 send ${DOMAIN_USER_PASSWORD}\n
178 USER="hanswurst" ${samba_texpect} "${tmpfile}" ${SMBGET} "smb://${DOMAIN};${DOMAIN_USER}@${SERVER_IP}/smbget/testfile"
179 ret=$?
180 rm -f "${tmpfile}"
181 if [ ${ret} -ne 0 ]; then
182 echo 'ERROR: RC does not match, expected: 0'
183 return 1
185 cmp --silent $WORKDIR/testfile ./testfile
186 ret=$?
187 if [ ${ret} -ne 0 ]; then
188 echo 'ERROR: file content does not match'
189 return 1
191 return 0
194 test_singlefile_authfile()
196 clear_download_area
197 cat >"${TMPDIR}/authfile" << EOF
198 username = ${SERVER}/${USERNAME}
199 password = $PASSWORD
201 $SMBGET --verbose --authentication-file="${TMPDIR}/authfile" smb://$SERVER_IP/smbget/testfile
202 rc=$?
203 rm -f $TMPDIR/authfile
204 if [ $rc -ne 0 ]; then
205 echo 'ERROR: RC does not match, expected: 0'
206 return 1
208 cmp --silent $WORKDIR/testfile ./testfile
209 if [ $? -ne 0 ]; then
210 echo 'ERROR: file content does not match'
211 return 1
213 return 0
216 test_recursive_U()
218 clear_download_area
219 $SMBGET --verbose --recursive -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/
220 if [ $? -ne 0 ]; then
221 echo 'ERROR: RC does not match, expected: 0'
222 return 1
225 cmp --silent $WORKDIR/testfile ./testfile &&
226 cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 &&
227 cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
228 if [ $? -ne 0 ]; then
229 echo 'ERROR: file content does not match'
230 return 1
233 return 0
236 test_recursive_existing_dir()
238 clear_download_area
239 mkdir dir1
240 $SMBGET --verbose --recursive -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/
241 if [ $? -ne 0 ]; then
242 echo 'ERROR: RC does not match, expected: 0'
243 return 1
246 cmp --silent $WORKDIR/testfile ./testfile &&
247 cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 &&
248 cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
249 if [ $? -ne 0 ]; then
250 echo 'ERROR: file content does not match'
251 return 1
254 return 0
257 test_recursive_with_empty()
258 { # see Bug 13199
259 clear_download_area
260 # create some additional empty directories
261 mkdir -p $WORKDIR/dir001/dir002/dir003
262 mkdir -p $WORKDIR/dir004/dir005/dir006
263 $SMBGET --verbose --recursive -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/
264 rc=$?
265 rm -rf $WORKDIR/dir001
266 rm -rf $WORKDIR/dir004
267 if [ $rc -ne 0 ]; then
268 echo 'ERROR: RC does not match, expected: 0'
269 return 1
272 cmp --silent $WORKDIR/testfile ./testfile &&
273 cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 &&
274 cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
275 if [ $? -ne 0 ]; then
276 echo 'ERROR: file content does not match'
277 return 1
280 if [ ! -d dir001/dir002/dir003 ] || [ ! -d dir004/dir005/dir006 ]; then
281 echo 'ERROR: empty directories are not present'
282 return 1
285 return 0
288 test_resume()
290 clear_download_area
291 cp $WORKDIR/testfile .
292 truncate -s 1024 testfile
293 $SMBGET --verbose --resume -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
294 if [ $? -ne 0 ]; then
295 echo 'ERROR: RC does not match, expected: 0'
296 return 1
299 cmp --silent $WORKDIR/testfile ./testfile
300 if [ $? -ne 0 ]; then
301 echo 'ERROR: file content does not match'
302 return 1
305 return 0
308 test_resume_modified()
310 clear_download_area
311 dd if=/dev/urandom bs=1024 count=2 of=testfile
312 $SMBGET --verbose --resume -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
313 if [ $? -ne 1 ]; then
314 echo 'ERROR: RC does not match, expected: 1'
315 return 1
318 return 0
321 test_update()
323 clear_download_area
324 $SMBGET --verbose -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
325 if [ $? -ne 0 ]; then
326 echo 'ERROR: RC does not match, expected: 0'
327 return 1
330 # secondary download should pass
331 $SMBGET --verbose --update -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
332 if [ $? -ne 0 ]; then
333 echo 'ERROR: RC does not match, expected: 0'
334 return 1
337 echo "modified" >>testfile
338 # touch source to trigger new download
339 sleep 1
340 touch -m $WORKDIR/testfile
341 $SMBGET --verbose --update -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
342 if [ $? -ne 0 ]; then
343 echo 'ERROR: RC does not match, expected: 0'
344 return 1
347 cmp --silent $WORKDIR/testfile ./testfile
348 if [ $? -ne 0 ]; then
349 echo 'ERROR: file content does not match'
350 return 1
353 return 0
356 # Test accessing an msdfs path.
357 test_msdfs_link()
359 clear_download_area
361 ${SMBGET} --verbose "-U${SERVER}/${USERNAME}%${PASSWORD}" \
362 "smb://${SERVER}/msdfs-share/deeppath/msdfs-src2/readable_file"
363 ret=$?
364 if [ ${ret} -ne 0 ]; then
365 echo "ERROR: smbget failed with ${ret}"
366 return 1
369 return 0
372 test_msdfs_link_domain()
374 clear_download_area
376 ${SMBGET} --verbose "-U${DOMAIN}/${DOMAIN_USER}%${DOMAIN_USER_PASSWORD}" \
377 "smb://${SERVER}/msdfs-share/deeppath/msdfs-src2/readable_file"
378 ret=$?
379 if [ ${ret} -ne 0 ]; then
380 echo "ERROR: smbget failed with ${ret}"
381 return 1
384 return 0
387 test_msdfs_link_upn()
389 clear_download_area
391 ${SMBGET} --verbose "-U${DOMAIN_USER}@${REALM}%${DOMAIN_USER_PASSWORD}" \
392 "smb://${SERVER}/msdfs-share/deeppath/msdfs-src2/readable_file"
393 ret=$?
394 if [ ${ret} -ne 0 ]; then
395 echo "ERROR: smbget failed with ${ret}"
396 return 1
399 return 0
402 # Tests --limit-rate. Getting the testfile (128K in size) with --limit-rate 100
403 # (that is 100KB/s) should take at least 1 sec to complete.
404 test_limit_rate()
406 clear_download_area
407 echo "$SMBGET --verbose --guest --limit-rate 100 smb://$SERVER_IP/smbget_guest/testfile"
408 time_begin=$(date +%s)
409 $SMBGET --verbose --guest --limit-rate 100 smb://$SERVER_IP/smbget_guest/testfile
410 if [ $? -ne 0 ]; then
411 echo 'ERROR: RC does not match, expected: 0'
412 return 1
414 time_end=$(date +%s)
415 cmp --silent $WORKDIR/testfile ./testfile
416 if [ $? -ne 0 ]; then
417 echo 'ERROR: file content does not match'
418 return 1
420 if [ $((time_end - time_begin)) -lt 1 ]; then
421 echo 'ERROR: It should take at least 1s to transfer 128KB with rate 100KB/s'
422 return 1
424 return 0
427 test_encrypt()
429 clear_download_area
430 $SMBGET --verbose --encrypt -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
431 if [ $? -ne 0 ]; then
432 echo 'ERROR: RC does not match, expected: 0'
433 return 1
435 cmp --silent $WORKDIR/testfile ./testfile
436 if [ $? -ne 0 ]; then
437 echo 'ERROR: file content does not match'
438 return 1
441 clear_download_area
442 $SMBGET --verbose --client-protection=encrypt -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
443 if [ $? -ne 0 ]; then
444 echo 'ERROR: RC does not match, expected: 0'
445 return 1
447 cmp --silent $WORKDIR/testfile ./testfile
448 if [ $? -ne 0 ]; then
449 echo 'ERROR: file content does not match'
450 return 1
453 return 0
456 test_kerberos()
458 clear_download_area
460 KRB5CCNAME_PATH="${TMPDIR}/smget_krb5ccache"
461 rm -f "${KRB5CCNAME_PATH}"
463 KRB5CCNAME="FILE:${KRB5CCNAME_PATH}"
464 export KRB5CCNAME
465 kerberos_kinit "${samba_kinit}" \
466 "${DOMAIN_USER}@${REALM}" "${DOMAIN_USER_PASSWORD}"
467 if [ $? -ne 0 ]; then
468 echo 'Failed to get Kerberos ticket'
469 return 1
472 $SMBGET --verbose --use-krb5-ccache="${KRB5CCNAME}" \
473 smb://$SERVER/smbget/testfile
474 if [ $? -ne 0 ]; then
475 echo 'ERROR: RC does not match, expected: 0'
476 return 1
479 cmp --silent $WORKDIR/testfile ./testfile
480 if [ $? -ne 0 ]; then
481 echo 'ERROR: file content does not match'
482 return 1
485 rm -f "${KRB5CCNAME_PATH}"
487 return 0
490 test_kerberos_trust()
492 clear_download_area
494 $SMBGET --verbose --use-kerberos=required \
495 -U"${TRUST_F_BOTH_USERNAME}@${TRUST_F_BOTH_REALM}%${TRUST_F_BOTH_PASSWORD}" \
496 smb://$SERVER.${REALM}/smbget/testfile
497 if [ $? -ne 0 ]; then
498 echo 'ERROR: RC does not match, expected: 0'
499 return 1
502 cmp --silent $WORKDIR/testfile ./testfile
503 if [ $? -ne 0 ]; then
504 echo 'ERROR: file content does not match'
505 return 1
508 return 0
511 # TODO FIXME
512 # This test does not work, as we can't tell the libsmb code that the
513 # principal is an enterprise principal. We need support for enterprise
514 # principals in kerberos_kinit_password_ext() and a way to pass it via the
515 # credenitals structure and commandline options.
516 # It works if you do: kinit -E testdenied_upn@${REALM}.upn
518 # test_kerberos_upn_denied()
520 # set -x
521 # clear_download_area
523 # $SMBGET --verbose --use-kerberos=required \
524 # -U"testdenied_upn@${REALM}.upn%${DC_PASSWORD}" \
525 # "smb://${SERVER}.${REALM}/smbget/testfile" -d10
526 # if [ $? -ne 0 ]; then
527 # echo 'ERROR: RC does not match, expected: 0'
528 # return 1
529 # fi
531 # cmp --silent $WORKDIR/testfile ./testfile
532 # if [ $? -ne 0 ]; then
533 # echo 'ERROR: file content does not match'
534 # return 1
535 # fi
537 # return 0
540 create_test_data
542 pushd $TMPDIR
544 failed=0
545 testit "download single file as guest" test_singlefile_guest ||
546 failed=$(expr $failed + 1)
548 testit "download single file with -U" test_singlefile_U ||
549 failed=$(expr $failed + 1)
551 testit "download single file with --update and domain" test_singlefile_U_domain ||
552 failed=$((failed + 1))
554 testit "download single file with --update and UPN" test_singlefile_U_UPN ||
555 failed=$((failed + 1))
557 testit "download single file with smb URL" test_singlefile_smburl ||
558 failed=$(expr $failed + 1)
560 testit "download single file with smb URL including domain" \
561 test_singlefile_smburl2 ||
562 failed=$(expr $failed + 1)
564 testit "download single file with smb URL interactive" \
565 test_singlefile_smburl_interactive ||
566 failed=$(expr $failed + 1)
568 testit "download single file with authfile" test_singlefile_authfile ||
569 failed=$(expr $failed + 1)
571 testit "recursive download" test_recursive_U ||
572 failed=$(expr $failed + 1)
574 testit "recursive download (existing target dir)" test_recursive_existing_dir ||
575 failed=$(expr $failed + 1)
577 testit "recursive download with empty directories" test_recursive_with_empty ||
578 failed=$(expr $failed + 1)
580 testit "resume download" test_resume ||
581 failed=$(expr $failed + 1)
583 testit "resume download (modified file)" test_resume_modified ||
584 failed=$(expr $failed + 1)
586 testit "update" test_update ||
587 failed=$(expr $failed + 1)
589 testit "msdfs" test_msdfs_link ||
590 failed=$((failed + 1))
592 testit "msdfs.domain" test_msdfs_link_domain ||
593 failed=$((failed + 1))
595 testit "msdfs.upn" test_msdfs_link_upn ||
596 failed=$((failed + 1))
598 testit "limit rate" test_limit_rate ||
599 failed=$((failed + 1))
601 testit "encrypt" test_encrypt ||
602 failed=$((failed + 1))
604 testit "kerberos" test_kerberos ||
605 failed=$((failed + 1))
607 testit "kerberos_trust" test_kerberos_trust ||
608 failed=$((failed + 1))
610 # testit "kerberos_upn_denied" test_kerberos_upn_denied ||
611 # failed=$((failed + 1))
613 clear_download_area
615 popd # TMPDIR
617 remove_test_data
619 exit $failed