key-locator: KeyDigest

refs #1426

Change-Id: Ib4de0c7348786879775e52c803f6a2eaeaa2100c
diff --git a/tests/unit-tests/test-key-locator.cpp b/tests/unit-tests/test-key-locator.cpp
index 066aefc..3e48233 100644
--- a/tests/unit-tests/test-key-locator.cpp
+++ b/tests/unit-tests/test-key-locator.cpp
@@ -20,17 +20,114 @@
  */
 
 #include "key-locator.hpp"
+#include "util/concepts.hpp"
+#include "encoding/block-helpers.hpp"
 
 #include "boost-test.hpp"
 
 namespace ndn {
 
+BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
+BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>));
+BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>));
+
 BOOST_AUTO_TEST_SUITE(TestKeyLocator)
 
+BOOST_AUTO_TEST_CASE(TypeNone)
+{
+  KeyLocator a;
+  BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_None);
+  BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
+  BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
+
+  Block wire;
+  BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
+
+  // These octets are obtained by the snippet below.
+  // This check is intended to detect unexpected encoding change in the future.
+  // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+  //   printf("0x%02x, ", *it);
+  // }
+  static const uint8_t expected[] = {
+    0x1c, 0x00
+  };
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
+
+  BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
+  KeyLocator b(wire);
+  BOOST_CHECK(a == b);
+  BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_None);
+  BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
+  BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
+}
+
+BOOST_AUTO_TEST_CASE(TypeName)
+{
+  KeyLocator a;
+  a.setName("/N");
+  BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Name);
+  BOOST_CHECK_EQUAL(a.getName(), Name("/N"));
+  BOOST_CHECK_THROW(a.getKeyDigest(), KeyLocator::Error);
+
+  Block wire;
+  BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
+
+  // These octets are obtained by the snippet below.
+  // This check is intended to detect unexpected encoding change in the future.
+  // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+  //   printf("0x%02x, ", *it);
+  // }
+  static const uint8_t expected[] = {
+    0x1c, 0x05, 0x07, 0x03, 0x08, 0x01, 0x4e
+  };
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
+
+  BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
+  KeyLocator b(wire);
+  BOOST_CHECK(a == b);
+  BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_Name);
+  BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
+  BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
+}
+
+BOOST_AUTO_TEST_CASE(TypeKeyDigest)
+{
+  char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
+  ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
+  Block expectedDigestBlock = dataBlock(tlv::KeyDigest, digestOctets, 8);
+
+  KeyLocator a;
+  a.setKeyDigest(digestBuffer);
+  BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_KeyDigest);
+  BOOST_CHECK(a.getKeyDigest() == expectedDigestBlock);
+  BOOST_CHECK_THROW(a.getName(), KeyLocator::Error);
+
+  Block wire;
+  BOOST_REQUIRE_NO_THROW(wire = a.wireEncode());
+
+  // These octets are obtained by the snippet below.
+  // This check is intended to detect unexpected encoding change in the future.
+  // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+  //   printf("0x%02x, ", *it);
+  // }
+  static const uint8_t expected[] = {
+    0x1c, 0x0a, 0x1d, 0x08, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
+  };
+  BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
+                                wire.begin(), wire.end());
+
+  BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
+  KeyLocator b(wire);
+  BOOST_CHECK(a == b);
+  BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_KeyDigest);
+  BOOST_CHECK(b.getKeyDigest() == expectedDigestBlock);
+  BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
+}
+
 BOOST_AUTO_TEST_CASE(Equality)
 {
-  BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
-
   KeyLocator a;
   KeyLocator b;
   BOOST_CHECK_EQUAL(a == b, true);
@@ -47,6 +144,36 @@
   b.setName("ndn:/A");
   BOOST_CHECK_EQUAL(a == b, true);
   BOOST_CHECK_EQUAL(a != b, false);
+
+  char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
+  ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
+
+  a.setKeyDigest(digestBuffer);
+  BOOST_CHECK_EQUAL(a == b, false);
+  BOOST_CHECK_EQUAL(a != b, true);
+
+  b.setKeyDigest(digestBuffer);
+  BOOST_CHECK_EQUAL(a == b, true);
+  BOOST_CHECK_EQUAL(a != b, false);
+}
+
+BOOST_AUTO_TEST_CASE(UnknownType)
+{
+  static const uint8_t wireOctets[] = {
+    0x1c, 0x03, 0x7f, 0x01, 0xcc
+  };
+  Block wire(wireOctets, sizeof(wireOctets));
+  BOOST_REQUIRE_NO_THROW(KeyLocator(wire));
+  KeyLocator a(wire);
+  BOOST_CHECK_EQUAL(a.getType(), KeyLocator::KeyLocator_Unknown);
+
+  KeyLocator b(wire);
+  BOOST_CHECK_EQUAL(a == b, true);
+  BOOST_CHECK_EQUAL(a != b, false);
+
+  b.setName("/N");
+  BOOST_CHECK_EQUAL(a == b, false);
+  BOOST_CHECK_EQUAL(a != b, true);
 }
 
 BOOST_AUTO_TEST_SUITE_END()