blob: 254bd48fdf066cacfc4c686ab88d7c92334c50d5 [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 Afanasyev6835ad82014-02-12 10:07:20 -080016 0x3, 0x41, // NDN Interest
17 0x1, 0x14, // Name
18 0x2, 0x5, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080019 0x6c, 0x6f, 0x63, 0x61, 0x6c,
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080020 0x2, 0x3, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080021 0x6e, 0x64, 0x6e,
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080022 0x2, 0x6, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080023 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
24 0x5, 0x1f, // Selectors
25 0x09, 0x1, 0x1, // MinSuffix
26 0x0a, 0x1, 0x1, // MaxSuffix
27 0x0c, 0x14, // Exclude
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080028 0x2, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080029 0x61, 0x6c, 0x65, 0x78,
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080030 0x2, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080031 0x78, 0x78, 0x78, 0x78,
32 0xf, 0x0, // Any
Alexander Afanasyev6835ad82014-02-12 10:07:20 -080033 0x2, 0x4, // NameComponent
Alexander Afanasyev636e9f12014-01-07 12:01:03 -080034 0x79, 0x79, 0x79, 0x79,
35 0x0d, 0x1, // ChildSelector
36 0x1,
37 0x6, 0x1, // Nonce
38 0x1,
39 0x7, 0x1, // Scope
40 0x1,
41 0x8, // InterestLifetime
42 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 Afanasyev52eb20d2014-02-06 18:25:54 -080092 i.getExclude()
93 .excludeOne(name::Component("alex"))
94 .excludeRange(name::Component("xxxx"), name::Component("yyyy"));
Alexander Afanasyev840139f2013-12-28 15:02:50 -080095 i.setNonce(1);
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -080096
97 const Block &wire = i.wireEncode();
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080098 BOOST_REQUIRE_EQUAL_COLLECTIONS(Interest1, Interest1+sizeof(Interest1),
Alexander Afanasyev84c2bd42014-01-09 22:35:35 -080099 wire.begin(), wire.end());
Alexander Afanasyev5fa9e9a2013-12-24 19:45:07 -0800100}
101
102BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800103
104} // namespace ndn