pkcallbacks.test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env bash
  2. #pkcallbacks.test
  3. # if we can, isolate the network namespace to eliminate port collisions.
  4. if [[ -n "$NETWORK_UNSHARE_HELPER" ]]; then
  5. if [[ -z "$NETWORK_UNSHARE_HELPER_CALLED" ]]; then
  6. export NETWORK_UNSHARE_HELPER_CALLED=yes
  7. exec "$NETWORK_UNSHARE_HELPER" "$0" "$@" || exit $?
  8. fi
  9. elif [ "${AM_BWRAPPED-}" != "yes" ]; then
  10. bwrap_path="$(command -v bwrap)"
  11. if [ -n "$bwrap_path" ]; then
  12. export AM_BWRAPPED=yes
  13. exec "$bwrap_path" --unshare-net --dev-bind / / "$0" "$@"
  14. fi
  15. unset AM_BWRAPPED
  16. fi
  17. exit_code=1
  18. counter=0
  19. # need a unique resume port since may run the same time as testsuite
  20. # use server port zero hack to get one
  21. pk_port=0
  22. #no_pid tells us process was never started if -1
  23. no_pid=-1
  24. #server_pid captured on startup, stores the id of the server process
  25. server_pid=$no_pid
  26. # let's use absolute path to a local dir (make distcheck may be in sub dir)
  27. # also let's add some randomness by adding pid in case multiple 'make check's
  28. # per source tree
  29. ready_file=`pwd`/wolfssl_pk_ready$$
  30. remove_ready_file() {
  31. if test -e "$ready_file"; then
  32. echo -e "removing existing ready file"
  33. rm "$ready_file"
  34. fi
  35. }
  36. do_cleanup() {
  37. echo "in cleanup"
  38. if [ $server_pid != $no_pid ]
  39. then
  40. echo "killing server"
  41. kill -9 $server_pid
  42. fi
  43. remove_ready_file
  44. }
  45. # trap this function so if user aborts with ^C or other kill signal we still
  46. # get an exit that will in turn clean up the file system
  47. abort_trap() {
  48. echo "script aborted"
  49. if [ $server_pid != $no_pid ]
  50. then
  51. echo "killing server"
  52. kill -9 $server_pid
  53. fi
  54. exit_code=2 #different exit code in case of user interrupt
  55. echo "got abort signal, exiting with $exit_code"
  56. exit $exit_code
  57. }
  58. trap abort_trap INT TERM
  59. # trap this function so that if we exit on an error the file system will still
  60. # be restored and the other tests may still pass. Never call this function
  61. # instead use "exit <some value>" and this function will run automatically
  62. restore_file_system() {
  63. remove_ready_file
  64. }
  65. trap restore_file_system EXIT
  66. run_test() {
  67. echo -e "\nStarting example server for pkcallbacks test...\n"
  68. remove_ready_file
  69. # starts the server on pk_port, -R generates ready file to be used as a
  70. # mutex lock, -P does pkcallbacks. We capture the processid
  71. # into the variable server_pid
  72. ./examples/server/server -P -R "$ready_file" -p $pk_port &
  73. server_pid=$!
  74. while [ ! -s "$ready_file" -a "$counter" -lt 20 ]; do
  75. echo -e "waiting for ready file..."
  76. sleep 0.1
  77. counter=$((counter+ 1))
  78. done
  79. if test -e "$ready_file"; then
  80. echo -e "found ready file, starting client..."
  81. else
  82. echo -e "NO ready file ending test..."
  83. exit 1
  84. fi
  85. # sleep for an additional 0.1 to mitigate race on write/read of $ready_file:
  86. sleep 0.1
  87. # get created port 0 ephemeral port
  88. pk_port=`cat "$ready_file"`
  89. # starts client on pk_port with pkcallbacks, captures the output from client
  90. capture_out=$(./examples/client/client -P -p $pk_port 2>&1)
  91. client_result=$?
  92. if [ $client_result != 0 ]
  93. then
  94. echo -e "client failed!"
  95. do_cleanup
  96. exit 1
  97. fi
  98. wait $server_pid
  99. server_result=$?
  100. if [ $server_result != 0 ]
  101. then
  102. echo -e "server failed!"
  103. exit 1
  104. fi
  105. }
  106. ######### begin program #########
  107. # run the test
  108. run_test
  109. # If we get to this, success
  110. echo "Success!"
  111. exit 0
  112. ########## end program ##########