blob: c1eb5dfad95f3ad79b9e19fc9779c8db0bd35ff2 [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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070023/*
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080024#include <boost/test/unit_test.hpp>
25#include <boost/test/output_test_stream.hpp>
26#include <map>
27using boost::test_tools::output_test_stream;
28
29#include <boost/make_shared.hpp>
30
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070031#include "sync-ccnx-wrapper.h"
32#include "sync-app-data-fetch.h"
33#include "sync-app-data-publish.h"
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080034
35using namespace Sync;
36using namespace std;
37using namespace boost;
38
Zhenkai Zhude59ea12012-03-09 14:41:49 -080039class TestStructApp {
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080040public:
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070041 map<string, string> data;
42 void set(string str1, string str2) {
43 data.insert(make_pair(str1, str2));
44 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080045
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070046 void erase(string str1, string str2) {
47 data.erase(str1);
48 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080049
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070050 string toString(){
51 map<string, string>::iterator it = data.begin();
52 string str = "";
53 for (; it != data.end(); ++it){
54 str += "<";
55 str += it->first;
56 str += "|";
57 str += it->second;
58 str += ">";
59 }
60 return str;
61 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080062
63};
64
65BOOST_AUTO_TEST_CASE (AppDataPublishAndFetchTest)
66{
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070067 TestStructApp foo;
68 TestStructApp bar;
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080069
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070070 string interest = "/april/fool";
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070071 string seq[5] = {"0", "1", "2", "3", "4" };
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070072 string str[5] = {"panda", "express", "tastes", "so", "good"};
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080073
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070074 for (int i = 0; i < 5; i++) {
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070075 foo.set(interest + "/" + "0/" + seq[i], str[i]);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070076 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080077
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070078 boost::function<void (string, string)> setFunc =
79 bind(&TestStructApp::set, &bar, _1, _2);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080080
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070081 shared_ptr<CcnxWrapper> handle(new CcnxWrapper());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080082
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070083 AppDataFetch fetcher(handle, setFunc);
84 AppDataPublish publisher(handle);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080085
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070086 for (int i = 1; i <= 5; i++) {
87 publisher.publishData(interest, 0, str[i - 1], 5);
88 }
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080089
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070090 BOOST_CHECK_EQUAL(publisher.getNextSeq(interest, 0), 5);
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070091 BOOST_CHECK_EQUAL(publisher.getRecentData(interest, 0), str[4]);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080092
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -070093 fetcher.onUpdate (interest, SeqNo (0,4), SeqNo (0,-1));
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070094 // give time for ccnd to react
95 sleep(1);
96 BOOST_CHECK_EQUAL(foo.toString(), bar.toString());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -080097
98
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -070099 boost::function<void (string, string)> eraseFunc =
100 bind(&TestStructApp::erase, &bar, _1, _2);
101 fetcher.setDataCallback(eraseFunc);
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800102
Zhenkai Zhu1ee93c92012-03-12 20:32:57 -0700103 fetcher.onUpdate (interest, SeqNo (0,4), SeqNo (0,-1));
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700104 // give time for ccnd to react
105 sleep(1);
106 TestStructApp poo;
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800107
Alexander Afanasyeva022f3d2012-03-11 18:14:48 -0700108 BOOST_CHECK_EQUAL(poo.toString(), bar.toString());
Zhenkai Zhubc7bfce2012-03-09 14:37:52 -0800109
110}
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700111*/