s3:tests: Add a test to check json output of smbstatus
[Samba.git] / source3 / script / tests / test_smbstatus.sh
blob6b58580f0b9f0318893b3712941a33c03ad8ce9e
1 #!/bin/sh
3 # This runs smbstatus tests
5 if [ $# -lt 12 ]; then
6 echo "Usage: test_smbstatus.sh SERVER SERVER_IP DOMAIN USERNAME PASSWORD USERID LOCAL_PATH PREFIX SMBCLIENT CONFIGURATION PROTOCOL"
7 exit 1
8 fi
10 SERVER="${1}"
11 SERVER_IP="${2}"
12 DOMAIN="${3}"
13 USERNAME="${4}"
14 PASSWORD="${5}"
15 USERID="${6}"
16 LOCAL_PATH="${7}"
17 PREFIX="${8}"
18 SMBCLIENT="${9}"
19 SMBSTATUS="${10}"
20 CONFIGURATION="${11}"
21 PROTOCOL="${12}"
23 shift 12
25 RAWARGS="${CONFIGURATION} -m${PROTOCOL}"
26 ADDARGS="${RAWARGS} $@"
28 incdir=$(dirname $0)/../../../testprogs/blackbox
29 . $incdir/subunit.sh
31 failed=0
33 test_smbstatus()
35 local cmdfile=$PREFIX/smbclient_commands
36 local tmpfile=$PREFIX/smclient_lock_file
37 local file=smclient_lock_file
38 local cmd=""
39 local ret=0
40 local userid=$(id -u $USERNAME)
42 cat >$tmpfile <<EOF
43 What a Wurst!
44 EOF
45 cat >$cmdfile <<EOF
46 lcd $PREFIX_ABS
47 put $file
48 open $file
49 !UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $SMBSTATUS
50 close 1
51 rm $file
52 quit
53 EOF
55 cmd="CLI_FORCE_INTERACTIVE=yes $SMBCLIENT -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS --quiet < $cmdfile 2>&1"
56 eval echo "$cmd"
57 out=$(eval $cmd)
58 ret=$?
59 rm -f $cmpfile
60 rm -f $tmpfile
62 if [ $ret -ne 0 ]; then
63 echo "Failed to run smbclient with error $ret"
64 echo "$out"
65 false
66 return
69 echo "$out" | grep -c 'NT_STATUS_'
70 ret=$?
71 if [ $ret -eq 0 ]; then
72 echo "Failed: got an NT_STATUS error!"
73 echo "$out"
74 false
75 return
78 echo "$out" | grep "$userid[ ]*DENY_NONE"
79 ret=$?
80 if [ $ret != 0 ]; then
81 echo "Failed to find userid in smbstatus locked file output"
82 echo "$out"
83 false
84 return
87 return 0
90 test_smbstatus_resolve_uids()
92 local cmdfile=$PREFIX/smbclient_commands
93 local tmpfile=$PREFIX/smclient_lock_file
94 local file=smclient_lock_file
95 local cmd=""
96 local ret=0
97 local userid=$(id -u $USERNAME)
99 cat >$tmpfile <<EOF
100 What a Wurst!
102 cat >$cmdfile <<EOF
103 lcd $PREFIX_ABS
104 put $file
105 open $file
106 !UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $SMBSTATUS --resolve-uids
107 close 1
108 rm $file
109 quit
112 cmd="CLI_FORCE_INTERACTIVE=yes $SMBCLIENT -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS --quiet < $cmdfile 2>&1"
113 eval echo "$cmd"
114 out=$(eval $cmd)
115 ret=$?
116 rm -f $cmpfile
117 rm -f $tmpfile
119 if [ $ret -ne 0 ]; then
120 echo "Failed to run smbclient with error $ret"
121 echo "$out"
122 false
123 return
126 echo "$out" | grep -c 'NT_STATUS_'
127 ret=$?
128 if [ $ret -eq 0 ]; then
129 echo "Failed: got an NT_STATUS error!"
130 echo "$out"
131 false
132 return
135 echo "$out" | grep "$USERNAME[ ]*DENY_NONE"
136 ret=$?
137 if [ $ret != 0 ]; then
138 echo "Failed to find userid in smbstatus locked file output"
139 echo "$out"
140 false
141 return
144 return 0
147 test_smbstatus_output()
149 local cmdfile=$PREFIX/smbclient_commands
150 local tmpfile=$PREFIX/smbclient_lock_file
151 local file=smbclient_lock_file
152 local status_shares=smbstatus_output_shares
153 local status_processes=smbstatus_output_processes
154 local status_locks=smbstatus_output_locks
156 cat >$tmpfile <<EOF
157 Hello World!
159 cat >$cmdfile <<EOF
160 lcd $PREFIX_ABS
161 put $file
162 open $file
163 !UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $SMBSTATUS --shares > $status_shares
164 !UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $SMBSTATUS --processes > $status_processes
165 !UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $SMBSTATUS --locks > $status_locks
166 close 1
167 rm $file
168 quit
171 cmd="CLI_FORCE_INTERACTIVE=yes $SMBCLIENT -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS --quiet < $cmdfile 2>&1"
172 eval echo "$cmd"
173 out=$(eval $cmd)
174 ret=$?
176 rm -f $cmpfile
177 rm -f $tmpfile
179 if [ $ret -ne 0 ]; then
180 echo "Failed to run smbclient with error $ret"
181 echo "$out"
182 return 1
185 out=$(cat $PREFIX/$status_processes)
186 echo "$out" | grep -c 'PID *Username'
187 ret=$?
188 if [ $ret -eq 1 ]; then
189 echo "Failed: Could not start smbstatus"
190 echo "$out"
191 return 1
193 echo "$out" | grep -c "$USERNAME"
194 ret=$?
195 if [ $ret -eq 1 ]; then
196 echo "Failed: open connection not found"
197 echo "$out"
198 return 1
201 out=$(cat $PREFIX/$status_shares)
202 echo "$out" | grep -c 'Service *pid'
203 ret=$?
204 if [ $ret -eq 1 ]; then
205 echo "Failed: Could not start smbstatus"
206 echo "$out"
207 return 1
209 echo "$out" | grep -c "tmp"
210 ret=$?
211 if [ $ret -eq 1 ]; then
212 echo "Failed: shares not found"
213 echo "$out"
214 return 1
217 out=$(cat $PREFIX/$status_locks)
218 echo "$out" | grep -c "Locked files:"
219 ret=$?
220 if [ $ret -eq 1 ]; then
221 echo "Failed: locked file not found"
222 echo "$out"
223 return 1
225 echo "$out" | grep -c "$file"
226 ret=$?
227 if [ $ret -eq 1 ]; then
228 echo "Failed: wrong file locked"
229 echo "$out"
230 return 1
233 rm $PREFIX/$status_shares
234 rm $PREFIX/$status_processes
235 rm $PREFIX/$status_locks
237 return 0
240 test_smbstatus_json()
242 local cmdfile=$PREFIX/smbclient_commands
243 local tmpfile=$PREFIX/smbclient_lock_file
244 local file=smbclient_lock_file
245 local status_json=smbstatus_output_json
246 local status_json_long=smbstatus_output_json_long
248 cat > $tmpfile <<EOF
249 Hello World!
251 cat > $cmdfile <<EOF
252 lcd $PREFIX_ABS
253 put $file
254 open $file
255 posix
256 !UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $SMBSTATUS --json > $status_json
257 !UID_WRAPPER_INITIAL_RUID=0 UID_WRAPPER_INITIAL_EUID=0 $SMBSTATUS --json -vBN > $status_json_long
258 close 1
259 rm $file
260 quit
263 cmd="CLI_FORCE_INTERACTIVE=yes $SMBCLIENT -U$USERNAME%$PASSWORD //$SERVER/tmp -I $SERVER_IP $ADDARGS --quiet < $cmdfile 2>&1"
264 out=$(eval $cmd)
265 echo $out
266 ret=$?
268 rm -f $cmdfile
269 rm -f $tmpfile
271 if [ $ret -ne 0 ]; then
272 echo "Failed to run smbclient with error $ret"
273 echo "$out"
274 return 1
277 echo $out | grep -c 'JSON support not available, please install lib Jansson'
278 ret=$?
279 if [ $ret -eq 0 ]; then
280 subunit_start_test "test_smbstatus_json"
281 subunit_skip_test "test_smbstatus_json" <<EOF
282 Test needs Jansson
284 return 0
287 out=$(cat $PREFIX/$status_json)
288 ret=$?
289 if [ $ret -ne 0 ]; then
290 echo "Failed: Could not print json output with error $ret"
291 echo "$out"
292 return 1
295 out=$(cat $PREFIX/$status_json | jq ".")
296 echo $out | grep -c 'jq: not found'
297 ret=$?
298 if [ $ret -eq 0 ]; then
299 subunit_start_test "test_smbstatus_json"
300 subunit_skip_test "test_smbstatus_json" <<EOF
301 Test needs jq
303 return 0
306 out=$(cat $PREFIX/$status_json | jq ".")
307 ret=$?
308 if [ $ret -ne 0 ]; then
309 echo "Failed: Could not parse json output from smbstatus with error $ret"
310 echo "$out"
311 return 1
314 # keys in --json
315 expected='["open_files","sessions","smb_conf","tcons","timestamp","version"]'
316 out=$(cat $PREFIX/$status_json | jq keys -c)
317 if [ "$expected" != "$out" ]; then
318 echo "Failed: Unexpected keys in smbstatus --json"
319 echo "Expected: $expected"
320 echo "Output: $out"
321 return 1
324 # keys in --json -vBN
325 expected='["byte_range_locks","notifies","open_files","sessions","smb_conf","tcons","timestamp","version"]'
326 out=$(cat $PREFIX/$status_json_long | jq keys -c)
327 if [ "$expected" != "$out" ]; then
328 echo "Failed: Unexpected keys in smbstatus --json"
329 echo "Expected: $expected"
330 echo "Output: $out"
331 return 1
334 # shares information in --json
335 out=$(cat $PREFIX/$status_json | jq ".tcons|.[].machine")
336 if [ "\"$SERVER_IP\"" != "$out" ]; then
337 echo "Failed: Unexpected value for tcons.machine in smbstatus --json"
338 echo "Expected: $SERVER_IP"
339 echo "Output: $out"
340 return 1
342 out=$(cat $PREFIX/$status_json | jq ".tcons|.[].service")
343 if [ '"tmp"' != "$out" ]; then
344 echo "Failed: Unexpected value for tcons.service in smbstatus --json"
345 echo "Expected: tmp"
346 echo "Output: $out"
347 return 1
350 # session information in --json
351 out=$(cat $PREFIX/$status_json | jq ".sessions|.[].username")
352 if [ "\"$USER\"" != "$out" ]; then
353 echo "Failed: Unexpected value for sessions.username in smbstatus --json"
354 echo "Expected: $USER"
355 echo "Output: $out"
356 return 1
358 out=$(cat $PREFIX/$status_json | jq -c ".sessions|.[].signing")
359 expected='{"cipher":"AES-128-GMAC","degree":"partial"}'
360 if [ "$expected" != "$out" ]; then
361 echo "Failed: Unexpected value for sessions.signing in smbstatus --json"
362 echo "Expected: partial(AES-128-GMAC)"
363 echo "Output: $out"
364 return 1
366 out=$(cat $PREFIX/$status_json | jq ".sessions|.[].remote_machine")
367 if [ "\"$SERVER_IP\"" != "$out" ]; then
368 echo "Failed: Unexpected value for sessions.remote_machine in smbstatus --json"
369 echo "Expected: $SERVER_IP"
370 echo "Output: $out"
371 return 1
374 # open_files information in --json
375 out=$(cat $PREFIX/$status_json | jq ".open_files|.[].filename")
376 if [ "\"$file\"" != "$out" ]; then
377 echo "Failed: Unexpected value for open_files.denymode in smbstatus --json"
378 echo "Expected: \"$file\""
379 echo "Output: $out"
380 return 1
382 out=$(cat $PREFIX/$status_json | jq ".open_files|.[].opens|.[].access_mask.hex")
383 if [ '"0x00000003"' != "$out" ]; then
384 echo "Failed: Unexpected value for open_files.access_mask.hex in smbstatus --json"
385 echo "Expected: 0x00000003"
386 echo "Output: $out"
387 return 1
390 rm $PREFIX/$status_json
391 rm $PREFIX/$status_json_long
393 return 0
396 testit "plain" \
397 test_smbstatus ||
398 failed=$(expr $failed + 1)
400 testit "resolve_uids" \
401 test_smbstatus ||
402 failed=$(expr $failed + 1)
404 testit "test_output" \
405 test_smbstatus_output ||
406 failed=$(expr $failed + 1)
408 testit "test_json" \
409 test_smbstatus_json || \
410 failed=`expr $failed + 1`
412 testok $0 $failed