blob: 651545bc53cd3b94e4d707a97be491224b13506f [file] [log] [blame]
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -07001/* -*- 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070020 * 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
Yingdi Yu43e71612013-10-30 22:19:31 -070030// #include <ndn.cxx/wrapper/wrapper.h>
31#include "sync-policy-manager.h"
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070032#include "sync-logic.h"
33#include "sync-seq-no.h"
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070034
35using namespace std;
36using namespace boost;
37using namespace Sync;
38
39struct Handler
40{
41 string instance;
42
43 Handler (const string &_instance)
44 : instance (_instance)
45 {
46 }
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070047
Zhenkai Zhu43ae5c72012-05-31 23:18:45 -070048 void wrapper (const vector<MissingDataInfo> &v) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070049 int n = v.size();
50 for (int i = 0; i < n; i++) {
51 onUpdate (v[i].prefix, v[i].high, v[i].low);
52 }
53 }
54
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070055 void onUpdate (const string &p/*prefix*/, const SeqNo &seq/*newSeq*/, const SeqNo &oldSeq/*oldSeq*/)
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070056 {
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070057 m_map[p] = seq.getSeq ();
58
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070059 // cout << instance << "\t";
60 // if (!oldSeq.isValid ())
61 // cout << "Inserted: " << p << " (" << seq << ")" << endl;
62 // else
63 // cout << "Updated: " << p << " ( " << oldSeq << ".." << seq << ")" << endl;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070064 }
65
66 void onRemove (const string &p/*prefix*/)
67 {
Alexander Afanasyev4f9ea482012-03-15 11:57:29 -070068 // cout << instance << "\tRemoved: " << p << endl;
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070069 m_map.erase (p);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070070 }
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070071
72 map<string, uint32_t> m_map;
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070073};
74
75BOOST_AUTO_TEST_CASE (SyncLogicTest)
76{
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070077 Handler h1 ("1");
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070078
Yingdi Yu43e71612013-10-30 22:19:31 -070079 ndn::Ptr<SyncPolicyManager> policyManager1 = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(ndn::Name("/ndn/ucla.edu/alice"), ndn::Name("/ndn/ucla.edu/alice/KEY/dsk-1382934202/ID-CERT/%FD%FF%FF%FF%FF%DEk%C0%0B"), ndn::Name("/bcast")));
80
81 SyncLogic l1 (ndn::Name("/bcast"), policyManager1, bind (&Handler::wrapper, &h1, _1), bind (&Handler::onRemove, &h1, _1));
Zhenkai Zhua2e0b082012-09-26 10:34:15 -070082
83 std::string oldDigest = l1.getRootDigest();
84
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070085 l1.addLocalNames ("/one", 1, 2);
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070086
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070087 BOOST_CHECK_EQUAL (h1.m_map.size (), 0);
88 sleep (1);
89 BOOST_CHECK_EQUAL (h1.m_map.size (), 0);
90
91 Handler h2 ("2");
Yingdi Yu43e71612013-10-30 22:19:31 -070092
93 ndn::Ptr<SyncPolicyManager> policyManager2 = ndn::Ptr<SyncPolicyManager>(new SyncPolicyManager(ndn::Name("/ndn/ucla.edu/bob"), ndn::Name("/ndn/ucla.edu/bob/KEY/dsk-1382934206/ID-CERT/%FD%FF%FF%FF%FF%DEl%0BC"), ndn::Name("/bcast")));
94
95 SyncLogic l2 (ndn::Name("/bcast"), policyManager2, bind (&Handler::wrapper, &h2, _1), bind (&Handler::onRemove, &h2, _1));
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070096
97 sleep (1);
98 BOOST_CHECK_EQUAL (h1.m_map.size (), 0);
99 BOOST_CHECK_EQUAL (h2.m_map.size (), 1);
100
Zhenkai Zhua2e0b082012-09-26 10:34:15 -0700101 l1.remove ("/one");
102 sleep(1);
103 std::string newDigest = l1.getRootDigest();
104 BOOST_CHECK(oldDigest != newDigest);
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700105
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700106}