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