blob: 7133628334cdcf8157af2539be39190fa993735b [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 Pesaventocaa60cc2024-02-18 18:18:37 -05003 * Copyright (c) 2014-2024, 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 Pesaventoa9b09b62022-06-04 14:07:25 -040031#include <boost/lexical_cast.hpp>
32
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Junxiao Shi3f21e582017-05-29 15:26:32 +000034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035using rib::Route;
Junxiao Shi3f21e582017-05-29 15:26:32 +000036
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050037BOOST_AUTO_TEST_SUITE(Rib)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040038BOOST_FIXTURE_TEST_SUITE(TestRoute, GlobalIoTimeFixture)
Junxiao Shi3f21e582017-05-29 15:26:32 +000039
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040040BOOST_AUTO_TEST_SUITE(CreateFromAnnouncement)
Junxiao Shid47cd632018-09-11 03:10:00 +000041
42BOOST_AUTO_TEST_CASE(NoValidity)
43{
44 auto pa = makePrefixAnn("/A", 212_s);
45 Route route(pa, 1815);
46 BOOST_CHECK_EQUAL(route.faceId, 1815);
47 BOOST_CHECK_EQUAL(route.origin, ndn::nfd::ROUTE_ORIGIN_PREFIXANN);
48 BOOST_CHECK_EQUAL(route.cost, 2048);
49 BOOST_CHECK_EQUAL(route.flags, ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
50 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
51 BOOST_REQUIRE(route.expires);
52 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
53}
54
55BOOST_AUTO_TEST_CASE(BeforeNotBefore)
56{
Davide Pesaventod6ea0b12023-03-13 21:35:03 -040057 auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(10_s, 80_s));
Junxiao Shid47cd632018-09-11 03:10:00 +000058 Route route(pa, 1053);
59 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
60 BOOST_REQUIRE(route.expires);
61 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
62}
63
64BOOST_AUTO_TEST_CASE(AfterNotAfter)
65{
Davide Pesaventod6ea0b12023-03-13 21:35:03 -040066 auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(-80_s, -10_s));
Junxiao Shid47cd632018-09-11 03:10:00 +000067 Route route(pa, 2972);
68 BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
69 BOOST_REQUIRE(route.expires);
70 BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
71}
72
73BOOST_AUTO_TEST_CASE(ExpirationLtValidity)
74{
Davide Pesaventod6ea0b12023-03-13 21:35:03 -040075 auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(-100_s, 300_s));
Junxiao Shid47cd632018-09-11 03:10:00 +000076 Route route(pa, 7804);
77 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
78 BOOST_REQUIRE(route.expires);
79 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
80}
81
82BOOST_AUTO_TEST_CASE(ValidityLtExpiration)
83{
Davide Pesaventod6ea0b12023-03-13 21:35:03 -040084 auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(-100_s, 200_s));
Junxiao Shid47cd632018-09-11 03:10:00 +000085 Route route(pa, 7804);
86 BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 200_s);
87 BOOST_REQUIRE(route.expires);
88 BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
89}
90
91BOOST_AUTO_TEST_SUITE_END() // CreateFromAnnouncement
92
Junxiao Shi3f21e582017-05-29 15:26:32 +000093BOOST_AUTO_TEST_CASE(Equality)
94{
95 Route a;
96 Route b;
97 BOOST_CHECK_EQUAL(a, b);
98
99 a.faceId = b.faceId = 15404;
100 a.origin = b.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
101 a.flags = b.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
102 a.cost = b.cost = 28826;
Junxiao Shid47cd632018-09-11 03:10:00 +0000103 a.expires = b.expires = time::steady_clock::now() + 26782232_ms;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000104 BOOST_CHECK_EQUAL(a, b);
105
106 b.faceId = 18559;
107 BOOST_CHECK_NE(a, b);
108 a.faceId = 18559;
109
110 b.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
111 BOOST_CHECK_NE(a, b);
112 a.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT;
113
114 b.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
115 BOOST_CHECK_NE(a, b);
116 a.flags = ndn::nfd::ROUTE_FLAG_CAPTURE;
117
118 b.cost = 103;
119 BOOST_CHECK_NE(a, b);
120 a.cost = 103;
121
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400122 b.expires = std::nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000123 BOOST_CHECK_NE(a, b);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400124 a.expires = std::nullopt;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000125
126 BOOST_CHECK_EQUAL(a, b);
Junxiao Shi3f21e582017-05-29 15:26:32 +0000127
Junxiao Shid47cd632018-09-11 03:10:00 +0000128 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 Pesaventocaa60cc2024-02-18 18:18:37 -0500135BOOST_AUTO_TEST_CASE(Print)
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
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500161BOOST_AUTO_TEST_SUITE_END() // Rib
Junxiao Shi3f21e582017-05-29 15:26:32 +0000162
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400163} // namespace nfd::tests