blob: df0cdf3e70509569695a586d3a265da691632914 [file] [log] [blame]
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08003 * See COPYING for copyright and distribution information.
4 */
5
6#include <boost/test/unit_test.hpp>
7
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008#include "interest.hpp"
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -08009
10using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080011namespace ndn {
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080012
13BOOST_AUTO_TEST_SUITE(TestInterest)
14
15const uint8_t Interest1[] = {
Alexander Afanasyev4b456282014-02-13 00:34:34 -080016 0x05, 0x41, // NDN Interest
17 0x07, 0x14, // Name
18 0x08, 0x5, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080019 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080020 0x08, 0x3, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080021 0x6e, 0x64, 0x6e,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080022 0x08, 0x6, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080023 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080024 0x09, 0x1f, // Selectors
25 0x0d, 0x1, 0x1, // MinSuffix
26 0x0e, 0x1, 0x1, // MaxSuffix
27 0x10, 0x14, // Exclude
28 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080029 0x61, 0x6c, 0x65, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080030 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080031 0x78, 0x78, 0x78, 0x78,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080032 0x13, 0x0, // Any
33 0x08, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080034 0x79, 0x79, 0x79, 0x79,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080035 0x11, 0x1, // ChildSelector
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080036 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080037 0x0a, 0x1, // Nonce
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080038 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080039 0x0b, 0x1, // Scope
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080040 0x1,
Alexander Afanasyev4b456282014-02-13 00:34:34 -080041 0x0c, // InterestLifetime
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080042 0x2, 0x3, 0xe8
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080043};
44
45BOOST_AUTO_TEST_CASE (Decode)
46{
47 Block interestBlock(Interest1, sizeof(Interest1));
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080048
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080049 ndn::Interest i;
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080050 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080051
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080052 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
53 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
54 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), 1000);
55 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
56 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
57 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
58 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
59 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
Alexander Afanasyev840139f2013-12-28 15:02:50 -080060 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080061}
62
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080063BOOST_AUTO_TEST_CASE (DecodeFromStream)
64{
65 boost::iostreams::stream<boost::iostreams::array_source> is (reinterpret_cast<const char *>(Interest1), sizeof(Interest1));
66
67 Block interestBlock(is);
68
69 ndn::Interest i;
70 BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock));
71
72 BOOST_REQUIRE_EQUAL(i.getName().toUri(), "/local/ndn/prefix");
73 BOOST_REQUIRE_EQUAL(i.getScope(), 1);
74 BOOST_REQUIRE_EQUAL(i.getInterestLifetime(), 1000);
75 BOOST_REQUIRE_EQUAL(i.getMinSuffixComponents(), 1);
76 BOOST_REQUIRE_EQUAL(i.getMaxSuffixComponents(), 1);
77 BOOST_REQUIRE_EQUAL(i.getChildSelector(), 1);
78 BOOST_REQUIRE_EQUAL(i.getMustBeFresh(), false);
79 BOOST_REQUIRE_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy");
80 BOOST_REQUIRE_EQUAL(i.getNonce(), 1);
81}
82
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080083BOOST_AUTO_TEST_CASE (Encode)
84{
85 ndn::Interest i(ndn::Name("/local/ndn/prefix"));
86 i.setScope(1);
87 i.setInterestLifetime(1000);
88 i.setMinSuffixComponents(1);
89 i.setMaxSuffixComponents(1);
90 i.setChildSelector(1);
91 i.setMustBeFresh(false);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080092 Exclude exclude;
93 exclude
Alexander Afanasyev52eb20d2014-02-06 18:25:54 -080094 .excludeOne(name::Component("alex"))
95 .excludeRange(name::Component("xxxx"), name::Component("yyyy"));
Alexander Afanasyevc348f832014-02-17 16:35:17 -080096 i.setExclude(exclude);
Alexander Afanasyev840139f2013-12-28 15:02:50 -080097 i.setNonce(1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080098
99 const Block &wire = i.wireEncode();
Alexander Afanasyev049f8f72013-12-26 19:07:15 -0800100 BOOST_REQUIRE_EQUAL_COLLECTIONS(Interest1, Interest1+sizeof(Interest1),
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -0800101 wire.begin(), wire.end());
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800102}
103
104BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800105
106} // namespace ndn