blob: d2444e7aab0ea2d2072ba68f76a79a1ddc08728c [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include <boost/test/unit_test.hpp>
24#include <boost/test/output_test_stream.hpp>
25#include <map>
26using boost::test_tools::output_test_stream;
27
28#include <boost/make_shared.hpp>
29
30#include <ndn-cpp-dev/security/validator-null.hpp>
31#include "sync-logic.h"
32#include "sync-seq-no.h"
33
34using namespace std;
35using namespace boost;
36using namespace Sync;
37
38struct Handler
39{
40 string instance;
41
42 Handler (const string &_instance)
43 : instance (_instance)
44 {
45 }
46
47 void wrapper (const vector<MissingDataInfo> &v) {
48 int n = v.size();
49 for (int i = 0; i < n; i++) {
50 onUpdate (v[i].prefix, v[i].high, v[i].low);
51 }
52 }
53
54 void onUpdate (const string &p/*prefix*/, const SeqNo &seq/*newSeq*/, const SeqNo &oldSeq/*oldSeq*/)
55 {
56 m_map[p] = seq.getSeq ();
57
58 // cout << instance << "\t";
59 // if (!oldSeq.isValid ())
60 // cout << "Inserted: " << p << " (" << seq << ")" << endl;
61 // else
62 // cout << "Updated: " << p << " ( " << oldSeq << ".." << seq << ")" << endl;
63 }
64
65 void onRemove (const string &p/*prefix*/)
66 {
67 // cout << instance << "\tRemoved: " << p << endl;
68 m_map.erase (p);
69 }
70
71 map<string, uint32_t> m_map;
72};
73
74class TestCore
75{
76public:
77 TestCore(ndn::shared_ptr<boost::asio::io_service> ioService)
78 : m_ioService(ioService)
79 {
80 m_l[0] = 0;
81 m_l[1] = 0;
82
83 m_validator = ndn::make_shared<ndn::ValidatorNull>();
84 }
85
86 ~TestCore()
87 {
88 if(m_l[0] != 0)
89 delete m_l[0];
90
91 if(m_l[1] != 0)
92 delete m_l[1];
93 }
94
95 void
96 finish()
97 {
98 }
99
100 void
101 createSyncLogic(int index,
102 ndn::shared_ptr<Handler> h)
103 {
104 m_faces[index] = ndn::make_shared<ndn::Face>(m_ioService);
105 m_l[index] = new SyncLogic(ndn::Name("/bcast"),
106 m_validator, m_faces[index],
107 bind (&Handler::wrapper, &*h, _1),
108 bind (&Handler::onRemove, &*h, _1));
109 }
110
111 void
112 getOldDigestForOne()
113 {
114 m_oldDigest = m_l[0]->getRootDigest();
115 }
116
117 void
118 getNewDigestForOne()
119 {
120 m_newDigest = m_l[0]->getRootDigest();
121 }
122
123 void
124 addLocalNamesForOne(ndn::Name name, uint64_t session, uint64_t seq)
125 {
126 m_l[0]->addLocalNames(name, session, seq);
127 }
128
129 void
130 removeForOne(ndn::Name name)
131 {
132 m_l[0]->remove(name);
133 }
134
135 void
136 checkDigest()
137 {
138 BOOST_CHECK(m_oldDigest != m_newDigest);
139 }
140
141
142public:
143 ndn::shared_ptr<boost::asio::io_service> m_ioService;
144 SyncLogic* m_l[2];
145 ndn::shared_ptr<ndn::Face> m_faces[2];
146 ndn::shared_ptr<ndn::ValidatorNull> m_validator;
147 string m_oldDigest;
148 string m_newDigest;
149};
150
151void
152checkMapSize(ndn::shared_ptr<Handler> h, int size)
153{ BOOST_CHECK_EQUAL (h->m_map.size (), size); }
154
155
156BOOST_AUTO_TEST_CASE (SyncLogicTest)
157{
158 ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>();
159 ndn::Scheduler scheduler(*ioService);
160 TestCore testCore(ioService);
161
162 ndn::shared_ptr<Handler> h1 = ndn::make_shared<Handler>("1");
163 ndn::shared_ptr<Handler> h2 = ndn::make_shared<Handler>("2");
164
165 scheduler.scheduleEvent(ndn::time::seconds(0), ndn::bind(&TestCore::createSyncLogic, &testCore, 0, h1));
166 scheduler.scheduleEvent(ndn::time::seconds(0.1), ndn::bind(&TestCore::getOldDigestForOne, &testCore));
167 scheduler.scheduleEvent(ndn::time::seconds(0.2), ndn::bind(&TestCore::addLocalNamesForOne, &testCore, "/one", 1, 2));
168 scheduler.scheduleEvent(ndn::time::seconds(0.3), ndn::bind(&checkMapSize, h1, 0));
169 scheduler.scheduleEvent(ndn::time::seconds(0.4), ndn::bind(&TestCore::createSyncLogic, &testCore, 1, h2));
170 scheduler.scheduleEvent(ndn::time::seconds(0.5), ndn::bind(&checkMapSize, h1, 0));
171 scheduler.scheduleEvent(ndn::time::seconds(0.6), ndn::bind(&checkMapSize, h2, 1));
172 scheduler.scheduleEvent(ndn::time::seconds(0.7), ndn::bind(&TestCore::removeForOne, &testCore, "/one"));
173 scheduler.scheduleEvent(ndn::time::seconds(0.8), ndn::bind(&TestCore::getNewDigestForOne, &testCore));
174 scheduler.scheduleEvent(ndn::time::seconds(0.9), ndn::bind(&TestCore::checkDigest, &testCore));
175 scheduler.scheduleEvent(ndn::time::seconds(1.0), ndn::bind(&TestCore::finish, &testCore));
176
177 ioService->run();
178
179}