tests.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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; rm /tmp/txt"
  75. "cat < $tmpf >> /tmp/txt; cat /tmp/txt; rm /tmp/txt"
  76. ### Same file name
  77. "cat < $tmpf > $tmpf"
  78. #"cat < $tmpf >> $tmpf" # This one is non stop adding many time which stopped the script
  79. ## Redirections avec fd de sortie spécifié
  80. ### Redirection closed
  81. "ls 1>&-"
  82. "ls 2>&-"
  83. "mkdir -p 2>&-"
  84. "mkdir -p 1>&-"
  85. ### Redirection opened
  86. "echo test 1>&2"
  87. "echo test >&2"
  88. "echo test 2>&1"
  89. #"echo test 2>>&1" # create file named "1"
  90. #"echo test 2<&1" # marked as failed
  91. "mkdir -p 2>&1 | cat -e" # La sortie doit être : "rm: nosuchfile: No such file or directory$"
  92. "echo No dollar character 1>&2 | cat -e" # La sortie doit être "No dollar character".
  93. ### Ridirections with pipes
  94. "ls | cat -e > $tmpf; cat $tmpf"
  95. ## Segfault and parsing tests on redirections
  96. ">"
  97. "ls>"
  98. "ls >"
  99. #"> toto", ">toto", ">> toto", ">>toto" # are not working
  100. ">>"
  101. "ls>>"
  102. "ls >>"
  103. "<"
  104. "cat<"
  105. "cat <"
  106. #"< toto" # marked as failed
  107. #"<toto" # marked as failed
  108. "<<"
  109. "cat<<"
  110. "cat <<"
  111. "<<end"
  112. "<< end"
  113. "echo test 2>&1toto; rm 1toto" # zsh create file named "1toto"
  114. "echo test2>&1 toto"
  115. "echo test2>&1toto; rm 1toto" # zsh create file named "1toto"
  116. ## cotes parsing
  117. "echo 'test'" # simple cotes
  118. "echo \"test\"" # double cotes
  119. "echo 'simple' \"double\"" # Mix simple and double cotes
  120. "echo \"6789\" \"2345\"" # two coted blocks
  121. "echo \"6789\" \"2345\"" # two coted blocks seperated with many spaces
  122. "echo \"6789\" test \"2345\"" # two coted blocks seperated with non coted block
  123. "echo nasruitersaui \"6789\" ransuite \"2345\" anruistenrasuit" # big one
  124. # test all ASCII char: check they are used to split str like ';', '>', '<' or '|'
  125. "echo \"nrsatu;nrtsa\"" # ;
  126. "echo \"rasnt>inausrt\" > toto; cat toto" # >
  127. "echo \"nrastiu<rasntiu\" > toto; cat toto" # <
  128. "echo \"nrau|sntiu\" | cat" # |
  129. "ls ; echo \"nasruit><rnasuti\" > toto ; cat < toto" # mix
  130. "echo \"!#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~\""
  131. # all ASCII char without ` and "
  132. ### Spaces tests
  133. "echo \"6789 \" \" 2345\""
  134. "echo \"6789 \" arsnatuir \" 2345\""
  135. "echo \" 67 89 \" \" 23 45 \""
  136. ### Back cotes: pipe
  137. #"echo \`ls\`" # back cotes # not yet implemented
  138. ### Potential segfault
  139. "\'"
  140. "\""
  141. "\`"
  142. "\"anrusit\" \"bnrst" # one coted block, one opening coted block
  143. ## Mix
  144. "rm $tmpf ; ls ; pwd; ls -a | cat -e | wc > $tmpf ; cat $tmpf ; rm $tmpf" # point-virgules, pipes et redirection
  145. "ls -a | cat -e | wc >> $tmpf ; cat $tmpf ; rm $tmpf" # pipes et double redirection
  146. "mkdir test ; cd test ; ls -a ; ls | cat | wc -c > fifi ; cat fifi ; rm fifi ; cd .. ; rm -rf test"
  147. )
  148. TSH="21sh"
  149. SH="zsh"
  150. TSHO="/tmp/"$TSH"_output"
  151. SHO="/tmp/"$SH"_output"
  152. nbr=0
  153. succeed=0
  154. clear
  155. echo "\033[0;35m##### TESTS UNITAIRES DU SHELL #####\033[0m"
  156. for tu in $tests
  157. do
  158. echo "\033[0;33m>>>> Test \"$tu\" <<<<\033[0m"
  159. ./$TSH -c $tu > $TSHO
  160. $SH -c $tu > $SHO
  161. diff $TSHO $SHO
  162. if [ `echo $?` = 0 ]; then
  163. echo "\033[0;32m Succeed \033[0m"
  164. succeed=$((succeed + 1))
  165. else
  166. echo "\033[0;31m Failed \033[0m"
  167. fi
  168. nbr=$((nbr + 1))
  169. echo ""
  170. done
  171. echo "\033[0;32m>>>> Test \"sh shell_script.sh\" <<<<\033[0m"
  172. ./$TSH shell_script.sh > $TSHO
  173. $SH shell_script.sh > $SHO
  174. diff $TSHO $SHO
  175. # Tests with acceptable diff: marked as fails but which succeed: unsetenv, env, ||,
  176. succeed=$((succeed + 3))
  177. echo "\033[0;35m Tests succeed: $succeed/$nbr \033[0m"