Code Review fix 1
Change-Id: I92fa40b6dd0a05913461006acc87542884c1f3a5
diff --git a/src/detail/ca-request-state.cpp b/src/detail/ca-request-state.cpp
index 0de0820..923f9be 100644
--- a/src/detail/ca-request-state.cpp
+++ b/src/detail/ca-request-state.cpp
@@ -24,7 +24,8 @@
namespace ndn {
namespace ndncert {
-std::string statusToString(Status status) {
+std::string statusToString(Status status)
+{
switch (status)
{
case Status::BEFORE_CHALLENGE:
@@ -46,6 +47,15 @@
}
}
+Status
+statusFromBlock(const Block& block)
+{
+ auto status_int = readNonNegativeInteger(block);
+ if (status_int > 6)
+ NDN_THROW(std::runtime_error("Unrecognized Status"));
+ return static_cast<Status>(status_int);
+}
+
namespace ca {
ChallengeState::ChallengeState(const std::string& challengeStatus,
diff --git a/src/detail/ca-request-state.hpp b/src/detail/ca-request-state.hpp
index 7fa9f23..27d19fa 100644
--- a/src/detail/ca-request-state.hpp
+++ b/src/detail/ca-request-state.hpp
@@ -45,6 +45,12 @@
std::string
statusToString(Status status);
+/**
+ * @brief Convert request status to string.
+ */
+Status
+statusFromBlock(const Block& block);
+
namespace ca {
/**
diff --git a/src/detail/challenge-encoder.cpp b/src/detail/challenge-encoder.cpp
index 22dc6c7..c9fa34b 100644
--- a/src/detail/challenge-encoder.cpp
+++ b/src/detail/challenge-encoder.cpp
@@ -24,10 +24,10 @@
namespace ndncert {
Block
-ChallengeEncoder::encodeDataContent(ca::RequestState& request, optional<Name> issuedCertName)
+ChallengeEncoder::encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
{
Block response = makeEmptyBlock(tlv::EncryptedPayload);
- response.push_back(makeNonNegativeIntegerBlock(tlv::Status, static_cast<size_t>(request.m_status)));
+ response.push_back(makeNonNegativeIntegerBlock(tlv::Status, static_cast<uint64_t>(request.m_status)));
if (request.m_challengeState) {
response.push_back(makeStringBlock(tlv::ChallengeStatus, request.m_challengeState->m_challengeStatus));
response.push_back(
@@ -35,8 +35,8 @@
response.push_back(
makeNonNegativeIntegerBlock(tlv::RemainingTime, request.m_challengeState->m_remainingTime.count()));
}
- if (issuedCertName.has_value()) {
- response.push_back(makeNestedBlock(tlv::IssuedCertName, *issuedCertName));
+ if (!issuedCertName.empty()) {
+ response.push_back(makeNestedBlock(tlv::IssuedCertName, issuedCertName));
}
response.encode();
return encodeBlockWithAesGcm128(ndn::tlv::Content, request.m_encryptionKey.data(),
@@ -45,13 +45,13 @@
}
void
-ChallengeEncoder::decodeDataContent(const Block& contentBlock, requester::RequestContext& state)
+ChallengeEncoder::decodeDataContent(const Block& contentBlock, requester::RequestState& state)
{
auto result = decodeBlockWithAesGcm128(contentBlock, state.m_aesKey.data(),
state.m_requestId.data(), state.m_requestId.size());
auto data = makeBinaryBlock(tlv::EncryptedPayload, result.data(), result.size());
data.parse();
- state.m_status = static_cast<Status>(readNonNegativeInteger(data.get(tlv::Status)));
+ state.m_status = statusFromBlock(data.get(tlv::Status));
if (data.find(tlv::ChallengeStatus) != data.elements_end()) {
state.m_challengeStatus = readString(data.get(tlv::ChallengeStatus));
}
diff --git a/src/detail/challenge-encoder.hpp b/src/detail/challenge-encoder.hpp
index 3eaf4c4..4aa3b5a 100644
--- a/src/detail/challenge-encoder.hpp
+++ b/src/detail/challenge-encoder.hpp
@@ -22,7 +22,7 @@
#define NDNCERT_DETAIL_CHALLENGE_ENCODER_HPP
#include "detail/ca-request-state.hpp"
-#include "requester-state.hpp"
+#include "requester-request-state.hpp"
namespace ndn {
namespace ndncert {
@@ -31,10 +31,10 @@
{
public:
static Block
- encodeDataContent(ca::RequestState& request, optional<Name> issuedCertName = nullopt);
+ encodeDataContent(ca::RequestState& request, const Name& issuedCertName = Name());
static void
- decodeDataContent(const Block& data, requester::RequestContext& state);
+ decodeDataContent(const Block& contentBlock, requester::RequestState& state);
};
} // namespace ndncert
diff --git a/src/detail/new-renew-revoke-encoder.cpp b/src/detail/new-renew-revoke-encoder.cpp
index 577d1de..fd4f48c 100644
--- a/src/detail/new-renew-revoke-encoder.cpp
+++ b/src/detail/new-renew-revoke-encoder.cpp
@@ -101,7 +101,7 @@
std::array<uint8_t, 32>& salt, RequestId& requestId, Status& status)
{
content.parse();
- status = static_cast<Status>(readNonNegativeInteger(content.get(tlv::Status)));
+ status = statusFromBlock(content.get(tlv::Status));
const auto& ecdhBlock = content.get(tlv::EcdhPub);
ecdhKey.resize(ecdhBlock.value_size());