Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 1 | /* -*- 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> |
| 26 | using boost::test_tools::output_test_stream; |
| 27 | |
| 28 | #include <boost/make_shared.hpp> |
| 29 | |
| 30 | #include "../model/sync-ccnx-wrapper.h" |
| 31 | #include "../model/sync-app-data-fetch.h" |
| 32 | #include "../model/sync-app-data-publish.h" |
| 33 | |
| 34 | using namespace Sync; |
| 35 | using namespace std; |
| 36 | using namespace boost; |
| 37 | |
Zhenkai Zhu | de59ea1 | 2012-03-09 14:41:49 -0800 | [diff] [blame] | 38 | class TestStructApp { |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 39 | public: |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 40 | map<string, string> data; |
| 41 | void set(string str1, string str2) { |
| 42 | data.insert(make_pair(str1, str2)); |
| 43 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 45 | void erase(string str1, string str2) { |
| 46 | data.erase(str1); |
| 47 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 49 | string toString(){ |
| 50 | map<string, string>::iterator it = data.begin(); |
| 51 | string str = ""; |
| 52 | for (; it != data.end(); ++it){ |
| 53 | str += "<"; |
| 54 | str += it->first; |
| 55 | str += "|"; |
| 56 | str += it->second; |
| 57 | str += ">"; |
| 58 | } |
| 59 | return str; |
| 60 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 61 | |
| 62 | }; |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE (AppDataPublishAndFetchTest) |
| 65 | { |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 66 | TestStructApp foo; |
| 67 | TestStructApp bar; |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 69 | string interest = "/april/fool"; |
| 70 | string seq[5] = {"1", "2", "3", "4", "5"}; |
| 71 | string str[5] = {"panda", "express", "tastes", "so", "good"}; |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 73 | for (int i = 0; i < 5; i++) { |
| 74 | foo.set(interest + "/" + seq[i], str[i]); |
| 75 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 77 | boost::function<void (string, string)> setFunc = |
| 78 | bind(&TestStructApp::set, &bar, _1, _2); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 80 | shared_ptr<CcnxWrapper> handle(new CcnxWrapper()); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 82 | AppDataFetch fetcher(handle, setFunc); |
| 83 | AppDataPublish publisher(handle); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 85 | for (int i = 1; i <= 5; i++) { |
| 86 | publisher.publishData(interest, 0, str[i - 1], 5); |
| 87 | } |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(publisher.getHighestSeq(interest, 0), 5); |
| 90 | BOOST_CHECK_EQUAL(publisher.getRecentData(interest, 0), str[4]); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 92 | fetcher.fetch(interest, 1, 5); |
| 93 | // give time for ccnd to react |
| 94 | sleep(1); |
| 95 | BOOST_CHECK_EQUAL(foo.toString(), bar.toString()); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 96 | |
| 97 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 98 | boost::function<void (string, string)> eraseFunc = |
| 99 | bind(&TestStructApp::erase, &bar, _1, _2); |
| 100 | fetcher.setDataCallback(eraseFunc); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 102 | fetcher.fetch(interest, 1, 5); |
| 103 | // give time for ccnd to react |
| 104 | sleep(1); |
| 105 | TestStructApp poo; |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | a022f3d | 2012-03-11 18:14:48 -0700 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(poo.toString(), bar.toString()); |
Zhenkai Zhu | bc7bfce | 2012-03-09 14:37:52 -0800 | [diff] [blame] | 108 | |
| 109 | } |
| 110 | |
| 111 | |