dump: handle exceptions thrown by lp::Packet::wireDecode()

Change-Id: Ia3bd8a4f57c70313a589c197ee453f517664ec43
Refs: #3943
diff --git a/tests/dump/invalid-tlv-length.pcap b/tests/dump/invalid-tlv-length.pcap
new file mode 100644
index 0000000..50769a1
--- /dev/null
+++ b/tests/dump/invalid-tlv-length.pcap
Binary files differ
diff --git a/tests/dump/LINUXSLL-tcp4.pcap b/tests/dump/linux-sll-tcp4.pcap
similarity index 100%
rename from tests/dump/LINUXSLL-tcp4.pcap
rename to tests/dump/linux-sll-tcp4.pcap
Binary files differ
diff --git a/tests/dump/LINUXSLL-tcp6.pcap b/tests/dump/linux-sll-tcp6.pcap
similarity index 100%
rename from tests/dump/LINUXSLL-tcp6.pcap
rename to tests/dump/linux-sll-tcp6.pcap
Binary files differ
diff --git a/tests/dump/LINUXSLL-udp4.pcap b/tests/dump/linux-sll-udp4.pcap
similarity index 100%
rename from tests/dump/LINUXSLL-udp4.pcap
rename to tests/dump/linux-sll-udp4.pcap
Binary files differ
diff --git a/tests/dump/LINUXSLL-udp6.pcap b/tests/dump/linux-sll-udp6.pcap
similarity index 100%
rename from tests/dump/LINUXSLL-udp6.pcap
rename to tests/dump/linux-sll-udp6.pcap
Binary files differ
diff --git a/tests/dump/ndndump.t.cpp b/tests/dump/ndndump.t.cpp
index b3f255e..e734f37 100644
--- a/tests/dump/ndndump.t.cpp
+++ b/tests/dump/ndndump.t.cpp
@@ -213,19 +213,63 @@
   }
 
   const std::string expected =
-    "1456768916.467099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n"
-    "1456768916.567099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n"
-    "1456768916.667099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n"
-    "1456768916.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, NACK: Congestion, /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n"
-    "1456768916.867099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n"
-    "1456768916.967099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n"
-    "1456768917.067099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n"
-    "1456768917.267099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: Congestion, /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n"
-    "1456768917.367099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n"
-    "1456768917.567099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n"
-    "1456768917.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=2002441365\n"
-    "1456768917.967099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n"
-    "1456768918.067099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n";
+    "1456768916.467099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, "
+    "INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n"
+    "1456768916.567099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, "
+    "INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n"
+    "1456768916.667099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: UDP, "
+    "INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n"
+    "1456768916.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, "
+    "NACK: Congestion, /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=2581361680\n"
+    "1456768916.867099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, "
+    "NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=4138343109\n"
+    "1456768916.967099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: UDP, "
+    "NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=4034910304\n"
+    "1456768917.067099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, "
+    "INTEREST: /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n"
+    "1456768917.267099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, "
+    "NACK: Congestion, /producer/nack/congestion?ndn.MustBeFresh=1&ndn.Nonce=3192497423\n"
+    "1456768917.367099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, "
+    "INTEREST: /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n"
+    "1456768917.567099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, "
+    "NACK: Duplicate, /producer/nack/duplicate?ndn.MustBeFresh=1&ndn.Nonce=522390724\n"
+    "1456768917.767099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, "
+    "NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=2002441365\n"
+    "1456768917.967099 From: 1.0.0.1, To: 1.0.0.2, Tunnel Type: TCP, "
+    "INTEREST: /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n"
+    "1456768918.067099 From: 1.0.0.2, To: 1.0.0.1, Tunnel Type: TCP, "
+    "NACK: None, /producer/nack/no-reason?ndn.MustBeFresh=1&ndn.Nonce=3776824408\n";
+  BOOST_CHECK(output.is_equal(expected));
+}
+
+BOOST_AUTO_TEST_CASE(InvalidTlvLength)
+{
+  dump.inputFile = "tests/dump/invalid-tlv-length.pcap";
+  dump.wantTimestamp = false;
+
+  {
+    StdCoutRedirector redirect(output);
+    dump.run();
+  }
+
+  const std::string expected =
+    "From: 128.196.203.36, To: 128.187.81.12, Tunnel Type: TCP, INVALID-NDNLPv2-PACKET: "
+    "TLV-LENGTH of sub-element of type 5 exceeds TLV-VALUE boundary of parent block\n";
+  BOOST_CHECK(output.is_equal(expected));
+}
+
+BOOST_AUTO_TEST_CASE(UnrecognizedLpField)
+{
+  dump.inputFile = "tests/dump/unrecognized-lp-field.pcap";
+  dump.wantTimestamp = false;
+
+  {
+    StdCoutRedirector redirect(output);
+    dump.run();
+  }
+
+  const std::string expected = "From: 128.196.203.36, To: 128.187.81.12, Tunnel Type: TCP, "
+                               "INVALID-NDNLPv2-PACKET: unrecognized field 4 cannot be ignored\n";
   BOOST_CHECK(output.is_equal(expected));
 }
 
diff --git a/tests/dump/unrecognized-lp-field.pcap b/tests/dump/unrecognized-lp-field.pcap
new file mode 100644
index 0000000..650eb5f
--- /dev/null
+++ b/tests/dump/unrecognized-lp-field.pcap
Binary files differ
diff --git a/tools/dump/ndndump.cpp b/tools/dump/ndndump.cpp
index 77743a8..4020d45 100644
--- a/tools/dump/ndndump.cpp
+++ b/tools/dump/ndndump.cpp
@@ -189,7 +189,13 @@
   Block netPacket;
 
   if (block.type() == lp::tlv::LpPacket) {
-    lpPacket = lp::Packet(block);
+    try {
+      lpPacket.wireDecode(block);
+    }
+    catch (const tlv::Error& e) {
+      std::cout << os.str() << ", " << "INVALID-NDNLPv2-PACKET: " << e.what() << std::endl;
+      return;
+    }
 
     Buffer::const_iterator begin, end;
     if (lpPacket.has<lp::FragmentField>()) {
@@ -237,7 +243,7 @@
     }
   }
   catch (const tlv::Error& e) {
-    std::cerr << e.what() << std::endl;
+    std::cout << os.str() << ", " << "INVALID-PACKET: " << e.what() << std::endl;
   }
 }