more work on test suite.
[monkeysphere.git] / tests / basic
1 #!/usr/bin/env bash
2
3 # Tests to ensure that the monkeysphere is working
4
5 # Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
6 # Date: 2008-09-13 13:40:15-0400
7
8 # these tests might be best run under fakeroot, particularly the
9 # "server-side" tests.  Using fakeroot, they should be able to be run
10 # as a non-privileged user.
11
12 # NOTE: these tests have *not* themselves been tested yet
13 # (2008-09-13).  Please exercise with caution!
14
15 # fail on fail
16 set -e
17
18 # these tests assume a commonly-trusted "Admin's key", a fake key
19 # permanently stored in ./home/admin/.gnupg:
20 gpgadmin() {
21     GNUPGHOME="$TESTDIR"/home/admin/.gnupg gpg "$@"
22 }
23
24 # cleanup:
25 cleanup() {
26
27     read -p "press enter to cleanup and remove tmp:"
28
29     # FIXME: stop the sshd process
30
31     echo
32     echo "### removing temp dir..."
33     rm -rf "$TEMPDIR"
34
35     # FIXME: how should we clear out the temporary $VARLIB?
36
37     # FIXME: clear out ssh client config file and known hosts.
38 }
39
40 ## setup trap
41 trap cleanup EXIT
42
43 ## set up some variables to ensure that we're operating strictly in
44 ## the tests, not system-wide:
45
46 export TESTDIR=$(pwd)
47
48 # make temp dir
49 TEMPDIR="$TESTDIR"/tmp
50 if [ -e "$TEMPDIR" ] ; then
51     echo "tempdir '$TEMPDIR' already exists."
52     exit 1
53 fi
54 mkdir "$TEMPDIR"
55
56 # Use the local copy of executables first, instead of system ones.
57 # This should help us test without installing.
58 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
59
60 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
61 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
62 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
63 export MONKEYSPHERE_MONKEYSPHERE_USER="$USER"
64 export MONKEYSPHERE_CHECK_KEYSERVER=false
65
66 SSHD_CONFIG="$TEMPDIR"/sshd_config
67 export SOCKET="$TEMPDIR"/ssh-socket
68
69
70 ### SERVER TESTS
71
72 # setup monkeysphere temp gnupghome directories
73 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
74 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
75 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
76 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
77 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
78 EOF
79
80 # create a new host key
81 echo "### generating server key..."
82 # add gpg.conf with quick-random
83 echo "quick-random" >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
84 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
85 # remove the gpg.conf
86 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
87
88 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\  )
89
90 # certify it with the "Admin's Key".
91 # (this would normally be done via keyservers)
92 echo "### certifying server key..."
93 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
94 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
95
96 # FIXME: how can we test publish-key without flooding junk into the
97 # keyservers?
98
99 # indicate that the "Admin's" key is an identity certifier for the
100 # host
101
102 echo "### adding admin as certifier..."
103 echo y | monkeysphere-server add-identity-certifier "$TESTDIR"/home/admin/.gnupg/pubkey.gpg
104
105 # initialize base sshd_config
106 cp etc/ssh/sshd_config "$SSHD_CONFIG"
107 # write the sshd_config
108 cat <<EOF >> "$SSHD_CONFIG"
109 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
110 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
111 EOF
112
113 # launch test sshd with the new host key.
114 echo "### starting sshd..."
115 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -d -d -d -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
116
117
118 ### TESTUSER TESTS
119
120 # copy testuser home directory into temp dir
121 echo "### seting up testuser home..."
122 cp -r "$TESTDIR"/home/testuser "$TEMPDIR"/
123
124 # generate an auth subkey for the test user
125 echo "### generating key for testuser..."
126 MONKEYSPHERE_GNUPGHOME="$TEMPDIR"/testuser/.gnupg \
127     monkeysphere gen-subkey --expire 0
128
129 # add server key to testuser keychain
130 echo "### export server key to testuser..."
131 gpgadmin --armor --export "$HOSTKEYID" | \
132     GNUPGHOME="$TEMPDIR"/testuser/.gnupg gpg --import
133
134 #GNUPGHOME="$TEMPDIR"/testuser/.gnupg gpg --list-keys
135 #read -p "?"
136
137 # connect to test sshd, using monkeysphere to verify the identity
138 # before connection.
139 echo "### testuser connecting to sshd socket..."
140 PROXY_COMMAND="monkeysphere-ssh-proxycommand --no-connect %h && socat STDIO UNIX:${SOCKET}"
141 GNUPGHOME="$TEMPDIR"/testuser/.gnupg ssh -oProxyCommand="$PROXY_COMMAND" testhost
142
143 # create a new client side key, certify it with the "CA", use it to
144 # log in.
145 ## FIXME: implement!