blob: 0c6e63bad141d19829786bd424d4d07debdc29a1 [file] [log] [blame]
Junxiao Shi20377642023-08-24 23:55:40 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shib5734842024-01-09 21:14:53 +00003 * Copyright (c) 2014-2024, The University of Memphis,
Junxiao Shi20377642023-08-24 23:55:40 +00004 * 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 "lsa/coordinate-lsa.hpp"
23
24#include "tests/boost-test.hpp"
25
Davide Pesavento288141a2024-02-13 17:30:35 -050026namespace nlsr::tests {
Junxiao Shi20377642023-08-24 23:55:40 +000027
28/*
29static void
30printBytes(ndn::span<const uint8_t> buf)
31{
32 std::string hex = ndn::toHex(buf);
33
34 for (size_t i = 0; i < hex.size(); i++) {
35 if (i > 0 && i % 30 == 0)
36 std::cout << "\n ";
37
38 std::cout << "0x" << hex[i];
39 std::cout << hex[++i];
40
41 if ((i + 1) != hex.size())
42 std::cout << ", ";
43 }
44 std::cout << "\n" << "};" << std::endl;
45}
46
47printBytes(block);
48*/
49
50BOOST_AUTO_TEST_SUITE(TestCoordinateLsa)
51
52const uint8_t COORDINATE_LSA1[] = {
53 0x85, 0x43, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
54 0x82, 0x01, 0x0C, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
55 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x87, 0x08, 0x40, 0x04, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x88, 0x08, 0x40, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88,
57 0x08, 0x40, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
58};
59
60const uint8_t COORDINATE_LSA_DIFF_ANGLE[] = {
61 0x85, 0x39, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
62 0x82, 0x01, 0x0C, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
63 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x87, 0x08, 0x40, 0x04, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x88, 0x08, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
65};
66
67const uint8_t COORDINATE_LSA_DIFF_RADIUS[] = {
68 0x85, 0x39, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
69 0x82, 0x01, 0x0C, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
70 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x87, 0x08, 0x40, 0x02, 0x66, 0x66,
71 0x66, 0x66, 0x66, 0x66, 0x88, 0x08, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
72};
73
74const uint8_t COORDINATE_LSA_DIFF_SEQ[] = {
75 0x85, 0x39, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
76 0x82, 0x01, 0x0E, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
77 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x87, 0x08, 0x40, 0x02, 0x66, 0x66,
78 0x66, 0x66, 0x66, 0x66, 0x88, 0x08, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
79};
80
81const uint8_t COORDINATE_LSA_DIFF_TS[] = {
82 0x85, 0x39, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31,
83 0x82, 0x01, 0x0E, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36,
84 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x34, 0x34, 0x87, 0x08, 0x40, 0x02, 0x66, 0x66,
85 0x66, 0x66, 0x66, 0x66, 0x88, 0x08, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
86};
87
88BOOST_AUTO_TEST_CASE(Basic)
89{
90 auto testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196014943));
91 std::vector<double> angles1{30.0}, angles2{30.0};
92 angles1.push_back(30.0);
93 angles2.push_back(30.0);
94 CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles1);
95 CoordinateLsa clsa2("router1", 12, testTimePoint, 2.5, angles2);
96
Junxiao Shib5734842024-01-09 21:14:53 +000097 BOOST_CHECK_CLOSE(clsa1.getRadius(), 2.5, 0.0001);
98 BOOST_TEST(clsa1.getTheta() == angles1, boost::test_tools::per_element());
Junxiao Shi20377642023-08-24 23:55:40 +000099
Junxiao Shib5734842024-01-09 21:14:53 +0000100 BOOST_CHECK_EQUAL(clsa1, clsa2);
Junxiao Shi20377642023-08-24 23:55:40 +0000101
102 BOOST_CHECK_EQUAL(clsa1.wireEncode(), clsa2.wireEncode());
103
104 auto wire = clsa1.wireEncode();
105 BOOST_TEST(wire == COORDINATE_LSA1, boost::test_tools::per_element());
106
107 std::vector<double> angles3{40.0};
Junxiao Shib5734842024-01-09 21:14:53 +0000108 clsa1.setTheta(angles3);
Junxiao Shi20377642023-08-24 23:55:40 +0000109 wire = clsa1.wireEncode();
110 BOOST_TEST(wire == COORDINATE_LSA_DIFF_ANGLE, boost::test_tools::per_element());
111
Junxiao Shib5734842024-01-09 21:14:53 +0000112 clsa1.setRadius(2.3);
Junxiao Shi20377642023-08-24 23:55:40 +0000113 wire = clsa1.wireEncode();
114 BOOST_TEST(wire == COORDINATE_LSA_DIFF_RADIUS, boost::test_tools::per_element());
115
116 clsa1.setSeqNo(14);
117 wire = clsa1.wireEncode();
118 BOOST_TEST(wire == COORDINATE_LSA_DIFF_SEQ, boost::test_tools::per_element());
119
120 testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196024993));
121 clsa1.setExpirationTimePoint(testTimePoint);
122 wire = clsa1.wireEncode();
123 BOOST_TEST(wire == COORDINATE_LSA_DIFF_TS, boost::test_tools::per_element());
124}
125
126BOOST_AUTO_TEST_CASE(InitializeFromContent)
127{
128 auto testTimePoint = ndn::time::system_clock::now();
129 std::vector<double> angles = {30, 40.0};
130 CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles);
131 CoordinateLsa clsa2(clsa1.wireEncode());
132 BOOST_CHECK_EQUAL(clsa1.wireEncode(), clsa2.wireEncode());
133}
134
135BOOST_AUTO_TEST_SUITE_END()
136
Davide Pesavento288141a2024-02-13 17:30:35 -0500137} // namespace nlsr::tests