testing.sh 3.6 KB

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