summaryrefslogtreecommitdiffstats
path: root/source3/script/tests/test_smbget.sh
blob: acc492d11c72c45bc2f1a84911111bd038608a4d (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#!/usr/bin/env bash
#
# Blackbox test for smbget.
#

if [ $# -lt 8 ]; then
	cat <<EOF
Usage: test_smbget SERVER SERVER_IP DOMAIN REALM USERNAME PASSWORD WORKDIR SMBGET
EOF
	exit 1
fi

SERVER=${1}
SERVER_IP=${2}
DOMAIN=${3}
REALM=${4}
USERNAME=${5}
PASSWORD=${6}
DOMAIN_USER=${7}
DOMAIN_USER_PASSWORD=${8}
WORKDIR=${9}
SMBGET="$VALGRIND ${10}"
shift 10

TMPDIR="$SELFTEST_TMPDIR"

incdir=$(dirname $0)/../../../testprogs/blackbox
. $incdir/subunit.sh
. "${incdir}/common_test_fns.inc"

samba_kinit=$(system_or_builddir_binary kinit "${BINDIR}" samba4kinit)
samba_texpect="${BINDIR}/texpect"

create_test_data()
{
	pushd $WORKDIR
	dd if=/dev/urandom bs=1024 count=128 of=testfile
	chmod 644 testfile
	mkdir dir1
	dd if=/dev/urandom bs=1024 count=128 of=dir1/testfile1
	mkdir dir2
	dd if=/dev/urandom bs=1024 count=128 of=dir2/testfile2
	popd
}

remove_test_data()
{
	pushd $WORKDIR
	rm -rf dir1 dir2 testfile
	popd
}

clear_download_area()
{
	rm -rf dir1 dir2 testfile dir001 dir004 readable_file
}

test_singlefile_guest()
{
	clear_download_area
	echo "$SMBGET --verbose --guest smb://$SERVER_IP/smbget_guest/testfile"
	$SMBGET --verbose --guest smb://$SERVER_IP/smbget_guest/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi
	return 0
}

test_singlefile_U()
{
	clear_download_area
	$SMBGET --verbose -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi
	return 0
}

test_singlefile_U_UPN()
{
	clear_download_area

	${SMBGET} --verbose -U"${DOMAIN_USER}@${REALM}%${DOMAIN_USER_PASSWORD}" \
		"smb://${SERVER_IP}/smbget/testfile"
	ret=${?}
	if [ ${ret} -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent "${WORKDIR}/testfile" ./testfile
	ret=${?}
	if [ ${ret} -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

test_singlefile_U_domain()
{
	clear_download_area

	${SMBGET} --verbose -U"${DOMAIN}/${DOMAIN_USER}%${DOMAIN_USER_PASSWORD}" \
		"smb://${SERVER_IP}/smbget/testfile"
	ret=${?}
	if [ ${ret} -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent "${WORKDIR}/testfile" ./testfile
	ret=${?}
	if [ ${ret} -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

test_singlefile_smburl()
{
	clear_download_area
	$SMBGET --workgroup $DOMAIN smb://${DOMAIN_USER}:$DOMAIN_USER_PASSWORD@$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi
	return 0
}

test_singlefile_smburl2()
{
	clear_download_area
	$SMBGET "smb://$DOMAIN;${DOMAIN_USER}:$DOMAIN_USER_PASSWORD@$SERVER_IP/smbget/testfile"
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi
	return 0
}

test_singlefile_smburl_interactive()
{
	clear_download_area

	tmpfile="$(mktemp --tmpdir="${TMPDIR}" expect_XXXXXXXXXX)"

	cat >"${tmpfile}" <<EOF
expect Password for
send ${DOMAIN_USER_PASSWORD}\n
EOF

	USER="hanswurst" ${samba_texpect} "${tmpfile}" ${SMBGET} "smb://${DOMAIN};${DOMAIN_USER}@${SERVER_IP}/smbget/testfile"
	ret=$?
	rm -f "${tmpfile}"
	if [ ${ret} -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	ret=$?
	if [ ${ret} -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi
	return 0
}

test_singlefile_authfile()
{
	clear_download_area
	cat >"${TMPDIR}/authfile" << EOF
username = ${SERVER}/${USERNAME}
password = $PASSWORD
EOF
	$SMBGET --verbose --authentication-file="${TMPDIR}/authfile" smb://$SERVER_IP/smbget/testfile
	rc=$?
	rm -f $TMPDIR/authfile
	if [ $rc -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi
	return 0
}

test_recursive_U()
{
	clear_download_area
	$SMBGET --verbose --recursive -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent $WORKDIR/testfile ./testfile &&
		cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 &&
		cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

test_recursive_existing_dir()
{
	clear_download_area
	mkdir dir1
	$SMBGET --verbose --recursive -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent $WORKDIR/testfile ./testfile &&
		cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 &&
		cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

test_recursive_with_empty()
{ # see Bug 13199
	clear_download_area
	# create some additional empty directories
	mkdir -p $WORKDIR/dir001/dir002/dir003
	mkdir -p $WORKDIR/dir004/dir005/dir006
	$SMBGET --verbose --recursive -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/
	rc=$?
	rm -rf $WORKDIR/dir001
	rm -rf $WORKDIR/dir004
	if [ $rc -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent $WORKDIR/testfile ./testfile &&
		cmp --silent $WORKDIR/dir1/testfile1 ./dir1/testfile1 &&
		cmp --silent $WORKDIR/dir2/testfile2 ./dir2/testfile2
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	if [ ! -d dir001/dir002/dir003 ] || [ ! -d dir004/dir005/dir006 ]; then
		echo 'ERROR: empty directories are not present'
		return 1
	fi

	return 0
}

test_resume()
{
	clear_download_area
	cp $WORKDIR/testfile .
	truncate -s 1024 testfile
	$SMBGET --verbose --resume -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

test_resume_modified()
{
	clear_download_area
	dd if=/dev/urandom bs=1024 count=2 of=testfile
	$SMBGET --verbose --resume -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 1 ]; then
		echo 'ERROR: RC does not match, expected: 1'
		return 1
	fi

	return 0
}

test_update()
{
	clear_download_area
	$SMBGET --verbose -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	# secondary download should pass
	$SMBGET --verbose --update -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	echo "modified" >>testfile
	# touch source to trigger new download
	sleep 1
	touch -m $WORKDIR/testfile
	$SMBGET --verbose --update -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

# Test accessing an msdfs path.
test_msdfs_link()
{
	clear_download_area

	${SMBGET} --verbose "-U${SERVER}/${USERNAME}%${PASSWORD}" \
		"smb://${SERVER}/msdfs-share/deeppath/msdfs-src2/readable_file"
	ret=$?
	if [ ${ret} -ne 0 ]; then
		echo "ERROR: smbget failed with ${ret}"
		return 1
	fi

	return 0
}

test_msdfs_link_domain()
{
	clear_download_area

	${SMBGET} --verbose "-U${DOMAIN}/${DOMAIN_USER}%${DOMAIN_USER_PASSWORD}" \
		"smb://${SERVER}/msdfs-share/deeppath/msdfs-src2/readable_file"
	ret=$?
	if [ ${ret} -ne 0 ]; then
		echo "ERROR: smbget failed with ${ret}"
		return 1
	fi

	return 0
}

test_msdfs_link_upn()
{
	clear_download_area

	${SMBGET} --verbose "-U${DOMAIN_USER}@${REALM}%${DOMAIN_USER_PASSWORD}" \
		"smb://${SERVER}/msdfs-share/deeppath/msdfs-src2/readable_file"
	ret=$?
	if [ ${ret} -ne 0 ]; then
		echo "ERROR: smbget failed with ${ret}"
		return 1
	fi

	return 0
}

# Tests --limit-rate. Getting the testfile (128K in size) with --limit-rate 100
# (that is 100KB/s) should take at least 1 sec to complete.
test_limit_rate()
{
	clear_download_area
	echo "$SMBGET --verbose --guest --limit-rate 100 smb://$SERVER_IP/smbget_guest/testfile"
	time_begin=$(date +%s)
	$SMBGET --verbose --guest --limit-rate 100 smb://$SERVER_IP/smbget_guest/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	time_end=$(date +%s)
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi
	if [ $((time_end - time_begin)) -lt 1 ]; then
		echo 'ERROR: It should take at least 1s to transfer 128KB with rate 100KB/s'
		return 1
	fi
	return 0
}

test_encrypt()
{
	clear_download_area
	$SMBGET --verbose --encrypt -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	clear_download_area
	$SMBGET --verbose --client-protection=encrypt -U${SERVER}/${USERNAME}%$PASSWORD smb://$SERVER_IP/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi
	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

test_kerberos()
{
	clear_download_area

	KRB5CCNAME_PATH="${TMPDIR}/smget_krb5ccache"
	rm -f "${KRB5CCNAME_PATH}"

	KRB5CCNAME="FILE:${KRB5CCNAME_PATH}"
	export KRB5CCNAME
	kerberos_kinit "${samba_kinit}" \
		"${DOMAIN_USER}@${REALM}" "${DOMAIN_USER_PASSWORD}"
	if [ $? -ne 0 ]; then
		echo 'Failed to get Kerberos ticket'
		return 1
	fi

	$SMBGET --verbose --use-krb5-ccache="${KRB5CCNAME}" \
		smb://$SERVER/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	rm -f "${KRB5CCNAME_PATH}"

	return 0
}

test_kerberos_trust()
{
	clear_download_area

	$SMBGET --verbose --use-kerberos=required \
		-U"${TRUST_F_BOTH_USERNAME}@${TRUST_F_BOTH_REALM}%${TRUST_F_BOTH_PASSWORD}" \
		smb://$SERVER.${REALM}/smbget/testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: RC does not match, expected: 0'
		return 1
	fi

	cmp --silent $WORKDIR/testfile ./testfile
	if [ $? -ne 0 ]; then
		echo 'ERROR: file content does not match'
		return 1
	fi

	return 0
}

# TODO FIXME
# This test does not work, as we can't tell the libsmb code that the
# principal is an enterprice principal. We need support for enterprise
# principals in kerberos_kinit_password_ext() and a way to pass it via the
# credenitals structure and commandline options.
# It works if you do: kinit -E testdenied_upn@${REALM}.upn
#
# test_kerberos_upn_denied()
# {
# 	set -x
# 	clear_download_area
#
# 	$SMBGET --verbose --use-kerberos=required \
# 		-U"testdenied_upn@${REALM}.upn%${DC_PASSWORD}" \
# 		"smb://${SERVER}.${REALM}/smbget/testfile" -d10
# 	if [ $? -ne 0 ]; then
# 		echo 'ERROR: RC does not match, expected: 0'
# 		return 1
# 	fi
#
# 	cmp --silent $WORKDIR/testfile ./testfile
# 	if [ $? -ne 0 ]; then
# 		echo 'ERROR: file content does not match'
# 		return 1
# 	fi
#
# 	return 0
# }

create_test_data

pushd $TMPDIR

failed=0
testit "download single file as guest" test_singlefile_guest ||
	failed=$(expr $failed + 1)

testit "download single file with -U" test_singlefile_U ||
	failed=$(expr $failed + 1)

testit "download single file with --update and domain" test_singlefile_U_domain ||
	failed=$((failed + 1))

testit "download single file with --update and UPN" test_singlefile_U_UPN ||
	failed=$((failed + 1))

testit "download single file with smb URL" test_singlefile_smburl ||
	failed=$(expr $failed + 1)

testit "download single file with smb URL including domain" \
	test_singlefile_smburl2 ||
	failed=$(expr $failed + 1)

testit "download single file with smb URL interactive" \
	test_singlefile_smburl_interactive ||
	failed=$(expr $failed + 1)

testit "download single file with authfile" test_singlefile_authfile ||
	failed=$(expr $failed + 1)

testit "recursive download" test_recursive_U ||
	failed=$(expr $failed + 1)

testit "recursive download (existing target dir)" test_recursive_existing_dir ||
	failed=$(expr $failed + 1)

testit "recursive download with empty directories" test_recursive_with_empty ||
	failed=$(expr $failed + 1)

testit "resume download" test_resume ||
	failed=$(expr $failed + 1)

testit "resume download (modified file)" test_resume_modified ||
	failed=$(expr $failed + 1)

testit "update" test_update ||
	failed=$(expr $failed + 1)

testit "msdfs" test_msdfs_link ||
	failed=$((failed + 1))

testit "msdfs.domain" test_msdfs_link_domain ||
	failed=$((failed + 1))

testit "msdfs.upn" test_msdfs_link_upn ||
	failed=$((failed + 1))

testit "limit rate" test_limit_rate ||
	failed=$((failed + 1))

testit "encrypt" test_encrypt ||
	failed=$((failed + 1))

testit "kerberos" test_kerberos ||
	failed=$((failed + 1))

testit "kerberos_trust" test_kerberos_trust ||
	failed=$((failed + 1))

# testit "kerberos_upn_denied" test_kerberos_upn_denied ||
# 	failed=$((failed + 1))

clear_download_area

popd # TMPDIR

remove_test_data

exit $failed