tests.sh 5.5 KB

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