tests.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/zsh
  2. tmpf="/tmp/toto"
  3. tests=(
  4. ## Execution ##
  5. "ls" # test execution simple
  6. "cat auteur" # execution simple
  7. "ls -l" # avec option
  8. "ls ~" # avec tilde
  9. "unsetenv PATH; ls;/bin/ls" # execution ls et /bin/ls sans PATH
  10. "/bin/ls" # execution avec le chemin entier
  11. "naritse" # cmd, élément qui n’existe pas
  12. "./21sh -c ls" # execution avec './'
  13. "./anrsiute" # execution élément qui n’existe pas
  14. ### permissions
  15. "/sbin/yubikey_shell" # execution sans les droits d’execution
  16. "yubikey_shell" #
  17. "touch $tmpf ; chmod 000 $tmpf ; ./$tmpf" # execution toto sans les droits avec './'
  18. "$tmpf" # execution toto sans les droits avec le path entier
  19. "rm -f $tmpf"
  20. ## Builtin ##
  21. ## CD ##
  22. "cd ; pwd"
  23. "cd ~ ; pwd"
  24. "cd / ; pwd"
  25. "cd - ; pwd"
  26. "cd . ; pwd"
  27. "cd .. ; pwd"
  28. "cd ~/. ; pwd"
  29. "cd nasrt" # élément qui n’éxiste pas
  30. "cd auteur" # cd fichier
  31. ## ENV ##
  32. "env"
  33. "export TOTO=toto" # test setenv
  34. "echo $PATH"
  35. "unsetenv PATH; echo $PATH"
  36. "unsetenv TOTO"
  37. ## Other ##
  38. "pwd"
  39. "exit ; ls" # test exit: exit and don't display ls result
  40. ## Point-virgule
  41. "cat auteur ; cat auteur" # test un point-virgule
  42. "cat auteur ; cat auteur ; cat auteur" # test deux points-virgule
  43. ## Parsing ##
  44. ";;;;,;;;; ;;;;"
  45. "; ;"
  46. ";"
  47. "ls;"
  48. ";ls"
  49. #" \t " # espaces, tabulations
  50. ## Pipe
  51. " " # empty
  52. "ls | cat" # pipe simple
  53. "ls | cat | cat" # pipe double
  54. "ls | cat | cat | cat" # pipe triple
  55. "ls -A -l | cat -e | cat -e" # pipe double with options
  56. "ls | cat | wc -l" # different commands
  57. "ls | cat | cat | cat | cat | cat | cat | cat | cat | cat | cat \
  58. | cat | cat | cat | cat | cat | cat | cat | cat | cat | cat" # twenty pipes
  59. "ls || cat" # Segfault
  60. "ls ||||||||| cat"
  61. "ls | | ls" # Segfault
  62. "|" # Segfault
  63. ## Redirections droite
  64. "ls > $tmpf ; cat $tmpf ; rm $tmpf" # simple
  65. "ls >> $tmpf ; cat $tmpf ; rm $tmpf" # double
  66. "mkdir 2> $tmpf; cat $tmpf ; rm $tmpf" # simple avec fd2 spécifié et erreur
  67. "mkdir 2>> $tmpf; cat $tmpf ; rm $tmpf" # double avec fd2 spécifié et erreur
  68. #"ls 2> $tmpf; cat $tmpf ; rm $tmpf" # simple avec fd2 spécifié sans erreur # marked as failed
  69. #"ls 2>> $tmpf; cat $tmpf ; rm $tmpf" # double avec fd2 spécifié sans erreur # marked as failed
  70. "ls>$tmpf ; cat $tmpf ; rm $tmpf" # collé
  71. "ls 1> $tmpf ; cat $tmpf" # fd 1 spécifié
  72. ## Redirections gauche
  73. "cat < $tmpf"
  74. #"cat << fin\nnrstai\nfin\n" # double gauche
  75. #"cat << toto\nnrstai\nrsnati\nairnst\ntoto" # double gauche avec autre string de fin
  76. ## Double redirection
  77. "cat < $tmpf > /tmp/txt; cat /tmp/txt; rm /tmp/txt"
  78. "cat < $tmpf >> /tmp/txt; cat /tmp/txt; rm /tmp/txt"
  79. ### Same file name
  80. "cat < $tmpf > $tmpf"
  81. #"cat < $tmpf >> $tmpf" # This one is non stop adding many time which stopped the script
  82. ## Redirections avec fd de sortie spécifié
  83. ### Redirection closed
  84. "ls 1>&-"
  85. "ls 2>&-"
  86. "mkdir -p 2>&-"
  87. "mkdir -p 1>&-"
  88. ### Redirection opened
  89. "echo test 1>&2"
  90. "echo test >&2"
  91. "echo test 2>&1"
  92. #"echo test 2>>&1" # create file named "1"
  93. #"echo test 2<&1" # marked as failed
  94. "mkdir -p 2>&1 | cat -e" # La sortie doit être : "rm: nosuchfile: No such file or directory$"
  95. "echo No dollar character 1>&2 | cat -e" # La sortie doit être "No dollar character".
  96. ### Ridirections with pipes
  97. "ls | cat -e > $tmpf; cat $tmpf"
  98. ## Segfault and parsing tests on redirections
  99. ">"
  100. "ls>"
  101. "ls >"
  102. #"> toto", ">toto", ">> toto", ">>toto" # are not working
  103. ">>"
  104. "ls>>"
  105. "ls >>"
  106. "<"
  107. "cat<"
  108. "cat <"
  109. #"< toto" # marked as failed
  110. #"<toto" # marked as failed
  111. "<<"
  112. "cat<<"
  113. "cat <<"
  114. "<<end"
  115. "<< end"
  116. "echo test 2>&1toto; rm 1toto" # zsh create file named "1toto"
  117. "echo test2>&1 toto"
  118. "echo test2>&1toto; rm 1toto" # zsh create file named "1toto"
  119. ## cotes parsing
  120. "echo 'test'" # simple cotes
  121. "echo \"test\"" # double cotes
  122. "echo 'simple' \"double\"" # Mix simple and double cotes
  123. "echo \"6789\" \"2345\"" # two coted blocks
  124. "echo \"6789\" \"2345\"" # two coted blocks seperated with many spaces
  125. "echo \"6789\" test \"2345\"" # two coted blocks seperated with non coted block
  126. "echo nasruitersaui \"6789\" ransuite \"2345\" anruistenrasuit" # big one
  127. # test all ASCII char: check they are used to split str like ';', '>', '<' or '|'
  128. "echo \"nrsatu;nrtsa\"" # ;
  129. "echo \"rasnt>inausrt\" > toto; cat toto" # >
  130. "echo \"nrastiu<rasntiu\" > toto; cat toto" # <
  131. "echo \"nrau|sntiu\" | cat" # |
  132. "ls ; echo \"nasruit><rnasuti\" > toto ; cat < toto" # mix
  133. "echo \"!#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~\""
  134. # all ASCII char without ` and "
  135. ### Spaces tests
  136. "echo \"6789 \" \" 2345\""
  137. "echo \"6789 \" arsnatuir \" 2345\""
  138. "echo \" 67 89 \" \" 23 45 \""
  139. ### Back cotes: pipe
  140. #"echo \`ls\`" # back cotes # not yet implemented
  141. ### Potential segfault
  142. "\'"
  143. "\""
  144. "\`"
  145. "\"anrusit\" \"bnrst" # one coted block, one opening coted block
  146. ## Mix
  147. "rm $tmpf ; ls ; pwd; ls -a | cat -e | wc > $tmpf ; cat $tmpf ; rm $tmpf" # point-virgules, pipes et redirection
  148. "ls -a | cat -e | wc >> $tmpf ; cat $tmpf ; rm $tmpf" # pipes et double redirection
  149. "mkdir test ; cd test ; ls -a ; ls | cat | wc -c > fifi ; cat fifi ; rm fifi ; cd .. ; rm -rf test"
  150. )
  151. TSH="21sh"
  152. SH="zsh"
  153. TSHO="/tmp/"$TSH"_output"
  154. SHO="/tmp/"$SH"_output"
  155. nbr=0
  156. succeed=0
  157. clear
  158. echo "\033[0;35m##### TESTS UNITAIRES DU SHELL #####\033[0m"
  159. for tu in $tests
  160. do
  161. echo "\033[0;33m>>>> Test \"$tu\" <<<<\033[0m"
  162. ./$TSH -c $tu > $TSHO
  163. $SH -c $tu > $SHO
  164. diff $TSHO $SHO
  165. if [ `echo $?` = 0 ]; then
  166. echo "\033[0;32m Succeed \033[0m"
  167. succeed=$((succeed + 1))
  168. else
  169. echo "\033[0;31m Failed \033[0m"
  170. fi
  171. nbr=$((nbr + 1))
  172. echo ""
  173. done
  174. echo "\033[0;32m>>>> Test \"sh shell_script.sh\" <<<<\033[0m"
  175. ./$TSH shell_script.sh > $TSHO
  176. $SH shell_script.sh > $SHO
  177. diff $TSHO $SHO
  178. # Tests with acceptable diff: marked as fails but which succeed: unsetenv, env, ||,
  179. succeed=$((succeed + 3))
  180. echo "\033[0;35m Tests succeed: $succeed/$nbr \033[0m"