ab95f596d91bfe70abf5608c8c054549e2edcb37
[monkeysphere.git] / tests / keytrans
1 #!/usr/bin/env bash
2
3 # Tests to ensure that the monkeysphere is working
4
5 # Authors: 
6 #   Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 #   Jameson Rollins <jrollins@fifthhorseman.net>
8 #   Micah Anderson <micah@riseup.net> 
9 #
10 # Copyright: 2008-2009
11 # License: GPL v3 or later
12
13 # these tests should all be able to run as a non-privileged user.
14
15 # all subcommands in this script should complete without failure:
16 set -e
17 # piped commands should return the code of the first non-zero return
18 set -o pipefail
19
20 # make sure the TESTDIR is an absolute path, not a relative one.
21 export TESTDIR=$(cd $(dirname "$0") && pwd)
22
23 source "$TESTDIR"/common
24
25 ## setup trap
26 trap failed_cleanup EXIT
27
28 ######################################################################
29 ### SETUP VARIABLES
30
31 ## set up some variables to ensure that we're operating strictly in
32 ## the tests, not system-wide:
33
34 mkdir -p "$TESTDIR"/tmp
35 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
36
37 mkdir "$TEMPDIR"/bin
38 ln -s "$TESTDIR"/../src/share/keytrans "$TEMPDIR"/bin/openpgp2ssh
39 ln -s "$TESTDIR"/../src/share/keytrans "$TEMPDIR"/bin/pem2openpgp
40
41 # Use the local copy of executables first, instead of system ones.
42 # This should help us test without installing.
43 export PATH="$TEMPDIR"/bin:"$PATH"
44
45 ######################################################################
46 ### TEST KEYTRANS
47
48 echo "##################################################"
49 echo "### generating openpgp key..."
50 export GNUPGHOME="$TEMPDIR"
51 chmod 700 "$TEMPDIR"
52
53
54 # create the key with the same preferences that monkeysphere uses.
55 cat > "$TEMPDIR"/gpg.conf <<EOF
56 default-preference-list SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1 ZLIB BZIP2 ZIP AES256 AES192 AES CAST5 3DES
57 cert-digest-algo SHA256
58 EOF
59
60 # generate a key
61 gpg --batch --$(get_gpg_prng_arg) --gen-key <<EOF
62 Key-Type: RSA
63 Key-Length: 1024
64 Key-Usage: sign
65 Name-Real: testtest
66 Expire-Date: 0
67
68 %commit
69 %echo done
70 EOF
71
72 echo "##################################################"
73 echo "### retrieving key timestamp..."
74 timestamp=$(gpg --list-key --with-colons --fixed-list-mode | \
75     grep ^pub: | cut -d: -f6)
76
77 echo "##################################################"
78 echo "### exporting key to ssh file..."
79 gpg --export-secret-key | openpgp2ssh > \
80     "$TEMPDIR"/test.pem
81
82 gpg --export-secret-key > "$TEMPDIR"/secret.key
83
84 PEM2OPENPGP_USAGE_FLAGS=sign,certify \
85 PEM2OPENPGP_TIMESTAMP="$timestamp" pem2openpgp testtest \
86  < "$TEMPDIR"/test.pem > "$TEMPDIR"/converted.secret.key
87
88 echo "##################################################"
89 echo "### reconvert key, and compare to key in gpg keyring..."
90 diff -u \
91     <(gpg --list-packets < "$TEMPDIR"/secret.key) \
92     <(gpg --list-packets < "$TEMPDIR"/converted.secret.key)
93
94 diff -u \
95     <(hd "$TEMPDIR"/secret.key) \
96     <(hd "$TEMPDIR"/converted.secret.key)
97
98 trap - EXIT
99
100 echo "##################################################"
101 echo " Monkeysphere keytrans test completed successfully!"
102 echo "##################################################"
103
104 cleanup