blob: 5a5abc5884940a508d5662b0f1049291be6a5872 [file] [log] [blame]
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -08001#include "ccnx-tunnel.h"
2#include "ccnx-closure.h"
3#include "ccnx-name.h"
4#include "ccnx-pco.h"
5#include <unistd.h>
6
7#include <boost/test/unit_test.hpp>
8
9
10using namespace Ccnx;
11using namespace std;
12using namespace boost;
13
14BOOST_AUTO_TEST_SUITE(CcnxTunnelTests)
15
16class DummyTunnel : public CcnxTunnel
17{
18public:
19 DummyTunnel();
20 virtual ~DummyTunnel() {}
21
22 virtual Name
23 queryRoutableName(const Name &name);
24
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080025 void
26 overridePrefix();
27
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080028};
29
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080030DummyTunnel::DummyTunnel() : CcnxTunnel()
31{
32 m_localPrefix = Name("/local");
33}
34
35void
36DummyTunnel::overridePrefix()
37{
38 CcnxWrapper::setInterestFilter(m_localPrefix, bind(&DummyTunnel::handleTunneledInterest, this, _1));
39}
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080040
41Name
42DummyTunnel::queryRoutableName (const Name &name)
43{
44 return Name("/local") + name;
45}
46
47CcnxWrapperPtr t1(new DummyTunnel());
48CcnxWrapperPtr t2(new DummyTunnel());
49CcnxWrapperPtr c1(new CcnxWrapper());
50
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080051DummyTunnel t3;
52
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080053// global data callback counter;
54int g_dc_i = 0;
55int g_dc_o = 0;
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080056int g_ic = 0;
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080057
58void innerCallback(const Name &name, const Bytes &content)
59{
60 g_dc_i ++;
61 string str((const char *)&content[0], content.size());
62 BOOST_CHECK_EQUAL(name, str);
63}
64
65void outerCallback(const Name &name, const Bytes &content)
66{
67 g_dc_o ++;
68}
69
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080070void interestCallback(const Name &name)
71{
72 string strName = name.toString();
73 t3.publishData(name, (const unsigned char *)strName.c_str(), strName.size(), 5);
74 g_ic++;
75}
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080076
77BOOST_AUTO_TEST_CASE (CcnxTunnelTest)
78{
79 // test publish
80 string inner = "/hello";
81
82 g_dc_o = 0;
83 t1->publishData(Name(inner), (const unsigned char *)inner.c_str(), inner.size(), 5);
84 usleep(100000);
85 Closure *outerClosure = new Closure(1, bind(outerCallback, _1, _2));
86 c1->sendInterest(Name("/local/hello"), outerClosure);
87 usleep(100000);
88 // it is indeed published as /local/hello
89 BOOST_CHECK_EQUAL(g_dc_o, 1);
90
91 g_dc_i = 0;
92 Closure *innerClosure = new Closure(1, bind(innerCallback, _1, _2));
93 t2->sendInterest(Name(inner), innerClosure);
94 usleep(100000);
95 BOOST_CHECK_EQUAL(g_dc_i, 1);
96
97 delete outerClosure;
98 delete innerClosure;
99
100}
101
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -0800102BOOST_AUTO_TEST_CASE (CcnxTunnelRegister)
103{
104
105 g_ic = 0;
106 g_dc_i = 0;
107 t3.overridePrefix();
108 t3.setInterestFilter(Name("/t3"), bind(interestCallback, _1));
109 usleep(100000);
110 Closure *innerClosure = new Closure(1, bind(innerCallback, _1, _2));
111 t1->sendInterest(Name("/t3/hello"), innerClosure);
112 usleep(100000);
113 BOOST_CHECK_EQUAL(g_dc_i, 1);
114 BOOST_CHECK_EQUAL(g_ic, 1);
115
116 t3.clearInterestFilter(Name("/t3"));
117 usleep(100000);
118 t1->sendInterest(Name("/t3/hello-there"), innerClosure);
119 usleep(100000);
120 BOOST_CHECK_EQUAL(g_dc_i, 1);
121 BOOST_CHECK_EQUAL(g_ic, 1);
122 delete innerClosure;
123
124}
125
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -0800126
127BOOST_AUTO_TEST_SUITE_END()