dtls.test 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/bash
  2. # This script can be run with several environment variables set dictating its
  3. # run. You can set the following to what you like:
  4. WOLFSSL_ROOT=${WOLFSSL_ROOT:-$(pwd)}
  5. UDP_PROXY_BIN=${UDP_PROXY_BIN:-"udp_proxy"}
  6. DTLS_VERSION=${DTLS_VERSION:-"-v4"}
  7. PCAP_FILENAME=${PCAP_FILENAME:-"dtls${DTLS_VERSION}.pcap"}
  8. # Additionally, you can add the following tests by setting it to '1':
  9. DO_EXTENDED_SERVER_PERMUTATION_TEST=${DO_EXTENDED_SERVER_PERMUTATION_TEST:-0}
  10. DO_DELAY_TEST=${DO_DELAY_TEST:-0}
  11. # An example use would be: DTLS_VERSION=-v3 scripts/dtls.test
  12. # Note the output also consists of a single PCAP file which has a set of
  13. # three packets (1-byte, strlen()-byte, 1-byte payload) deliniating each test.
  14. #set -x # enable debug output
  15. # bwrap execution environment to avoid port conflicts
  16. if [ "${AM_BWRAPPED-}" != "yes" ]; then
  17. bwrap_path="$(command -v bwrap)"
  18. if [ -n "$bwrap_path" ]; then
  19. export AM_BWRAPPED=yes
  20. exec "$bwrap_path" --cap-add ALL --unshare-net --dev-bind / / "$0" "$@"
  21. fi
  22. fi
  23. kill_server() {
  24. for i in $(jobs -pr); do
  25. if [ "$i" != "$TCPDUMP_PID" ]; then
  26. kill -9 $i
  27. fi
  28. done
  29. # empty print to show which backgrounded processes were killed
  30. sleep 0.2 && echo
  31. }
  32. cleanup () {
  33. echo
  34. echo "Cleaning up..."
  35. kill_server
  36. if [ ! -z "$TCPDUMP_PID" ];then
  37. echo "Killing tcpdump $TCPDUMP_PID"
  38. sleep 1
  39. kill $TCPDUMP_PID
  40. fi
  41. }
  42. trap cleanup err exit
  43. PROXY_PORT=1234
  44. SERVER_PORT=4321
  45. KEY_UPDATE_SIZE=35
  46. NUM_TESTS_FAILED=0
  47. NUM_TESTS_RUN=0
  48. if [ "$DTLS_VERSION" = "-v4" ]; then
  49. UDP_PROXY_EXTRA_ARGS="-u"
  50. fi
  51. # $WOLFSSL_ROOT/tests/unit.test tests/test-dtls13.conf
  52. set -o pipefail
  53. prepend() { # Usage: cmd 2>&1 | prepend "sometext "
  54. while read line; do echo "${1}${line}"; done
  55. }
  56. run_test() { # usage: run_test "<testName>" "<udp-proxy args>" "<server args>" "<client args>"
  57. ((NUM_TESTS_RUN++))
  58. echo "" | nc -u 127.0.0.1 $SERVER_PORT # This is a marker for the PCAP file
  59. echo "$1" | nc -u 127.0.0.1 $SERVER_PORT # This is a marker for the PCAP file
  60. echo "" | nc -u 127.0.0.1 $SERVER_PORT # This is a marker for the PCAP file
  61. echo -e "\n${1}\n"
  62. stdbuf -oL -eL $WOLFSSL_ROOT/examples/server/server -u -p$SERVER_PORT $DTLS_VERSION $3 2>&1 | prepend "[server] " &
  63. sleep 0.2
  64. stdbuf -oL -eL $UDP_PROXY_BIN -p $PROXY_PORT -s 127.0.0.1:$SERVER_PORT $UDP_PROXY_EXTRA_ARGS $2 2>&1 | prepend "[udp-proxy] " &
  65. sleep 0.2
  66. # Wrap this command in a timeout so that a deadlock won't bring down the entire test
  67. timeout -s KILL 1m stdbuf -oL -eL $WOLFSSL_ROOT/examples/client/client -u -p$PROXY_PORT $DTLS_VERSION $4 2>&1 | prepend "[client] "
  68. if [ $? != 0 ]; then
  69. echo "***Test failed***"
  70. ((NUM_TESTS_FAILED++))
  71. fi
  72. kill_server
  73. }
  74. test_dropping_packets () {
  75. for i in $(seq 0 11);do
  76. run_test "Dropping ${i}th packet" "-f $i" "-Ta" ""
  77. done
  78. # dropping last ack would be client error as wolfssl_read doesn't support WANT_WRITE as returned error
  79. for i in $(seq 0 10);do
  80. run_test "Testing WANT_WRITE: dropping packet $i" "-f $i" "-Ta -6" "-6"
  81. done
  82. }
  83. # this test is based on detecting newSessionTicket message by its size. This is rather fragile.
  84. test_dropping_new_session_ticket() { # usage: test_dropping_new_session_ticket <size>
  85. run_test "Dropping new session ticket packet of size $1" "-F $1" "-w" "-w --waitTicket"
  86. }
  87. test_permutations () {
  88. SIDE=$1
  89. PERMUTATIONS=$(python3 << EOF
  90. import itertools
  91. for p in itertools.permutations("$2"):
  92. print(''.join(p))
  93. EOF
  94. )
  95. for i in $PERMUTATIONS;do
  96. UDP_LOGFILE=$(mktemp)
  97. run_test "Testing $SIDE permutations order $i" "-r $i -S $SIDE -l $UDP_LOGFILE" "-Ta -w" "-w"
  98. echo "...produced $(grep -P 'client:|server:' $UDP_LOGFILE | wc -l) messages"
  99. rm -f $UDP_LOGFILE
  100. done
  101. echo "All $SIDE msg permutations succeeded"
  102. }
  103. test_time_delays () {
  104. DELAYS=$(python3 << EOF
  105. import itertools
  106. t = [0.1, 0.5, 1.1]
  107. tt = []
  108. for i in itertools.product(t, t, t):
  109. tt.append(i * 15)
  110. for i in tt:
  111. print(','.join(map(lambda x: str(x) , i)))
  112. EOF
  113. )
  114. for DELAY in $DELAYS;do
  115. UDP_LOGFILE=$(mktemp)
  116. run_test "Testing delay $DELAY" "-l $UDP_LOGFILE -t $DELAY" "-Ta -w" "-w"
  117. echo "...produced $(grep -P 'client:|server:' $UDP_LOGFILE | wc -l) messages"
  118. rm -f $UDP_LOGFILE
  119. done
  120. }
  121. echo "Starting capture"
  122. tcpdump -i lo -n port ${SERVER_PORT} -w ${PCAP_FILENAME} -U &
  123. TCPDUMP_PID=$!
  124. sleep 0.5
  125. test_dropping_packets
  126. test_permutations client 012
  127. if [ "$DO_EXTENDED_SERVER_PERMUTATION_TEST" = "1" ];then
  128. test_permutations server 0123456
  129. else
  130. test_permutations server 012
  131. fi
  132. test_dropping_new_session_ticket 200
  133. # TODO: fix udp_proxy to not re-order close alert before app data
  134. if [ "$DO_DELAY_TEST" = "1" ];then
  135. test_time_delays
  136. fi
  137. if [ $NUM_TESTS_FAILED == 0 ]; then
  138. echo -e "\nAll $NUM_TESTS_RUN tests SUCCEEDED!!!\n"
  139. else
  140. echo -e "\nThere were $NUM_TESTS_FAILED failures out of $NUM_TESTS_RUN tests\n"
  141. fi
  142. echo "The script ran for $SECONDS seconds"
  143. exit $NUM_TESTS_FAILED