Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.96 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 "##########  ${test_name}  ##########"
  78.   echo "Sending [$cmd^M$]"
  79.   echo "$cmd" >$PIPE
  80.   getcode $code
  81.   if [[ ! $? -eq 1 ]]; then
  82.     print_failed "$test_name" "$code"
  83.   else
  84.     echo -e "Reply-code ${GREEN}OK${WHITE}"
  85.   fi
  86. }
  87.  
  88. kill_client()
  89. {
  90.   local nc=`which nc`
  91.  
  92.   if [ `pidof $TAIL | wc -l` -ne 0 ]
  93.   then
  94.     killall $TAIL &>/dev/null
  95.   fi
  96.   if [ `pidof $nc | wc -l` -ne 0 ]
  97.   then
  98.     killall $nc &>/dev/null
  99.   fi  
  100.   rm -f $PIPE $OUT &> /dev/null
  101. }
  102.  
  103. clean()
  104. {
  105.   rm -f $PIPE $OUT log &>/dev/null
  106. }
  107.  
  108. rm -f   fifo
  109. rm -rf test
  110.  
  111. launch_client $HOST $PORT
  112. if [[ ! $? -eq 1 ]]; then
  113.     echo "KO"
  114.     kill_client
  115.     return
  116. fi
  117.  
  118. launch_test "NO LOG NOOP" "NOOP" 200
  119. launch_test "NO LOG CWD directory exist" "CWD test" 530
  120. launch_test "NO LOG CDUP" "CDUP" 530
  121. launch_test "NO LOG CWD directory doesn't exist" "CWD test" 530
  122. launch_test "NO LOG DELE file exist" "DELE test" 530
  123. launch_test "NO LOG DELE file doesn't exist" "DELE test" 530
  124. launch_test "NO LOG PWD" "PWD" 530
  125. launch_test "NO LOG HELP" "HELP" 214
  126.  
  127. launch_test "PASS before USER" "PASS " 503
  128. launch_test "USER" "USER Test" 331
  129. launch_test "PASS" "PASS " 530
  130. launch_test "USER" "USER Anonymous" 331
  131. launch_test "PASS" "PASS " 230
  132. launch_test "NOOP" "NOOP" 200
  133. mkdir test
  134. launch_test "CWD directory exist" "CWD test" 250
  135. launch_test "CDUP" "CDUP" 200
  136. rmdir test
  137. launch_test "CWD directory doesn't exist" "CWD test" 550
  138. touch test
  139. launch_test "DELE file exist" "DELE test" 250
  140. launch_test "DELE file doesn't exist" "DELE test" 550
  141. launch_test "PWD" "PWD" 257
  142. launch_test "HELP" "HELP" 214
  143. launch_test "QUIT" "QUIT" 221
  144. clean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement