Cope with certificate files that have no line breaks.
[libdcp.git] / test / certificates_test.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/test/unit_test.hpp>
21 #include "certificate.h"
22 #include "certificate_chain.h"
23 #include "util.h"
24 #include "exceptions.h"
25 #include "test.h"
26
27 using std::list;
28 using std::string;
29 using boost::shared_ptr;
30
31 /** Check that loading certificates from files via strings works */
32 BOOST_AUTO_TEST_CASE (certificates1)
33 {
34         dcp::CertificateChain c;
35
36         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
37         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
38         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
39
40         dcp::CertificateChain::List leaf_to_root = c.leaf_to_root ();
41
42         dcp::CertificateChain::List::iterator i = leaf_to_root.begin ();
43
44         /* Leaf */
45         BOOST_CHECK_EQUAL (*i, c.leaf ());
46
47         BOOST_CHECK_EQUAL (
48                 c.leaf().issuer(),
49                 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
50                 );
51
52         BOOST_CHECK_EQUAL (
53                 c.leaf().subject(),
54                 "dnQualifier=QFVlym7fuql6bPOnY38aaO1ZPW4=,CN=CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
55                 );
56
57         ++i;
58
59         /* Intermediate */
60         BOOST_CHECK_EQUAL (
61                 i->issuer(),
62                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
63                 );
64
65         BOOST_CHECK_EQUAL (
66                 i->subject(),
67                 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
68                 );
69
70         ++i;
71
72         /* Root */
73         BOOST_CHECK_EQUAL (*i, c.root ());
74         BOOST_CHECK_EQUAL (
75                 c.root().issuer(),
76                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
77                 );
78
79         BOOST_CHECK_EQUAL (c.root().serial(), "5");
80
81         BOOST_CHECK_EQUAL (
82                 c.root().subject(),
83                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
84                 );
85
86         /* Check that reconstruction from a string works */
87         dcp::Certificate test (c.root().certificate (true));
88         BOOST_CHECK_EQUAL (test.certificate(), c.root().certificate());
89 }
90
91 /** Check some more certificate-from-strings */
92 BOOST_AUTO_TEST_CASE (certificates2)
93 {
94         dcp::Certificate c (dcp::file_to_string (private_test / "CA.GDC-TECH.COM_SA2100_A14903.crt.crt"));
95         BOOST_CHECK_EQUAL (c.certificate(true), dcp::file_to_string (private_test / "CA.GDC-TECH.COM_SA2100_A14903.crt.crt.reformatted"));
96
97         BOOST_CHECK_THROW (dcp::Certificate ("foo"), dcp::MiscError);
98 }
99
100 /** Check that dcp::CertificateChain::valid() and ::attempt_reorder() basically work */
101 BOOST_AUTO_TEST_CASE (certificates_validation)
102 {
103         dcp::CertificateChain good1;
104         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
105         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
106         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
107         BOOST_CHECK (good1.valid ());
108
109         dcp::CertificateChain good2;
110         good2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
111         BOOST_CHECK (good2.valid ());
112
113         dcp::CertificateChain bad1;
114         bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
115         bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
116         BOOST_CHECK (!bad1.valid ());
117         BOOST_CHECK (!bad1.attempt_reorder ());
118
119         dcp::CertificateChain bad2;
120         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
121         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
122         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
123         BOOST_CHECK (!bad2.valid ());
124         BOOST_CHECK (bad2.attempt_reorder ());
125
126         dcp::CertificateChain bad3;
127         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
128         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
129         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
130         BOOST_CHECK (!bad3.valid ());
131         BOOST_CHECK (bad3.attempt_reorder ());
132
133         dcp::CertificateChain bad4;
134         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
135         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
136         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
137         BOOST_CHECK (!bad4.valid ());
138         BOOST_CHECK (bad4.attempt_reorder ());
139
140         dcp::CertificateChain bad5;
141         bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
142         bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
143         BOOST_CHECK (!bad5.valid ());
144         BOOST_CHECK (!bad5.attempt_reorder ());
145 }
146
147 /** Check that dcp::Signer::valid() basically works */
148 BOOST_AUTO_TEST_CASE (signer_validation)
149 {
150         /* Check a valid signer */
151         dcp::CertificateChain chain;
152         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
153         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
154         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
155         chain.set_key (dcp::file_to_string ("test/ref/crypt/leaf.key"));
156         BOOST_CHECK (chain.valid ());
157
158         /* Put in an unrelated key and the signer should no longer be valid */
159         dcp::CertificateChain another_chain ("openssl");
160         chain.set_key (another_chain.key().get ());
161         BOOST_CHECK (!chain.valid ());
162 }