add two functions for CA Info Packet and Metadata Packet
Change-Id: Ibbfb799f07d715b964e829f66b193d7d36717317
diff --git a/src/ca-module.cpp b/src/ca-module.cpp
index 74bfc18..9f95157 100644
--- a/src/ca-module.cpp
+++ b/src/ca-module.cpp
@@ -30,6 +30,7 @@
#include <ndn-cxx/security/verification-helpers.hpp>
#include <ndn-cxx/security/signing-helpers.hpp>
#include <ndn-cxx/util/random.hpp>
+#include <ndn-cxx/metadata-object.hpp>
namespace ndn {
namespace ndncert {
@@ -119,23 +120,57 @@
return false;
}
-void
-CaModule::onInfo(const Interest& request)
+shared_ptr<Data>
+CaModule::generateCaConfigMetaData()
{
- _LOG_TRACE("Received INFO request");
+ // @TODO
+ // make metadata a class member variable m_infoMetadata
+ // check whether the m_infoMetadata has the latest versioned name, if not, then generate a new one
+ // otherwise, directly reply m_infoMetadata.makeData
+
+ auto infoPacket = generateCaConfigData();
+ MetadataObject metadata;
+ metadata.setVersionedName(infoPacket->getName().getPrefix(-1));
+ Name discoveryInterestName(infoPacket->getName().getPrefix(-2));
+ name::Component metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata"));
+ discoveryInterestName.append(metadataComponent);
+ auto metadataData = metadata.makeData(discoveryInterestName, m_keyChain, signingByIdentity(m_config.m_caPrefix));
+ return make_shared<Data>(metadataData);
+}
+
+shared_ptr<Data>
+CaModule::generateCaConfigData()
+{
+ // @TODO
+ // make CaInfo Data packet a class member variable m_infoData
+ // check whether the m_infoData is still valid, if not, then generate a new one
+ // otherwise, directly reply m_infoData
const auto& pib = m_keyChain.getPib();
const auto& identity = pib.getIdentity(m_config.m_caPrefix);
const auto& cert = identity.getDefaultKey().getDefaultCertificate();
Block contentTLV = INFO::encodeContentFromCAConfig(m_config, cert);
- Data result;
- result.setName(request.getName());
- result.setContent(contentTLV);
- result.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
+ Name infoPacketName(m_config.m_caPrefix);
+ infoPacketName.append("CA").append("INFO").appendVersion().appendSegment(0);
+ Data infoData(infoPacketName);
+ infoData.setContent(contentTLV);
+ infoData.setFreshnessPeriod(DEFAULT_DATA_FRESHNESS_PERIOD);
+ m_keyChain.sign(infoData, signingByIdentity(m_config.m_caPrefix));
+ return make_shared<Data>(infoData);
+}
- m_keyChain.sign(result, signingByIdentity(m_config.m_caPrefix));
- m_face.put(result);
+void
+CaModule::onInfo(const Interest& request)
+{
+ _LOG_TRACE("Received INFO request");
+
+ if (request.getName().get(-1).type() == 32) {
+ m_face.put(*generateCaConfigMetaData());
+ }
+ else {
+ m_face.put(*generateCaConfigData());
+ }
_LOG_TRACE("Handle INFO: send out the INFO response");
}
@@ -442,6 +477,7 @@
return certRequest;
}
+<<<<<<< HEAD
/**
* @brief Generate JSON file to response PROBE insterest
*
@@ -556,6 +592,8 @@
return root;
}
+=======
+>>>>>>> add two functions for CA Info Packet and Metadata Packet
void
CaModule::onRegisterFailed(const std::string& reason)
{
diff --git a/src/ca-module.hpp b/src/ca-module.hpp
index 80aaac0..7e9c7e6 100644
--- a/src/ca-module.hpp
+++ b/src/ca-module.hpp
@@ -71,6 +71,12 @@
dataContentFromJson(const JsonSection& jsonSection);
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+ shared_ptr<Data>
+ generateCaConfigMetaData();
+
+ shared_ptr<Data>
+ generateCaConfigData();
+
void
onInfo(const Interest& request);
@@ -96,22 +102,6 @@
registerPrefix();
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- JsonSection
- genProbeResponseJson(const Name& identifier,
- const std::string& m_probe,
- const JsonSection& parameterJson);
-
- JsonSection
- genInfoResponseJson();
-
- JsonSection
- genNewResponseJson(const std::string& ecdhKey, const std::string& salt,
- const CertificateRequest& request, const std::list<std::string>& challenges);
-
- JsonSection
- genChallengeResponseJson(const CertificateRequest& request);
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Face& m_face;
CaConfig m_config;
unique_ptr<CaStorage> m_storage;
diff --git a/tests/unit-tests/bench.t.cpp b/tests/unit-tests/bench.t.cpp
index 91622da..038e97b 100644
--- a/tests/unit-tests/bench.t.cpp
+++ b/tests/unit-tests/bench.t.cpp
@@ -20,24 +20,168 @@
#include "ca-module.hpp"
#include "identity-management-fixture.hpp"
+#include "client-module.hpp"
+#include "challenge-module/challenge-pin.hpp"
+#include "protocol-detail/info.hpp"
-#include <ndn-cxx/security/transform/base64-encode.hpp>
-#include <ndn-cxx/security/transform/buffer-source.hpp>
-#include <ndn-cxx/security/transform/stream-sink.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
+#include <ndn-cxx/security/transform/public-key.hpp>
+#include <ndn-cxx/security/verification-helpers.hpp>
+#include <ndn-cxx/metadata-object.hpp>
+#include <iostream>
namespace ndn {
namespace ndncert {
namespace tests {
-BOOST_FIXTURE_TEST_SUITE(TestForBenchmark, IdentityManagementFixture)
+BOOST_FIXTURE_TEST_SUITE(TestForBenchmark, IdentityManagementTimeFixture)
-BOOST_AUTO_TEST_CASE(ReadConfigFile)
+BOOST_AUTO_TEST_CASE(PacketSize0)
{
- BOOST_CHECK(true);
+ auto identity = addIdentity(Name("/ndn"));
+ auto key = identity.getDefaultKey();
+ auto cert = key.getDefaultCertificate();
+
+ util::DummyClientFace face(io, {true, true});
+ CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
+ ca.setProbeHandler([&](const Block& probeInfo) {
+ return "example";
+ });
+ advanceClocks(time::milliseconds(20), 60);
+
+ Interest interest = MetadataObject::makeDiscoveryInterest(Name("/ndn/CA/INFO"));
+ std::cout << "CA Config discovery Interest Size: " << interest.wireEncode().size() << std::endl;
+ shared_ptr<Interest> infoInterest = nullptr;
+
+ int count = 0;
+ face.onSendData.connect([&](const Data& response) {
+ if (count == 0) {
+ count++;
+ std::cout << "CA Config MetaData Size: " << response.wireEncode().size() << std::endl;
+ auto block = response.getContent();
+ block.parse();
+ Interest interest(Name(block.get(tlv::Name)));
+ interest.setCanBePrefix(true);
+ infoInterest = make_shared<Interest>(interest);
+ std::cout << "CA Config fetch Interest Size: " << infoInterest->wireEncode().size() << std::endl;
+
+ }
+ else {
+ count++;
+ std::cout << "CA Config Data Size: " << response.wireEncode().size() << std::endl;
+ BOOST_CHECK(security::verifySignature(response, cert));
+ auto contentBlock = response.getContent();
+ contentBlock.parse();
+ auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
+ BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
+ BOOST_CHECK_EQUAL(caItem.m_probe, "");
+ BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
+ BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
+ }
+ });
+ face.receive(interest);
+ advanceClocks(time::milliseconds(20), 60);
+ face.receive(*infoInterest);
+ advanceClocks(time::milliseconds(20), 60);
+
+ BOOST_CHECK_EQUAL(count, 2);
}
-BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
+BOOST_AUTO_TEST_CASE(PacketSize1)
+{
+ auto identity = addIdentity(Name("/ndn"));
+ auto key = identity.getDefaultKey();
+ auto cert = key.getDefaultCertificate();
-} // namespace tests
-} // namespace ndncert
-} // namespace ndn
+ util::DummyClientFace face(io, {true, true});
+ CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
+ advanceClocks(time::milliseconds(20), 60);
+
+ // generate NEW Interest
+ ClientModule client(m_keyChain);
+ ClientCaItem item;
+ item.m_caPrefix = Name("/ndn");
+ item.m_anchor = cert;
+ client.getClientConf().m_caItems.push_back(item);
+ auto newInterest = client.generateNewInterest(time::system_clock::now(),
+ time::system_clock::now() + time::days(1), Name("/ndn/alice"));
+
+ std::cout << "New Interest Size: " << newInterest->wireEncode().size() << std::endl;
+
+ // generate CHALLENGE Interest
+ ChallengePin pinChallenge;
+ shared_ptr<Interest> challengeInterest = nullptr;
+ shared_ptr<Interest> challengeInterest2 = nullptr;
+ shared_ptr<Interest> challengeInterest3 = nullptr;
+
+ int count = 0;
+ face.onSendData.connect([&](const Data& response) {
+ if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
+ std::cout << "NEW Data Size: " << response.wireEncode().size() << std::endl;
+ client.onNewResponse(response);
+ auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
+ challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
+ client.m_challengeStatus,
+ paramJson));
+ }
+ else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+
+ client.onChallengeResponse(response);
+ BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
+ BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE);
+
+ auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
+ challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
+ client.m_challengeStatus,
+ paramJson));
+ }
+ else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+
+ client.onChallengeResponse(response);
+ BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE);
+ BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE);
+
+ auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
+ auto request = ca.getCertificateRequest(*challengeInterest2);
+ auto secret = request.m_challengeSecrets.get(ChallengePin::JSON_PIN_CODE, "");
+ for (auto& i : paramJson) {
+ if (i.first == ChallengePin::JSON_PIN_CODE)
+ i.second.put("", secret);
+ }
+ challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
+ client.m_challengeStatus,
+ paramJson));
+ std::cout << "CHALLENGE Interest Size: " << challengeInterest3->wireEncode().size() << std::endl;
+ }
+ else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
+ std::cout << "CHALLENGE Data Size: " << response.wireEncode().size() << std::endl;
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+
+ client.onChallengeResponse(response);
+ BOOST_CHECK_EQUAL(client.m_status, STATUS_SUCCESS);
+ BOOST_CHECK_EQUAL(client.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
+ }
+ });
+
+ face.receive(*newInterest);
+ advanceClocks(time::milliseconds(20), 60);
+ face.receive(*challengeInterest);
+ advanceClocks(time::milliseconds(20), 60);
+ face.receive(*challengeInterest2);
+ advanceClocks(time::milliseconds(20), 60);
+ face.receive(*challengeInterest3);
+ advanceClocks(time::milliseconds(20), 60);
+ BOOST_CHECK_EQUAL(count, 3);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestCaConfig
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn
diff --git a/tests/unit-tests/ca-module.t.cpp b/tests/unit-tests/ca-module.t.cpp
index 104c505..095ac19 100644
--- a/tests/unit-tests/ca-module.t.cpp
+++ b/tests/unit-tests/ca-module.t.cpp
@@ -19,17 +19,17 @@
*/
#include "ca-module.hpp"
-#include "database-fixture.hpp"
-#include "client-module.hpp"
#include "challenge-module.hpp"
-#include "challenge-module/challenge-pin.hpp"
#include "challenge-module/challenge-email.hpp"
+#include "challenge-module/challenge-pin.hpp"
+#include "client-module.hpp"
+#include "database-fixture.hpp"
#include "protocol-detail/info.hpp"
-#include <ndn-cxx/util/dummy-client-face.hpp>
#include <ndn-cxx/security/signing-helpers.hpp>
#include <ndn-cxx/security/transform/public-key.hpp>
#include <ndn-cxx/security/verification-helpers.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
namespace ndn {
namespace ndncert {
@@ -51,7 +51,7 @@
advanceClocks(time::milliseconds(20), 60);
BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 2);
- BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 4); // onInfo, onProbe, onNew, onChallenge
+ BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 4); // onInfo, onProbe, onNew, onChallenge
}
BOOST_AUTO_TEST_CASE(HandleProbe)
@@ -62,9 +62,9 @@
util::DummyClientFace face(io, {true, true});
CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
- ca.setProbeHandler([&] (const Block& probeInfo) {
- return "example";
- });
+ ca.setProbeHandler([&](const Block& probeInfo) {
+ return "example";
+ });
advanceClocks(time::milliseconds(20), 60);
Interest interest("/ndn/CA/PROBE");
@@ -78,17 +78,17 @@
interest.setApplicationParameters(paramTLV);
int count = 0;
- face.onSendData.connect([&] (const Data& response) {
- count++;
- BOOST_CHECK(security::verifySignature(response, cert));
- Block contentBlock = response.getContent();
- contentBlock.parse();
- Block probeResponse = contentBlock.get(tlv_probe_response);
- probeResponse.parse();
- Name caName;
- caName.wireDecode(probeResponse.get(tlv::Name));
- BOOST_CHECK_EQUAL(caName, "/ndn/example");
- });
+ face.onSendData.connect([&](const Data& response) {
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+ Block contentBlock = response.getContent();
+ contentBlock.parse();
+ Block probeResponse = contentBlock.get(tlv_probe_response);
+ probeResponse.parse();
+ Name caName;
+ caName.wireDecode(probeResponse.get(tlv::Name));
+ BOOST_CHECK_EQUAL(caName, "/ndn/example");
+ });
face.receive(interest);
advanceClocks(time::milliseconds(20), 60);
@@ -103,26 +103,26 @@
util::DummyClientFace face(io, {true, true});
CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory");
- ca.setProbeHandler([&] (const Block& probeInfo) {
- return "example";
- });
+ ca.setProbeHandler([&](const Block& probeInfo) {
+ return "example";
+ });
advanceClocks(time::milliseconds(20), 60);
Interest interest("/ndn/CA/INFO");
interest.setCanBePrefix(false);
int count = 0;
- face.onSendData.connect([&] (const Data& response) {
- count++;
- BOOST_CHECK(security::verifySignature(response, cert));
- auto contentBlock = response.getContent();
- contentBlock.parse();
- auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
- BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
- BOOST_CHECK_EQUAL(caItem.m_probe, "");
- BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
- BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
- });
+ face.onSendData.connect([&](const Data& response) {
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+ auto contentBlock = response.getContent();
+ contentBlock.parse();
+ auto caItem = INFO::decodeClientConfigFromContent(contentBlock);
+ BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn");
+ BOOST_CHECK_EQUAL(caItem.m_probe, "");
+ BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode());
+ BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca");
+ });
face.receive(interest);
advanceClocks(time::milliseconds(20), 60);
@@ -150,17 +150,17 @@
interest.setApplicationParameters(paramTLV);
int count = 0;
- face.onSendData.connect([&] (const Data& response) {
- count++;
- BOOST_CHECK(security::verifySignature(response, cert));
- auto contentBlock = response.getContent();
- contentBlock.parse();
- auto probeResponseBlock = contentBlock.get(tlv_probe_response);
- probeResponseBlock.parse();
- Name caPrefix;
- caPrefix.wireDecode(probeResponseBlock.get(tlv::Name));
- BOOST_CHECK(caPrefix != "");
- });
+ face.onSendData.connect([&](const Data& response) {
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+ auto contentBlock = response.getContent();
+ contentBlock.parse();
+ auto probeResponseBlock = contentBlock.get(tlv_probe_response);
+ probeResponseBlock.parse();
+ Name caPrefix;
+ caPrefix.wireDecode(probeResponseBlock.get(tlv::Name));
+ BOOST_CHECK(caPrefix != "");
+ });
face.receive(interest);
advanceClocks(time::milliseconds(20), 60);
@@ -188,29 +188,29 @@
Name("/ndn/zhiyi"));
int count = 0;
- face.onSendData.connect([&] (const Data& response) {
- count++;
- BOOST_CHECK(security::verifySignature(response, cert));
- auto contentBlock = response.getContent();
- contentBlock.parse();
+ face.onSendData.connect([&](const Data& response) {
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+ auto contentBlock = response.getContent();
+ contentBlock.parse();
- BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != "");
- BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != "");
- BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != "");
+ BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != "");
+ BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != "");
+ BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != "");
- auto challengeBlockCount = 0;
- for (auto const& element: contentBlock.elements()) {
- if (element.type() == tlv_challenge) {
- challengeBlockCount++;
- }
+ auto challengeBlockCount = 0;
+ for (auto const& element : contentBlock.elements()) {
+ if (element.type() == tlv_challenge) {
+ challengeBlockCount++;
}
+ }
- BOOST_CHECK(challengeBlockCount != 0);
+ BOOST_CHECK(challengeBlockCount != 0);
- client.onNewResponse(response);
- BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey),
- ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey));
- });
+ client.onNewResponse(response);
+ BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey),
+ ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey));
+ });
face.receive(*interest);
advanceClocks(time::milliseconds(20), 60);
@@ -240,9 +240,9 @@
auto interest3 = client.generateNewInterest(current_tp - time::hours(1),
current_tp + time::hours(2),
Name("/ndn/zhiyi"));
- face.onSendData.connect([&] (const Data& response) {
- BOOST_CHECK(false);
- });
+ face.onSendData.connect([&](const Data& response) {
+ BOOST_CHECK(false);
+ });
face.receive(*interest1);
face.receive(*interest2);
face.receive(*interest3);
@@ -275,10 +275,10 @@
Name("/ndn/zhiyi"), data);
int count = 0;
- face.onSendData.connect([&] (const Data& response) {
- count++;
- BOOST_CHECK(security::verifySignature(response, cert));
- });
+ face.onSendData.connect([&](const Data& response) {
+ count++;
+ BOOST_CHECK(security::verifySignature(response, cert));
+ });
face.receive(*interest);
advanceClocks(time::milliseconds(20), 60);
@@ -311,13 +311,13 @@
shared_ptr<Interest> challengeInterest3 = nullptr;
int count = 0;
- face.onSendData.connect([&] (const Data& response) {
+ face.onSendData.connect([&](const Data& response) {
if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) {
client.onNewResponse(response);
auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
- client.m_challengeStatus,
- paramJson));
+ client.m_challengeStatus,
+ paramJson));
}
else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) {
count++;
@@ -329,8 +329,8 @@
auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus);
challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
- client.m_challengeStatus,
- paramJson));
+ client.m_challengeStatus,
+ paramJson));
}
else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) {
count++;
@@ -348,8 +348,8 @@
i.second.put("", secret);
}
challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status,
- client.m_challengeStatus,
- paramJson));
+ client.m_challengeStatus,
+ paramJson));
}
else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) {
count++;
@@ -372,8 +372,8 @@
BOOST_CHECK_EQUAL(count, 3);
}
-BOOST_AUTO_TEST_SUITE_END() // TestCaModule
+BOOST_AUTO_TEST_SUITE_END() // TestCaModule
-} // namespace tests
-} // namespace ndncert
-} // namespace ndn
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn