common_tests 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. TEST_FILE=$(mktemp)
  3. echo "== Testing sysctl behavior against ${TARGET} =="
  4. set_orig()
  5. {
  6. echo "${ORIG}" > "${TARGET}"
  7. }
  8. set_test()
  9. {
  10. echo "${TEST_STR}" > "${TARGET}"
  11. }
  12. verify()
  13. {
  14. local seen
  15. seen=$(cat "$1")
  16. if [ "${seen}" != "${TEST_STR}" ]; then
  17. return 1
  18. fi
  19. return 0
  20. }
  21. trap 'set_orig; rm -f "${TEST_FILE}"' EXIT
  22. rc=0
  23. echo -n "Writing test file ... "
  24. echo "${TEST_STR}" > "${TEST_FILE}"
  25. if ! verify "${TEST_FILE}"; then
  26. echo "FAIL" >&2
  27. exit 1
  28. else
  29. echo "ok"
  30. fi
  31. echo -n "Checking sysctl is not set to test value ... "
  32. if verify "${TARGET}"; then
  33. echo "FAIL" >&2
  34. exit 1
  35. else
  36. echo "ok"
  37. fi
  38. echo -n "Writing sysctl from shell ... "
  39. set_test
  40. if ! verify "${TARGET}"; then
  41. echo "FAIL" >&2
  42. exit 1
  43. else
  44. echo "ok"
  45. fi
  46. echo -n "Resetting sysctl to original value ... "
  47. set_orig
  48. if verify "${TARGET}"; then
  49. echo "FAIL" >&2
  50. exit 1
  51. else
  52. echo "ok"
  53. fi
  54. # Now that we've validated the sanity of "set_test" and "set_orig",
  55. # we can use those functions to set starting states before running
  56. # specific behavioral tests.
  57. echo -n "Writing entire sysctl in single write ... "
  58. set_orig
  59. dd if="${TEST_FILE}" of="${TARGET}" bs=4096 2>/dev/null
  60. if ! verify "${TARGET}"; then
  61. echo "FAIL" >&2
  62. rc=1
  63. else
  64. echo "ok"
  65. fi
  66. echo -n "Writing middle of sysctl after synchronized seek ... "
  67. set_test
  68. dd if="${TEST_FILE}" of="${TARGET}" bs=1 seek=1 skip=1 2>/dev/null
  69. if ! verify "${TARGET}"; then
  70. echo "FAIL" >&2
  71. rc=1
  72. else
  73. echo "ok"
  74. fi
  75. echo -n "Writing beyond end of sysctl ... "
  76. set_orig
  77. dd if="${TEST_FILE}" of="${TARGET}" bs=20 seek=2 2>/dev/null
  78. if verify "${TARGET}"; then
  79. echo "FAIL" >&2
  80. rc=1
  81. else
  82. echo "ok"
  83. fi
  84. echo -n "Writing sysctl with multiple long writes ... "
  85. set_orig
  86. (perl -e 'print "A" x 50;'; echo "${TEST_STR}") | \
  87. dd of="${TARGET}" bs=50 2>/dev/null
  88. if verify "${TARGET}"; then
  89. echo "FAIL" >&2
  90. rc=1
  91. else
  92. echo "ok"
  93. fi