1ea7c3f0cSRavi Teja #include "config.h" 2ea7c3f0cSRavi Teja 3ea7c3f0cSRavi Teja #include "bmc-vmi-ca/ca_certs_manager.hpp" 4ea7c3f0cSRavi Teja 5ea7c3f0cSRavi Teja #include <iterator> 6ea7c3f0cSRavi Teja #include <sdeventplus/event.hpp> 7ea7c3f0cSRavi Teja #include <string> 8ea7c3f0cSRavi Teja #include <xyz/openbmc_project/Certs/error.hpp> 9ea7c3f0cSRavi Teja #include <xyz/openbmc_project/Common/error.hpp> 10ea7c3f0cSRavi Teja 11ea7c3f0cSRavi Teja #include <gtest/gtest.h> 12ea7c3f0cSRavi Teja 13e1289adfSNan Zhou namespace ca::cert 14e1289adfSNan Zhou { 15e1289adfSNan Zhou namespace 16e1289adfSNan Zhou { 17ea7c3f0cSRavi Teja using InvalidArgument = 18ea7c3f0cSRavi Teja sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; 19ea7c3f0cSRavi Teja 20ea7c3f0cSRavi Teja class MockCACertMgr : public CACertMgr 21ea7c3f0cSRavi Teja { 22ea7c3f0cSRavi Teja public: 23*b3dbfb37SPatrick Williams MockCACertMgr(sdbusplus::bus_t& bus, const char* path) : 24cf06ccdcSNan Zhou CACertMgr(bus, path) 25ea7c3f0cSRavi Teja { 26ea7c3f0cSRavi Teja } 27ea7c3f0cSRavi Teja 28ea7c3f0cSRavi Teja void deleteAll() 29ea7c3f0cSRavi Teja { 30ea7c3f0cSRavi Teja CACertMgr::deleteAll(); 31ea7c3f0cSRavi Teja } 32ea7c3f0cSRavi Teja 33ea7c3f0cSRavi Teja void erase(uint32_t entryId) 34ea7c3f0cSRavi Teja { 35ea7c3f0cSRavi Teja CACertMgr::erase(entryId); 36ea7c3f0cSRavi Teja } 37ea7c3f0cSRavi Teja std::string createCSRObject(std::string csrString) 38ea7c3f0cSRavi Teja { 39ea7c3f0cSRavi Teja return (signCSR(csrString)); 40ea7c3f0cSRavi Teja } 41ea7c3f0cSRavi Teja 42ea7c3f0cSRavi Teja uint32_t getNumOfEntries() 43ea7c3f0cSRavi Teja { 44ea7c3f0cSRavi Teja return entries.size(); 45ea7c3f0cSRavi Teja } 46ea7c3f0cSRavi Teja 47ea7c3f0cSRavi Teja friend class TestCACertMgr; 48ea7c3f0cSRavi Teja }; 49ea7c3f0cSRavi Teja /** 50ea7c3f0cSRavi Teja * Class to create certificate authority manager unit testcases. 51ea7c3f0cSRavi Teja */ 52ea7c3f0cSRavi Teja class TestCACertMgr : public ::testing::Test 53ea7c3f0cSRavi Teja { 54ea7c3f0cSRavi Teja public: 55ea7c3f0cSRavi Teja TestCACertMgr() : bus(sdbusplus::bus::new_default()) 56ea7c3f0cSRavi Teja { 57ea7c3f0cSRavi Teja } 58ea7c3f0cSRavi Teja 59ea7c3f0cSRavi Teja protected: 60*b3dbfb37SPatrick Williams sdbusplus::bus_t bus; 61ea7c3f0cSRavi Teja }; 62ea7c3f0cSRavi Teja 63ea7c3f0cSRavi Teja TEST_F(TestCACertMgr, testObjectCreation) 64ea7c3f0cSRavi Teja { 65ea7c3f0cSRavi Teja auto bus = sdbusplus::bus::new_default(); 66ea7c3f0cSRavi Teja std::string objPath = "/xyz/openbmc_project/certs/ca"; 67ea7c3f0cSRavi Teja auto event = sdeventplus::Event::get_default(); 68ea7c3f0cSRavi Teja bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 69cf06ccdcSNan Zhou MockCACertMgr manager(bus, objPath.c_str()); 70ea7c3f0cSRavi Teja 71ea7c3f0cSRavi Teja std::string csrString = "csr string"; 72ea7c3f0cSRavi Teja EXPECT_NO_THROW(objPath = manager.createCSRObject(csrString)); 73ea7c3f0cSRavi Teja EXPECT_TRUE(manager.getNumOfEntries() == 1); 74ea7c3f0cSRavi Teja } 75ea7c3f0cSRavi Teja 76ea7c3f0cSRavi Teja TEST_F(TestCACertMgr, testInvalidArgument) 77ea7c3f0cSRavi Teja { 78ea7c3f0cSRavi Teja auto bus = sdbusplus::bus::new_default(); 79ea7c3f0cSRavi Teja std::string objPath = "/xyz/openbmc_project/certs/ca"; 80ea7c3f0cSRavi Teja auto event = sdeventplus::Event::get_default(); 81ea7c3f0cSRavi Teja bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 82cf06ccdcSNan Zhou MockCACertMgr manager(bus, objPath.c_str()); 83ea7c3f0cSRavi Teja 84ea7c3f0cSRavi Teja std::string csrString(4097, 'C'); 85ea7c3f0cSRavi Teja 86ea7c3f0cSRavi Teja EXPECT_THROW(objPath = manager.createCSRObject(csrString), InvalidArgument); 87ea7c3f0cSRavi Teja } 88ea7c3f0cSRavi Teja TEST_F(TestCACertMgr, DeleteAllCSRObjects) 89ea7c3f0cSRavi Teja { 90ea7c3f0cSRavi Teja auto bus = sdbusplus::bus::new_default(); 91ea7c3f0cSRavi Teja std::string objPath = "/xyz/openbmc_project/certs/ca"; 92ea7c3f0cSRavi Teja auto event = sdeventplus::Event::get_default(); 93ea7c3f0cSRavi Teja 94ea7c3f0cSRavi Teja bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 95cf06ccdcSNan Zhou MockCACertMgr manager(bus, objPath.c_str()); 96ea7c3f0cSRavi Teja 97ea7c3f0cSRavi Teja std::string csrString = "csr string"; 98ea7c3f0cSRavi Teja 99ea7c3f0cSRavi Teja objPath = manager.createCSRObject(csrString); 100ea7c3f0cSRavi Teja objPath = manager.createCSRObject(csrString); 101ea7c3f0cSRavi Teja EXPECT_TRUE(manager.getNumOfEntries() == 2); 102ea7c3f0cSRavi Teja manager.deleteAll(); 103ea7c3f0cSRavi Teja 104ea7c3f0cSRavi Teja EXPECT_TRUE(manager.getNumOfEntries() == 0); 105ea7c3f0cSRavi Teja } 106ea7c3f0cSRavi Teja TEST_F(TestCACertMgr, DeleteObjectEntry) 107ea7c3f0cSRavi Teja { 108ea7c3f0cSRavi Teja 109ea7c3f0cSRavi Teja auto bus = sdbusplus::bus::new_default(); 110ea7c3f0cSRavi Teja std::string objPath = "/xyz/openbmc_project/certs/ca"; 111ea7c3f0cSRavi Teja auto event = sdeventplus::Event::get_default(); 112ea7c3f0cSRavi Teja bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); 113cf06ccdcSNan Zhou MockCACertMgr manager(bus, objPath.c_str()); 114ea7c3f0cSRavi Teja 115ea7c3f0cSRavi Teja std::string csrString = "csr string"; 116ea7c3f0cSRavi Teja std::string entryPath = manager.createCSRObject(csrString); 117ea7c3f0cSRavi Teja std::size_t pos = entryPath.rfind("/"); 118ea7c3f0cSRavi Teja 119ea7c3f0cSRavi Teja std::string id; 120ea7c3f0cSRavi Teja if (pos != std::string::npos) 121ea7c3f0cSRavi Teja { 122ea7c3f0cSRavi Teja id = entryPath.substr(pos + 1); 123ea7c3f0cSRavi Teja } 124ea7c3f0cSRavi Teja 125ea7c3f0cSRavi Teja manager.erase(std::stoi(id)); 126ea7c3f0cSRavi Teja EXPECT_TRUE(manager.getNumOfEntries() == 0); 127ea7c3f0cSRavi Teja } 128e1289adfSNan Zhou } // namespace 129e1289adfSNan Zhou } // namespace ca::cert 130