LCOV - code coverage report
Current view: top level - test/unitTest - certstore.cpp (source / functions) Hit Total Coverage
Test: dring-coverage-filtered.info Lines: 47 47 100.0 %
Date: 2019-02-25 22:01:53 Functions: 14 14 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2011-2019 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  Author: florian Wiesweg <florian.wiesweg@campus.tu-berlin.de>
       5             :  *
       6             :  *  This program is free software; you can redistribute it and/or modify
       7             :  *  it under the terms of the GNU General Public License as published by
       8             :  *  the Free Software Foundation; either version 3 of the License, or
       9             :  *  (at your option) any later version.
      10             :  *
      11             :  *  This program is distributed in the hope that it will be useful,
      12             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  *  GNU General Public License for more details.
      15             :  *
      16             :  *  You should have received a copy of the GNU General Public License
      17             :  *  along with this program; if not, write to the Free Software
      18             :  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
      19             :  */
      20             : 
      21             : #include <cppunit/TestAssert.h>
      22             : #include <cppunit/TestFixture.h>
      23             : #include <cppunit/extensions/HelperMacros.h>
      24             : 
      25             : #include "test_runner.h"
      26             : 
      27             : #include "security/certstore.h"
      28             : 
      29             : namespace ring { namespace test {
      30             : 
      31           6 : class CertStoreTest : public CppUnit::TestFixture {
      32             : public:
      33           4 :     static std::string name() { return "certstore"; }
      34             : 
      35             : private:
      36             :     void trustStoreTest();
      37             : 
      38           6 :     CPPUNIT_TEST_SUITE(CertStoreTest);
      39           2 :     CPPUNIT_TEST(trustStoreTest);
      40          10 :     CPPUNIT_TEST_SUITE_END();
      41             : };
      42             : 
      43           2 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CertStoreTest, CertStoreTest::name());
      44             : 
      45             : void
      46           2 : CertStoreTest::trustStoreTest()
      47             : {
      48           4 :     ring::tls::TrustStore trustStore;
      49             : 
      50           4 :     auto ca = dht::crypto::generateIdentity("test CA");
      51           4 :     auto account = dht::crypto::generateIdentity("test account", ca, 4096, true);
      52           4 :     auto device = dht::crypto::generateIdentity("test device", account);
      53           4 :     auto device2 = dht::crypto::generateIdentity("test device 2", account);
      54             : 
      55           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(ca.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::UNDEFINED);
      56           2 :     trustStore.setCertificateStatus(ca.second, ring::tls::TrustStore::PermissionStatus::ALLOWED);
      57           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(ca.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::ALLOWED);
      58           2 :     trustStore.setCertificateStatus(ca.second, ring::tls::TrustStore::PermissionStatus::UNDEFINED);
      59           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(ca.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::UNDEFINED);
      60           2 :     trustStore.setCertificateStatus(ca.second, ring::tls::TrustStore::PermissionStatus::ALLOWED);
      61           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(ca.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::ALLOWED);
      62             : 
      63           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*ca.second));
      64           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*account.second));
      65           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*device.second));
      66             : 
      67             :     // Ban device
      68           2 :     trustStore.setCertificateStatus(device.second, ring::tls::TrustStore::PermissionStatus::BANNED);
      69           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(device.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::BANNED);
      70           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(ca.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::ALLOWED);
      71             : 
      72           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*ca.second));
      73           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*account.second));
      74           2 :     CPPUNIT_ASSERT(not trustStore.isAllowed(*device.second));
      75             : 
      76             :     // Ban account
      77           2 :     trustStore.setCertificateStatus(account.second, ring::tls::TrustStore::PermissionStatus::BANNED);
      78           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(account.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::BANNED);
      79           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*ca.second));
      80           2 :     CPPUNIT_ASSERT(not trustStore.isAllowed(*account.second));
      81           2 :     CPPUNIT_ASSERT(not trustStore.isAllowed(*device2.second));
      82             : 
      83             :     // Unban account
      84           2 :     trustStore.setCertificateStatus(account.second, ring::tls::TrustStore::PermissionStatus::ALLOWED);
      85           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(account.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::ALLOWED);
      86           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*ca.second));
      87           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*account.second));
      88           2 :     CPPUNIT_ASSERT(trustStore.isAllowed(*device2.second));
      89             : 
      90             :     // Ban CA
      91           2 :     trustStore.setCertificateStatus(ca.second, ring::tls::TrustStore::PermissionStatus::BANNED);
      92           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(ca.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::BANNED);
      93           2 :     CPPUNIT_ASSERT(not trustStore.isAllowed(*ca.second));
      94           2 :     CPPUNIT_ASSERT(not trustStore.isAllowed(*account.second));
      95           2 :     CPPUNIT_ASSERT(not trustStore.isAllowed(*device2.second));
      96             : 
      97           2 :     trustStore.setCertificateStatus(ca.second, ring::tls::TrustStore::PermissionStatus::BANNED);
      98           2 :     CPPUNIT_ASSERT(trustStore.getCertificateStatus(ca.second->getId().toString()) == ring::tls::TrustStore::PermissionStatus::BANNED);
      99           2 : }
     100             : 
     101             : }} // namespace ring::test
     102             : 
     103           8 : RING_TEST_RUNNER(ring::test::CertStoreTest::name());

Generated by: LCOV version 1.12