testing.sh 3.5 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 "optionflag"
  7. # The following environment variables may be set to enable optional behavior
  8. # in "testing":
  9. # VERBOSE - Print the diff -u of each failed test case.
  10. # DEBUG - Enable command tracing.
  11. # SKIP - do not perform this test (this is set by "optionflag")
  12. #
  13. # The "testing" function takes five arguments:
  14. # $1) Description to display when running command
  15. # $2) Command line arguments to command
  16. # $3) Expected result (on stdout)
  17. # $4) Data written to file "input"
  18. # $5) Data written to stdin
  19. #
  20. # The exit value of testing is the exit value of the command it ran.
  21. #
  22. # The environment variable "FAILCOUNT" contains a cumulative total of the
  23. # number of failed tests.
  24. # The "optional" function is used to skip certain tests, ala:
  25. # optionflag CONFIG_FEATURE_THINGY
  26. #
  27. # The "optional" function checks the environment variable "OPTIONFLAGS",
  28. # which is either empty (in which case it always clears SKIP) or
  29. # else contains a colon-separated list of features (in which case the function
  30. # clears SKIP if the flag was found, or sets it to 1 if the flag was not found).
  31. export FAILCOUNT=0
  32. export SKIP=
  33. # Helper functions
  34. optional()
  35. {
  36. option=`echo "$OPTIONFLAGS" | egrep "(^|:)$1(:|\$)"`
  37. # Not set?
  38. if [ -z "$1" ] || [ -z "$OPTIONFLAGS" ] || [ ${#option} -ne 0 ]
  39. then
  40. SKIP=""
  41. return
  42. fi
  43. SKIP=1
  44. }
  45. # The testing function
  46. testing ()
  47. {
  48. NAME="$1"
  49. [ -z "$1" ] && NAME=$2
  50. if [ $# -ne 5 ]
  51. then
  52. echo "Test $NAME has the wrong number of arguments ($# $*)" >&2
  53. exit
  54. fi
  55. [ -n "$DEBUG" ] && set -x
  56. if [ -n "$SKIP" ]
  57. then
  58. echo "SKIPPED: $NAME"
  59. return 0
  60. fi
  61. echo -ne "$3" > expected
  62. echo -ne "$4" > input
  63. [ -z "$VERBOSE" ] || echo "echo '$5' | $2"
  64. echo -ne "$5" | eval "$2" > actual
  65. RETVAL=$?
  66. cmp expected actual > /dev/null
  67. if [ $? -ne 0 ]
  68. then
  69. FAILCOUNT=$[$FAILCOUNT+1]
  70. echo "FAIL: $NAME"
  71. [ -n "$VERBOSE" ] && diff -u expected actual
  72. else
  73. echo "PASS: $NAME"
  74. fi
  75. rm -f input expected actual
  76. [ -n "$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. function mkchroot
  84. {
  85. [ $# -lt 2 ] && return
  86. echo -n .
  87. dest=$1
  88. shift
  89. for i in "$@"
  90. do
  91. [ "${i:0:1}" == "/" ] || i=$(which $i)
  92. [ -f "$dest/$i" ] && continue
  93. if [ -e "$i" ]
  94. then
  95. d=`echo "$i" | grep -o '.*/'` &&
  96. mkdir -p "$dest/$d" &&
  97. cat "$i" > "$dest/$i" &&
  98. chmod +x "$dest/$i"
  99. else
  100. echo "Not found: $i"
  101. fi
  102. mkchroot "$dest" $(ldd "$i" | egrep -o '/.* ')
  103. done
  104. }
  105. # Set up a chroot environment and run commands within it.
  106. # Needed commands listed on command line
  107. # Script fed to stdin.
  108. function dochroot
  109. {
  110. mkdir tmpdir4chroot
  111. mount -t ramfs tmpdir4chroot tmpdir4chroot
  112. mkdir -p tmpdir4chroot/{etc,sys,proc,tmp,dev}
  113. cp -L testing.sh tmpdir4chroot
  114. # Copy utilities from command line arguments
  115. echo -n "Setup chroot"
  116. mkchroot tmpdir4chroot $*
  117. echo
  118. mknod tmpdir4chroot/dev/tty c 5 0
  119. mknod tmpdir4chroot/dev/null c 1 3
  120. mknod tmpdir4chroot/dev/zero c 1 5
  121. # Copy script from stdin
  122. cat > tmpdir4chroot/test.sh
  123. chmod +x tmpdir4chroot/test.sh
  124. chroot tmpdir4chroot /test.sh
  125. umount -l tmpdir4chroot
  126. rmdir tmpdir4chroot
  127. }