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