Codebase list ssshtest / bullseye-backports/main ssshtest
bullseye-backports/main

Tree @bullseye-backports/main (Download .tar.gz)

ssshtest @bullseye-backports/mainraw · history · blame

  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
#!/bin/bash

############################################################
#  Program: ssshtest
#  Authors : Ryan M Layer ryan.layer@gmail.com
#            Brent S Pedersen bpederse@gmail.com

# (c) 2015 - Ryan Layer, Brent Pedersen
############################################################

PROGRAM_NAME=sshtest
VERSION=0.1.5

RED='\033[0;31m'
BRED='\033[1;31m' # bold

GREEN='\033[0;32m'
BGREEN='\033[1;32m' # bold

BLUE='\033[0;33m'
BOLD='\033[0;1m'
NC='\033[0m' # No Color

PASS=" ${BGREEN}PASS${NC}"
FAIL=" ${BRED}FAIL${NC}"

COLS=`tput cols`

STDOUT_FILE=${TMPDIR:-/tmp}/o.$$
STDERR_FILE=${TMPDIR:-/tmp}/e.$$
OUTVAL=
ERRVAL=
RETVAL=
CMD=
VERBOSE=

TOTAL=0
SUCCESSES=0
FAILS=0

FLAG=0

STOP_ON_FAIL=0

trap report EXIT

TESTS_TO_RUN=($@)

RUN_NAME=

#{{{ Command line parsing
usage()
{
    cat << EOF

usage: $0 OPTIONS

OPTIONS can be:
    -h      Show this message
    -v      Print success messages
EOF
}

# Check options passed in.
while getopts "h v" OPTION
do
    case $OPTION in
        h)
            usage
            exit 1
            ;;
        v)
            VERBOSE=1
            ;;
        ?)
            usage
            exit
            ;;
    esac
done
#}}}

#{{{ exit codes
EX_OK=0

#The command was used incorrectly, e.g., with the wrong number of arguments, a
#bad flag, a bad syntax in a parameter, or whatever.
EX_USAGE=64

#The input data was incorrect in some way.  This should only be used for user's
#data and not system files.
EX_DATAERR=65

#An input file (not a system file) did not exist or was not readable.  This
#could also include errors like ``No message'' to a mailer (if it cared to
#catch it).
EX_NOINPUT=66

#The user specified did not exist.  This might be used for mail addresses or
#remote logins.
EX_NOUSER=67

#The host specified did not exist.  This is used in mail addresses or network
#requests.
EX_NOHOST=68

#A service is unavailable.  This can occur if a support program or file does
#not exist.  This can also be used as a catchall message when something you
#wanted to do doesn't work, but you don't know why.
EX_UNAVAILABLE=69

#An internal software error has been detected.  This should be limited to
#non-operating system related errors as possible.
EX_SOFTWARE=70

#An operating system error has been detected.  This is intended to be used for
#such things as ``cannot fork'', ``cannot create pipe'', or the like.  It
#includes things like getuid returning a user that does not exist in the passwd
#file.
EX_OSERR=71

#Some system file (e.g., /etc/passwd, /var/run/utmp, etc.) does not exist,
#cannot be opened, or has some sort of error (e.g., syntax error).
EX_OSFILE=72

#A (user specified) output file cannot be created.
EX_CANTCREAT=73

#An error occurred while doing I/O on some file.
EX_IOERR=74

#Temporary failure, indicating something that is not really an error.  In
#sendmail, this means that a mailer (e.g.) could not create a connection, and
#the request should be reattempted later.
EX_TEMPFAIL=75

#The remote system returned something that was ``not possible'' during a
#protocol exchange.
EX_PROTOCOL=76

#You did not have sufficient permission to perform the operation.  This is not
#intended for file system problems, which should use EX_NOINPUT or
#EX_CANTCREAT, but rather for higher level permissions.
EX_NOPERM=77

#Something was found in an unconfigured or misconfigured state.
EX_CONFIG=78
#}}}

#{{{ function report {
function report {
    rm -f $STDOUT_FILE $STDERR_FILE

    echo -e "\n$PROGRAM_NAME v$VERSION\n"

    if [ "$STOP_ON_FAIL" -ne "0" ]
    then
        if [ "$FAILS" -ne "0" ]
        then
            printf "${BOLD}TESTING STOPPED ON FIRST FAIL${NC}\n\n"
        fi
    fi

    printf "${NC}%-10s${NC}Tests\n" $TOTAL

    if [ "$FAILS" -ne "0" ]
    then
        printf "${BRED}%-10s${NC}${BOLD}Failures${NC}\n" $FAILS
        printf "${BGREEN}%-10s${NC}Successes\n" $SUCCESSES
    else
        printf "${BRED}%-10s${NC}Failures\n" $FAILS
        printf "${BGREEN}%-10s${NC}${BOLD}Successes${NC}\n" $SUCCESSES
    fi

    tear_down

    exit $FAILS
}
#}}}

#{{{ function run {
function run {
    RUN_NAME=$1
    shift

    FLAG=0
    if [ "${#TESTS_TO_RUN[*]}" -eq 0 ]
    then
        FLAG=1
	
	else 
	
		for i in "${TESTS_TO_RUN[@]}"
		do
			if [ "$RUN_NAME" == "$i" ]
			then
				FLAG=1
				break
			fi
		done
    fi

    if [ "$FLAG" -eq 0 ]
    then
        return
    else
        export $RUN_NAME=1
    fi

    CMD="$@"

    START=$(date +%s);
    O="$("$@" >$STDOUT_FILE 2>$STDERR_FILE)"
    RETVAL=$?
    END=$(date +%s);
    TOTAL_TIME=$((END-START))

    RUN_TIME="$TOTAL_TIME sec"


    OUTVAL=`cat $STDOUT_FILE`
    ERRVAL=`cat $STDERR_FILE`

    #make it pretty
    RUN_NAME=${BOLD}$RUN_NAME${NC}
	ELINES=$(wc -l $STDERR_FILE | awk '{print $1 }' &)
	OLINES=$(wc -l $STDOUT_FILE | awk '{print $1 }' &)
	wait
    echo -e "\n$RUN_NAME ran in $RUN_TIME with $ELINES/$OLINES lines to STDERR/OUT"
}
#}}}

#{{{ function print_exit_code {
function print_exit_code {
    case $1 in
        $EX_OK)
            echo "EX_OK"
            ;;
        $EX_USAGE)
            echo "EX_USAGE"
            ;;
        $EX_DATAERR)
            echo "EX_DATAERR"
            ;;
        $EX_NOINPUT)
            echo "EX_NOINPUT"
            ;;
        $EX_NOUSER)
            echo "EX_NOUSER"
            ;;
        $EX_NOHOST)
            echo "EX_NOHOST"
            ;;
        $EX_UNAVAILABLE)
            echo "EX_UNAVAILABLE"
            ;;
        $EX_SOFTWARE)
            echo "EX_SOFTWARE"
            ;;
        $EX_OSERR)
            echo "EX_OSERR"
            ;;
        $EX_OSFILE)
            echo "EX_OSFILE"
            ;;
        $EX_CANTCREAT)
            echo "EX_CANTCREAT"
            ;;
        $EX_IOERR)
            echo "EX_IOERR"
            ;;
        $EX_TEMPFAIL)
            echo "EX_TEMPFAIL"
            ;;
        $EX_PROTOCOL)
            echo "EX_PROTOCOL"
            ;;
        $EX_NOPERM)
            echo "EX_NOPERM"
            ;;
        $EX_CONFIG)
            echo "EX_CONFIG"
            ;;
        *)
            echo "Unknown code: $1"
    esac
}
#}}}

#{{{function assert_exit_code {
function assert_exit_code {
    
    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    E=$(print_exit_code $1)
    O=$(print_exit_code $RETVAL)
    if [ $RETVAL -ne $1 ]
    then
        FAILS=$((FAILS + 1))
        echo -e "$FAIL EXIT CODE (LINE $LINE)"
        echo -e "-->\texpected $E, observed $O"
        tail $STDERR_FILE
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    else
        SUCCESSES=$((SUCCESSES + 1))
        echo -e "$PASS EXIT CODE (LINE $LINE)"
        if [ $VERBOSE ] 
        then
            echo -e "-->\texpected $E, observed $O"
        fi
    fi
}
#}}}

#{{{ function assert_no_stdout {
function assert_no_stdout {

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ -n "$OUTVAL" ]
    then
        FAILS=$((FAILS + 1))
        echo -e "$FAIL NON-EMPTY STDOUT (LINE $LINE)"
        echo -e "-->\t$OUTVAL"
        tail $STDERR_FILE
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    else
        SUCCESSES=$((SUCCESSES + 1))
        echo -e "$PASS EMPTY STDOUT (LINE $LINE)"
    fi
}
#}}}

#{{{ function assert_no_stderr {
function assert_no_stderr {

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ -n "$ERRVAL" ]
    then
        FAILS=$((FAILS + 1))
        echo -e "$FAIL NON-EMPTY STDERR(LINE $LINE)"
        echo -e "-->\t$ERRVAL"
        tail $STDERR_FILE 
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    else
        SUCCESSES=$((SUCCESSES + 1))
        echo -e "$PASS EMPTY STDERR(LINE $LINE)"
    fi
}
#}}}

#{{{function assert_stderr {
function assert_stderr {

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ -z "$ERRVAL" ]
    then
        FAILS=$((FAILS + 1))
        echo -e "$FAIL EMPTY STDERR(LINE $LINE)"
        tail $STDERR_FILE
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    else
        SUCCESSES=$((SUCCESSES + 1))
        echo -e "$PASS EMPTY STDERR(LINE $LINE)"
        if [ $VERBOSE ] 
        then
            echo -e "-->\t$ERRVAL"
        fi
    fi
}
#}}}

#{{{function assert_stdout {
function assert_stdout {

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ -z "$OUTVAL" ]
    then
        FAILS=$((FAILS + 1))
        echo -e "$FAIL EMPTY STDOUT (LINE $LINE)"
        tail $STDERR_FILE 
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    else
        SUCCESSES=$((SUCCESSES + 1))
        echo -e "$PASS NON-EMPTY STDOUT (LINE $LINE)"
        if [ $VERBOSE ] 
        then
            echo -e "-->\t$ERRVAL"
        fi
    fi
}
#}}}

#{{{function assert_in_stderr {
function assert_in_stderr {

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ -z "$ERRVAL" ]
    then
        FAILS=$((FAILS + 1))
        echo -e "$FAIL EMPTY STDERR (LINE $LINE)"
        tail $STDERR_FILE
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    else
        if [[ $ERRVAL == *"$1"* ]]
        then
            SUCCESSES=$((SUCCESSES + 1))
            echo -e "$PASS STDERR CONTAINS \"$1\" (LINE $LINE)"
            if [ $VERBOSE ] 
            then
                echo -e "-->\t$ERRVAL"
            fi
        else
            FAILS=$((FAILS + 1))
            echo -e "$FAIL STDERR DOES NOT CONTAIN \"$1\" (LINE $LINE)"
            echo -e "-->\t$ERRVAL"
            tail $STDERR_FILE
            if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
        fi
    fi
}
#}}}

#{{{function assert_in_stdout {
function assert_in_stdout {

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ -z "$OUTVAL" ]
    then
        FAILS=$((FAILS + 1))
        echo -e "$FAIL EMPTY STDOUT (LINE $LINE)"
        tail $STDERR_FILE
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    else
        if [[ $OUTVAL == *"$1"* ]]
        then
            SUCCESSES=$((SUCCESSES + 1))
            echo -e "$PASS STDOUT CONTAINS \"$1\" (LINE $LINE)"
            if [ $VERBOSE ] 
            then
                echo -e "-->\t$OUTVAL"
            fi
        else
            FAILS=$((FAILS + 1))
            echo -e "$FAIL STDOUT DOES NOT CONTAIN \"$1\" (LINE $LINE)"
            echo -e "-->\t$OUTVAL"
            tail $STDERR_FILE
            if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
        fi
    fi
}
#}}}

#{{{ function assert_equal {
function assert_equal {

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ "$1" == "$2" ]
    then
        SUCCESSES=$((SUCCESSES + 1))
        echo -e "$PASS \"$1\" == \"$2\" (LINE $LINE)"
    else
        FAILS=$((FAILS + 1))
        echo -e "$FAIL \"$1\" != \"$2\" (LINE $LINE)"
        tail $STDERR_FILE
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    fi
}
#}}}

#{{{ function assert_true {
function assert_true {

    COMMAND=("$@")
    RES=`${COMMAND[@]}`
    echo $RES || "AAAAAAAAAAA"

    if [ "$FLAG" -eq 0 ];then return; fi

    LINE=$(caller | cut -d " " -f1)

    TOTAL=$((TOTAL + 1))
    if [ "${COMMAND[@]}" == true ]
    then
        SUCCESSES=$((SUCCESSES + 1))
        echo -e "$PASS $* (LINE $LINE)"
    else
        FAILS=$((FAILS + 1))
        echo -e "$FAIL $* (LINE $LINE)"
        tail $STDERR_FILE
        if [ $STOP_ON_FAIL -ne "0" ];then exit; fi
    fi
}
#}}}

#{{{function tear_down 
function tear_down 
{
    :
    #define this function in your test to clean things up in the end
}
#}}}