blob: 910e641be713412663ba0802f17f0d48f51e6ecb [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 Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tests {
Junxiao Shi3f21e582017-05-29 15:26:32 +000032
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033using rib::Route;
Junxiao Shi3f21e582017-05-29 15:26:32 +000034
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040035BOOST_FIXTURE_TEST_SUITE(TestRoute, GlobalIoTimeFixture)
Junxiao Shi3f21e582017-05-29 15:26:32 +000036
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040037BOOST_AUTO_TEST_SUITE(CreateFromAnnouncement)
Junxiao Shid47cd632018-09-11 03:10:00 +000038
39BOOST_AUTO_TEST_CASE(NoValidity)
40{
41 auto pa = makePrefixAnn("/A", 212_s);
42 Route route(pa, 1815);
43 BOOST_CHECK_EQUAL(route.faceId, 1815);
44 BOOST_CHECK_EQUAL(route.origin, ndn::nfd::ROUTE_ORIGIN_PREFIXANN);
45 BOOST_CHECK_EQUAL(route.cost, 2048);
46 BOOST_CHECK_EQUAL(route.flags, ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
47 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
48 BOOST_REQUIRE(route.expires);
49 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
50}
51
52BOOST_AUTO_TEST_CASE(BeforeNotBefore)
53{
54 auto pa = makePrefixAnn("/A", 212_s, {10_s, 80_s});
55 Route route(pa, 1053);
56 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
57 BOOST_REQUIRE(route.expires);
58 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
59}
60
61BOOST_AUTO_TEST_CASE(AfterNotAfter)
62{
63 auto pa = makePrefixAnn("/A", 212_s, {-80_s, -10_s});
64 Route route(pa, 2972);
65 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
66 BOOST_REQUIRE(route.expires);
67 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
68}
69
70BOOST_AUTO_TEST_CASE(ExpirationLtValidity)
71{
72 auto pa = makePrefixAnn("/A", 212_s, {-100_s, 300_s});
73 Route route(pa, 7804);
74 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
75 BOOST_REQUIRE(route.expires);
76 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
77}
78
79BOOST_AUTO_TEST_CASE(ValidityLtExpiration)
80{
81 auto pa = makePrefixAnn("/A", 212_s, {-100_s, 200_s});
82 Route route(pa, 7804);
83 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 200_s);
84 BOOST_REQUIRE(route.expires);
85 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
86}
87
88BOOST_AUTO_TEST_SUITE_END() // CreateFromAnnouncement
89
Junxiao Shi3f21e582017-05-29 15:26:32 +000090BOOST_AUTO_TEST_CASE(Equality)
91{
92 Route a;
93 Route b;
94 BOOST_CHECK_EQUAL(a, b);
95
96 a.faceId = b.faceId = 15404;
97 a.origin = b.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
98 a.flags = b.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
99 a.cost = b.cost = 28826;
Junxiao Shid47cd632018-09-11 03:10:00 +0000100 a.expires = b.expires = time::steady_clock::now() + 26782232_ms;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000101 BOOST_CHECK_EQUAL(a, b);
102
103 b.faceId = 18559;
104 BOOST_CHECK_NE(a, b);
105 a.faceId = 18559;
106
107 b.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
108 BOOST_CHECK_NE(a, b);
109 a.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
110
111 b.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
112 BOOST_CHECK_NE(a, b);
113 a.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
114
115 b.cost = 103;
116 BOOST_CHECK_NE(a, b);
117 a.cost = 103;
118
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400119 b.expires = std::nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000120 BOOST_CHECK_NE(a, b);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400121 a.expires = std::nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000122
123 BOOST_CHECK_EQUAL(a, b);
124}
125
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400126BOOST_AUTO_TEST_CASE(EqualityAnn)
Junxiao Shid47cd632018-09-11 03:10:00 +0000127{
128 auto ann1 = makePrefixAnn("/ann", 1_h);
129 auto ann2 = makePrefixAnn("/ann", 2_h);
130 BOOST_CHECK_EQUAL(Route(ann1, 7001), Route(ann1, 7001));
131 BOOST_CHECK_NE(Route(ann1, 7001), Route(ann1, 7002));
132 BOOST_CHECK_NE(Route(ann1, 7001), Route(ann2, 7001));
133}
134
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400135BOOST_AUTO_TEST_CASE(Output)
Junxiao Shi3f21e582017-05-29 15:26:32 +0000136{
137 Route r;
138 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
139 "Route(faceid: 0, origin: app, cost: 0, flags: 0x0, never expires)");
140
141 r.faceId = 4980;
142 r.origin = ndn::nfd::ROUTE_ORIGIN_STATIC;
143 r.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
144 r.cost = 2312;
Junxiao Shid47cd632018-09-11 03:10:00 +0000145 r.expires = time::steady_clock::now() + 791214234_ms;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000146 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
Junxiao Shid47cd632018-09-11 03:10:00 +0000147 "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, expires in: "
148 "791214234 milliseconds)");
Junxiao Shi3f21e582017-05-29 15:26:32 +0000149
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400150 r.expires = std::nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000151 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
152 "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, never expires)");
Junxiao Shid47cd632018-09-11 03:10:00 +0000153
154 r = Route(makePrefixAnn("/ann", 1_h), 3247);
155 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
156 "Route(faceid: 3247, origin: prefixann, cost: 2048, flags: 0x1, expires in: "
157 "3600000 milliseconds, announcement: (/ann expires=3600000 milliseconds))");
Junxiao Shi3f21e582017-05-29 15:26:32 +0000158}
159
160BOOST_AUTO_TEST_SUITE_END() // TestRoute
161
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400162} // namespace nfd::tests