tests+mgmt: Import common unit test code NFD

This commit introduces BaseFixture, UnitTestTimeFixture,
IdentityManagementFixture, and IdentityManagementTimeFixture.
In addition to that, the command line of the unit test binary (for boost
libraries before 1.62) are extended to support multi-format output.

This commit also addresses a strange bug in the management tool that was
causing spurious segmentation faults during unit tests.

Change-Id: Ib34b4007773f0b35d9ed919eebbdc4e0d413fa1e
diff --git a/src/mgmt/management-tool.cpp b/src/mgmt/management-tool.cpp
index 62b57af..9ade73f 100644
--- a/src/mgmt/management-tool.cpp
+++ b/src/mgmt/management-tool.cpp
@@ -337,36 +337,40 @@
     throw Error("input does not contain a valid Data packet");
   }
 
-  //determine whether the data is a self-signed certificate
+  // determine whether the data is a self-signed certificate
   shared_ptr<Regex> regex1 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>");
   if (regex1->match(data->getName())) {
     IdentityCertificate scert(*data);
     Name keyName = scert.getPublicKeyName();
+    if (keyName.getPrefix(zoneName.size()) != zoneName) {
+      throw Error("the input key does not belong to the zone");
+    }
+
     Name keyLocator = scert.getSignature().getKeyLocator().getName();
 
-    //if it is, extract the content and name from the data, and resign it using the dsk.
+    // if it is, extract the content and name from the data, and resign it using the dsk.
     shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT>");
     BOOST_VERIFY(regex2->match(keyLocator) == true);
     if (keyName == regex2->expand("\\1\\2")) {
-      shared_ptr<Data> pre = data;
-      Name name = pre->getName();
-      //check whether the name is legal or not. if not converting it to a legal name
-      if (zoneName != regex1->expand("\\1")) {
-        Name comp1 = regex1->expand("\\1").getSubName(zoneName.size());
-        Name comp2 = regex1->expand("\\2");
-        name = zoneName;
-        name.append("KEY");
-        name.append(comp1);
-        name.append(comp2);
-        name.append("ID-CERT");
-        name.append(pre->getName().get(-1));
+
+      Name canonicalName;
+      canonicalName
+        .append(zoneName)
+        .append("KEY")
+        .append(keyName.getSubName(zoneName.size(), keyName.size() - zoneName.size()))
+        .append("ID-CERT")
+        .append(data->getName().get(-1));
+
+      if (data->getName() != canonicalName) {
+        // name need to be adjusted
+        auto newData = make_shared<Data>();
+        newData->setName(canonicalName);
+        newData->setMetaInfo(data->getMetaInfo());
+        newData->setContent(data->getContent());
+        m_keyChain.sign(*newData);
+
+        data = newData;
       }
-
-      data = make_shared<Data>();
-      data->setName(name);
-      data->setContent(pre->getContent());
-
-      m_keyChain.sign(*data, dskCertName);
     }
   }