implement tests (and looser constraints on) peer names for peers who are clients
[monkeysphere-validation-agent.git] / tests / basic
1 #!/bin/bash
2
3 # simple set of tests to exercise the msva.
4
5 # these tests currently depend on the user having the following tools
6 # installed locally:
7
8 # monkeysphere (for pem2openpgp)
9 # openssl (for openssl req)
10 # openssh-client (for ssh-keygen)
11 # gpg (for obvious reasons)
12 # bash (yes, this test script isn't posix-compliant)
13
14 # note that this test requires the ability to bind on the loopback
15 # interface, which might not be possible in some build environments.
16
17 # Author: Daniel Kahn Gillmor
18 # Copyright: 2010
19 # License: This is licensed under the GPL v3 or later
20 #          (see the top-level COPYING file in this distribution)
21
22 set -e
23
24 srcdir=$(dirname $0)/..
25
26 REPS=5
27
28 CERTTYPES="x509pem x509der opensshpubkey rfc4716"
29
30 printf "testing %d reps of simple/quick true/false:\n" "$REPS"
31 for n in $(seq 1 "$REPS") ; do
32     "${srcdir}"/test-msva msva-perl true
33     printf "+"
34     ! "${srcdir}"/test-msva msva-perl false
35     printf "-"
36 done
37 printf "\ndone\n"
38
39 WORKDIR=$(mktemp -d)
40 mkdir -m 0700 "${WORKDIR}/"{pkc,sec,gnupg}
41 export GNUPGHOME="${WORKDIR}/gnupg"
42
43 if gpg --quick-random --version ; then
44     GPGQR=--quick-random
45 elif gpg --debug-quick-random --version ; then
46     GPGQR=--debug-quick-random
47 else
48     GPGQR=
49 fi
50
51 # make a CA
52 printf "Key-Type: RSA\nKey-Length: 1024\nKey-Usage: sign\nName-Real: MSVA Test Certificate Authority (DO NOT USE!)\n" | gpg --batch --no-tty $GPGQR --gen-key
53
54 # make 3 websites (X, Y, and Z) with self-signed certs:
55 for name in x y z ; do 
56     openssl req -x509 -subj "/CN=${name}.example.net/" -nodes -sha256 -newkey rsa:1024 -keyout "${WORKDIR}/sec/${name}.key" -outform DER -out "${WORKDIR}/pkc/${name}.x509der"
57     chmod 0400  "${WORKDIR}/sec/${name}.key"
58     openssl x509 -inform DER -outform PEM < "${WORKDIR}/pkc/${name}.x509der" > "${WORKDIR}/pkc/${name}.x509pem"
59     ssh-keygen -y -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.opensshpubkey"
60     ssh-keygen -e -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.rfc4716"
61 done
62
63 # make 2 client certs (A and B) with self-signed certs
64 for name in a b ; do 
65     openssl req -x509 -subj "/eMail=${name}@example.net/CN=${name}/" -nodes -sha256 -newkey rsa:1024 -keyout "${WORKDIR}/sec/${name}.key" -outform DER -out "${WORKDIR}/pkc/${name}.x509der"
66     chmod 0400  "${WORKDIR}/sec/${name}.key"
67     openssl x509 -inform DER -outform PEM < "${WORKDIR}/pkc/${name}.x509der" > "${WORKDIR}/pkc/${name}.x509pem"
68     ssh-keygen -y -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.opensshpubkey"
69     ssh-keygen -e -P '' -f "${WORKDIR}/sec/${name}.key" > "${WORKDIR}/pkc/${name}.rfc4716"
70 done
71
72 # translate X and Y's keys into OpenPGP cert
73 for name in x y; do
74     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "https://${name}.example.net" < "${WORKDIR}/sec/${name}.key" | gpg --import
75 done
76 # and the same for the clients A and B
77 for name in a b; do
78     PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "${name} <${name}@example.net>" < "${WORKDIR}/sec/${name}.key" | gpg --import
79 done
80
81 runtests() {
82     # X should not validate as X or Y or Z:
83     for name in x y z; do
84         for ctype in $CERTTYPES; do
85             ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${name}.example.net" "${ctype}" < "${WORKDIR}/pkc/x.${ctype}"
86         done
87     done
88     # A shouldn't validate as A or B:
89     for name in a b; do
90         for ctype in $CERTTYPES; do
91             ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${name} <${name}@example.net>" "${ctype}" client < "${WORKDIR}/pkc/a.${ctype}"
92         done
93     done
94     
95     # certify X and A's OpenPGP cert with CA
96     gpg --batch --yes --sign-key https://x.example.net
97     gpg --batch --yes --sign-key a@example.net
98
99     echo "Testing bad data:"
100     # it should fail if we pass it the wrong kind of data:
101     ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https x.example.net "x509der" < "${WORKDIR}/pkc/x.x509pem"
102     ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https x.example.net "x509pem" < "${WORKDIR}/pkc/x.x509der"
103     echo "Done testing bad data."
104         
105     for ctype in $CERTTYPES; do 
106     # X should now validate as X
107         "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https x.example.net "${ctype}" < "${WORKDIR}/pkc/x.${ctype}"
108         "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https 'a <a@example.net>' "${ctype}" client < "${WORKDIR}/pkc/a.${ctype}"
109         
110     # but X should not validate as Y or Z:
111         for name in x y z; do
112             ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${name}.example.net" "${ctype}" < "${WORKDIR}/pkc/x.${ctype}"
113         done
114         # and A shouldn't validate as B:
115         ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "b <b@example.net>" "${ctype}" client < "${WORKDIR}/pkc/a.${ctype}"
116
117     # neither Y nor Z should validate as any of them:
118         for src in y z; do
119             for targ in x y z; do
120                 ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "${targ}.example.net" "${ctype}" < "${WORKDIR}/pkc/${src}.${ctype}"
121             done
122         done
123         # B should also still not validate as itself:
124         ! "${srcdir}"/test-msva msva-perl "${srcdir}"/test-msva msva-query-agent https "b <b@example.net>" "${ctype}" client < "${WORKDIR}/pkc/b.${ctype}"
125     done
126 }
127
128 set -x
129 MSVA_KEYSERVER_POLICY=never runtests
130 set +x
131 echo "Completed all tests as expected!"
132
133 rm -rf "$WORKDIR"