security: Identity/Key comparison operators

This commit adds ==, !=, and stream insertion operators to
Identity and Key types, so that `ndnsec list` can display
default identity and key properly.

Also, a missing assignment is added to `ndnsec list` routine.

refs #4085

Change-Id: I025af1d6281acb9e9261461520bbd74af53b811e
diff --git a/src/security/pib/identity.cpp b/src/security/pib/identity.cpp
index 79cb090..0f7dc26 100644
--- a/src/security/pib/identity.cpp
+++ b/src/security/pib/identity.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -103,6 +103,24 @@
   return impl;
 }
 
+bool
+operator!=(const Identity& lhs, const Identity& rhs)
+{
+  return lhs.m_impl.owner_before(rhs.m_impl) || rhs.m_impl.owner_before(lhs.m_impl);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const Identity& id)
+{
+  if (id) {
+    os << id.getName();
+  }
+  else {
+    os << "(empty)";
+  }
+  return os;
+}
+
 } // namespace pib
 } // namespace security
 } // namespace ndn
diff --git a/src/security/pib/identity.hpp b/src/security/pib/identity.hpp
index cc46bfd..228ef16 100644
--- a/src/security/pib/identity.hpp
+++ b/src/security/pib/identity.hpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -100,6 +100,7 @@
   /*
    * @return True if the identity instance is valid
    */
+  explicit
   operator bool() const;
 
   /**
@@ -157,8 +158,21 @@
   weak_ptr<detail::IdentityImpl> m_impl;
 
   friend class v2::KeyChain;
+  friend bool operator!=(const Identity&, const Identity&);
 };
 
+bool
+operator!=(const Identity& lhs, const Identity& rhs);
+
+inline bool
+operator==(const Identity& lhs, const Identity& rhs)
+{
+  return !(lhs != rhs);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const Identity& id);
+
 } // namespace pib
 
 using pib::Identity;
diff --git a/src/security/pib/key.cpp b/src/security/pib/key.cpp
index 3d7b83c..7396a5a 100644
--- a/src/security/pib/key.cpp
+++ b/src/security/pib/key.cpp
@@ -123,6 +123,24 @@
   return impl;
 }
 
+bool
+operator!=(const Key& lhs, const Key& rhs)
+{
+  return lhs.m_impl.owner_before(rhs.m_impl) || rhs.m_impl.owner_before(lhs.m_impl);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const Key& key)
+{
+  if (key) {
+    os << key.getName();
+  }
+  else {
+    os << "(empty)";
+  }
+  return os;
+}
+
 } // namespace pib
 
 namespace v2 {
diff --git a/src/security/pib/key.hpp b/src/security/pib/key.hpp
index 90f4f5c..fbbed24 100644
--- a/src/security/pib/key.hpp
+++ b/src/security/pib/key.hpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -125,6 +125,7 @@
   /**
    * @brief Check if the Key instance is valid.
    */
+  explicit
   operator bool() const;
 
   /**
@@ -181,8 +182,21 @@
   weak_ptr<detail::KeyImpl> m_impl;
 
   friend class v2::KeyChain;
+  friend bool operator!=(const Key&, const Key&);
 };
 
+bool
+operator!=(const Key& lhs, const Key& rhs);
+
+inline bool
+operator==(const Key& lhs, const Key& rhs)
+{
+  return !(lhs != rhs);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const Key& key);
+
 } // namespace pib
 
 using pib::Key;
diff --git a/tests/unit-tests/security/pib/identity.t.cpp b/tests/unit-tests/security/pib/identity.t.cpp
index 49bd8da..239da12 100644
--- a/tests/unit-tests/security/pib/identity.t.cpp
+++ b/tests/unit-tests/security/pib/identity.t.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -78,6 +78,9 @@
   auto identityImpl = make_shared<IdentityImpl>(id1, make_shared<pib::PibMemory>(), true);
   Identity identity1(identityImpl);
   Identity identity2(identityImpl);
+  BOOST_CHECK_EQUAL(identity1, identity2);
+  BOOST_CHECK_NE(identity1, Identity());
+  BOOST_CHECK_EQUAL(Identity(), Identity());
 
   identity1.addKey(id1Key1.buf(), id1Key1.size(), id1Key1Name);
   BOOST_CHECK_NO_THROW(identity2.getKey(id1Key1Name));
diff --git a/tests/unit-tests/security/pib/key.t.cpp b/tests/unit-tests/security/pib/key.t.cpp
index 2275c2e..a3543a3 100644
--- a/tests/unit-tests/security/pib/key.t.cpp
+++ b/tests/unit-tests/security/pib/key.t.cpp
@@ -79,6 +79,9 @@
   auto keyImpl = make_shared<KeyImpl>(id1Key1Name, id1Key1.buf(), id1Key1.size(), make_shared<pib::PibMemory>());
   Key key1(keyImpl);
   Key key2(keyImpl);
+  BOOST_CHECK_EQUAL(key1, key2);
+  BOOST_CHECK_NE(key1, Key());
+  BOOST_CHECK_EQUAL(Key(), Key());
 
   key1.addCertificate(id1Key1Cert1);
   BOOST_CHECK_NO_THROW(key2.getCertificate(id1Key1Cert1.getName()));
diff --git a/tools/ndnsec/list.cpp b/tools/ndnsec/list.cpp
index 36026d7..955fb6f 100644
--- a/tools/ndnsec/list.cpp
+++ b/tools/ndnsec/list.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -157,7 +157,7 @@
   // TODO add API to check for default identity (may be from the identity itself)
   security::Identity defaultIdentity;
   try {
-    keyChain.getPib().getDefaultIdentity();
+    defaultIdentity = keyChain.getPib().getDefaultIdentity();
   }
   catch (const security::Pib::Error&) {
     // no default identity