testing.sh 3.9 KB

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