fix all unit tests
Change-Id: I81ddefe248a563f0dda79b6bb27897599b84ce59
diff --git a/src/ca-module.cpp b/src/ca-module.cpp
index 8b1c72a..0f1d800 100644
--- a/src/ca-module.cpp
+++ b/src/ca-module.cpp
@@ -184,41 +184,38 @@
// PROBE Naming Convention: /<CA-Prefix>/CA/PROBE/[ParametersSha256DigestComponent]
_LOG_TRACE("Received PROBE request");
- // process PROBE requests: find an available name
- std::string availableId = "";
- const auto& parameterTLV = request.getApplicationParameters();
- parameterTLV.parse();
- if (!parameterTLV.hasValue()) {
- _LOG_ERROR("Empty TLV obtained from the Interest parameter.");
- return;
+ // process PROBE requests: collect probe parameters
+ auto parameters = PROBE::decodeApplicationParameters(request.getApplicationParameters());
+ std::vector<std::string> availableComponents;
+ if (m_config.m_nameAssignmentFunc) {
+ try {
+ availableComponents = m_config.m_nameAssignmentFunc(parameters);
+ }
+ catch (const std::exception& e) {
+ _LOG_TRACE("Cannot parse probe parameters: " << e.what());
+ m_face.put(generateErrorDataPacket(request.getName(), ErrorCode::INVALID_PARAMETER,
+ "Cannot parse probe parameters: " + std::string(e.what())));
+ return;
+ }
+ }
+ else {
+ // if there is no app-specified name lookup, use a random name id
+ availableComponents.push_back(std::to_string(random::generateSecureWord64()));
+ }
+ std::vector<Name> availableNames;
+ for (const auto& component : availableComponents) {
+ Name newIdentityName = m_config.m_caItem.m_caPrefix;
+ newIdentityName.append(component);
+ availableNames.push_back(newIdentityName);
}
- // if (m_config.m_caItem.m_nameAssignmentFunc) {
- // try {
- // availableId = m_config.m_caItem.m_nameAssignmentFunc(parameterTLV);
- // }
- // catch (const std::exception& e) {
- // _LOG_TRACE("Cannot find PROBE input from PROBE parameters: " << e.what());
- // return;
- // }
- // }
- // else {
- // // if there is no app-specified name lookup, use a random name id
- // availableId = std::to_string(random::generateSecureWord64());
- // }
- // Name newIdentityName = m_config.m_caItem.m_caPrefix;
- // newIdentityName.append(availableId);
- // _LOG_TRACE("Handle PROBE: generate an identity " << newIdentityName);
-
- // Block contentTLV = PROBE::encodeDataContent(newIdentityName.toUri(), m_config.m_caItem.m_probe, parameterTLV);
-
- // Data result;
- // result.setName(request.getName());
- // result.setContent(contentTLV);
- // result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
- // m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
- // m_face.put(result);
- // _LOG_TRACE("Handle PROBE: send out the PROBE response");
+ Data result;
+ result.setName(request.getName());
+ result.setContent(PROBE::encodeDataContent(availableNames, m_config.m_caItem.m_maxSuffixLength));
+ result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
+ m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
+ m_face.put(result);
+ _LOG_TRACE("Handle PROBE: send out the PROBE response");
}
void
diff --git a/src/client-module.cpp b/src/client-module.cpp
index 0e7810e..8fdb7b2 100644
--- a/src/client-module.cpp
+++ b/src/client-module.cpp
@@ -68,7 +68,7 @@
ClientModule::verifyInfoResponse(const Data& reply)
{
// parse the ca item
- auto caItem = INFO::decodeDataContentToCaProfile(reply.getContent());
+ auto caItem = INFO::decodeDataContent(reply.getContent());
// verify the probe Data's sig
if (!security::verifySignature(reply, *caItem.m_cert)) {
@@ -84,7 +84,7 @@
const Block& contentBlock = reply.getContent();
// parse the ca item
- auto caItem = INFO::decodeDataContentToCaProfile(contentBlock);
+ auto caItem = INFO::decodeDataContent(contentBlock);
// update the local config
bool findItem = false;
diff --git a/src/protocol-detail/info.cpp b/src/protocol-detail/info.cpp
index da8b969..06eba0e 100644
--- a/src/protocol-detail/info.cpp
+++ b/src/protocol-detail/info.cpp
@@ -49,7 +49,7 @@
}
CaConfigItem
-INFO::decodeDataContentToCaProfile(const Block& block)
+INFO::decodeDataContent(const Block& block)
{
CaConfigItem result;
block.parse();
diff --git a/src/protocol-detail/info.hpp b/src/protocol-detail/info.hpp
index 1c375bb..03f0d61 100644
--- a/src/protocol-detail/info.hpp
+++ b/src/protocol-detail/info.hpp
@@ -38,7 +38,7 @@
* Decode CA configuration from the TLV block of INFO Data packet content.
*/
static CaConfigItem
- decodeDataContentToCaProfile(const Block& block);
+ decodeDataContent(const Block& block);
};
} // namespace ndncert
diff --git a/src/protocol-detail/probe.cpp b/src/protocol-detail/probe.cpp
index 93ba020..c7b72d5 100644
--- a/src/protocol-detail/probe.cpp
+++ b/src/protocol-detail/probe.cpp
@@ -19,8 +19,9 @@
*/
#include "probe.hpp"
-#include <ndn-cxx/encoding/tlv.hpp>
+
#include <boost/throw_exception.hpp>
+#include <ndn-cxx/encoding/tlv.hpp>
namespace ndn {
namespace ndncert {
@@ -38,7 +39,19 @@
return content;
}
-// For CA
+std::vector<std::tuple<std::string, std::string>>
+PROBE::decodeApplicationParameters(const Block& block)
+{
+ std::vector<std::tuple<std::string, std::string>> result;
+ block.parse();
+ for (size_t i = 0; i < block.elements().size() - 1; ++i) {
+ if (block.elements().at(i).type() == tlv_parameter_key && block.elements().at(i + 1).type() == tlv_parameter_value) {
+ result.push_back(std::make_tuple(readString(block.elements().at(i)), readString(block.elements().at(i + 1))));
+ }
+ }
+ return result;
+}
+
Block
PROBE::encodeDataContent(const std::vector<Name>& identifiers, boost::optional<size_t> maxSuffixLength)
{
@@ -53,5 +66,18 @@
return content;
}
+std::vector<Name>
+PROBE::decodeDataContent(const Block& block)
+{
+ std::vector<Name> result;
+ block.parse();
+ for (const auto& item : block.elements()) {
+ if (item.type() == tlv_probe_response) {
+ result.push_back(Name(item.blockFromValue()));
+ }
+ }
+ return result;
+}
+
} // namespace ndncert
} // namespace ndn
\ No newline at end of file
diff --git a/src/protocol-detail/probe.hpp b/src/protocol-detail/probe.hpp
index 126fba4..8d08e4b 100644
--- a/src/protocol-detail/probe.hpp
+++ b/src/protocol-detail/probe.hpp
@@ -28,11 +28,19 @@
class PROBE {
public:
+ // For CA use
static Block
encodeApplicationParameters(std::vector<std::tuple<std::string, std::string>>&& parameters);
+ static std::vector<Name>
+ decodeDataContent(const Block& block);
+
+ // For client use
static Block
encodeDataContent(const std::vector<Name>& identifiers, boost::optional<size_t> maxSuffixLength);
+
+ static std::vector<std::tuple<std::string, std::string>>
+ decodeApplicationParameters(const Block& block);
};
} // namespace ndncert
diff --git a/tests/unit-tests/bench.t.cpp b/tests/unit-tests/bench.t.cpp
index 226f789..da7e699 100644
--- a/tests/unit-tests/bench.t.cpp
+++ b/tests/unit-tests/bench.t.cpp
@@ -63,7 +63,7 @@
BOOST_CHECK(security::verifySignature(response, cert));
auto contentBlock = response.getContent();
contentBlock.parse();
- auto caItem = INFO::decodeDataContentToCaProfile(contentBlock);
+ auto caItem = INFO::decodeDataContent(contentBlock);
BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.size(), 1);
BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.front(), "full name");
diff --git a/tests/unit-tests/ca-module.t.cpp b/tests/unit-tests/ca-module.t.cpp
index 22c0d56..d6a2980 100644
--- a/tests/unit-tests/ca-module.t.cpp
+++ b/tests/unit-tests/ca-module.t.cpp
@@ -68,7 +68,7 @@
BOOST_CHECK(security::verifySignature(response, cert));
auto contentBlock = response.getContent();
contentBlock.parse();
- auto caItem = INFO::decodeDataContentToCaProfile(contentBlock);
+ auto caItem = INFO::decodeDataContent(contentBlock);
BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
BOOST_CHECK_EQUAL(caItem.m_probeParameterKeys.size(), 1);
BOOST_CHECK_EQUAL(caItem.m_cert->wireEncode(), cert.wireEncode());
@@ -480,7 +480,7 @@
CaModule ca(face, m_keyChain, "tests/unit-tests/config-files/config-ca-1", "ca-storage-memory");
advanceClocks(time::milliseconds(20), 60);
- //generate a certificate
+ // generate a certificate
auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty");
auto clientKey = clientIdentity.getDefaultKey();
security::v2::Certificate clientCert;
@@ -501,14 +501,17 @@
auto interest = client.generateRevokeInterest(clientCert);
- int count = 0;
+ bool receiveData = false;
face.onSendData.connect([&](const Data& response) {
- count++;
+ receiveData = true;
+ auto contentTlv = response.getContent();
+ contentTlv.parse();
+ BOOST_CHECK(static_cast<ErrorCode>(readNonNegativeInteger(contentTlv.get(tlv_error_code))) != ErrorCode::NO_ERROR);
});
face.receive(*interest);
advanceClocks(time::milliseconds(20), 60);
- BOOST_CHECK_EQUAL(count, 0);
+ BOOST_CHECK_EQUAL(receiveData, true);
}
BOOST_AUTO_TEST_SUITE_END() // TestCaModule
diff --git a/tests/unit-tests/configuration.t.cpp b/tests/unit-tests/configuration.t.cpp
index 4f2f256..f1dd87f 100644
--- a/tests/unit-tests/configuration.t.cpp
+++ b/tests/unit-tests/configuration.t.cpp
@@ -128,7 +128,7 @@
const auto& identity = addIdentity("/test");
const auto& cert = identity.getDefaultKey().getDefaultCertificate();
auto encoded = INFO::encodeDataContent(config.m_caItem, cert);
- auto decoded = INFO::decodeDataContentToCaProfile(encoded);
+ auto decoded = INFO::decodeDataContent(encoded);
BOOST_CHECK_EQUAL(config.m_caItem.m_caPrefix, decoded.m_caPrefix);
BOOST_CHECK_EQUAL(config.m_caItem.m_caInfo, decoded.m_caInfo);
BOOST_CHECK_EQUAL(config.m_caItem.m_maxValidityPeriod, decoded.m_maxValidityPeriod);
diff --git a/tools/ndncert-client.cpp b/tools/ndncert-client.cpp
index 8e2da5e..e81d792 100644
--- a/tools/ndncert-client.cpp
+++ b/tools/ndncert-client.cpp
@@ -201,7 +201,7 @@
std::cerr << "The fetched CA information cannot be trusted because its integrity is broken" << std::endl;
return;
}
- auto caItem = INFO::decodeDataContentToCaProfile(contentBlock);
+ auto caItem = INFO::decodeDataContent(contentBlock);
std::cerr << "Will use a new trust anchor, please double check the identity info: \n"
<< "This trust anchor information is signed by " << reply.getSignature().getKeyLocator()