Merge commit 'mjgoins/master'
[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 perl -MCrypt::OpenSSL::RSA -e 1 2>/dev/null || { echo "You must have the perl module Crypt::OpenSSL::RSA installed to run this test.  
26 On debian-derived systems, you can set this up with:
27   apt-get install libcrypt-openssl-rsa-perl" ; exit 1; }
28
29 perl -MDigest::SHA -e 1 2>/dev/null || { echo "You must have the perl module Digest::SHA installed to run this test.  
30 On debian-derived systems, you can set this up with:
31   apt-get install libdigest-sha1-perl" ; exit 1; }
32
33
34 ######################################################################
35 ### SETUP VARIABLES
36
37 ## set up some variables to ensure that we're operating strictly in
38 ## the tests, not system-wide:
39
40 mkdir -p "$TESTDIR"/tmp
41 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
42
43 mkdir "$TEMPDIR"/bin
44 ln -s "$TESTDIR"/../src/share/keytrans "$TEMPDIR"/bin/openpgp2ssh
45 ln -s "$TESTDIR"/../src/share/keytrans "$TEMPDIR"/bin/pem2openpgp
46
47 # Use the local copy of executables first, instead of system ones.
48 # This should help us test without installing.
49 export PATH="$TEMPDIR"/bin:"$PATH"
50
51 ## setup trap
52 trap failed_cleanup EXIT
53
54 ######################################################################
55 ### TEST KEYTRANS
56
57 echo "##################################################"
58 echo "### generating openpgp key..."
59 export GNUPGHOME="$TEMPDIR"
60 chmod 700 "$TEMPDIR"
61
62
63 # create the key with the same preferences that monkeysphere uses.
64 cat > "$TEMPDIR"/gpg.conf <<EOF
65 default-preference-list SHA512 SHA384 SHA256 SHA224 RIPEMD160 SHA1 ZLIB BZIP2 ZIP AES256 AES192 AES CAST5 3DES
66 cert-digest-algo SHA256
67 EOF
68
69 # generate a key
70 gpg --batch --$(get_gpg_prng_arg) --gen-key <<EOF
71 Key-Type: RSA
72 Key-Length: 1024
73 Key-Usage: sign
74 Name-Real: testtest
75 Expire-Date: 0
76
77 %commit
78 %echo done
79 EOF
80
81 echo "##################################################"
82 echo "### retrieving key timestamp..."
83 timestamp=$(gpg --list-key --with-colons --fixed-list-mode | \
84     grep ^pub: | cut -d: -f6)
85
86 echo "##################################################"
87 echo "### exporting key to ssh file..."
88 gpg --export-secret-key | openpgp2ssh > \
89     "$TEMPDIR"/test.pem
90
91 gpg --export-secret-key > "$TEMPDIR"/secret.key
92
93 PEM2OPENPGP_USAGE_FLAGS=sign,certify \
94 PEM2OPENPGP_TIMESTAMP="$timestamp" pem2openpgp testtest \
95  < "$TEMPDIR"/test.pem > "$TEMPDIR"/converted.secret.key
96
97 echo "##################################################"
98 echo "### reconvert key, and compare to key in gpg keyring..."
99 diff -u \
100     <(gpg --list-packets < "$TEMPDIR"/secret.key) \
101     <(gpg --list-packets < "$TEMPDIR"/converted.secret.key)
102
103 diff -u \
104     <(hd "$TEMPDIR"/secret.key) \
105     <(hd "$TEMPDIR"/converted.secret.key)
106
107 trap - EXIT
108
109 echo "##################################################"
110 echo " Monkeysphere keytrans test completed successfully!"
111 echo "##################################################"
112
113 cleanup