Remove useless export in zero length key test
[tor.git] / src / test / zero_length_keys.sh
blob729baebfea61299d11c22557455861ad52c27e72
1 #!/bin/sh
2 # Check that tor regenerates keys when key files are zero-length
3 # Test for bug #13111 - Tor fails to start if onion keys are zero length
5 # Usage:
6 # ./zero_length_keys.sh
7 # Run all the tests below
8 # ./zero_length_keys.sh -z
9 # Check tor will launch and regenerate zero-length keys
10 # ./zero_length_keys.sh -d
11 # Check tor regenerates deleted keys (existing behaviour)
12 # ./zero_length_keys.sh -e
13 # Check tor does not overwrite existing keys (existing behaviour)
15 # Exit Statuses:
16 # 0: test succeeded - tor regenerated/kept the files
17 # 1: test failed - tor did not regenerate/keep the files
18 # 254: test failed - tor did not generate the key files on first run
19 # 255: a command failed - the test could not be completed
22 if [ $# -lt 1 ]; then
23 echo "Testing that tor correctly handles zero-length keys"
24 "$0" -z && "$0" -d && "$0" -e
25 exit $?
28 DATA_DIR=`mktemp -d -t tor_zero_length_keys.XXXXXX`
29 # DisableNetwork means that the ORPort won't actually be opened.
30 # 'ExitRelay 0' suppresses a warning.
31 TOR="./src/or/tor --hush --DisableNetwork 1 --ShutdownWaitLength 0 --ORPort 12345 --ExitRelay 0"
33 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
34 echo "Failure: Previous tor keys present in tor data directory"
35 exit 255
36 else
37 echo "Generating initial tor keys"
38 $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid &
39 TOR_PID=$!
40 # generate SIGTERM, hopefully after the keys have been regenerated
41 sleep 5
42 kill $TOR_PID
43 wait $TOR_PID
45 # tor must successfully generate non-zero-length key files
46 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
47 true #echo "tor generated the initial key files"
48 else
49 echo "Failure: tor failed to generate the initial key files"
50 exit 254
54 #ls -lh "$DATA_DIR"/keys/ || exit 255
56 # backup and keep/delete/create zero-length files for the keys
58 FILE_DESC="keeps existing"
59 # make a backup
60 cp -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old
62 # delete keys for -d or -z
63 if [ "$1" != "-e" ]; then
64 FILE_DESC="regenerates deleted"
65 rm "$DATA_DIR"/keys/secret_id_key || exit 255
66 rm "$DATA_DIR"/keys/secret_onion_key || exit 255
67 rm "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255
70 # create empty files for -z
71 if [ "$1" = "-z" ]; then
72 FILE_DESC="regenerates zero-length"
73 touch "$DATA_DIR"/keys/secret_id_key || exit 255
74 touch "$DATA_DIR"/keys/secret_onion_key || exit 255
75 touch "$DATA_DIR"/keys/secret_onion_key_ntor || exit 255
78 echo "Running tor again to check if it $FILE_DESC keys"
79 $TOR --DataDirectory "$DATA_DIR" --PidFile "$DATA_DIR"/pid &
80 TOR_PID=$!
81 # generate SIGTERM, hopefully after the keys have been regenerated
82 sleep 5
83 kill $TOR_PID
84 wait $TOR_PID
86 #ls -lh "$DATA_DIR"/keys/ || exit 255
88 # tor must always have non-zero-length key files
89 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
90 # check if the keys are different to the old ones
91 diff -q -r "$DATA_DIR"/keys "$DATA_DIR"/keys.old > /dev/null
92 SAME_KEYS=$?
93 # if we're not testing existing keys,
94 # the current keys should be different to the old ones
95 if [ "$1" != "-e" ]; then
96 if [ $SAME_KEYS -ne 0 ]; then
97 echo "Success: test that tor $FILE_DESC key files: different keys"
98 exit 0
99 else
100 echo "Failure: test that tor $FILE_DESC key files: same keys"
101 exit 1
103 else #[ "$1" == "-e" ]; then
104 if [ $SAME_KEYS -eq 0 ]; then
105 echo "Success: test that tor $FILE_DESC key files: same keys"
106 exit 0
107 else
108 echo "Failure: test that tor $FILE_DESC key files: different keys"
109 exit 1
112 else
113 echo "Failure: test that tor $FILE_DESC key files: no key files"
114 exit 1