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