testing.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if test x"${OPTIONFLAGS/*:$1:*/y}" != x"y"; then
  52. SKIP=1
  53. return
  54. fi
  55. shift
  56. done
  57. }
  58. # The testing function
  59. testing()
  60. {
  61. NAME="$1"
  62. [ -n "$1" ] || NAME="$2"
  63. if [ $# -ne 5 ]
  64. then
  65. echo "Test $NAME has wrong number of arguments: $# (must be 5)" >&2
  66. exit 1
  67. fi
  68. [ -z "$DEBUG" ] || set -x
  69. if [ -n "$SKIP" ]
  70. then
  71. echo "SKIPPED: $NAME"
  72. return 0
  73. fi
  74. $ECHO -ne "$3" > expected
  75. $ECHO -ne "$4" > input
  76. [ -z "$VERBOSE" ] || echo "echo -ne '$4' >input"
  77. [ -z "$VERBOSE" ] || echo "echo -ne '$5' | $2"
  78. $ECHO -ne "$5" | eval "$2" > actual
  79. RETVAL=$?
  80. if cmp expected actual >/dev/null 2>/dev/null
  81. then
  82. echo "PASS: $NAME"
  83. else
  84. FAILCOUNT=$(($FAILCOUNT + 1))
  85. echo "FAIL: $NAME"
  86. [ -z "$VERBOSE" ] || diff -u expected actual
  87. fi
  88. rm -f input expected actual
  89. [ -z "$DEBUG" ] || set +x
  90. return $RETVAL
  91. }
  92. # Recursively grab an executable and all the libraries needed to run it.
  93. # Source paths beginning with / will be copied into destpath, otherwise
  94. # the file is assumed to already be there and only its library dependencies
  95. # are copied.
  96. mkchroot()
  97. {
  98. [ $# -lt 2 ] && return
  99. $ECHO -n .
  100. dest=$1
  101. shift
  102. for i in "$@"
  103. do
  104. #bashism: [ "${i:0:1}" == "/" ] || i=$(which $i)
  105. i=$(which $i) # no-op for /bin/prog
  106. [ -f "$dest/$i" ] && continue
  107. if [ -e "$i" ]
  108. then
  109. d=`echo "$i" | grep -o '.*/'` &&
  110. mkdir -p "$dest/$d" &&
  111. cat "$i" > "$dest/$i" &&
  112. chmod +x "$dest/$i"
  113. else
  114. echo "Not found: $i"
  115. fi
  116. mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
  117. done
  118. }
  119. # Set up a chroot environment and run commands within it.
  120. # Needed commands listed on command line
  121. # Script fed to stdin.
  122. dochroot()
  123. {
  124. mkdir tmpdir4chroot
  125. mount -t ramfs tmpdir4chroot tmpdir4chroot
  126. mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
  127. cp -L testing.sh tmpdir4chroot
  128. # Copy utilities from command line arguments
  129. $ECHO -n "Setup chroot"
  130. mkchroot tmpdir4chroot $*
  131. echo
  132. mknod tmpdir4chroot/dev/tty c 5 0
  133. mknod tmpdir4chroot/dev/null c 1 3
  134. mknod tmpdir4chroot/dev/zero c 1 5
  135. # Copy script from stdin
  136. cat > tmpdir4chroot/test.sh
  137. chmod +x tmpdir4chroot/test.sh
  138. chroot tmpdir4chroot /test.sh
  139. umount -l tmpdir4chroot
  140. rmdir tmpdir4chroot
  141. }