From d695f7b4235bb1a54658e5a5ba2f7ce025ffda92 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Fri, 6 Feb 2015 21:53:22 +0100 Subject: [PATCH 1/7] Don't use invalid exit values in zero length key test Shell exit values must fall into the range of [0-255], so let's honour this. In practice, the "exit -1" from the old code set an exit value of 255 on most systems, so let's pick that. Fixes part of bug #14478, patch idea suggested by an anonymous contributor. Thanks! --- src/test/zero_length_keys.sh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/zero_length_keys.sh b/src/test/zero_length_keys.sh index 3a99ca1f1d..6c82c3d3d3 100755 --- a/src/test/zero_length_keys.sh +++ b/src/test/zero_length_keys.sh @@ -13,10 +13,10 @@ # Check tor does not overwrite existing keys (existing behaviour) # # Exit Statuses: -# -2: test failed - tor did not generate the key files on first run -# -1: a command failed - the test could not be completed # 0: test succeeded - tor regenerated/kept the files # 1: test failed - tor did not regenerate/keep the files +# 254: test failed - tor did not generate the key files on first run +# 255: a command failed - the test could not be completed # if [ $# -lt 1 ]; then @@ -32,7 +32,7 @@ TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 1234 if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then echo "Failure: Previous tor keys present in tor data directory" - exit -1 + exit 255 else echo "Generating initial tor keys" $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid & @@ -47,11 +47,11 @@ else true #echo "tor generated the initial key files" else echo "Failure: tor failed to generate the initial key files" - exit -2 + exit 254 fi fi -#ls -lh "$DATA_DIR"/keys/ || exit -1 +#ls -lh "$DATA_DIR"/keys/ || exit 255 # backup and keep/delete/create zero-length files for the keys @@ -62,17 +62,17 @@ cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old # delete keys for -d or -z if [ "$1" != "-e" ]; then FILE_DESC="regenerates deleted" - rm "$DATA_DIR"/keys/secret_id_key || exit -1 - rm "$DATA_DIR"/keys/secret_onion_key || exit -1 - rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit -1 + rm "$DATA_DIR"/keys/secret_id_key || exit 255 + rm "$DATA_DIR"/keys/secret_onion_key || exit 255 + rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255 fi # create empty files for -z if [ "$1" = "-z" ]; then FILE_DESC="regenerates zero-length" - touch "$DATA_DIR"/keys/secret_id_key || exit -1 - touch "$DATA_DIR"/keys/secret_onion_key || exit -1 - touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit -1 + touch "$DATA_DIR"/keys/secret_id_key || exit 255 + touch "$DATA_DIR"/keys/secret_onion_key || exit 255 + touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255 fi echo "Running tor again to check if it $FILE_DESC keys" @@ -83,7 +83,7 @@ sleep 5 kill $TOR_PID wait $TOR_PID -#ls -lh "$DATA_DIR"/keys/ || exit -1 +#ls -lh "$DATA_DIR"/keys/ || exit 255 # tor must always have non-zero-length key files if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then From 136b1d8ed0deefc74fb9ddd668695f940cb1b892 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Fri, 6 Feb 2015 21:56:26 +0100 Subject: [PATCH 2/7] Remove useless export in zero length key test Fixes part of bug #14478, patch idea suggested by an anonymous contributor. Thanks! --- src/test/zero_length_keys.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/zero_length_keys.sh b/src/test/zero_length_keys.sh index 6c82c3d3d3..729baebfea 100755 --- a/src/test/zero_length_keys.sh +++ b/src/test/zero_length_keys.sh @@ -25,7 +25,7 @@ if [ $# -lt 1 ]; then exit $? fi -export DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX` +DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX` # DisableNetwork means that the ORPort won't actually be opened. # 'ExitRelay 0' suppresses a warning. TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0" From f84a54c159e89e90fb4b3ce07a9ddfbd6f1dc8a0 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Fri, 6 Feb 2015 23:21:20 +0100 Subject: [PATCH 3/7] Clean up after zero length key test --- src/test/zero_length_keys.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/zero_length_keys.sh b/src/test/zero_length_keys.sh index 729baebfea..7c99564f1a 100755 --- a/src/test/zero_length_keys.sh +++ b/src/test/zero_length_keys.sh @@ -26,6 +26,12 @@ if [ $# -lt 1 ]; then fi DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX` +if [ -z "$DATA_DIR" ]; then + echo "Failure: mktemp invocation returned empty string" + exit 255 +fi +trap "rm -rf '$DATA_DIR'" 0 + # DisableNetwork means that the ORPort won't actually be opened. # 'ExitRelay 0' suppresses a warning. TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0" From 0a0c5d7c0e6d45789f4351c0b56511c80f13eaa0 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Sat, 7 Feb 2015 14:48:06 +0100 Subject: [PATCH 4/7] Check that mktemp result is a directory Fixes part of bug #14478, patch idea suggested by an anonymous contributor. Thanks! --- src/test/zero_length_keys.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/zero_length_keys.sh b/src/test/zero_length_keys.sh index 7c99564f1a..0dd67c1780 100755 --- a/src/test/zero_length_keys.sh +++ b/src/test/zero_length_keys.sh @@ -30,6 +30,10 @@ if [ -z "$DATA_DIR" ]; then echo "Failure: mktemp invocation returned empty string" exit 255 fi +if [ -d "$DATA_DIR" ]; then + echo "Failure: mktemp invocation result doesn't point to directory" + exit 255 +fi trap "rm -rf '$DATA_DIR'" 0 # DisableNetwork means that the ORPort won't actually be opened. From 6ecd6e27dab79f7b6ee913edca1bec219f1cd2a0 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Sat, 7 Feb 2015 14:54:21 +0100 Subject: [PATCH 5/7] Avoid undefined behaviour of test Fixes part of bug #14478, patch idea suggested by an anonymous contributor. Thanks! --- src/test/zero_length_keys.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/test/zero_length_keys.sh b/src/test/zero_length_keys.sh index 0dd67c1780..cd1b60d065 100755 --- a/src/test/zero_length_keys.sh +++ b/src/test/zero_length_keys.sh @@ -30,7 +30,8 @@ if [ -z "$DATA_DIR" ]; then echo "Failure: mktemp invocation returned empty string" exit 255 fi -if [ -d "$DATA_DIR" ]; then +if [ ! -d "$DATA_DIR" ]; then + echo "$DATA_DIR" echo "Failure: mktemp invocation result doesn't point to directory" exit 255 fi @@ -40,7 +41,8 @@ trap "rm -rf '$DATA_DIR'" 0 # 'ExitRelay 0' suppresses a warning. TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0" -if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then +if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] && + [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then echo "Failure: Previous tor keys present in tor data directory" exit 255 else @@ -53,7 +55,8 @@ else wait $TOR_PID # tor must successfully generate non-zero-length key files - if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then + if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] && + [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then true #echo "tor generated the initial key files" else echo "Failure: tor failed to generate the initial key files" @@ -96,7 +99,8 @@ wait $TOR_PID #ls -lh "$DATA_DIR"/keys/ || exit 255 # tor must always have non-zero-length key files -if [ -s "$DATA_DIR"/keys/secret_id_key -a -s "$DATA_DIR"/keys/secret_onion_key -a -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then +if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] && + [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then # check if the keys are different to the old ones diff -q -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old > /dev/null SAME_KEYS=$? From c5f176b9b0199b63ac55a5f833766dfe027f28b1 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Wed, 11 Feb 2015 21:40:49 +0100 Subject: [PATCH 6/7] Change exit code of zero_length_keys script Weasel notes that exit codes above 128 are for when a script dies from a signal. --- src/test/zero_length_keys.sh | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/test/zero_length_keys.sh b/src/test/zero_length_keys.sh index cd1b60d065..eca750c956 100755 --- a/src/test/zero_length_keys.sh +++ b/src/test/zero_length_keys.sh @@ -15,8 +15,8 @@ # Exit Statuses: # 0: test succeeded - tor regenerated/kept the files # 1: test failed - tor did not regenerate/keep the files -# 254: test failed - tor did not generate the key files on first run -# 255: a command failed - the test could not be completed +# 2: test failed - tor did not generate the key files on first run +# 3: a command failed - the test could not be completed # if [ $# -lt 1 ]; then @@ -28,12 +28,12 @@ fi DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX` if [ -z "$DATA_DIR" ]; then echo "Failure: mktemp invocation returned empty string" - exit 255 + exit 3 fi if [ ! -d "$DATA_DIR" ]; then echo "$DATA_DIR" echo "Failure: mktemp invocation result doesn't point to directory" - exit 255 + exit 3 fi trap "rm -rf '$DATA_DIR'" 0 @@ -44,7 +44,7 @@ TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 1234 if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then echo "Failure: Previous tor keys present in tor data directory" - exit 255 + exit 3 else echo "Generating initial tor keys" $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid & @@ -60,11 +60,11 @@ else true #echo "tor generated the initial key files" else echo "Failure: tor failed to generate the initial key files" - exit 254 + exit 2 fi fi -#ls -lh "$DATA_DIR"/keys/ || exit 255 +#ls -lh "$DATA_DIR"/keys/ || exit 3 # backup and keep/delete/create zero-length files for the keys @@ -75,17 +75,17 @@ cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old # delete keys for -d or -z if [ "$1" != "-e" ]; then FILE_DESC="regenerates deleted" - rm "$DATA_DIR"/keys/secret_id_key || exit 255 - rm "$DATA_DIR"/keys/secret_onion_key || exit 255 - rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255 + rm "$DATA_DIR"/keys/secret_id_key || exit 3 + rm "$DATA_DIR"/keys/secret_onion_key || exit 3 + rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3 fi # create empty files for -z if [ "$1" = "-z" ]; then FILE_DESC="regenerates zero-length" - touch "$DATA_DIR"/keys/secret_id_key || exit 255 - touch "$DATA_DIR"/keys/secret_onion_key || exit 255 - touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255 + touch "$DATA_DIR"/keys/secret_id_key || exit 3 + touch "$DATA_DIR"/keys/secret_onion_key || exit 3 + touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 3 fi echo "Running tor again to check if it $FILE_DESC keys" @@ -96,7 +96,7 @@ sleep 5 kill $TOR_PID wait $TOR_PID -#ls -lh "$DATA_DIR"/keys/ || exit 255 +#ls -lh "$DATA_DIR"/keys/ || exit 3 # tor must always have non-zero-length key files if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] && From dfb409b8b0b9bfcc4531e0a41b1924d55128c991 Mon Sep 17 00:00:00 2001 From: Sebastian Hahn Date: Wed, 11 Feb 2015 21:43:41 +0100 Subject: [PATCH 7/7] Put error messages to stderr Thanks weasel :) Also remove stray debug output --- src/test/zero_length_keys.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/zero_length_keys.sh b/src/test/zero_length_keys.sh index eca750c956..81ba3e0e17 100755 --- a/src/test/zero_length_keys.sh +++ b/src/test/zero_length_keys.sh @@ -27,12 +27,11 @@ fi DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX` if [ -z "$DATA_DIR" ]; then - echo "Failure: mktemp invocation returned empty string" + echo "Failure: mktemp invocation returned empty string" >&2 exit 3 fi if [ ! -d "$DATA_DIR" ]; then - echo "$DATA_DIR" - echo "Failure: mktemp invocation result doesn't point to directory" + echo "Failure: mktemp invocation result doesn't point to directory" >&2 exit 3 fi trap "rm -rf '$DATA_DIR'" 0 @@ -43,7 +42,7 @@ TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 1234 if [ -s "$DATA_DIR"/keys/secret_id_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key ] && [ -s "$DATA_DIR"/keys/secret_onion_key_ntor ]; then - echo "Failure: Previous tor keys present in tor data directory" + echo "Failure: Previous tor keys present in tor data directory" >&2 exit 3 else echo "Generating initial tor keys"