blob: 1caefcf3b89a38cc1ea3bf6a892ef5e91b08a099 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -05002/**
akmhoque3d06e792014-05-27 16:23:20 -05003 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author Ashlesh Gawande <agawande@memphis.edu>
21 *
22 **/
Vince Lehman7c603292014-09-11 17:48:16 -050023
24#include "test-common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050025#include "dummy-face.hpp"
Vince Lehman7c603292014-09-11 17:48:16 -050026
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050027#include "lsdb.hpp"
28#include "nlsr.hpp"
29#include "lsa.hpp"
30#include "name-prefix-list.hpp"
31#include <boost/test/unit_test.hpp>
akmhoquec7a79b22014-05-26 08:06:19 -050032#include <ndn-cxx/util/time.hpp>
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050033
34namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050035namespace test {
36
Vince Lehman904c2412014-09-23 19:36:11 -050037using ndn::DummyFace;
38using ndn::shared_ptr;
39
40class LsdbFixture : public BaseFixture
41{
42public:
43 LsdbFixture()
44 : face(ndn::makeDummyFace())
45 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
46 , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter())
47 , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib")
48 , REGISTER_VERB("register")
49 {
50 }
51
52 void extractParameters(ndn::Interest& interest, ndn::Name::Component& verb,
53 ndn::nfd::ControlParameters& extractedParameters)
54 {
55 const ndn::Name& name = interest.getName();
56 verb = name[REGISTER_COMMAND_PREFIX.size()];
57 const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1];
58
59 ndn::Block rawParameters = parameterComponent.blockFromValue();
60 extractedParameters.wireDecode(rawParameters);
61 }
62
63public:
64 shared_ptr<DummyFace> face;
65 Nlsr nlsr;
66 SyncLogicHandler sync;
67
68 ndn::Name REGISTER_COMMAND_PREFIX;
69 ndn::Name::Component REGISTER_VERB;
70};
71
72BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050073
74BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
75{
Vince Lehmanf99b87f2014-08-26 15:54:27 -050076 INIT_LOGGERS("/tmp", "DEBUG");
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -070077
akmhoquec7a79b22014-05-26 08:06:19 -050078 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050079 NamePrefixList npl1;
80
akmhoquefdbddb12014-05-02 18:35:19 -050081 std::string s1 = "name1";
82 std::string s2 = "name2";
83 std::string router1 = "router1/1";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050084
85 npl1.insert(s1);
86 npl1.insert(s2);
87
Vince Lehman904c2412014-09-23 19:36:11 -050088 //For NameLsa lsType is name.
89 //12 is seqNo, randomly generated.
90 //1800 is the default life time.
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -070091 NameLsa nlsa1(ndn::Name("/router1/1"), std::string("name"), 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050092
Vince Lehman904c2412014-09-23 19:36:11 -050093 Lsdb lsdb1(nlsr, g_scheduler);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050094
akmhoque31d1d4b2014-05-05 22:08:14 -050095 lsdb1.installNameLsa(nlsa1);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -070096 lsdb1.writeNameLsdbLog();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050097
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -070098 BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), std::string("name")));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050099
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 lsdb1.removeNameLsa(router1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500101
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700102 BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), std::string("name")), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500103}
104
Vince Lehman904c2412014-09-23 19:36:11 -0500105BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild)
106{
107 ConfParameter& conf = nlsr.getConfParameter();
108 conf.setNetwork("/ndn");
109 conf.setSiteName("/site");
110 conf.setRouterName("/%C1.router/this-router");
111
112 nlsr.initialize();
113
114 Lsdb& lsdb = nlsr.getLsdb();
115 face->processEvents(ndn::time::milliseconds(1));
116 face->m_sentInterests.clear();
117
118 // Should register Sync prefix
119 lsdb.buildAndInstallOwnAdjLsa();
120 face->processEvents(ndn::time::milliseconds(1));
121
122 std::vector<ndn::Interest>& interests = face->m_sentInterests;
123
124 BOOST_REQUIRE(interests.size() > 0);
125
126 ndn::nfd::ControlParameters extractedParameters;
127 ndn::Name::Component verb;
128 extractParameters(interests[0], verb, extractedParameters);
129
130 BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
131 BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix());
132
133 // Should not register Sync prefix
134 face->m_sentInterests.clear();
135 lsdb.buildAndInstallOwnAdjLsa();
136 face->processEvents(ndn::time::milliseconds(1));
137
138 BOOST_CHECK_EQUAL(interests.size(), 0);
139}
140
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500141BOOST_AUTO_TEST_SUITE_END()
142
143} //namespace test
144} //namespace nlsr