blob: 46157a5ba26194a1499f8d94850e44a85be32452 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventocb065f12019-12-27 01:03:34 -05003 * Copyright (c) 2014-2019, The University of Memphis,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#include "tlv/lsa-info.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050023
24#include "tests/boost-test.hpp"
Nick Gordon9212bd42017-10-23 10:59:38 -050025#include "tests/mocks/lsa.hpp"
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080026
27namespace nlsr {
Nick Gordon727d4832017-10-13 18:04:25 -050028namespace tlv {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080029namespace test {
30
31BOOST_AUTO_TEST_SUITE(TlvTestLsaInfo)
32
33const uint8_t LsaInfoData[] =
34{
35 // Header
36 0x80, 0x21,
37 // OriginRouter
38 0x81, 0x18, 0x07, 0x16, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x08, 0x03, 0x6c, 0x73,
39 0x61, 0x08, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x08, 0x03, 0x74, 0x6c, 0x76,
40 // SequenceNumber
41 0x82, 0x01, 0x80,
42 // ExpirationPeriod
43 0x8b, 0x02, 0x27, 0x10
44};
45
46const uint8_t LsaInfoDataInfiniteExpirationPeriod[] =
47{
48 // Header
49 0x80, 0x1d,
50 // OriginRouter
51 0x81, 0x18, 0x07, 0x16, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x08, 0x03, 0x6c, 0x73,
52 0x61, 0x08, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x08, 0x03, 0x74, 0x6c, 0x76,
53 // SequenceNumber
54 0x82, 0x1, 0x80
55};
56
57BOOST_AUTO_TEST_CASE(LsaInfoEncode)
58{
59 LsaInfo lsaInfo;
60 lsaInfo.setOriginRouter("/test/lsa/info/tlv");
61 lsaInfo.setSequenceNumber(128);
62 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
63
64 const ndn::Block& wire = lsaInfo.wireEncode();
65
66 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsaInfoData,
67 LsaInfoData + sizeof(LsaInfoData),
68 wire.begin(), wire.end());
69}
70
71BOOST_AUTO_TEST_CASE(LsaInfoDecode)
72{
73 LsaInfo lsaInfo;
74
75 lsaInfo.wireDecode(ndn::Block(LsaInfoData, sizeof(LsaInfoData)));
76
77 ndn::Name originRouter("/test/lsa/info/tlv");
78 BOOST_REQUIRE_EQUAL(lsaInfo.getOriginRouter(), originRouter);
79 BOOST_REQUIRE_EQUAL(lsaInfo.getSequenceNumber(), 128);
80 BOOST_REQUIRE_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
81 BOOST_REQUIRE_EQUAL(lsaInfo.hasInfiniteExpirationPeriod(), false);
82}
83
84BOOST_AUTO_TEST_CASE(LsaInfoInfiniteExpirationPeriodEncode)
85{
86 LsaInfo lsaInfo;
87 lsaInfo.setOriginRouter("/test/lsa/info/tlv");
88 lsaInfo.setSequenceNumber(128);
89 lsaInfo.setExpirationPeriod(LsaInfo::INFINITE_EXPIRATION_PERIOD);
90
91 const ndn::Block& wire = lsaInfo.wireEncode();
92
93 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsaInfoDataInfiniteExpirationPeriod,
94 LsaInfoDataInfiniteExpirationPeriod +
95 sizeof(LsaInfoDataInfiniteExpirationPeriod),
96 wire.begin(), wire.end());
97}
98
99BOOST_AUTO_TEST_CASE(LsaInfoInfiniteExpirationPeriodDecode)
100{
101 LsaInfo lsaInfo;
102
103 lsaInfo.wireDecode(ndn::Block(LsaInfoDataInfiniteExpirationPeriod,
104 sizeof(LsaInfoDataInfiniteExpirationPeriod)));
105
106 ndn::Name originRouter("/test/lsa/info/tlv");
107 BOOST_REQUIRE_EQUAL(lsaInfo.getOriginRouter(), originRouter);
108 BOOST_REQUIRE_EQUAL(lsaInfo.getSequenceNumber(), 128);
109 BOOST_REQUIRE_EQUAL(lsaInfo.getExpirationPeriod(), LsaInfo::INFINITE_EXPIRATION_PERIOD);
110 BOOST_REQUIRE_EQUAL(lsaInfo.hasInfiniteExpirationPeriod(), true);
111}
112
113BOOST_AUTO_TEST_CASE(LsaInfoOutputStream)
114{
115 LsaInfo lsaInfo;
116 lsaInfo.setOriginRouter("/test/lsa/info/tlv");
117 lsaInfo.setSequenceNumber(128);
118 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
119
120 std::ostringstream os;
121 os << lsaInfo;
122
123 BOOST_CHECK_EQUAL(os.str(), "LsaInfo(OriginRouter: /test/lsa/info/tlv, SequenceNumber: 128, "
124 "ExpirationPeriod: 10000 milliseconds)");
125}
126
Jiewen Tana0497d82015-02-02 21:59:18 -0800127BOOST_AUTO_TEST_CASE(LsaInfoMake)
128{
Nick Gordon9212bd42017-10-23 10:59:38 -0500129 nlsr::test::MockLsa lsa;
Jiewen Tana0497d82015-02-02 21:59:18 -0800130 lsa.setOrigRouter("/test/lsa/info/tlv");
131 lsa.setLsSeqNo(128);
132 lsa.setExpirationTimePoint(ndn::time::system_clock::now());
133
134 std::shared_ptr<LsaInfo> lsaInfo = makeLsaInfo(lsa);
135 BOOST_CHECK_EQUAL(lsaInfo->getOriginRouter(), lsa.getOrigRouter());
136 BOOST_CHECK_EQUAL(lsaInfo->getSequenceNumber(), lsa.getLsSeqNo());
137 BOOST_CHECK_LE(lsaInfo->getExpirationPeriod(), ndn::time::milliseconds(0));
138}
139
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800140BOOST_AUTO_TEST_SUITE_END()
141
142} // namespace test
143} // namespace tlv
144} // namespace nlsr