Migrate to C++17 and refactor
Change-Id: I53407266939258990a1c3a9363c3ebe9ea113fd2
diff --git a/src/detail/ca-configuration.cpp b/src/detail/ca-configuration.cpp
index 55b8a7c..667d98b 100644
--- a/src/detail/ca-configuration.cpp
+++ b/src/detail/ca-configuration.cpp
@@ -25,8 +25,7 @@
#include <boost/filesystem.hpp>
#include <boost/property_tree/json_parser.hpp>
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
void
CaConfig::load(const std::string& fileName)
@@ -38,33 +37,35 @@
catch (const std::exception& error) {
NDN_THROW(std::runtime_error("Failed to parse configuration file " + fileName + ", " + error.what()));
}
+
if (configJson.begin() == configJson.end()) {
NDN_THROW(std::runtime_error("No JSON configuration found in file: " + fileName));
}
caProfile = CaProfile::fromJson(configJson);
- if (caProfile.supportedChallenges.size() == 0) {
+ if (caProfile.supportedChallenges.empty()) {
NDN_THROW(std::runtime_error("At least one challenge should be specified."));
}
- // parse redirection section if appears
+
+ // parse redirection section if present
redirection.clear();
auto redirectionItems = configJson.get_child_optional(CONFIG_REDIRECTION);
if (redirectionItems) {
for (const auto& item : *redirectionItems) {
auto caPrefixStr = item.second.get(CONFIG_CA_PREFIX, "");
auto caCertStr = item.second.get(CONFIG_CERTIFICATE, "");
- if (caCertStr == "") {
+ if (caCertStr.empty()) {
NDN_THROW(std::runtime_error("Redirect-to item's certificate cannot be empty."));
}
std::istringstream ss(caCertStr);
auto caCert = ndn::io::load<Certificate>(ss);
- if (caPrefixStr != "" && Name(caPrefixStr) != caCert->getIdentity()) {
+ if (!caPrefixStr.empty() && Name(caPrefixStr) != caCert->getIdentity()) {
NDN_THROW(std::runtime_error("Redirect-to item's prefix and certificate does not match."));
}
auto policyType = item.second.get(CONFIG_REDIRECTION_POLICY_TYPE, "");
auto policyParam = item.second.get(CONFIG_REDIRECTION_POLICY_PARAM, "");
if (policyType.empty()) {
- NDN_THROW(std::runtime_error("Redirect-to policy type expected but not provided."));
+ NDN_THROW(std::runtime_error("Redirect-to policy type expected but not provided."));
}
auto policy = RedirectionPolicy::createPolicyFunc(policyType, policyParam);
if (policy == nullptr) {
@@ -73,12 +74,13 @@
redirection.emplace_back(caCert, std::move(policy));
}
}
- // parse name assignment if appears
+
+ // parse name assignment if present
nameAssignmentFuncs.clear();
auto nameAssignmentItems = configJson.get_child_optional(CONFIG_NAME_ASSIGNMENT);
if (nameAssignmentItems) {
- for (const auto& item : *nameAssignmentItems) {
- auto func = NameAssignmentFunc::createNameAssignmentFunc(item.first, item.second.data());
+ for (const auto& [key, val] : *nameAssignmentItems) {
+ auto func = NameAssignmentFunc::createNameAssignmentFunc(key, val.data());
if (func == nullptr) {
NDN_THROW(std::runtime_error("Error on creating name assignment function"));
}
@@ -87,5 +89,4 @@
}
}
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-configuration.hpp b/src/detail/ca-configuration.hpp
index 0a02777..5f39887 100644
--- a/src/detail/ca-configuration.hpp
+++ b/src/detail/ca-configuration.hpp
@@ -21,12 +21,11 @@
#ifndef NDNCERT_DETAIL_CA_CONFIGURATION_HPP
#define NDNCERT_DETAIL_CA_CONFIGURATION_HPP
+#include "ca-profile.hpp"
#include "name-assignment/assignment-func.hpp"
#include "redirection/redirection-policy.hpp"
-#include "ca-profile.hpp"
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
/**
* @brief CA's configuration on NDNCERT.
@@ -74,7 +73,6 @@
std::vector<std::unique_ptr<NameAssignmentFunc>> nameAssignmentFuncs;
};
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
#endif // NDNCERT_DETAIL_CA_CONFIGURATION_HPP
diff --git a/src/detail/ca-memory.cpp b/src/detail/ca-memory.cpp
index b3c5efd..cab365b 100644
--- a/src/detail/ca-memory.cpp
+++ b/src/detail/ca-memory.cpp
@@ -20,13 +20,12 @@
#include "detail/ca-memory.hpp"
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
const std::string CaMemory::STORAGE_TYPE = "ca-storage-memory";
NDNCERT_REGISTER_CA_STORAGE(CaMemory);
-CaMemory::CaMemory(const Name& caName, const std::string& path)
+CaMemory::CaMemory(const Name&, const std::string&)
: CaStorage()
{
}
@@ -34,21 +33,18 @@
RequestState
CaMemory::getRequest(const RequestId& requestId)
{
- auto search = m_requests.find(requestId);
- if (search == m_requests.end()) {
- NDN_THROW(std::runtime_error("Request " + ndn::toHex(requestId) + " does not exists"));
+ auto it = m_requests.find(requestId);
+ if (it == m_requests.end()) {
+ NDN_THROW(std::runtime_error("Request " + ndn::toHex(requestId) + " does not exist"));
}
- return search->second;
+ return it->second;
}
void
CaMemory::addRequest(const RequestState& request)
{
- auto search = m_requests.find(request.requestId);
- if (search == m_requests.end()) {
- m_requests.insert(std::make_pair(request.requestId, request));
- }
- else {
+ auto result = m_requests.insert({request.requestId, request});
+ if (!result.second) {
NDN_THROW(std::runtime_error("Request " + ndn::toHex(request.requestId) + " already exists"));
}
}
@@ -56,23 +52,13 @@
void
CaMemory::updateRequest(const RequestState& request)
{
- auto search = m_requests.find(request.requestId);
- if (search == m_requests.end()) {
- m_requests.insert(std::make_pair(request.requestId, request));
- }
- else {
- search->second = request;
- }
+ m_requests.insert_or_assign(request.requestId, request);
}
void
CaMemory::deleteRequest(const RequestId& requestId)
{
- auto search = m_requests.find(requestId);
- auto keyName = search->second.cert.getKeyName();
- if (search != m_requests.end()) {
- m_requests.erase(search);
- }
+ m_requests.erase(requestId);
}
std::list<RequestState>
@@ -97,5 +83,4 @@
return result;
}
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-memory.hpp b/src/detail/ca-memory.hpp
index 6d8b115..2c78997 100644
--- a/src/detail/ca-memory.hpp
+++ b/src/detail/ca-memory.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,25 +23,20 @@
#include "detail/ca-storage.hpp"
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
class CaMemory : public CaStorage
{
public:
- CaMemory(const Name& caName = Name(), const std::string& path = "");
- const static std::string STORAGE_TYPE;
+ static const std::string STORAGE_TYPE;
+
+ explicit
+ CaMemory(const Name& caName = "", const std::string& path = "");
public:
- /**
- * @throw if request cannot be fetched from underlying data storage
- */
RequestState
getRequest(const RequestId& requestId) override;
- /**
- * @throw if there is an existing request with the same request ID
- */
void
addRequest(const RequestState& request) override;
@@ -61,7 +56,6 @@
std::map<RequestId, RequestState> m_requests;
};
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
#endif // NDNCERT_DETAIL_CA_MEMORY_HPP
diff --git a/src/detail/ca-profile.cpp b/src/detail/ca-profile.cpp
index 624ec7a..41c5d56 100644
--- a/src/detail/ca-profile.cpp
+++ b/src/detail/ca-profile.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.
*
@@ -41,7 +41,7 @@
// CA max validity period
profile.maxValidityPeriod = time::seconds(json.get(CONFIG_MAX_VALIDITY_PERIOD, 86400));
// CA max suffix length
- profile.maxSuffixLength = nullopt;
+ profile.maxSuffixLength = std::nullopt;
auto maxSuffixLength = json.get_optional<size_t>(CONFIG_MAX_SUFFIX_LENGTH);
if (maxSuffixLength) {
profile.maxSuffixLength = *maxSuffixLength;
@@ -78,7 +78,7 @@
// anchor certificate
profile.cert = nullptr;
auto certificateStr = json.get(CONFIG_CERTIFICATE, "");
- if (certificateStr != "") {
+ if (!certificateStr.empty()) {
std::istringstream ss(certificateStr);
profile.cert = ndn::io::load<Certificate>(ss);
}
@@ -100,7 +100,7 @@
for (const auto& key : probeParameterKeys) {
JsonSection keyJson;
keyJson.put(CONFIG_PROBE_PARAMETER, key);
- probeParametersJson.push_back(std::make_pair("", keyJson));
+ probeParametersJson.push_back({"", keyJson});
}
caItem.add_child("", probeParametersJson);
}
@@ -109,7 +109,7 @@
for (const auto& challenge : supportedChallenges) {
JsonSection challengeJson;
challengeJson.put(CONFIG_CHALLENGE, challenge);
- challengeListJson.push_back(std::make_pair("", challengeJson));
+ challengeListJson.push_back({"", challengeJson});
}
caItem.add_child("", challengeListJson);
}
diff --git a/src/detail/ca-profile.hpp b/src/detail/ca-profile.hpp
index 74af5c0..620f49b 100644
--- a/src/detail/ca-profile.hpp
+++ b/src/detail/ca-profile.hpp
@@ -83,7 +83,7 @@
* E.g., When its value is 2, at most 2 name components can be assigned after m_caPrefix.
* Default: none.
*/
- optional<size_t> maxSuffixLength = nullopt;
+ std::optional<size_t> maxSuffixLength;
/**
* @brief A list of supported challenges. Only CA side will have m_supportedChallenges.
*/
diff --git a/src/detail/ca-request-state.hpp b/src/detail/ca-request-state.hpp
index 2c4a1d6..5fbb8f7 100644
--- a/src/detail/ca-request-state.hpp
+++ b/src/detail/ca-request-state.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.
*
@@ -27,7 +27,7 @@
namespace ndncert {
-typedef std::array<uint8_t, 8> RequestId;
+using RequestId = std::array<uint8_t, 8>;
enum class Status : uint16_t {
BEFORE_CHALLENGE = 0,
@@ -127,7 +127,7 @@
/**
* @brief The challenge state.
*/
- optional<ChallengeState> challengeState;
+ std::optional<ChallengeState> challengeState;
};
std::ostream&
diff --git a/src/detail/ca-sqlite.cpp b/src/detail/ca-sqlite.cpp
index 0632edc..3794ce0 100644
--- a/src/detail/ca-sqlite.cpp
+++ b/src/detail/ca-sqlite.cpp
@@ -28,8 +28,7 @@
#include <boost/filesystem.hpp>
#include <boost/property_tree/json_parser.hpp>
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
using ndn::util::Sqlite3Statement;
@@ -148,7 +147,7 @@
std::memcpy(state.encryptionKey.data(), statement.getBlob(11), statement.getSize(11));
state.encryptionIv = std::vector<uint8_t>(statement.getBlob(12), statement.getBlob(12) + statement.getSize(12));
state.decryptionIv = std::vector<uint8_t>(statement.getBlob(13), statement.getBlob(13) + statement.getSize(13));
- if (state.challengeType != "") {
+ if (!state.challengeType.empty()) {
ChallengeState challengeState(statement.getString(3), time::fromIsoString(statement.getString(7)),
statement.getInt(8), time::seconds(statement.getInt(9)),
convertString2Json(statement.getString(6)));
@@ -279,7 +278,7 @@
std::memcpy(state.encryptionKey.data(), statement.getBlob(12), statement.getSize(12));
state.encryptionIv = std::vector<uint8_t>(statement.getBlob(13), statement.getBlob(13) + statement.getSize(13));
state.decryptionIv = std::vector<uint8_t>(statement.getBlob(14), statement.getBlob(14) + statement.getSize(14));
- if (state.challengeType != "") {
+ if (!state.challengeType.empty()) {
ChallengeState challengeState(statement.getString(4), time::fromIsoString(statement.getString(8)),
statement.getInt(9), time::seconds(statement.getInt(10)),
convertString2Json(statement.getString(7)));
@@ -299,5 +298,4 @@
statement.step();
}
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-sqlite.hpp b/src/detail/ca-sqlite.hpp
index 969cacf..5ad10d9 100644
--- a/src/detail/ca-sqlite.hpp
+++ b/src/detail/ca-sqlite.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.
*
@@ -25,13 +25,12 @@
struct sqlite3;
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
class CaSqlite : public CaStorage
{
public:
- const static std::string STORAGE_TYPE;
+ static const std::string STORAGE_TYPE;
explicit
CaSqlite(const Name& caName, const std::string& path = "");
@@ -39,15 +38,9 @@
~CaSqlite() override;
public:
- /**
- * @throw if request cannot be fetched from underlying data storage
- */
RequestState
getRequest(const RequestId& requestId) override;
- /**
- * @throw if there is an existing request with the same request ID
- */
void
addRequest(const RequestState& request) override;
@@ -67,7 +60,6 @@
sqlite3* m_database;
};
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
#endif // NDNCERT_DETAIL_CA_SQLITE_HPP
diff --git a/src/detail/ca-storage.cpp b/src/detail/ca-storage.cpp
index 24fe38f..c3c08a7 100644
--- a/src/detail/ca-storage.cpp
+++ b/src/detail/ca-storage.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.
*
@@ -20,13 +20,12 @@
#include "detail/ca-storage.hpp"
-namespace ndncert {
-namespace ca {
+namespace ndncert::ca {
std::unique_ptr<CaStorage>
CaStorage::createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path)
{
- CaStorageFactory& factory = getFactory();
+ auto& factory = getFactory();
auto i = factory.find(caStorageType);
return i == factory.end() ? nullptr : i->second(caName, path);
}
@@ -34,9 +33,8 @@
CaStorage::CaStorageFactory&
CaStorage::getFactory()
{
- static CaStorage::CaStorageFactory factory;
+ static CaStorageFactory factory;
return factory;
}
-} // namespace ca
-} // namespace ndncert
+} // namespace ndncert::ca
diff --git a/src/detail/ca-storage.hpp b/src/detail/ca-storage.hpp
index 0fa7238..6b8f03c 100644
--- a/src/detail/ca-storage.hpp
+++ b/src/detail/ca-storage.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,20 +23,24 @@
#include "detail/ca-request-state.hpp"
-namespace ndncert {
-namespace ca {
+#include <map>
+
+namespace ndncert::ca {
class CaStorage : boost::noncopyable
{
-public: // request related
+public:
+ virtual
+ ~CaStorage() = default;
+
/**
- * @throw if request cannot be fetched from underlying data storage
+ * @throw std::runtime_error The request cannot be fetched from underlying data storage
*/
virtual RequestState
getRequest(const RequestId& requestId) = 0;
/**
- * @throw if there is an existing request with the same request ID
+ * @throw std::runtime_error There is an existing request with the same request ID
*/
virtual void
addRequest(const RequestState& request) = 0;
@@ -56,11 +60,11 @@
public: // factory
template<class CaStorageType>
static void
- registerCaStorage(const std::string& caStorageType = CaStorageType::STORAGE_TYPE)
+ registerCaStorage(const std::string& type = CaStorageType::STORAGE_TYPE)
{
- CaStorageFactory& factory = getFactory();
- BOOST_ASSERT(factory.count(caStorageType) == 0);
- factory[caStorageType] = [] (const Name& caName, const std::string& path) {
+ auto& factory = getFactory();
+ BOOST_ASSERT(factory.count(type) == 0);
+ factory[type] = [] (const Name& caName, const std::string& path) {
return std::make_unique<CaStorageType>(caName, path);
};
}
@@ -68,28 +72,24 @@
static std::unique_ptr<CaStorage>
createCaStorage(const std::string& caStorageType, const Name& caName, const std::string& path);
- virtual
- ~CaStorage() = default;
-
private:
- using CaStorageCreateFunc = std::function<std::unique_ptr<CaStorage> (const Name&, const std::string&)>;
- using CaStorageFactory = std::map<std::string, CaStorageCreateFunc>;
+ using CreateFunc = std::function<std::unique_ptr<CaStorage>(const Name&, const std::string&)>;
+ using CaStorageFactory = std::map<std::string, CreateFunc>;
static CaStorageFactory&
getFactory();
};
-#define NDNCERT_REGISTER_CA_STORAGE(C) \
-static class NdnCert ## C ## CaStorageRegistrationClass \
-{ \
-public: \
- NdnCert ## C ## CaStorageRegistrationClass() \
- { \
- ::ndncert::ca::CaStorage::registerCaStorage<C>(); \
- } \
-} g_NdnCert ## C ## CaStorageRegistrationVariable
+} // namespace ndncert::ca
-} // namespace ca
-} // namespace ndncert
+#define NDNCERT_REGISTER_CA_STORAGE(C) \
+static class NdnCert##C##CaStorageRegistrationClass \
+{ \
+public: \
+ NdnCert##C##CaStorageRegistrationClass() \
+ { \
+ ::ndncert::ca::CaStorage::registerCaStorage<C>(); \
+ } \
+} g_NdnCert##C##CaStorageRegistrationVariable
#endif // NDNCERT_DETAIL_CA_STORAGE_HPP
diff --git a/src/detail/challenge-encoder.cpp b/src/detail/challenge-encoder.cpp
index 1a7a83d..9f36042 100644
--- a/src/detail/challenge-encoder.cpp
+++ b/src/detail/challenge-encoder.cpp
@@ -20,10 +20,10 @@
#include "detail/challenge-encoder.hpp"
-namespace ndncert {
+namespace ndncert::challengetlv {
Block
-challengetlv::encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
+encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
{
Block response(tlv::EncryptedPayload);
response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::Status, static_cast<uint64_t>(request.status)));
@@ -52,7 +52,7 @@
}
void
-challengetlv::decodeDataContent(const Block& contentBlock, requester::Request& state)
+decodeDataContent(const Block& contentBlock, requester::Request& state)
{
auto result = decodeBlockWithAesGcm128(contentBlock, state.m_aesKey.data(),
state.m_requestId.data(), state.m_requestId.size(),
@@ -122,4 +122,4 @@
}
}
-} // namespace ndncert
+} // namespace ndncert::challengetlv
diff --git a/src/detail/challenge-encoder.hpp b/src/detail/challenge-encoder.hpp
index 2013061..5c502d2 100644
--- a/src/detail/challenge-encoder.hpp
+++ b/src/detail/challenge-encoder.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.
*
@@ -24,8 +24,7 @@
#include "detail/ca-request-state.hpp"
#include "requester-request.hpp"
-namespace ndncert {
-namespace challengetlv {
+namespace ndncert::challengetlv {
Block
encodeDataContent(ca::RequestState& request, const Name& issuedCertName = Name());
@@ -33,7 +32,6 @@
void
decodeDataContent(const Block& contentBlock, requester::Request& state);
-} // namespace challengetlv
-} // namespace ndncert
+} // namespace ndncert::challengetlv
#endif // NDNCERT_DETAIL_CHALLENGE_ENCODER_HPP
diff --git a/src/detail/error-encoder.cpp b/src/detail/error-encoder.cpp
index 990ce84..3dcb6ca 100644
--- a/src/detail/error-encoder.cpp
+++ b/src/detail/error-encoder.cpp
@@ -22,10 +22,10 @@
NDN_LOG_INIT(ndncert.encode.error);
-namespace ndncert {
+namespace ndncert::errortlv {
Block
-errortlv::encodeDataContent(ErrorCode errorCode, const std::string& description)
+encodeDataContent(ErrorCode errorCode, const std::string& description)
{
Block response(ndn::tlv::Content);
response.push_back(ndn::makeNonNegativeIntegerBlock(tlv::ErrorCode, static_cast<size_t>(errorCode)));
@@ -35,7 +35,7 @@
}
std::tuple<ErrorCode, std::string>
-errortlv::decodefromDataContent(const Block& block)
+decodefromDataContent(const Block& block)
{
try {
block.parse();
@@ -47,34 +47,35 @@
for (const auto& item : block.elements()) {
if (item.type() == tlv::ErrorCode) {
error = static_cast<ErrorCode>(readNonNegativeInteger(block.get(tlv::ErrorCode)));
- codeCount ++;
+ codeCount++;
}
else if (item.type() == tlv::ErrorInfo) {
errorInfo = readString(block.get(tlv::ErrorInfo));
- infoCount ++;
+ infoCount++;
}
else if (ndn::tlv::isCriticalType(item.type())) {
- otherCriticalCount ++;
+ otherCriticalCount++;
}
else {
//ignore
}
}
if (codeCount == 0 && infoCount == 0) {
- return std::make_tuple(ErrorCode::NO_ERROR, "");
+ return {ErrorCode::NO_ERROR, ""};
}
if (codeCount != 1 || infoCount != 1) {
NDN_THROW(std::runtime_error("Error TLV contains " + std::to_string(codeCount) + " error code(s) and " +
- std::to_string(infoCount) + "error info(s), instead of expected 1 times each."));
+ std::to_string(infoCount) + " error info(s), instead of expected 1 time each."));
}
if (otherCriticalCount > 0) {
NDN_THROW(std::runtime_error("Unknown Critical TLV type in error packet"));
}
- return std::make_tuple(error, errorInfo);
- } catch (const std::exception& e) {
- NDN_LOG_ERROR("[errortlv::DecodeFromDataContent] Exception in error message decoding: " << e.what());
- return std::make_tuple(ErrorCode::NO_ERROR, "");
+ return {error, errorInfo};
+ }
+ catch (const std::exception& e) {
+ NDN_LOG_ERROR("Exception in error message decoding: " << e.what());
+ return {ErrorCode::NO_ERROR, ""};
}
}
-} // namespace ndncert
+} // namespace ndncert::errortlv
diff --git a/src/detail/error-encoder.hpp b/src/detail/error-encoder.hpp
index cd136cf..57fdf92 100644
--- a/src/detail/error-encoder.hpp
+++ b/src/detail/error-encoder.hpp
@@ -23,8 +23,7 @@
#include "detail/ca-profile.hpp"
-namespace ndncert {
-namespace errortlv {
+namespace ndncert::errortlv {
/**
* Encode error information into a Data content TLV
@@ -38,7 +37,6 @@
std::tuple<ErrorCode, std::string>
decodefromDataContent(const Block& block);
-} // namespace errortlv
-} // namespace ndncert
+} // namespace ndncert::errortlv
#endif // NDNCERT_DETAIL_ERROR_ENCODER_HPP
diff --git a/src/detail/info-encoder.cpp b/src/detail/info-encoder.cpp
index 9331f1f..2a9df67 100644
--- a/src/detail/info-encoder.cpp
+++ b/src/detail/info-encoder.cpp
@@ -22,15 +22,15 @@
NDN_LOG_INIT(ndncert.encode.info);
-namespace ndncert {
+namespace ndncert::infotlv {
Block
-infotlv::encodeDataContent(const CaProfile& caConfig, const Certificate& certificate)
+encodeDataContent(const CaProfile& caConfig, const Certificate& certificate)
{
Block content(ndn::tlv::Content);
content.push_back(makeNestedBlock(tlv::CaPrefix, caConfig.caPrefix));
- std::string caInfo = "";
- if (caConfig.caInfo == "") {
+ std::string caInfo;
+ if (caConfig.caInfo.empty()) {
caInfo = "Issued by " + certificate.getSignatureInfo().getKeyLocator().getName().toUri();
}
else {
@@ -48,7 +48,8 @@
}
CaProfile
-infotlv::decodeDataContent(const Block& block) {
+decodeDataContent(const Block& block)
+{
CaProfile result;
block.parse();
for (auto const &item : block.elements()) {
@@ -74,13 +75,10 @@
if (ndn::tlv::isCriticalType(item.type())) {
NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
}
- else {
- //ignore
- }
break;
}
}
return result;
}
-} // namespace ndncert
+} // namespace ndncert::infotlv
diff --git a/src/detail/info-encoder.hpp b/src/detail/info-encoder.hpp
index 4eaa967..e4a9cab 100644
--- a/src/detail/info-encoder.hpp
+++ b/src/detail/info-encoder.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,8 +23,7 @@
#include "detail/ca-profile.hpp"
-namespace ndncert {
-namespace infotlv {
+namespace ndncert::infotlv {
/**
* Encode CA configuration and its certificate into a TLV block as INFO Data packet content.
@@ -38,7 +37,6 @@
CaProfile
decodeDataContent(const Block& block);
-} // namespace infotlv
-} // namespace ndncert
+} // namespace ndncert::infotlv
#endif // NDNCERT_DETAIL_INFO_ENCODER_HPP
diff --git a/src/detail/ndncert-common.hpp b/src/detail/ndncert-common.hpp
index d9863a5..5549550 100644
--- a/src/detail/ndncert-common.hpp
+++ b/src/detail/ndncert-common.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.
*
@@ -37,8 +37,11 @@
#include <cstddef>
#include <cstdint>
+#include <memory>
+#include <optional>
#include <string>
#include <tuple>
+#include <vector>
#include <ndn-cxx/data.hpp>
#include <ndn-cxx/encoding/block.hpp>
@@ -48,7 +51,6 @@
#include <ndn-cxx/security/certificate.hpp>
#include <ndn-cxx/util/exception.hpp>
#include <ndn-cxx/util/logger.hpp>
-#include <ndn-cxx/util/optional.hpp>
#include <ndn-cxx/util/time.hpp>
#include <boost/algorithm/string.hpp>
@@ -65,9 +67,6 @@
using ndn::SignatureInfo;
using ndn::security::Certificate;
-using ndn::optional;
-using ndn::nullopt;
-
namespace time = ndn::time;
using namespace ndn::time_literals;
using namespace std::string_literals;
diff --git a/src/detail/probe-encoder.cpp b/src/detail/probe-encoder.cpp
index 13b040e..cdace7e 100644
--- a/src/detail/probe-encoder.cpp
+++ b/src/detail/probe-encoder.cpp
@@ -20,10 +20,10 @@
#include "detail/probe-encoder.hpp"
-namespace ndncert {
+namespace ndncert::probetlv {
Block
-probetlv::encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters)
+encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters)
{
Block content(ndn::tlv::ApplicationParameters);
for (const auto& items : parameters) {
@@ -35,7 +35,8 @@
}
std::multimap<std::string, std::string>
-probetlv::decodeApplicationParameters(const Block& block) {
+decodeApplicationParameters(const Block& block)
+{
std::multimap<std::string, std::string> result;
block.parse();
const auto& elements = block.elements();
@@ -56,8 +57,8 @@
}
Block
-probetlv::encodeDataContent(const std::vector<Name>& identifiers, optional<size_t> maxSuffixLength,
- std::vector<ndn::Name> redirectionItems)
+encodeDataContent(const std::vector<Name>& identifiers, std::optional<size_t> maxSuffixLength,
+ const std::vector<Name>& redirectionItems)
{
Block content(ndn::tlv::Content);
for (const auto& name : identifiers) {
@@ -78,9 +79,9 @@
}
void
-probetlv::decodeDataContent(const Block& block,
- std::vector<std::pair<Name, int>>& availableNames,
- std::vector<Name>& availableRedirection) {
+decodeDataContent(const Block& block, std::vector<std::pair<Name, int>>& availableNames,
+ std::vector<Name>& availableRedirection)
+{
block.parse();
for (const auto& item : block.elements()) {
if (item.type() == tlv::ProbeResponse) {
@@ -121,4 +122,4 @@
}
}
-} // namespace ndncert
+} // namespace ndncert::probetlv
diff --git a/src/detail/probe-encoder.hpp b/src/detail/probe-encoder.hpp
index 223c61b..bd15463 100644
--- a/src/detail/probe-encoder.hpp
+++ b/src/detail/probe-encoder.hpp
@@ -23,27 +23,26 @@
#include "detail/ndncert-common.hpp"
-namespace ndncert {
-namespace probetlv {
+namespace ndncert::probetlv {
// For Client use
Block
encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters);
void
-decodeDataContent(const Block& block, std::vector<std::pair<Name, int>>& availableNames,
+decodeDataContent(const Block& block,
+ std::vector<std::pair<Name, int>>& availableNames,
std::vector<Name>& availableRedirection);
// For CA use
Block
encodeDataContent(const std::vector<Name>& identifiers,
- optional<size_t> maxSuffixLength = nullopt,
- std::vector<ndn::Name> redirectionItems = std::vector<ndn::Name>());
+ std::optional<size_t> maxSuffixLength = std::nullopt,
+ const std::vector<Name>& redirectionItems = {});
std::multimap<std::string, std::string>
decodeApplicationParameters(const Block& block);
-} // namespace probetlv
-} // namespace ndncert
+} // namespace ndncert::probetlv
#endif // NDNCERT_DETAIL_PROBE_ENCODER_HPP
diff --git a/src/detail/profile-storage.cpp b/src/detail/profile-storage.cpp
index 8ae2bb6..ebd33ac 100644
--- a/src/detail/profile-storage.cpp
+++ b/src/detail/profile-storage.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.
*
@@ -20,11 +20,9 @@
#include "detail/profile-storage.hpp"
-#include <boost/filesystem.hpp>
#include <boost/property_tree/json_parser.hpp>
-namespace ndncert {
-namespace requester {
+namespace ndncert::requester {
void
ProfileStorage::load(const std::string& fileName)
@@ -47,13 +45,12 @@
{
m_caProfiles.clear();
auto caList = json.get_child("ca-list");
- for (auto item : caList) {
- CaProfile caItem;
- caItem = CaProfile::fromJson(item.second);
- if (caItem.cert == nullptr) {
+ for (const auto& item : caList) {
+ auto profile = CaProfile::fromJson(item.second);
+ if (profile.cert == nullptr) {
NDN_THROW(std::runtime_error("No CA certificate is loaded from JSON configuration."));
}
- m_caProfiles.push_back(std::move(caItem));
+ m_caProfiles.push_back(std::move(profile));
}
}
@@ -62,14 +59,12 @@
{
JsonSection configJson;
for (const auto& caItem : m_caProfiles) {
- configJson.push_back(std::make_pair("", caItem.toJson()));
+ configJson.push_back({"", caItem.toJson()});
}
std::stringstream ss;
boost::property_tree::write_json(ss, configJson);
- std::ofstream configFile;
- configFile.open(fileName);
+ std::ofstream configFile(fileName);
configFile << ss.str();
- configFile.close();
}
void
@@ -96,5 +91,4 @@
return m_caProfiles;
}
-} // namespace requester
-} // namespace ndncert
+} // namespace ndncert::requester
diff --git a/src/detail/profile-storage.hpp b/src/detail/profile-storage.hpp
index 3df6407..41791ed 100644
--- a/src/detail/profile-storage.hpp
+++ b/src/detail/profile-storage.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,8 +23,7 @@
#include "detail/ca-profile.hpp"
-namespace ndncert {
-namespace requester {
+namespace ndncert::requester {
/**
* @brief CA profiles kept by a requester.
@@ -53,8 +52,7 @@
/**
* @brief Add a new CA profile
- *
- * Be cautious. This will add a new trust anchor for requesters.
+ * @warning This will add a new trust anchor for requesters.
*/
void
addCaProfile(const CaProfile& profile);
@@ -66,7 +64,6 @@
std::list<CaProfile> m_caProfiles;
};
-} // namespace requester
-} // namespace ndncert
+} // namespace ndncert::requester
#endif // NDNCERT_DETAIL_PROFILE_STORAGE_HPP
diff --git a/src/detail/request-encoder.hpp b/src/detail/request-encoder.hpp
index e94571e..a5b80f6 100644
--- a/src/detail/request-encoder.hpp
+++ b/src/detail/request-encoder.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,8 +23,7 @@
#include "detail/ca-request-state.hpp"
-namespace ndncert {
-namespace requesttlv {
+namespace ndncert::requesttlv {
Block
encodeApplicationParameters(RequestType requestType, const std::vector<uint8_t>& ecdhPub,
@@ -42,7 +41,6 @@
decodeDataContent(const Block& content, std::vector<uint8_t>& ecdhKey,
std::array<uint8_t, 32>& salt, RequestId& requestId);
-} // namespace requesttlv
-} // namespace ndncert
+} // namespace ndncert::requesttlv
#endif // NDNCERT_DETAIL_REQUEST_ENCODER_HPP