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 |
| 96 | finish() |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | createSyncLogic(int index, |
| 102 | ndn::shared_ptr<Handler> h) |
| 103 | { |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 104 | ndn::Name identity("/tmp-" + boost::lexical_cast<std::string>(ndn::time::now())); |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 105 | 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] | 106 | m_faces[index] = ndn::make_shared<ndn::Face>(m_ioService); |
| 107 | m_l[index] = new SyncLogic(ndn::Name("/bcast"), |
Yingdi Yu | 3da10fe | 2014-02-27 16:37:34 -0800 | [diff] [blame] | 108 | *cert, |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 109 | m_validator, m_faces[index], |
| 110 | bind (&Handler::wrapper, &*h, _1), |
| 111 | bind (&Handler::onRemove, &*h, _1)); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | getOldDigestForOne() |
| 116 | { |
| 117 | m_oldDigest = m_l[0]->getRootDigest(); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | getNewDigestForOne() |
| 122 | { |
| 123 | m_newDigest = m_l[0]->getRootDigest(); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | addLocalNamesForOne(ndn::Name name, uint64_t session, uint64_t seq) |
| 128 | { |
| 129 | m_l[0]->addLocalNames(name, session, seq); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | removeForOne(ndn::Name name) |
| 134 | { |
| 135 | m_l[0]->remove(name); |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | checkDigest() |
| 140 | { |
| 141 | BOOST_CHECK(m_oldDigest != m_newDigest); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | public: |
Yingdi Yu | 0eee600 | 2014-02-11 15:54:17 -0800 | [diff] [blame] | 146 | ndn::KeyChain m_keyChain; |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 147 | ndn::shared_ptr<boost::asio::io_service> m_ioService; |
| 148 | SyncLogic* m_l[2]; |
| 149 | ndn::shared_ptr<ndn::Face> m_faces[2]; |
| 150 | ndn::shared_ptr<ndn::ValidatorNull> m_validator; |
| 151 | string m_oldDigest; |
| 152 | string m_newDigest; |
| 153 | }; |
| 154 | |
| 155 | void |
| 156 | checkMapSize(ndn::shared_ptr<Handler> h, int size) |
| 157 | { BOOST_CHECK_EQUAL (h->m_map.size (), size); } |
| 158 | |
| 159 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 160 | BOOST_AUTO_TEST_CASE (SyncLogicTest) |
| 161 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 162 | ndn::shared_ptr<boost::asio::io_service> ioService = ndn::make_shared<boost::asio::io_service>(); |
| 163 | ndn::Scheduler scheduler(*ioService); |
| 164 | TestCore testCore(ioService); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 165 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 166 | ndn::shared_ptr<Handler> h1 = ndn::make_shared<Handler>("1"); |
| 167 | ndn::shared_ptr<Handler> h2 = ndn::make_shared<Handler>("2"); |
Yingdi Yu | 43e7161 | 2013-10-30 22:19:31 -0700 | [diff] [blame] | 168 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 169 | scheduler.scheduleEvent(ndn::time::seconds(0), ndn::bind(&TestCore::createSyncLogic, &testCore, 0, h1)); |
| 170 | scheduler.scheduleEvent(ndn::time::seconds(0.1), ndn::bind(&TestCore::getOldDigestForOne, &testCore)); |
| 171 | scheduler.scheduleEvent(ndn::time::seconds(0.2), ndn::bind(&TestCore::addLocalNamesForOne, &testCore, "/one", 1, 2)); |
| 172 | scheduler.scheduleEvent(ndn::time::seconds(0.3), ndn::bind(&checkMapSize, h1, 0)); |
| 173 | scheduler.scheduleEvent(ndn::time::seconds(0.4), ndn::bind(&TestCore::createSyncLogic, &testCore, 1, h2)); |
| 174 | scheduler.scheduleEvent(ndn::time::seconds(0.5), ndn::bind(&checkMapSize, h1, 0)); |
| 175 | scheduler.scheduleEvent(ndn::time::seconds(0.6), ndn::bind(&checkMapSize, h2, 1)); |
| 176 | scheduler.scheduleEvent(ndn::time::seconds(0.7), ndn::bind(&TestCore::removeForOne, &testCore, "/one")); |
| 177 | scheduler.scheduleEvent(ndn::time::seconds(0.8), ndn::bind(&TestCore::getNewDigestForOne, &testCore)); |
| 178 | scheduler.scheduleEvent(ndn::time::seconds(0.9), ndn::bind(&TestCore::checkDigest, &testCore)); |
| 179 | scheduler.scheduleEvent(ndn::time::seconds(1.0), ndn::bind(&TestCore::finish, &testCore)); |
Zhenkai Zhu | a2e0b08 | 2012-09-26 10:34:15 -0700 | [diff] [blame] | 180 | |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 181 | ioService->run(); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 183 | } |