helpers: Several optimizations with command interest helpers

Both command interest helpers are now header only.   TestCommandInterest
is combined with TestSignedInterest as a second test case under the same
test suite.

Change-Id: Ieb80b24d76ded516a3d40770b65fcc1afd3e00a5
diff --git a/tests/security/test-signed-interest.cpp b/tests/security/test-signed-interest.cpp
index aef2530..80f55a4 100644
--- a/tests/security/test-signed-interest.cpp
+++ b/tests/security/test-signed-interest.cpp
@@ -8,14 +8,16 @@
 
 #include "security/key-chain.hpp"
 #include "security/validator.hpp"
-#include <iostream>
+
+#include "helpers/command-interest-generator.hpp"
+#include "helpers/command-interest-validator.hpp"
 
 using namespace std;
 namespace ndn {
 
 BOOST_AUTO_TEST_SUITE(TestSignedInterest)
 
-BOOST_AUTO_TEST_CASE (SignVerify)
+BOOST_AUTO_TEST_CASE (SignedInterest)
 {
   KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
 
@@ -39,6 +41,94 @@
   BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identityName));
 }
 
+class CommandInterestFixture
+{
+public:
+  CommandInterestFixture()
+    : m_validity(false)
+  {}
+  
+  void
+  validated(const shared_ptr<const Interest>& interest)
+  { m_validity = true; }
+
+  void
+  validationFailed(const shared_ptr<const Interest>& interest)
+  { m_validity = false; }
+
+  void
+  reset()
+  { m_validity = false; }
+
+  bool m_validity;
+}; 
+
+BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture)
+{
+  KeyChain keyChain;
+  Name identity("/TestCommandInterest/Validation");
+  Name certName;
+  BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
+
+  CommandInterestGenerator generator;
+  CommandInterestValidator validator;
+
+  validator.addInterestRule("^<TestCommandInterest><Validation>", *keyChain.getCertificate(certName));
+
+  //Test a legitimate command
+  shared_ptr<Interest> commandInterest1 = make_shared<Interest>("/TestCommandInterest/Validation/Command1");
+  generator.generateWithIdentity(*commandInterest1, identity);
+  validator.validate(*commandInterest1,
+  		     bind(&CommandInterestFixture::validated, this, _1),
+  		     bind(&CommandInterestFixture::validationFailed, this, _1));
+  
+  BOOST_CHECK_EQUAL(m_validity, true);
+  
+  //Test an outdated command
+  reset();
+  shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2");
+  int64_t timestamp = time::now() / 1000000;
+  timestamp -= 5000;
+  Name commandName = commandInterest2->getName();
+  commandName
+    .append(name::Component::fromNumber(timestamp))
+    .append(name::Component::fromNumber(random::generateWord64()));
+  commandInterest2->setName(commandName);
+  
+  keyChain.signByIdentity(*commandInterest2, identity);
+  validator.validate(*commandInterest2,
+  		     bind(&CommandInterestFixture::validated, this, _1),
+  		     bind(&CommandInterestFixture::validationFailed, this, _1));
+  
+  BOOST_CHECK_EQUAL(m_validity, false);
+  
+  //Test an unauthorized command
+  Name identity2("/TestCommandInterest/Validation2");
+  Name certName2;
+  BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
+  
+  shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3");
+  generator.generateWithIdentity(*commandInterest3, identity2);
+  validator.validate(*commandInterest3,
+  		     bind(&CommandInterestFixture::validated, this, _1),
+  		     bind(&CommandInterestFixture::validationFailed, this, _1));
+  
+  BOOST_CHECK_EQUAL(m_validity, false);
+
+  //Test another unauthorized command
+  shared_ptr<Interest> commandInterest4 = make_shared<Interest>("/TestCommandInterest/Validation2/Command");
+  generator.generateWithIdentity(*commandInterest4, identity);
+  validator.validate(*commandInterest4,
+  		     bind(&CommandInterestFixture::validated, this, _1),
+  		     bind(&CommandInterestFixture::validationFailed, this, _1));
+  
+  BOOST_CHECK_EQUAL(m_validity, false);
+
+  BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
+  BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2));
+}
+
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace ndn