blob: b227572aef1a8c8992bdf704ed21d206f58b58ae [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:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070040 map<string, string> data;
41 void set(string str1, string str2) {
42 data.insert(make_pair(str1, str2));
43 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080044
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070045 void erase(string str1, string str2) {
46 data.erase(str1);
47 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080048
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070049 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 Zhubc7bfce2012-03-09 14:37:52 -080061
62};
63
64BOOST_AUTO_TEST_CASE (AppDataPublishAndFetchTest)
65{
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070066 TestStructApp foo;
67 TestStructApp bar;
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080068
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070069 string interest = "/april/fool";
70 string seq[5] = {"1", "2", "3", "4", "5"};
71 string str[5] = {"panda", "express", "tastes", "so", "good"};
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080072
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070073 for (int i = 0; i < 5; i++) {
74 foo.set(interest + "/" + seq[i], str[i]);
75 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080076
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070077 boost::function<void (string, string)> setFunc =
78 bind(&TestStructApp::set, &bar, _1, _2);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080079
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070080 shared_ptr<CcnxWrapper> handle(new CcnxWrapper());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080081
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070082 AppDataFetch fetcher(handle, setFunc);
83 AppDataPublish publisher(handle);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080084
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070085 for (int i = 1; i <= 5; i++) {
86 publisher.publishData(interest, 0, str[i - 1], 5);
87 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080088
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070089 BOOST_CHECK_EQUAL(publisher.getHighestSeq(interest, 0), 5);
90 BOOST_CHECK_EQUAL(publisher.getRecentData(interest, 0), str[4]);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080091
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070092 fetcher.fetch(interest, 1, 5);
93 // give time for ccnd to react
94 sleep(1);
95 BOOST_CHECK_EQUAL(foo.toString(), bar.toString());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080096
97
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070098 boost::function<void (string, string)> eraseFunc =
99 bind(&TestStructApp::erase, &bar, _1, _2);
100 fetcher.setDataCallback(eraseFunc);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800101
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700102 fetcher.fetch(interest, 1, 5);
103 // give time for ccnd to react
104 sleep(1);
105 TestStructApp poo;
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800106
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700107 BOOST_CHECK_EQUAL(poo.toString(), bar.toString());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800108
109}
110
111