TEST_F(TestLink, DecodeInterestWithLink) { Interest interest; interest.wireDecode(Blob(InterestWithLink, sizeof(InterestWithLink))); Link* link = interest.getLink(); ASSERT_TRUE(link) << "Interest link object not specified"; ASSERT_EQ(Name("/local/ndn/prefix"), link->getName()); const DelegationSet& delegations = link->getDelegations(); for (size_t i = 0; i < delegations.size(); ++i) { if (i == 0) { ASSERT_EQ(10, delegations.get(i).getPreference()); ASSERT_EQ(Name("local"), delegations.get(i).getName()); } if (i == 1) { ASSERT_EQ(20, delegations.get(i).getPreference()); ASSERT_EQ(Name("ndn"), delegations.get(i).getName()); } } ASSERT_TRUE(interest.getSelectedDelegationIndex() >= 0) << "Interest selected delegation not specified"; ASSERT_EQ (Name("local"), delegations.get(interest.getSelectedDelegationIndex()).getName()); }
TEST_F(TestInterestDump, Redecode) { // check that we encode and decode correctly Blob encoding = referenceInterest.wireEncode(); Interest reDecodedInterest; reDecodedInterest.wireDecode(encoding); vector<string> redecodedDump = dumpInterest(reDecodedInterest); ASSERT_EQ(initialDump, redecodedDump) << "Re-decoded interest does not match original"; }
TEST_F(TestLink, SelectedDelegationIsNotNonNegativeInteger) { Interest interest; ASSERT_THROW (interest.wireDecode (Blob(InterestWithLinkNotNonIntegerSelectedDelegation, sizeof(InterestWithLinkNotNonIntegerSelectedDelegation))), runtime_error) << "Expected error decoding an interest with selected delegation not a non-negative integer"; }
TEST_F(TestLink, InterestContainingSelectedDelegationButNoLink) { Interest interest; ASSERT_THROW (interest.wireDecode (Blob(InterestWithSelectedDelegationButNoLink, sizeof(InterestWithSelectedDelegationButNoLink))), runtime_error) << "Expected error decoding an interest with a selected delegation but no link"; }
TEST_F(TestLink, InterestLinkObjectWrongContentType) { Interest interest; interest.wireDecode (Blob(InterestWithLinkWrongContentType, sizeof(InterestWithLinkWrongContentType))); ASSERT_THROW (interest.getLink(), runtime_error) << "Expected error decoding a link with the wrong content type"; }
TEST_F(TestLink, InterestLinkObjectNoMetaInfo) { Interest interest; interest.wireDecode (Blob(InterestWithLinkNoMetaInfo, sizeof(InterestWithLinkNoMetaInfo))); ASSERT_THROW (interest.getLink(), runtime_error) << "Expected error decoding a link with no meta info"; }
TEST_F(TestInterestDump, CreateFresh) { Interest freshInterest = createFreshInterest(); vector<string> freshDump = dumpInterest(freshInterest); ASSERT_TRUE(interestDumpsEqual(initialDump, freshDump)) << "Fresh interest does not match original"; Interest reDecodedFreshInterest; reDecodedFreshInterest.wireDecode(freshInterest.wireEncode()); vector<string> reDecodedFreshDump = dumpInterest(reDecodedFreshInterest); ASSERT_TRUE(interestDumpsEqual(freshDump, reDecodedFreshDump)) << "Redecoded fresh interest does not match original"; }
TEST_F(TestLink, DecodeInterestWithLinkNonDecreasingOrder) { Interest interest; interest.wireDecode(Blob (InterestWithLinkNonDecreasingOrder, sizeof(InterestWithLinkNonDecreasingOrder))); Link* link = interest.getLink(); ASSERT_TRUE(interest.getSelectedDelegationIndex() >= 0); ASSERT_EQ (Name("ndn"), link->getDelegations().get(interest.getSelectedDelegationIndex()).getName()); }
TEST_F(TestInterestDump, RedecodeImplicitDigestExclude) { // Check that we encode and decode correctly with an implicit digest exclude. Interest interest(Name("/A")); interest.getExclude().appendComponent(Name("/sha256digest=" "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").get(0)); vector<string> dump = dumpInterest(interest); Blob encoding = interest.wireEncode(); Interest reDecodedInterest; reDecodedInterest.wireDecode(encoding); vector<string> redecodedDump = dumpInterest(reDecodedInterest); ASSERT_TRUE(interestDumpsEqual(dump, redecodedDump)) << "Re-decoded interest does not match original"; }
TEST_F(TestLink, EncodeDecodeInterestWithLink) { Link link1; link1.setName(Name("test")); link1.addDelegation(10, Name("/test1")); link1.addDelegation(20, Name("/test2")); link1.addDelegation(100, Name("/test3")); keyChain_->sign(link1, certificateName_); Interest interestA; interestA.setName(Name("/Test/Encode/Decode/With/Link")); interestA.setChildSelector(1); interestA.setInterestLifetimeMilliseconds(10000); interestA.setLinkWireEncoding(link1.wireEncode()); Blob interestEncoding = interestA.wireEncode(); Interest interestB; interestB.wireDecode(interestEncoding); ASSERT_EQ(interestA.getName(), interestB.getName()); Link* link2 = interestB.getLink(); ASSERT_TRUE(link2) << "Interest link object not specified"; const DelegationSet& delegations = link2->getDelegations(); for (size_t i = 0; i < delegations.size(); ++i) { if (i == 0) { ASSERT_EQ(10, delegations.get(i).getPreference()); ASSERT_EQ(Name("/test1"), delegations.get(i).getName()); } if (i == 1) { ASSERT_EQ(20, delegations.get(i).getPreference()); ASSERT_EQ(Name("/test2"), delegations.get(i).getName()); } if (i == 2) { ASSERT_EQ(100, delegations.get(i).getPreference()); ASSERT_EQ(Name("/test3"), delegations.get(i).getName()); } } }
int main(int argc, char** argv) { try { Interest interest; interest.wireDecode(TlvInterest, sizeof(TlvInterest)); cout << "Interest:" << endl; dumpInterest(interest); // Set the name again to clear the cached encoding so we encode again. interest.setName(interest.getName()); Blob encoding = interest.wireEncode(); cout << endl << "Re-encoded interest " << encoding.toHex() << endl; Interest reDecodedInterest; reDecodedInterest.wireDecode(encoding); cout << "Re-decoded Interest:" << endl; dumpInterest(reDecodedInterest); Interest freshInterest(Name("/ndn/abc")); freshInterest.setMinSuffixComponents(4) .setMaxSuffixComponents(6) .setInterestLifetimeMilliseconds(30000) .setChildSelector(1) .setMustBeFresh(true); freshInterest.getKeyLocator().setType(ndn_KeyLocatorType_KEY_LOCATOR_DIGEST); uint8_t digest[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F }; freshInterest.getKeyLocator().setKeyData(Blob(digest, sizeof(digest))); freshInterest.getExclude().appendComponent(Name("abc")[0]).appendAny(); ptr_lib::shared_ptr<MemoryIdentityStorage> identityStorage (new MemoryIdentityStorage()); ptr_lib::shared_ptr<MemoryPrivateKeyStorage> privateKeyStorage (new MemoryPrivateKeyStorage()); KeyChain keyChain (ptr_lib::make_shared<IdentityManager>(identityStorage, privateKeyStorage), ptr_lib::make_shared<SelfVerifyPolicyManager>(identityStorage.get())); // Initialize the storage. Name keyName("/testname/DSK-123"); Name certificateName = keyName.getSubName(0, keyName.size() - 1).append ("KEY").append(keyName[-1]).append("ID-CERT").append("0"); identityStorage->addKey (keyName, KEY_TYPE_RSA, Blob(DEFAULT_RSA_PUBLIC_KEY_DER, sizeof(DEFAULT_RSA_PUBLIC_KEY_DER))); privateKeyStorage->setKeyPairForKeyName (keyName, KEY_TYPE_RSA, DEFAULT_RSA_PUBLIC_KEY_DER, sizeof(DEFAULT_RSA_PUBLIC_KEY_DER), DEFAULT_RSA_PRIVATE_KEY_DER, sizeof(DEFAULT_RSA_PRIVATE_KEY_DER)); // Make a Face just so that we can sign the interest. Face face("localhost"); face.setCommandSigningInfo(keyChain, certificateName); face.makeCommandInterest(freshInterest); ptr_lib::shared_ptr<Interest> reDecodedFreshInterest(new Interest()); reDecodedFreshInterest->wireDecode(freshInterest.wireEncode()); cout << endl << "Re-decoded fresh Interest:" << endl; dumpInterest(*reDecodedFreshInterest); keyChain.verifyInterest (reDecodedFreshInterest, bind(&onVerified, "Freshly-signed Interest", _1), bind(&onVerifyFailed, "Freshly-signed Interest", _1)); } catch (std::exception& e) { cout << "exception: " << e.what() << endl; } return 0; }