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