testing.sh 3.6 KB

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