Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.50 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [ "$#" -ne 2 ]; then
  4.     echo "USAGE: $0 host port"
  5.     exit 0
  6. fi
  7.  
  8. USERNAME="Anonymous"
  9. PASS=""
  10.  
  11. RED='\033[0;31m'
  12. GREEN='\033[0;32m'
  13. WHITE='\033[m' # No Color
  14.  
  15. HOST=$1
  16. PORT=$2
  17. MKFIFO=`which mkfifo`
  18. PIPE=fifo
  19. OUT=outfile
  20. TAIL=`which tail`
  21. NC="`which nc` -C"
  22. TIMEOUT=1 #max time before reading server response
  23.  
  24.  
  25. getcode()
  26. {
  27.   sleep $TIMEOUT
  28.   local code=$1
  29.   echo "Waiting for $code reply-code"
  30.   local data=`$TAIL -n 1 $OUT |cat -e |grep "^$code.*[$]$" |wc -l`
  31.   return $data
  32. }
  33.  
  34. print_failed()
  35. {
  36.     echo "$1 test failed"
  37.     echo "Expected reply-code: $2"
  38.     echo "Received : ["`$TAIL -n 1 $OUT| cat -e`"]"
  39.     echo -e "${RED}KO${WHITE}"
  40. }
  41.  
  42. print_succeeded()
  43. {
  44.   echo "$1 test succeeded"
  45.   echo -e "${GREEN}OK${WHITE}"
  46.   kill_client 2>&1 >/dev/null
  47. }
  48.  
  49. launch_client()
  50. {
  51.   local host=$1
  52.   local port=$2
  53.  
  54.   $MKFIFO $PIPE
  55.   ($TAIL -f $PIPE 2>/dev/null | $NC $host $port &> $OUT &) >/dev/null 2>/dev/null
  56.  
  57.   echo "Connecting to $host : $port"
  58.   sleep $TIMEOUT
  59.   getcode 220
  60.   if [[ $? -eq 1 ]]; then
  61.     echo -e "Reply-code ${GREEN}OK${WHITE}"
  62.     return 1
  63.   else
  64.     echo "Connection to $host:$port failed"
  65.     echo "Expected reply-code: 220"
  66.     echo "Received : ["`tail -n 1 $OUT |cat -e`"]"
  67.     return 0
  68.   fi  
  69. }
  70.  
  71. launch_test()
  72. {
  73.   local test_name=$1
  74.   local cmd=$2
  75.   local code=$3
  76.  
  77.   echo "Sending [$cmd^M$]"
  78.   echo "$cmd" >$PIPE
  79.   getcode $code
  80.   if [[ ! $? -eq 1 ]]; then
  81.     print_failed "$test_name" "$code"
  82.   else
  83.     echo -e "Reply-code ${GREEN}OK${WHITE}"
  84.   fi
  85. }
  86.  
  87. kill_client()
  88. {
  89.   local nc=`which nc`
  90.  
  91.   if [ `pidof $TAIL | wc -l` -ne 0 ]
  92.   then
  93.     killall $TAIL &>/dev/null
  94.   fi
  95.   if [ `pidof $nc | wc -l` -ne 0 ]
  96.   then
  97.     killall $nc &>/dev/null
  98.   fi  
  99.   rm -f $PIPE $OUT &> /dev/null
  100. }
  101.  
  102. clean()
  103. {
  104.   rm -f $PIPE $OUT log &>/dev/null
  105. }
  106.  
  107. launch_client $HOST $PORT
  108. if [[ ! $? -eq 1 ]]; then
  109.     echo "KO"
  110.     kill_client
  111.     return
  112. fi
  113.  
  114. launch_test "PASS before USER" "PASS" 503
  115. launch_test "USER" "USER Test" 331
  116. launch_test "PASS" "PASS" 530
  117. launch_test "USER" "USER Anonymous" 331
  118. launch_test "PASS" "PASS" 230
  119. launch_test "NOOP" "NOOP" 200
  120. mkdir test
  121. launch_test "CWD directory exist" "CWD test" 250
  122. launch_test "CDUP" "CDUP" 250
  123. rmdir test
  124. launch_test "CWD directory doesn't exist" "CWD test" 550
  125. touch test
  126. launch_test "DELE file exist" "DELE test" 250
  127. launch_test "DELE file doesn't exist" "DELE test" 550
  128. launch_test "PWD" "PWD" 257
  129. launch_test "HELP" "HELP" 214
  130. launch_test "QUIT" "QUIT" 221
  131. clean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement