Whitespace.
[libdcp.git] / test / certificates_test.cc
index 8ae01eed6ee615e8583b0a3fad77c50216fe58da..1dc1193221f12c3342faa65281c442131c18caef 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2019 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
 
     You should have received a copy of the GNU General Public License
     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
 */
 
 #include "certificate.h"
@@ -27,7 +41,7 @@
 
 using std::list;
 using std::string;
-using boost::shared_ptr;
+using std::shared_ptr;
 
 /** Check that loading certificates from files via strings works */
 BOOST_AUTO_TEST_CASE (certificates1)
@@ -44,6 +58,7 @@ BOOST_AUTO_TEST_CASE (certificates1)
 
        /* Leaf */
        BOOST_CHECK_EQUAL (*i, c.leaf ());
+       BOOST_CHECK_EQUAL (i->thumbprint(), "EZg5wDcihccWqwdg59Y8D+IJpYM=");
 
        BOOST_CHECK_EQUAL (
                c.leaf().issuer(),
@@ -58,6 +73,7 @@ BOOST_AUTO_TEST_CASE (certificates1)
        ++i;
 
        /* Intermediate */
+       BOOST_CHECK_EQUAL (i->thumbprint(), "GwM6ex2UVlWclH8f1uV7W1n0EEU=");
        BOOST_CHECK_EQUAL (
                i->issuer(),
                "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
@@ -72,6 +88,7 @@ BOOST_AUTO_TEST_CASE (certificates1)
 
        /* Root */
        BOOST_CHECK_EQUAL (*i, c.root ());
+       BOOST_CHECK_EQUAL (i->thumbprint(), "zU8NVNwI2PYejmSYRntG7c6sdTw=");
        BOOST_CHECK_EQUAL (
                c.root().issuer(),
                "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
@@ -111,51 +128,108 @@ BOOST_AUTO_TEST_CASE (certificates2)
        BOOST_CHECK_THROW (dcp::Certificate ("foo"), dcp::MiscError);
 }
 
-/** Check that dcp::CertificateChain::valid() and ::attempt_reorder() basically work */
-BOOST_AUTO_TEST_CASE (certificates_validation)
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation1)
 {
-       dcp::CertificateChain good1;
-       good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
-       good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
-       good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
-       BOOST_CHECK (good1.valid ());
-
-       dcp::CertificateChain good2;
-       good2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
-       BOOST_CHECK (good2.valid ());
-
-       dcp::CertificateChain bad1;
-       bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
-       bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
-       BOOST_CHECK (!bad1.valid ());
-       BOOST_CHECK (!bad1.attempt_reorder ());
-
-       dcp::CertificateChain bad2;
-       bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
-       bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
-       bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
-       BOOST_CHECK (!bad2.valid ());
-       BOOST_CHECK (bad2.attempt_reorder ());
-
-       dcp::CertificateChain bad3;
-       bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
-       bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
-       bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
-       BOOST_CHECK (!bad3.valid ());
-       BOOST_CHECK (bad3.attempt_reorder ());
-
-       dcp::CertificateChain bad4;
-       bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
-       bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
-       bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
-       BOOST_CHECK (!bad4.valid ());
-       BOOST_CHECK (bad4.attempt_reorder ());
-
-       dcp::CertificateChain bad5;
-       bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
-       bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
-       BOOST_CHECK (!bad5.valid ());
-       BOOST_CHECK (!bad5.attempt_reorder ());
+       dcp::CertificateChain good;
+       good.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       good.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
+       good.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
+       BOOST_CHECK (good.chain_valid(good._certificates));
+}
+
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation2)
+{
+       dcp::CertificateChain good;
+       good.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       BOOST_CHECK (good.chain_valid(good._certificates));
+}
+
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation3)
+{
+       dcp::CertificateChain bad;
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
+       BOOST_CHECK (!bad.chain_valid(bad._certificates));
+       BOOST_CHECK_THROW (bad.root_to_leaf(), dcp::CertificateChainError);
+}
+
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation4)
+{
+       dcp::CertificateChain bad;
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
+       BOOST_CHECK (!bad.chain_valid(bad._certificates));
+       BOOST_CHECK_NO_THROW (bad.root_to_leaf());
+}
+
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation5)
+{
+       dcp::CertificateChain bad;
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       BOOST_CHECK (!bad.chain_valid(bad._certificates));
+       BOOST_CHECK_NO_THROW (bad.root_to_leaf());
+}
+
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation6)
+{
+       dcp::CertificateChain bad;
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       BOOST_CHECK (!bad.chain_valid(bad._certificates));
+       BOOST_CHECK_NO_THROW (bad.root_to_leaf());
+}
+
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation7)
+{
+       dcp::CertificateChain bad;
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
+       BOOST_CHECK (!bad.chain_valid(bad._certificates));
+       BOOST_CHECK_THROW (bad.root_to_leaf(), dcp::CertificateChainError);
+}
+
+/** Check that dcp::CertificateChain::chain_valid() and ::root_to_leaf() basically work */
+BOOST_AUTO_TEST_CASE (certificates_validation8)
+{
+       dcp::CertificateChain bad;
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
+       bad.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
+       BOOST_CHECK (!bad.chain_valid(bad._certificates));
+       BOOST_CHECK_THROW (bad.root_to_leaf(), dcp::CertificateChainError);
+}
+
+/** Check that we can create a valid chain */
+BOOST_AUTO_TEST_CASE (certificates_validation9)
+{
+       dcp::CertificateChain good (
+               boost::filesystem::path ("openssl"),
+               "dcpomatic.com",
+               "dcpomatic.com",
+               ".dcpomatic.smpte-430-2.ROOT",
+               ".dcpomatic.smpte-430-2.INTERMEDIATE",
+               "CS.dcpomatic.smpte-430-2.LEAF"
+               );
+
+       BOOST_CHECK_NO_THROW (good.root_to_leaf());
+}
+
+/** Check that we can create a valid chain */
+BOOST_AUTO_TEST_CASE (certificates_validation10)
+{
+       dcp::CertificateChain good (boost::filesystem::path ("openssl"));
+       BOOST_CHECK_NO_THROW (good.root_to_leaf());
 }
 
 /** Check that dcp::Signer::valid() basically works */
@@ -184,3 +258,23 @@ BOOST_AUTO_TEST_CASE (certificate_chain_from_string)
        dcp::CertificateChain b (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem"));
        BOOST_CHECK_EQUAL (b.root_to_leaf().size(), 1);
 }
+
+/** Check not_before and not_after */
+BOOST_AUTO_TEST_CASE (certificate_not_before_after)
+{
+       dcp::Certificate c (dcp::file_to_string("test/ref/crypt/ca.self-signed.pem"));
+       struct tm not_before = c.not_before();
+       BOOST_CHECK_EQUAL (not_before.tm_sec, 8);
+       BOOST_CHECK_EQUAL (not_before.tm_min, 20);
+       BOOST_CHECK_EQUAL (not_before.tm_hour, 13);
+       BOOST_CHECK_EQUAL (not_before.tm_mday, 5);
+       BOOST_CHECK_EQUAL (not_before.tm_mon, 5);
+       BOOST_CHECK_EQUAL (not_before.tm_year, 115);
+       struct tm not_after = c.not_after();
+       BOOST_CHECK_EQUAL (not_after.tm_sec, 8);
+       BOOST_CHECK_EQUAL (not_after.tm_min, 20);
+       BOOST_CHECK_EQUAL (not_after.tm_hour, 13);
+       BOOST_CHECK_EQUAL (not_after.tm_mday, 2);
+       BOOST_CHECK_EQUAL (not_after.tm_mon, 5);
+       BOOST_CHECK_EQUAL (not_after.tm_year, 125);
+}