blob: 1f9d7fe447ab2e642670aeff61e9c1f3c93d56ac [file] [log] [blame]
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -08001/* -*- 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 "../model/sync-ccnx-wrapper.h"
31#include "../model/sync-app-data-fetch.h"
32#include "../model/sync-app-data-publish.h"
33
34using namespace Sync;
35using namespace std;
36using namespace boost;
37
Zhenkai Zhude59ea12012-03-09 14:41:49 -080038class TestStructApp {
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080039public:
40 map<string, string> data;
41 void set(string str1, string str2) {
42 data.insert(make_pair(str1, str2));
43 }
44
45 void erase(string str1, string str2) {
46 data.erase(str1);
47 }
48
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 }
61
62};
63
64BOOST_AUTO_TEST_CASE (AppDataPublishAndFetchTest)
65{
Zhenkai Zhude59ea12012-03-09 14:41:49 -080066 TestStructApp foo;
67 TestStructApp bar;
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080068
69 string interest = "/april/fool";
70 string seq[5] = {"1", "2", "3", "4", "5"};
71 string str[5] = {"panda", "express", "tastes", "so", "good"};
72
73 for (int i = 0; i < 5; i++) {
74 foo.set(interest + "/" + seq[i], str[i]);
75 }
76
77 boost::function<void (string, string)> setFunc =
Zhenkai Zhude59ea12012-03-09 14:41:49 -080078 bind(&TestStructApp::set, &bar, _1, _2);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080079
80 shared_ptr<CcnxWrapper> handle(new CcnxWrapper());
81
82 AppDataFetch fetcher(handle, setFunc);
83 AppDataPublish publisher(handle);
84
85 for (int i = 1; i <= 5; i++) {
86 publisher.publishData(interest, 0, str[i - 1], 5);
87 }
88
89 BOOST_CHECK_EQUAL(publisher.getHighestSeq(interest, 0), 5);
90 BOOST_CHECK_EQUAL(publisher.getRecentData(interest, 0), str[4]);
91
92 fetcher.fetch(interest, 1, 5);
93 // give time for ccnd to react
94 sleep(1);
95 BOOST_CHECK_EQUAL(foo.toString(), bar.toString());
96
97
98 boost::function<void (string, string)> eraseFunc =
Zhenkai Zhude59ea12012-03-09 14:41:49 -080099 bind(&TestStructApp::erase, &bar, _1, _2);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800100 fetcher.setDataCallback(eraseFunc);
101
102 fetcher.fetch(interest, 1, 5);
103 // give time for ccnd to react
104 sleep(1);
Zhenkai Zhude59ea12012-03-09 14:41:49 -0800105 TestStructApp poo;
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800106
107 BOOST_CHECK_EQUAL(poo.toString(), bar.toString());
108
109}
110
111