security: Implement operator<< for v2::Certificate
This commit also implements operator<< for SignatureInfoValue and
KeyLocator.
Change-Id: I71d3840ab63bacf1278d755b5eed9630c5a2f48f
diff --git a/tests/unit-tests/encoding/tlv.t.cpp b/tests/unit-tests/encoding/tlv.t.cpp
index 635aecf..6576e04 100644
--- a/tests/unit-tests/encoding/tlv.t.cpp
+++ b/tests/unit-tests/encoding/tlv.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -25,6 +25,7 @@
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>
+#include <boost/lexical_cast.hpp>
namespace ndn {
namespace tlv {
@@ -401,6 +402,26 @@
BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
+BOOST_AUTO_TEST_SUITE(PrintHelpers)
+
+BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
+{
+ SignatureTypeValue value = DigestSha256;
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "DigestSha256");
+
+ value = SignatureSha256WithRsa;
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithRsa");
+
+ value = SignatureSha256WithEcdsa;
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithEcdsa");
+
+ value = static_cast<SignatureTypeValue>(-1);
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "Unknown Signature Type");
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestTlv
+
+
BOOST_AUTO_TEST_SUITE_END() // TestTlv
BOOST_AUTO_TEST_SUITE_END() // Encoding
diff --git a/tests/unit-tests/key-locator.t.cpp b/tests/unit-tests/key-locator.t.cpp
index 72e4d66..8ae8d48 100644
--- a/tests/unit-tests/key-locator.t.cpp
+++ b/tests/unit-tests/key-locator.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -23,6 +23,7 @@
#include "encoding/block-helpers.hpp"
#include "boost-test.hpp"
+#include <boost/lexical_cast.hpp>
namespace ndn {
namespace tests {
@@ -56,6 +57,8 @@
BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_None);
BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
+
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "None");
}
BOOST_AUTO_TEST_CASE(TypeName)
@@ -86,13 +89,15 @@
BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_Name);
BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
+
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Name=/N");
}
BOOST_AUTO_TEST_CASE(TypeKeyDigest)
{
- char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
- ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
- Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets, 8);
+ std::string digestOctets = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45";
+ ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets.c_str(), digestOctets.size());
+ Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets.c_str(), digestOctets.size());
KeyLocator a;
a.setKeyDigest(digestBuffer);
@@ -109,7 +114,7 @@
// printf("0x%02x, ", *it);
// }
static const uint8_t expected[] = {
- 0x1c, 0x0a, 0x1d, 0x08, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
+ 0x1c, 0x0c, 0x1d, 0x0a, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
};
BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
wire.begin(), wire.end());
@@ -120,6 +125,12 @@
BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_KeyDigest);
BOOST_CHECK(b.getKeyDigest() == expectedDigestBlock);
BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
+
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=123456789A...");
+
+ std::string shortDigest = "\xbc\xde\xf1";
+ b.setKeyDigest(make_shared<Buffer>(shortDigest.c_str(), shortDigest.size()));
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=BCDEF1");
}
BOOST_AUTO_TEST_CASE(Equality)
@@ -167,6 +178,8 @@
BOOST_CHECK_EQUAL(a == b, true);
BOOST_CHECK_EQUAL(a != b, false);
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Unknown");
+
b.setName("/N");
BOOST_CHECK_EQUAL(a == b, false);
BOOST_CHECK_EQUAL(a != b, true);
diff --git a/tests/unit-tests/security/v2/certificate.t.cpp b/tests/unit-tests/security/v2/certificate.t.cpp
index 8836e05..9522b55 100644
--- a/tests/unit-tests/security/v2/certificate.t.cpp
+++ b/tests/unit-tests/security/v2/certificate.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -256,6 +256,30 @@
BOOST_CHECK_THROW(cert.getPublicKey(), Certificate::Error);
}
+BOOST_AUTO_TEST_CASE(PrintCertificateInfo)
+{
+ const std::string expectedCertificateInfo = std::string(R"INFO(
+Certificate name:
+ /ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B
+Validity:
+ NotBefore: 20150814T223739
+ NotAfter: 20150818T223738
+Public key bits:
+ MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCeBj5HhbI0N6qFR6wDJIO1nKgF
+ OiQe64kBu+mbssMirGjj8GwCzmimxNCnBpCcqhsIHYtDmjNnRG0hoxuImpdeWcQV
+ C9ksvVEHYYKtwbjXv5vPfSTCY/OXF+v+YiW6W02Kwnq9Q4qPuPLxxWow01CMyJrf
+ 7+0153pi6nZ8uwgmxwIBEQ==
+Signature Information:
+ Signature Type: SignatureSha256WithRsa
+ Key Locator: Name=/ndn/site1/KEY/ksk-2516425377094
+)INFO").substr(1);
+
+ Certificate certificate(Block(CERT, sizeof(CERT)));
+ BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(certificate), expectedCertificateInfo);
+
+ // @todo Check output formats of other certificates
+}
+
BOOST_AUTO_TEST_SUITE_END() // TestCertificate
BOOST_AUTO_TEST_SUITE_END() // V2
BOOST_AUTO_TEST_SUITE_END() // Security