Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 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> |
| 26 | using boost::test_tools::output_test_stream; |
| 27 | |
| 28 | #include <boost/make_shared.hpp> |
| 29 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 30 | #include <ndn-cpp-dev/security/validator-null.hpp> |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 31 | #include "sync-logic.h" |
| 32 | #include "sync-seq-no.h" |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 33 | |
| 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | using namespace Sync; |
| 37 | |
| 38 | struct Handler |
| 39 | { |
| 40 | string instance; |
| 41 | |
| 42 | Handler (const string &_instance) |
| 43 | : instance (_instance) |
| 44 | { |
| 45 | } |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 46 | |
Zhenkai Zhu | 43ae5c7 | 2012-05-31 23:18:45 -0700 | [diff] [blame] | 47 | void wrapper (const vector<MissingDataInfo> &v) { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 54 | void onUpdate (const string &p/*prefix*/, const SeqNo &seq/*newSeq*/, const SeqNo &oldSeq/*oldSeq*/) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 55 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 56 | m_map[p] = seq.getSeq (); |
| 57 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 58 | // cout << instance << "\t"; |
| 59 | // if (!oldSeq.isValid ()) |
| 60 | // cout << "Inserted: " << p << " (" << seq << ")" << endl; |
| 61 | // else |
| 62 | // cout << "Updated: " << p << " ( " << oldSeq << ".." << seq << ")" << endl; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void onRemove (const string &p/*prefix*/) |
| 66 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 67 | // cout << instance << "\tRemoved: " << p << endl; |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 68 | m_map.erase (p); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 69 | } |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 70 | |
| 71 | map<string, uint32_t> m_map; |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 74 | class TestCore |
| 75 | { |
| 76 | public: |
| 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 |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 96 | finish(ndn::shared_ptr<boost::asio::io_service> ioService) |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 97 | { |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 98 | ioService->stop(); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void |
| 102 | createSyncLogic(int index, |
| 103 | ndn::shared_ptr<Handler> h) |
| 104 | { |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 105 | ndn::Name identity("/tmp-" + boost::lexical_cast<std::string>(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count())); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 106 | ndn::shared_ptr<ndn::IdentityCertificate> cert = m_keyChain.getCertificate(m_keyChain.createIdentity(identity)); |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 107 | m_faces[index] = ndn::make_shared<ndn::Face>(m_ioService); |
| 108 | m_l[index] = new SyncLogic(ndn::Name("/bcast"), |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 109 | *cert, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 110 | m_validator, m_faces[index], |
| 111 | bind (&Handler::wrapper, &*h, _1), |
| 112 | bind (&Handler::onRemove, &*h, _1)); |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | getOldDigestForOne() |
| 117 | { |
| 118 | m_oldDigest = m_l[0]->getRootDigest(); |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | getNewDigestForOne() |
| 123 | { |
| 124 | m_newDigest = m_l[0]->getRootDigest(); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | addLocalNamesForOne(ndn::Name name, uint64_t session, uint64_t seq) |
| 129 | { |
| 130 | m_l[0]->addLocalNames(name, session, seq); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | removeForOne(ndn::Name name) |
| 135 | { |
| 136 | m_l[0]->remove(name); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | checkDigest() |
| 141 | { |
| 142 | BOOST_CHECK(m_oldDigest != m_newDigest); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | public: |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 147 | ndn::KeyChain m_keyChain; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 148 | ndn::shared_ptr<boost::asio::io_service> m_ioService; |
| 149 | SyncLogic* m_l[2]; |
| 150 | ndn::shared_ptr<ndn::Face> m_faces[2]; |
| 151 | ndn::shared_ptr<ndn::ValidatorNull> m_validator; |
| 152 | string m_oldDigest; |
| 153 | string m_newDigest; |
| 154 | }; |
| 155 | |
| 156 | void |
| 157 | checkMapSize(ndn::shared_ptr<Handler> h, int size) |
| 158 | { BOOST_CHECK_EQUAL (h->m_map.size (), size); } |
| 159 | |
| 160 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 161 | BOOST_AUTO_TEST_CASE (SyncLogicTest) |
| 162 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 163 | ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>(); |
| 164 | ndn::Scheduler scheduler(*ioService); |
| 165 | TestCore testCore(ioService); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 166 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 167 | ndn::shared_ptr<Handler> h1 = ndn::make_shared<Handler>("1"); |
| 168 | ndn::shared_ptr<Handler> h2 = ndn::make_shared<Handler>("2"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 169 | |
Yingdi Yu | 6e1c9cd | 2014-03-25 10:26:54 -0700 | [diff] [blame] | 170 | scheduler.scheduleEvent(ndn::time::milliseconds(0), ndn::bind(&TestCore::createSyncLogic, &testCore, 0, h1)); |
| 171 | scheduler.scheduleEvent(ndn::time::milliseconds(100), ndn::bind(&TestCore::getOldDigestForOne, &testCore)); |
| 172 | scheduler.scheduleEvent(ndn::time::milliseconds(200), ndn::bind(&TestCore::addLocalNamesForOne, &testCore, "/one", 1, 2)); |
| 173 | scheduler.scheduleEvent(ndn::time::milliseconds(300), ndn::bind(&checkMapSize, h1, 0)); |
| 174 | scheduler.scheduleEvent(ndn::time::milliseconds(400), ndn::bind(&TestCore::createSyncLogic, &testCore, 1, h2)); |
| 175 | scheduler.scheduleEvent(ndn::time::milliseconds(500), ndn::bind(&checkMapSize, h1, 0)); |
| 176 | scheduler.scheduleEvent(ndn::time::milliseconds(600), ndn::bind(&checkMapSize, h2, 1)); |
| 177 | scheduler.scheduleEvent(ndn::time::milliseconds(700), ndn::bind(&TestCore::removeForOne, &testCore, "/one")); |
| 178 | scheduler.scheduleEvent(ndn::time::milliseconds(800), ndn::bind(&TestCore::getNewDigestForOne, &testCore)); |
| 179 | scheduler.scheduleEvent(ndn::time::milliseconds(900), ndn::bind(&TestCore::checkDigest, &testCore)); |
| 180 | scheduler.scheduleEvent(ndn::time::milliseconds(1000), ndn::bind(&TestCore::finish, &testCore, ioService)); |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 181 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 182 | ioService->run(); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 184 | } |