blob: 9941d1d1e2f4aee06b287447490195b94efff692 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04002/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, The University of Memphis,
Vince Lehman6151e952015-02-16 12:36:00 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehman6151e952015-02-16 12:36:00 -060021
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050022#include "sequencing-manager.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050023
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040024#include "tests/boost-test.hpp"
25
Davide Pesaventob0716542024-12-16 19:12:11 -050026#include <filesystem>
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050027#include <fstream>
Davide Pesaventob0716542024-12-16 19:12:11 -050028#include <system_error>
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050029
Davide Pesavento288141a2024-02-13 17:30:35 -050030namespace nlsr::tests {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050031
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040032using namespace ndn;
33
34class SequencingManagerFixture
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050035{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050036public:
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050037 ~SequencingManagerFixture()
38 {
Davide Pesaventob0716542024-12-16 19:12:11 -050039 std::error_code ec;
40 std::filesystem::remove(m_seqFile, ec); // ignore error
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050041 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050042
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050043 void
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060044 writeToFile(const std::string& testSeq)
45 {
Davide Pesaventob0716542024-12-16 19:12:11 -050046 std::ofstream outputFile(m_seqFile, std::ofstream::trunc);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050047 outputFile << testSeq;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050048 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050049
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050050 void
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060051 initiateFromFile()
52 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 m_seqManager.initiateSeqNoFromFile();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050054 }
55
56 void
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060057 checkSeqNumbers(const uint64_t& name, const uint64_t& adj, const uint64_t& cor)
58 {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050059 BOOST_CHECK_EQUAL(m_seqManager.getNameLsaSeq(), name);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050060 BOOST_CHECK_EQUAL(m_seqManager.getAdjLsaSeq(), adj);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050061 BOOST_CHECK_EQUAL(m_seqManager.getCorLsaSeq(), cor);
62 }
63
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064public:
Davide Pesaventob0716542024-12-16 19:12:11 -050065 SequencingManager m_seqManager{"/tmp", HYPERBOLIC_STATE_OFF};
66
67private:
68 std::filesystem::path m_seqFile{"/tmp/nlsrSeqNo.txt"};
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050069};
70
71BOOST_FIXTURE_TEST_SUITE(TestSequencingManager, SequencingManagerFixture)
72
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050073BOOST_AUTO_TEST_CASE(SeparateSeqNumber)
Vince Lehman6151e952015-02-16 12:36:00 -060074{
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060075 initiateFromFile();
76 checkSeqNumbers(0, 0, 0);
77
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050078 // LS
79 writeToFile("NameLsaSeq 100\nAdjLsaSeq 100\nCorLsaSeq 0");
Ashlesh Gawande85998a12017-12-07 22:22:13 -060080 m_seqManager.m_hyperbolicState = HYPERBOLIC_STATE_OFF;
81 initiateFromFile();
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060082 checkSeqNumbers(100 + 10, 100 + 10, 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050083
84 // HR
85 writeToFile("NameLsa 100\nAdjLsa 0\nCorLsa 100");
Ashlesh Gawande85998a12017-12-07 22:22:13 -060086 m_seqManager.m_hyperbolicState = HYPERBOLIC_STATE_ON;
87 initiateFromFile();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050088 // AdjLsa is set to 0 since HR is on
Ashlesh Gawande55d1b5c2019-12-19 16:21:58 -060089 checkSeqNumbers(100 + 10, 0, 100 + 10);
90}
91
92BOOST_AUTO_TEST_CASE(CorruptFile)
93{
94 writeToFile("NameLsaSeq");
95 initiateFromFile();
96 checkSeqNumbers(10, 10, 0);
Vince Lehman6151e952015-02-16 12:36:00 -060097}
98
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050099BOOST_AUTO_TEST_SUITE_END()
100
Davide Pesavento288141a2024-02-13 17:30:35 -0500101} // namespace nlsr::tests