blob: b6e0ddbb7c0ecc81df1e2f9ecfbfb44a5d2a0c9d [file] [log] [blame]
Junxiao Shi3f21e582017-05-29 15:26:32 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi3f21e582017-05-29 15:26:32 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "rib/route.hpp"
Davide Pesavento87fc0f82018-04-11 23:43:51 -040027
Junxiao Shi3f21e582017-05-29 15:26:32 +000028#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi3f21e582017-05-29 15:26:32 +000030
31namespace nfd {
32namespace rib {
33namespace tests {
34
35using namespace nfd::tests;
36
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040037BOOST_FIXTURE_TEST_SUITE(TestRoute, GlobalIoTimeFixture)
Junxiao Shi3f21e582017-05-29 15:26:32 +000038
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040039BOOST_AUTO_TEST_SUITE(CreateFromAnnouncement)
Junxiao Shid47cd632018-09-11 03:10:00 +000040
41BOOST_AUTO_TEST_CASE(NoValidity)
42{
43 auto pa = makePrefixAnn("/A", 212_s);
44 Route route(pa, 1815);
45 BOOST_CHECK_EQUAL(route.faceId, 1815);
46 BOOST_CHECK_EQUAL(route.origin, ndn::nfd::ROUTE_ORIGIN_PREFIXANN);
47 BOOST_CHECK_EQUAL(route.cost, 2048);
48 BOOST_CHECK_EQUAL(route.flags, ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
49 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
50 BOOST_REQUIRE(route.expires);
51 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
52}
53
54BOOST_AUTO_TEST_CASE(BeforeNotBefore)
55{
56 auto pa = makePrefixAnn("/A", 212_s, {10_s, 80_s});
57 Route route(pa, 1053);
58 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
59 BOOST_REQUIRE(route.expires);
60 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
61}
62
63BOOST_AUTO_TEST_CASE(AfterNotAfter)
64{
65 auto pa = makePrefixAnn("/A", 212_s, {-80_s, -10_s});
66 Route route(pa, 2972);
67 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
68 BOOST_REQUIRE(route.expires);
69 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
70}
71
72BOOST_AUTO_TEST_CASE(ExpirationLtValidity)
73{
74 auto pa = makePrefixAnn("/A", 212_s, {-100_s, 300_s});
75 Route route(pa, 7804);
76 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
77 BOOST_REQUIRE(route.expires);
78 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
79}
80
81BOOST_AUTO_TEST_CASE(ValidityLtExpiration)
82{
83 auto pa = makePrefixAnn("/A", 212_s, {-100_s, 200_s});
84 Route route(pa, 7804);
85 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 200_s);
86 BOOST_REQUIRE(route.expires);
87 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
88}
89
90BOOST_AUTO_TEST_SUITE_END() // CreateFromAnnouncement
91
Junxiao Shi3f21e582017-05-29 15:26:32 +000092BOOST_AUTO_TEST_CASE(Equality)
93{
94 Route a;
95 Route b;
96 BOOST_CHECK_EQUAL(a, b);
97
98 a.faceId = b.faceId = 15404;
99 a.origin = b.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
100 a.flags = b.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
101 a.cost = b.cost = 28826;
Junxiao Shid47cd632018-09-11 03:10:00 +0000102 a.expires = b.expires = time::steady_clock::now() + 26782232_ms;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000103 BOOST_CHECK_EQUAL(a, b);
104
105 b.faceId = 18559;
106 BOOST_CHECK_NE(a, b);
107 a.faceId = 18559;
108
109 b.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
110 BOOST_CHECK_NE(a, b);
111 a.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
112
113 b.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
114 BOOST_CHECK_NE(a, b);
115 a.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
116
117 b.cost = 103;
118 BOOST_CHECK_NE(a, b);
119 a.cost = 103;
120
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400121 b.expires = nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000122 BOOST_CHECK_NE(a, b);
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400123 a.expires = nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000124
125 BOOST_CHECK_EQUAL(a, b);
126}
127
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400128BOOST_AUTO_TEST_CASE(EqualityAnn)
Junxiao Shid47cd632018-09-11 03:10:00 +0000129{
130 auto ann1 = makePrefixAnn("/ann", 1_h);
131 auto ann2 = makePrefixAnn("/ann", 2_h);
132 BOOST_CHECK_EQUAL(Route(ann1, 7001), Route(ann1, 7001));
133 BOOST_CHECK_NE(Route(ann1, 7001), Route(ann1, 7002));
134 BOOST_CHECK_NE(Route(ann1, 7001), Route(ann2, 7001));
135}
136
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400137BOOST_AUTO_TEST_CASE(Output)
Junxiao Shi3f21e582017-05-29 15:26:32 +0000138{
139 Route r;
140 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
141 "Route(faceid: 0, origin: app, cost: 0, flags: 0x0, never expires)");
142
143 r.faceId = 4980;
144 r.origin = ndn::nfd::ROUTE_ORIGIN_STATIC;
145 r.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
146 r.cost = 2312;
Junxiao Shid47cd632018-09-11 03:10:00 +0000147 r.expires = time::steady_clock::now() + 791214234_ms;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000148 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
Junxiao Shid47cd632018-09-11 03:10:00 +0000149 "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, expires in: "
150 "791214234 milliseconds)");
Junxiao Shi3f21e582017-05-29 15:26:32 +0000151
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400152 r.expires = nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000153 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
154 "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, never expires)");
Junxiao Shid47cd632018-09-11 03:10:00 +0000155
156 r = Route(makePrefixAnn("/ann", 1_h), 3247);
157 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
158 "Route(faceid: 3247, origin: prefixann, cost: 2048, flags: 0x1, expires in: "
159 "3600000 milliseconds, announcement: (/ann expires=3600000 milliseconds))");
Junxiao Shi3f21e582017-05-29 15:26:32 +0000160}
161
162BOOST_AUTO_TEST_SUITE_END() // TestRoute
163
164} // namespace tests
165} // namespace rib
166} // namespace nfd