config.h.in: use const variables instead of macros
Most style guides try to avoid preprocessor macros, especially the use
case here: const objects. This change replaced them with const
variables. Their names are also changed according to the OpenBMC style
guide.
Reference:
https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: I0786c7c83f3a0d892c14f1cb813d0aa16d627b3e
diff --git a/bmc-vmi-ca/ca_certs_manager.cpp b/bmc-vmi-ca/ca_certs_manager.cpp
index 0f4e908..bbb8025 100644
--- a/bmc-vmi-ca/ca_certs_manager.cpp
+++ b/bmc-vmi-ca/ca_certs_manager.cpp
@@ -30,7 +30,8 @@
Argument::ARGUMENT_VALUE(csr.c_str()));
}
auto id = lastEntryId + 1;
- objPath = fs::path(OBJPATH) / "ca" / "entry" / std::to_string(id);
+ objPath =
+ fs::path(objectNamePrefix) / "ca" / "entry" / std::to_string(id);
std::string cert;
// Creating the dbus object here with the empty certificate string
// actual signing is being done by the hypervisor, once it signs then
diff --git a/certificate.cpp b/certificate.cpp
index de7a455..f367267 100644
--- a/certificate.cpp
+++ b/certificate.cpp
@@ -112,7 +112,7 @@
snprintf(hashBuf, CERT_HASH_LENGTH, "%08lx", hash);
const std::string certHash(hashBuf);
- for (int i = 0; i < AUTHORITY_CERTIFICATES_LIMIT; ++i)
+ for (size_t i = 0; i < maxNumAuthorityCertificates; ++i)
{
const std::string certDstFileX509Path =
certDstDirPath + "/" + certHash + "." + std::to_string(i);
@@ -617,7 +617,7 @@
log<level::INFO>("Private key not present in file",
entry("FILE=%s", filePath.c_str()));
fs::path privateKeyFile = fs::path(certInstallPath).parent_path();
- privateKeyFile = privateKeyFile / PRIV_KEY_FILE_NAME;
+ privateKeyFile = privateKeyFile / defaultPrivateKeyFileName;
if (!fs::exists(privateKeyFile))
{
log<level::ERR>("Private key file is not found",
diff --git a/certs_manager.cpp b/certs_manager.cpp
index ed6e375..9adca20 100644
--- a/certs_manager.cpp
+++ b/certs_manager.cpp
@@ -155,7 +155,7 @@
elog<NotAllowed>(Reason("Certificate already exist"));
}
else if (certType == phosphor::certs::AUTHORITY &&
- installedCerts.size() >= AUTHORITY_CERTIFICATES_LIMIT)
+ installedCerts.size() >= maxNumAuthorityCertificates)
{
elog<NotAllowed>(Reason("Certificates limit reached"));
}
@@ -418,7 +418,7 @@
}
// Write private key to file
- writePrivateKey(pKey, PRIV_KEY_FILE_NAME);
+ writePrivateKey(pKey, defaultPrivateKeyFileName);
// set sign key of x509 req
ret = X509_REQ_sign(x509Req.get(), pKey.get(), EVP_sha256());
@@ -429,7 +429,7 @@
}
log<level::INFO>("Writing CSR to file");
- fs::path csrFilePath = certParentInstallPath / CSR_FILE_NAME;
+ fs::path csrFilePath = certParentInstallPath / defaultCSRFileName;
writeCSR(csrFilePath.string(), x509Req);
}
@@ -778,14 +778,14 @@
void Manager::createRSAPrivateKeyFile()
{
fs::path rsaPrivateKeyFileName =
- certParentInstallPath / RSA_PRIV_KEY_FILE_NAME;
+ certParentInstallPath / defaultRSAPrivateKeyFileName;
try
{
if (!fs::exists(rsaPrivateKeyFileName))
{
writePrivateKey(generateRSAKeyPair(SUPPORTED_KEYBITLENGTH),
- RSA_PRIV_KEY_FILE_NAME);
+ defaultRSAPrivateKeyFileName);
}
}
catch (const InternalFailure& e)
@@ -807,7 +807,7 @@
Argument::ARGUMENT_VALUE(std::to_string(keyBitLength).c_str()));
}
fs::path rsaPrivateKeyFileName =
- certParentInstallPath / RSA_PRIV_KEY_FILE_NAME;
+ certParentInstallPath / defaultRSAPrivateKeyFileName;
FILE* privateKeyFile = std::fopen(rsaPrivateKeyFileName.c_str(), "r");
if (!privateKeyFile)
diff --git a/config.h.in b/config.h.in
index 9b937f6..6e6a3e7 100644
--- a/config.h.in
+++ b/config.h.in
@@ -1,19 +1,20 @@
#pragma once
+#include <cstddef>
-/* The DBus busname to own */
-#define BUSNAME "xyz.openbmc_project.Certs.Manager"
+/* The prefix of the DBus busname to own */
+inline constexpr char busNamePrefix[] = "xyz.openbmc_project.Certs.Manager";
-/* The certifiicate manager DBus root */
-#define OBJPATH "/xyz/openbmc_project/certs"
+/* The prefix of the certificate manager DBus object name */
+inline constexpr char objectNamePrefix[] = "/xyz/openbmc_project/certs";
-/* The CSR file */
-#define CSR_FILE_NAME "domain.csr"
+/* The default name of the CSR file */
+inline constexpr char defaultCSRFileName[] = "domain.csr";
-/* The private key file. */
-#define PRIV_KEY_FILE_NAME "privkey.pem"
+/* The default name of the private key file. */
+inline constexpr char defaultPrivateKeyFileName[] = "privkey.pem";
-/* The rsa private key file. */
-#define RSA_PRIV_KEY_FILE_NAME ".rsaprivkey.pem"
+/* The default name of the rsa private key file. */
+inline constexpr char defaultRSAPrivateKeyFileName[] = ".rsaprivkey.pem";
-/* Authority certificates limit. */
-#define AUTHORITY_CERTIFICATES_LIMIT @authority_limit@
+/* The maximum number of Authority certificates the service allows. */
+inline constexpr size_t maxNumAuthorityCertificates = @authority_limit@;
diff --git a/csr.cpp b/csr.cpp
index 1ea3991..677b48b 100644
--- a/csr.cpp
+++ b/csr.cpp
@@ -37,7 +37,7 @@
elog<InternalFailure>();
}
fs::path csrFilePath = certInstallPath;
- csrFilePath = csrFilePath.parent_path() / CSR_FILE_NAME;
+ csrFilePath = csrFilePath.parent_path() / defaultCSRFileName;
if (!fs::exists(csrFilePath))
{
log<level::ERR>("CSR file doesn't exists",
diff --git a/mainapp.cpp b/mainapp.cpp
index 0238e5e..98a560b 100644
--- a/mainapp.cpp
+++ b/mainapp.cpp
@@ -67,7 +67,7 @@
// unit is an optional parameter
auto unit = std::move((options)["unit"]);
auto bus = sdbusplus::bus::new_default();
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
// Add sdbusplus ObjectManager
sdbusplus::server::manager::manager objManager(bus, objPath.c_str());
@@ -84,7 +84,7 @@
// Adjusting Interface name as per std convention
capitalize(type);
capitalize(endpoint);
- auto busName = std::string(BUSNAME) + '.' + type + '.' + endpoint;
+ auto busName = std::string(busNamePrefix) + '.' + type + '.' + endpoint;
bus.request_name(busName.c_str());
event.loop();
return 0;
diff --git a/test/certs_manager_test.cpp b/test/certs_manager_test.cpp
index 1131df1..b807b1e 100644
--- a/test/certs_manager_test.cpp
+++ b/test/certs_manager_test.cpp
@@ -231,7 +231,7 @@
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -252,7 +252,7 @@
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -272,7 +272,7 @@
std::string type("authority");
std::string verifyDir(certDir);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -313,7 +313,7 @@
std::string type("authority");
std::string verifyDir(certDir);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -352,7 +352,7 @@
std::string type("authority");
std::string verifyDir(certDir);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -406,7 +406,7 @@
std::string type("authority");
std::string verifyDir(certDir);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -452,7 +452,7 @@
}
/** @brief Check if in authority mode user can't install more than
- * AUTHORITY_CERTIFICATES_LIMIT certificates.
+ * maxNumAuthorityCertificates certificates.
*/
TEST_F(TestCertificates, InvokeInstallAuthCertLimit)
{
@@ -461,7 +461,7 @@
std::string type("authority");
std::string verifyDir(certDir);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -475,7 +475,7 @@
std::vector<std::string> verifyPaths;
// Prepare maximum number of ceritificates
- for (std::size_t i = 0; i < AUTHORITY_CERTIFICATES_LIMIT; ++i)
+ for (std::size_t i = 0; i < maxNumAuthorityCertificates; ++i)
{
// Prepare new certificatate
createNewCertificate(true);
@@ -520,7 +520,7 @@
// Check that the original certificate has been not removed
EXPECT_FALSE(fs::is_empty(verifyDir));
- for (int i = 0; i < AUTHORITY_CERTIFICATES_LIMIT; ++i)
+ for (size_t i = 0; i < maxNumAuthorityCertificates; ++i)
{
EXPECT_TRUE(fs::exists(verifyPaths[i]));
}
@@ -536,7 +536,7 @@
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -558,7 +558,7 @@
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
std::string verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
std::string uploadFile = "nofile.pem";
EXPECT_THROW(
{
@@ -590,7 +590,7 @@
std::string type("server");
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -616,7 +616,7 @@
std::string type("authority");
std::string verifyDir(certDir);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -663,7 +663,7 @@
std::string type("authority");
std::string verifyDir(certDir);
UnitsToRestart verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -715,7 +715,7 @@
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
std::string verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
std::string emptyFile("emptycert.pem");
std::ofstream ofs;
ofs.open(emptyFile, std::ofstream::out);
@@ -760,7 +760,7 @@
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
std::string verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
EXPECT_THROW(
{
try
@@ -839,7 +839,7 @@
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
std::string verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
EXPECT_THROW(
{
try
@@ -871,7 +871,7 @@
std::string installPath(certDir + "/" + keyFile);
std::string verifyPath(installPath);
std::string verifyUnit(unit);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
EXPECT_THROW(
{
try
@@ -905,7 +905,7 @@
std::string type("client");
std::string installPath(certDir + "/" + certificateFile);
std::string verifyPath(installPath);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -956,7 +956,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
// Attach the bus to sd_event to service user requests
bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -1024,7 +1024,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1071,7 +1071,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1117,7 +1117,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1162,7 +1162,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1208,7 +1208,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1257,7 +1257,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1302,7 +1302,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1352,7 +1352,7 @@
std::string state("TS");
std::string surname("surname");
std::string unstructuredName("unstructuredName");
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
Manager manager(bus, event, objPath.c_str(), type, std::move(unit),
std::move(installPath));
@@ -1376,7 +1376,7 @@
std::string unit("");
std::string type("Server");
std::string installPath(certDir + "/" + certificateFile);
- auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint;
+ auto objPath = std::string(objectNamePrefix) + '/' + type + '/' + endpoint;
auto event = sdeventplus::Event::get_default();
EXPECT_FALSE(fs::exists(rsaPrivateKeyFilePath));