3
0

testing.sh 3.8 KB

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