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