Migrate to C++17 and refactor
Change-Id: I53407266939258990a1c3a9363c3ebe9ea113fd2
diff --git a/src/challenge/challenge-module.cpp b/src/challenge/challenge-module.cpp
index bf3b966..73594c0 100644
--- a/src/challenge/challenge-module.cpp
+++ b/src/challenge/challenge-module.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
*
* This file is part of ndncert, a certificate management system based on NDN.
*
@@ -19,6 +19,7 @@
*/
#include "challenge/challenge-module.hpp"
+
#include <ndn-cxx/util/random.hpp>
namespace ndncert {
@@ -35,15 +36,15 @@
bool
ChallengeModule::isChallengeSupported(const std::string& challengeType)
{
- ChallengeFactory& factory = getFactory();
+ auto& factory = getFactory();
auto i = factory.find(challengeType);
- return i == factory.end() ? false : true;
+ return i != factory.end();
}
std::unique_ptr<ChallengeModule>
ChallengeModule::createChallengeModule(const std::string& challengeType)
{
- ChallengeFactory& factory = getFactory();
+ auto& factory = getFactory();
auto i = factory.find(challengeType);
return i == factory.end() ? nullptr : i->second();
}
@@ -51,7 +52,7 @@
ChallengeModule::ChallengeFactory&
ChallengeModule::getFactory()
{
- static ChallengeModule::ChallengeFactory factory;
+ static ChallengeFactory factory;
return factory;
}
@@ -61,10 +62,9 @@
uint32_t securityCode = 0;
do {
securityCode = ndn::random::generateSecureWord32();
- }
- while (securityCode >= 4294000000);
+ } while (securityCode >= 4294000000);
securityCode /= 4294;
- std::string result = ndn::to_string(securityCode);
+ std::string result = std::to_string(securityCode);
while (result.length() < 6) {
result = "0" + result;
}
@@ -76,8 +76,8 @@
{
request.status = Status::FAILURE;
request.challengeType = "";
- request.challengeState = nullopt;
- return std::make_tuple(errorCode, std::move(errorInfo));
+ request.challengeState = std::nullopt;
+ return {errorCode, std::move(errorInfo)};
}
std::tuple<ErrorCode, std::string>
@@ -89,7 +89,7 @@
request.challengeType = CHALLENGE_TYPE;
request.challengeState = ca::ChallengeState(challengeStatus, time::system_clock::now(), remainingTries,
remainingTime, std::move(challengeSecret));
- return std::make_tuple(ErrorCode::NO_ERROR, "");
+ return {ErrorCode::NO_ERROR, ""};
}
std::tuple<ErrorCode, std::string>
@@ -97,8 +97,8 @@
{
request.status = Status::PENDING;
request.challengeType = CHALLENGE_TYPE;
- request.challengeState = nullopt;
- return std::make_tuple(ErrorCode::NO_ERROR, "");
+ request.challengeState = std::nullopt;
+ return {ErrorCode::NO_ERROR, ""};
}
} // namespace ndncert
diff --git a/src/challenge/challenge-module.hpp b/src/challenge/challenge-module.hpp
index 1e33bfc..a0c7b60 100644
--- a/src/challenge/challenge-module.hpp
+++ b/src/challenge/challenge-module.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2017-2021, Regents of the University of California.
+ * Copyright (c) 2017-2022, Regents of the University of California.
*
* This file is part of ndncert, a certificate management system based on NDN.
*
@@ -23,32 +23,18 @@
#include "detail/ca-request-state.hpp"
+#include <map>
+
namespace ndncert {
class ChallengeModule : boost::noncopyable
{
public:
- explicit
ChallengeModule(const std::string& challengeType, size_t maxAttemptTimes, time::seconds secretLifetime);
virtual
~ChallengeModule() = default;
- template <class ChallengeType>
- static void
- registerChallengeModule(const std::string& typeName)
- {
- ChallengeFactory& factory = getFactory();
- BOOST_ASSERT(factory.count(typeName) == 0);
- factory[typeName] = [] { return std::make_unique<ChallengeType>(); };
- }
-
- static bool
- isChallengeSupported(const std::string& challengeType);
-
- static std::unique_ptr<ChallengeModule>
- createChallengeModule(const std::string& challengeType);
-
// For CA
virtual std::tuple<ErrorCode, std::string>
handleChallengeRequest(const Block& params, ca::RequestState& request) = 0;
@@ -61,13 +47,27 @@
genChallengeRequestTLV(Status status, const std::string& challengeStatus,
const std::multimap<std::string, std::string>& params) = 0;
- // helpers
+public: // factory
+ template<class ChallengeType>
+ static void
+ registerChallengeModule(const std::string& type)
+ {
+ auto& factory = getFactory();
+ BOOST_ASSERT(factory.count(type) == 0);
+ factory[type] = [] { return std::make_unique<ChallengeType>(); };
+ }
+
+ static bool
+ isChallengeSupported(const std::string& challengeType);
+
+ static std::unique_ptr<ChallengeModule>
+ createChallengeModule(const std::string& challengeType);
+
+protected: // helpers used by concrete challenge modules
static std::string
generateSecretCode();
-protected:
- // used by challenge modules
- std::tuple<ErrorCode, std::string>
+ static std::tuple<ErrorCode, std::string>
returnWithError(ca::RequestState& request, ErrorCode errorCode, std::string errorInfo);
std::tuple<ErrorCode, std::string>
@@ -79,27 +79,29 @@
public:
const std::string CHALLENGE_TYPE;
+
+protected:
const size_t m_maxAttemptTimes;
const time::seconds m_secretLifetime;
private:
- typedef std::function<std::unique_ptr<ChallengeModule>()> ChallengeCreateFunc;
- typedef std::map<std::string, ChallengeCreateFunc> ChallengeFactory;
+ using CreateFunc = std::function<std::unique_ptr<ChallengeModule>()>;
+ using ChallengeFactory = std::map<std::string, CreateFunc>;
static ChallengeFactory&
getFactory();
};
-#define NDNCERT_REGISTER_CHALLENGE(C, T) \
- static class NdnCert##C##ChallengeRegistrationClass \
- { \
- public: \
- NdnCert##C##ChallengeRegistrationClass() \
- { \
- ::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
- } \
- } g_NdnCert##C##ChallengeRegistrationVariable
-
} // namespace ndncert
+#define NDNCERT_REGISTER_CHALLENGE(C, T) \
+static class NdnCert##C##ChallengeRegistrationClass \
+{ \
+public: \
+ NdnCert##C##ChallengeRegistrationClass() \
+ { \
+ ::ndncert::ChallengeModule::registerChallengeModule<C>(T); \
+ } \
+} g_NdnCert##C##ChallengeRegistrationVariable
+
#endif // NDNCERT_CHALLENGE_MODULE_HPP
diff --git a/src/challenge/challenge-possession.cpp b/src/challenge/challenge-possession.cpp
index d4c8185..2299a51 100644
--- a/src/challenge/challenge-possession.cpp
+++ b/src/challenge/challenge-possession.cpp
@@ -57,7 +57,7 @@
}
catch (const boost::property_tree::file_parser_error& error) {
NDN_THROW(std::runtime_error("Failed to parse configuration file " + m_configFile + ": " +
- error.message() + " on line " + ndn::to_string(error.line())));
+ error.message() + " on line " + std::to_string(error.line())));
}
if (config.begin() == config.end()) {
@@ -243,12 +243,12 @@
const auto& issuedCertTlv = issuedCert.wireEncode();
auto signature = keyChain.getTpm().sign({nonce}, keyName, ndn::DigestAlgorithm::SHA256);
- for (auto& item : params) {
- if (item.first == PARAMETER_KEY_CREDENTIAL_CERT) {
- item.second = std::string(reinterpret_cast<const char*>(issuedCertTlv.wire()), issuedCertTlv.size());
+ for (auto& [key, val] : params) {
+ if (key == PARAMETER_KEY_CREDENTIAL_CERT) {
+ val = std::string(reinterpret_cast<const char*>(issuedCertTlv.wire()), issuedCertTlv.size());
}
- else if (item.first == PARAMETER_KEY_PROOF) {
- item.second = std::string(signature->get<char>(), signature->size());
+ else if (key == PARAMETER_KEY_PROOF) {
+ val = std::string(signature->get<char>(), signature->size());
}
}
}