1#!/bin/sh
2#
3# Test 'net tdb' command.
4#
5# Verify that the command returns the correct information in the
6# expected format. The 'dump' option is tested, but the output is not
7# checked, since the internal data structure could change in the
8# future.
9#
10# Copyright (C) 2017 Christof Schmitt
11
12if [ $# -lt 7 ]; then
13cat <<EOF
14Usage: $0 SMBCLIENT SERVER SHARE USER PASS CONFIGURATION LOCALPATH LOCKDIR
15EOF
16exit 1;
17fi
18
19SMBCLIENT=$1
20SERVER=$2
21SHARE=$3
22USER=$4
23PASS=$5
24CONFIGURATION=$6
25LOCALPATH=$7
26LOCKDIR=$8
27
28FILENAME=net_tdb_testfile
29
30samba_tdbtool=tdbtool
31if test -x $BINDIR/tdbtool; then
32	samba_tdbtool=$BINDIR/tdbtool
33fi
34
35failed=0
36
37incdir=`dirname $0`/../../../testprogs/blackbox
38. $incdir/subunit.sh
39
40touch $LOCALPATH/$FILENAME
41
42printf "open %s\n"'!sleep 10'"\n" ${FILENAME} | \
43	$SMBCLIENT //$SERVER/$SHARE -U$USER%$PASS &
44SMBCLIENTPID=$!
45
46# Give smbclient a chance to open the file
47sleep 1
48
49testit "Looking for record key of open file" \
50	$samba_tdbtool $LOCKDIR/locking.tdb hexkeys || \
51	failed=$(expr $failed + 1)
52
53# The assumption here is that only one file is open, so only one
54# record can exist in the database.
55
56# Output of 'tdbtool hexkeys' is in this format:
57#[000] 01 FD 00 00 00 00 00 00  56 02 5C 00 00 00 00 00  ....... V.\....
58#[010] 00 00 00 00 00 00 00 00                           .......
59# Select only the hex data, remove space and join every thing together
60key=0x$($samba_tdbtool $LOCKDIR/locking.tdb hexkeys | \
61	grep '\[' | cut -c 7-56 | sed -e 's/ //g' | tr -d '\n')
62
63testit "Looking for open file in locking.tdb" \
64       $BINDIR/net $CONFIGURATION tdb locking $key || \
65   failed=$(expr $failed + 1)
66out=$($BINDIR/net $CONFIGURATION tdb locking $key)
67
68out=$($BINDIR/net $CONFIGURATION tdb locking $key | \
69	      grep 'Share path: ' | sed -e 's/Share path: \+//')
70testit "Verify pathname in output" \
71       test "$out" = "$LOCALPATH" || \
72	failed=$(expr $failed + 1)
73
74out=$($BINDIR/net $CONFIGURATION tdb locking $key | \
75	      grep 'Name:' | sed -e 's/Name: \+//')
76testit "Verify filename in output" \
77       test "$out" = "$FILENAME" || \
78	failed=$(expr $failed + 1)
79
80out=$($BINDIR/net $CONFIGURATION tdb locking $key | \
81	      grep 'Number of share modes:' | \
82	      sed -e 's/Number of share modes: \+//')
83testit "Verify number of share modes in output" \
84       test "$out" = "1" || \
85	failed=$(expr $failed + 1)
86
87testit "Complete record dump" \
88       $BINDIR/net $CONFIGURATION tdb locking $key dump || \
89	failed=$(expr $failed + 1)
90
91$BINDIR/net $CONFIGURATION tdb locking $key dump | grep -q $FILENAME
92RC=$?
93testit "Verify filename in dump output" \
94       test $RC = 0 || \
95	failed=$(expr $failed + 1)
96$BINDIR/net $CONFIGURATION tdb locking $key dump | grep -q $LOCALPATH
97RC=$?
98testit "Verify share path in dump output" \
99       test $RC = 0 || \
100	failed=$(expr $failed + 1)
101
102kill $SMBCLIENTPID
103
104testok $0 $failed
105