testing.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # Simple test harness infrastructure for BusyBox
  2. #
  3. # Copyright 2005 by Rob Landley
  4. #
  5. # License is GPLv2, see LICENSE in the busybox tarball for full license text.
  6. # This file defines two functions, "testing" and "optional"
  7. # and a couple more...
  8. # The following environment variables may be set to enable optional behavior
  9. # in "testing":
  10. # VERBOSE - Print the diff -u of each failed test case.
  11. # DEBUG - Enable command tracing.
  12. # SKIP - do not perform this test (this is set by "optional")
  13. #
  14. # The "testing" function takes five arguments:
  15. # $1) Test description
  16. # $2) Command(s) to run. May have pipes, redirects, etc
  17. # $3) Expected result on stdout
  18. # $4) Data to be written to file "input"
  19. # $5) Data to be written to stdin
  20. #
  21. # The exit value of testing is the exit value of $2 it ran.
  22. #
  23. # The environment variable "FAILCOUNT" contains a cumulative total of the
  24. # number of failed tests.
  25. # The "optional" function is used to skip certain tests, ala:
  26. # optional FEATURE_THINGY
  27. #
  28. # The "optional" function checks the environment variable "OPTIONFLAGS",
  29. # which is either empty (in which case it always clears SKIP) or
  30. # else contains a colon-separated list of features (in which case the function
  31. # clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
  32. export FAILCOUNT=0
  33. export SKIP=
  34. # Helper for helpers. Oh my...
  35. test x"$ECHO" != x"" || {
  36. ECHO="echo"
  37. test x"`echo -ne`" = x"" || {
  38. # Compile and use a replacement 'echo' which understands -e -n
  39. ECHO="$PWD/echo-ne"
  40. test -x "$ECHO" || {
  41. gcc -Os -o "$ECHO" ../scripts/echo.c || exit 1
  42. }
  43. }
  44. export ECHO
  45. }
  46. # Helper functions
  47. optional()
  48. {
  49. SKIP=
  50. while test "$1"; do
  51. case $1 in
  52. "!"*)
  53. case "${OPTIONFLAGS}" in
  54. *:${1#!}:*) SKIP=1; return;;
  55. esac
  56. shift
  57. ;;
  58. *)
  59. case "${OPTIONFLAGS}" in
  60. *:$1:*) ;;
  61. *) SKIP=1; return ;;
  62. esac
  63. shift
  64. ;;
  65. esac
  66. done
  67. }
  68. # The testing function
  69. testing()
  70. {
  71. NAME="$1"
  72. [ -n "$1" ] || NAME="$2"
  73. if [ $# -ne 5 ]
  74. then
  75. echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
  76. exit 1
  77. fi
  78. [ -z "$DEBUG" ] || set -x
  79. if [ -n "$SKIP" ]
  80. then
  81. echo "SKIPPED: $NAME"
  82. return 0
  83. fi
  84. $ECHO -ne "$3" > expected
  85. $ECHO -ne "$4" > input
  86. [ -z "$VERBOSE" ] || echo ======================
  87. [ -z "$VERBOSE" ] || echo "echo -ne '$4' >input"
  88. [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2"
  89. $ECHO -ne "$5" | eval "$2" > actual
  90. RETVAL=$?
  91. if cmp expected actual >/dev/null 2>/dev/null
  92. then
  93. echo "PASS: $NAME"
  94. else
  95. FAILCOUNT=$(($FAILCOUNT + 1))
  96. echo "FAIL: $NAME"
  97. [ -z "$VERBOSE" ] || diff -u expected actual
  98. fi
  99. rm -f input expected actual
  100. [ -z "$DEBUG" ] || set +x
  101. return $RETVAL
  102. }
  103. # Recursively grab an executable and all the libraries needed to run it.
  104. # Source paths beginning with / will be copied into destpath, otherwise
  105. # the file is assumed to already be there and only its library dependencies
  106. # are copied.
  107. mkchroot()
  108. {
  109. [ $# -lt 2 ] && return
  110. $ECHO -n .
  111. dest=$1
  112. shift
  113. for i in "$@"
  114. do
  115. #bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
  116. i=$(which $i) # no-op for /bin/prog
  117. [ -f "$dest/$i" ] && continue
  118. if [ -e "$i" ]
  119. then
  120. d=`echo "$i" | grep -o '.*/'` &&
  121. mkdir -p "$dest/$d" &&
  122. cat "$i" > "$dest/$i" &&
  123. chmod +x "$dest/$i"
  124. else
  125. echo "Not found: $i"
  126. fi
  127. mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
  128. done
  129. }
  130. # Set up a chroot environment and run commands within it.
  131. # Needed commands listed on command line
  132. # Script fed to stdin.
  133. dochroot()
  134. {
  135. mkdir tmpdir4chroot
  136. mount -t ramfs tmpdir4chroot tmpdir4chroot
  137. mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
  138. cp -L testing.sh tmpdir4chroot
  139. # Copy utilities from command line arguments
  140. $ECHO -n "Setup chroot"
  141. mkchroot tmpdir4chroot $*
  142. echo
  143. mknod tmpdir4chroot/dev/tty c 5 0
  144. mknod tmpdir4chroot/dev/null c 1 3
  145. mknod tmpdir4chroot/dev/zero c 1 5
  146. # Copy script from stdin
  147. cat > tmpdir4chroot/test.sh
  148. chmod +x tmpdir4chroot/test.sh
  149. chroot tmpdir4chroot /test.sh
  150. umount -l tmpdir4chroot
  151. rmdir tmpdir4chroot
  152. }