blob: b1390b84db5a8281639b8d3b7616f373a4a006dc [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/*
3 * Copyright (c) 2014-2018, 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"
29
30namespace nfd {
31namespace rib {
32namespace tests {
33
34using namespace nfd::tests;
35
36BOOST_FIXTURE_TEST_SUITE(TestRoute, BaseFixture)
37
Junxiao Shid47cd632018-09-11 03:10:00 +000038BOOST_FIXTURE_TEST_SUITE(CreateFromAnnouncement, UnitTestTimeFixture)
39
40BOOST_AUTO_TEST_CASE(NoValidity)
41{
42 auto pa = makePrefixAnn("/A", 212_s);
43 Route route(pa, 1815);
44 BOOST_CHECK_EQUAL(route.faceId, 1815);
45 BOOST_CHECK_EQUAL(route.origin, ndn::nfd::ROUTE_ORIGIN_PREFIXANN);
46 BOOST_CHECK_EQUAL(route.cost, 2048);
47 BOOST_CHECK_EQUAL(route.flags, ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
48 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
49 BOOST_REQUIRE(route.expires);
50 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
51}
52
53BOOST_AUTO_TEST_CASE(BeforeNotBefore)
54{
55 auto pa = makePrefixAnn("/A", 212_s, {10_s, 80_s});
56 Route route(pa, 1053);
57 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
58 BOOST_REQUIRE(route.expires);
59 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
60}
61
62BOOST_AUTO_TEST_CASE(AfterNotAfter)
63{
64 auto pa = makePrefixAnn("/A", 212_s, {-80_s, -10_s});
65 Route route(pa, 2972);
66 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
67 BOOST_REQUIRE(route.expires);
68 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
69}
70
71BOOST_AUTO_TEST_CASE(ExpirationLtValidity)
72{
73 auto pa = makePrefixAnn("/A", 212_s, {-100_s, 300_s});
74 Route route(pa, 7804);
75 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
76 BOOST_REQUIRE(route.expires);
77 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
78}
79
80BOOST_AUTO_TEST_CASE(ValidityLtExpiration)
81{
82 auto pa = makePrefixAnn("/A", 212_s, {-100_s, 200_s});
83 Route route(pa, 7804);
84 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 200_s);
85 BOOST_REQUIRE(route.expires);
86 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
87}
88
89BOOST_AUTO_TEST_SUITE_END() // CreateFromAnnouncement
90
Junxiao Shi3f21e582017-05-29 15:26:32 +000091BOOST_AUTO_TEST_CASE(Equality)
92{
93 Route a;
94 Route b;
95 BOOST_CHECK_EQUAL(a, b);
96
97 a.faceId = b.faceId = 15404;
98 a.origin = b.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
99 a.flags = b.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
100 a.cost = b.cost = 28826;
Junxiao Shid47cd632018-09-11 03:10:00 +0000101 a.expires = b.expires = time::steady_clock::now() + 26782232_ms;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000102 BOOST_CHECK_EQUAL(a, b);
103
104 b.faceId = 18559;
105 BOOST_CHECK_NE(a, b);
106 a.faceId = 18559;
107
108 b.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
109 BOOST_CHECK_NE(a, b);
110 a.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
111
112 b.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
113 BOOST_CHECK_NE(a, b);
114 a.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
115
116 b.cost = 103;
117 BOOST_CHECK_NE(a, b);
118 a.cost = 103;
119
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400120 b.expires = nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000121 BOOST_CHECK_NE(a, b);
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400122 a.expires = nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000123
124 BOOST_CHECK_EQUAL(a, b);
125}
126
Junxiao Shid47cd632018-09-11 03:10:00 +0000127BOOST_FIXTURE_TEST_CASE(EqualityAnn, UnitTestTimeFixture)
128{
129 auto ann1 = makePrefixAnn("/ann", 1_h);
130 auto ann2 = makePrefixAnn("/ann", 2_h);
131 BOOST_CHECK_EQUAL(Route(ann1, 7001), Route(ann1, 7001));
132 BOOST_CHECK_NE(Route(ann1, 7001), Route(ann1, 7002));
133 BOOST_CHECK_NE(Route(ann1, 7001), Route(ann2, 7001));
134}
135
Junxiao Shi3f21e582017-05-29 15:26:32 +0000136BOOST_FIXTURE_TEST_CASE(Output, UnitTestTimeFixture)
137{
138 Route r;
139 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
140 "Route(faceid: 0, origin: app, cost: 0, flags: 0x0, never expires)");
141
142 r.faceId = 4980;
143 r.origin = ndn::nfd::ROUTE_ORIGIN_STATIC;
144 r.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
145 r.cost = 2312;
Junxiao Shid47cd632018-09-11 03:10:00 +0000146 r.expires = time::steady_clock::now() + 791214234_ms;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000147 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
Junxiao Shid47cd632018-09-11 03:10:00 +0000148 "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, expires in: "
149 "791214234 milliseconds)");
Junxiao Shi3f21e582017-05-29 15:26:32 +0000150
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400151 r.expires = nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000152 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
153 "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, never expires)");
Junxiao Shid47cd632018-09-11 03:10:00 +0000154
155 r = Route(makePrefixAnn("/ann", 1_h), 3247);
156 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
157 "Route(faceid: 3247, origin: prefixann, cost: 2048, flags: 0x1, expires in: "
158 "3600000 milliseconds, announcement: (/ann expires=3600000 milliseconds))");
Junxiao Shi3f21e582017-05-29 15:26:32 +0000159}
160
161BOOST_AUTO_TEST_SUITE_END() // TestRoute
162
163} // namespace tests
164} // namespace rib
165} // namespace nfd