table: limit prefix length in Measurements

refs #4262

Change-Id: Ife04f8df1fb833b477d055a1cd1f37d2af474e7f
diff --git a/daemon/table/measurements.cpp b/daemon/table/measurements.cpp
index 3830c10..2ed0f65 100644
--- a/daemon/table/measurements.cpp
+++ b/daemon/table/measurements.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -40,6 +40,8 @@
 Entry&
 Measurements::get(name_tree::Entry& nte)
 {
+  BOOST_ASSERT(nte.getName().size() <= NameTree::getMaxDepth());
+
   Entry* entry = nte.getMeasurementsEntry();
   if (entry != nullptr) {
     return *entry;
@@ -59,21 +61,21 @@
 Entry&
 Measurements::get(const Name& name)
 {
-  name_tree::Entry& nte = m_nameTree.lookup(name);
+  name_tree::Entry& nte = m_nameTree.lookup(name, true);
   return this->get(nte);
 }
 
 Entry&
 Measurements::get(const fib::Entry& fibEntry)
 {
-  name_tree::Entry& nte = m_nameTree.lookup(fibEntry);
+  name_tree::Entry& nte = m_nameTree.lookup(fibEntry.getPrefix(), true);
   return this->get(nte);
 }
 
 Entry&
 Measurements::get(const pit::Entry& pitEntry)
 {
-  name_tree::Entry& nte = m_nameTree.lookup(pitEntry);
+  name_tree::Entry& nte = m_nameTree.lookup(pitEntry.getName(), true);
   return this->get(nte);
 }
 
@@ -108,13 +110,13 @@
 Entry*
 Measurements::findLongestPrefixMatch(const Name& name, const EntryPredicate& pred) const
 {
-  return this->findLongestPrefixMatchImpl(name, pred);
+  return this->findLongestPrefixMatchImpl(name.getPrefix(NameTree::getMaxDepth()), pred);
 }
 
 Entry*
 Measurements::findLongestPrefixMatch(const pit::Entry& pitEntry, const EntryPredicate& pred) const
 {
-  return this->findLongestPrefixMatchImpl(pitEntry, pred);
+  return this->findLongestPrefixMatch(pitEntry.getName(), pred);
 }
 
 Entry*
diff --git a/tests/daemon/table/measurements.t.cpp b/tests/daemon/table/measurements.t.cpp
index 539c409..a7e315d 100644
--- a/tests/daemon/table/measurements.t.cpp
+++ b/tests/daemon/table/measurements.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -69,6 +69,24 @@
   BOOST_CHECK_EQUAL(&entry0, entry0c);
 }
 
+BOOST_AUTO_TEST_CASE(GetLongName)
+{
+  Name n;
+  while (n.size() < NameTree::getMaxDepth() - 1) {
+    n.append("A");
+  }
+  Entry& entry1 = measurements.get(n);
+  BOOST_CHECK_EQUAL(entry1.getName().size(), NameTree::getMaxDepth() - 1);
+
+  n.append("B");
+  Entry& entry2 = measurements.get(n);
+  BOOST_CHECK_EQUAL(entry2.getName().size(), NameTree::getMaxDepth());
+
+  n.append("C");
+  Entry& entry3 = measurements.get(n);
+  BOOST_CHECK_EQUAL(entry3.getName().size(), NameTree::getMaxDepth());
+}
+
 BOOST_AUTO_TEST_CASE(GetWithFibEntry)
 {
   Fib fib(nameTree);