blob: baa3ec74ee4203deefa08d1d75425653961b4e03 [file] [log] [blame]
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -07001#include "ndnx-tunnel.h"
2#include "ndnx-closure.h"
3#include "ndnx-name.h"
4#include "ndnx-pco.h"
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -08005#include <unistd.h>
6
7#include <boost/test/unit_test.hpp>
8
9
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070010using namespace Ndnx;
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080011using namespace std;
12using namespace boost;
13
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070014BOOST_AUTO_TEST_SUITE(NdnxTunnelTests)
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080015
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070016class DummyTunnel : public NdnxTunnel
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080017{
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
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070030DummyTunnel::DummyTunnel() : NdnxTunnel()
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080031{
32 m_localPrefix = Name("/local");
33}
34
35void
36DummyTunnel::overridePrefix()
37{
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070038 NdnxWrapper::setInterestFilter(m_localPrefix, bind(&DummyTunnel::handleTunneledInterest, this, _1));
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080039}
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080040
41Name
42DummyTunnel::queryRoutableName (const Name &name)
43{
44 return Name("/local") + name;
45}
46
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070047NdnxWrapperPtr t1(new DummyTunnel());
48NdnxWrapperPtr t2(new DummyTunnel());
49NdnxWrapperPtr c1(new NdnxWrapper());
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080050
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
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070077BOOST_AUTO_TEST_CASE (NdnxTunnelTest)
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080078{
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);
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080085
86 c1->sendInterest(Name("/local/hello"), Closure(bind(outerCallback, _1, _2)));
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080087 usleep(100000);
88 // it is indeed published as /local/hello
89 BOOST_CHECK_EQUAL(g_dc_o, 1);
90
91 g_dc_i = 0;
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080092 t2->sendInterest(Name(inner), Closure(bind(innerCallback, _1, _2)));
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080093 usleep(100000);
94 BOOST_CHECK_EQUAL(g_dc_i, 1);
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080095}
96
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070097BOOST_AUTO_TEST_CASE (NdnxTunnelRegister)
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -080098{
99
100 g_ic = 0;
101 g_dc_i = 0;
102 t3.overridePrefix();
103 t3.setInterestFilter(Name("/t3"), bind(interestCallback, _1));
104 usleep(100000);
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -0800105 Closure innerClosure (bind(innerCallback, _1, _2));
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -0800106 t1->sendInterest(Name("/t3/hello"), innerClosure);
107 usleep(100000);
108 BOOST_CHECK_EQUAL(g_dc_i, 1);
109 BOOST_CHECK_EQUAL(g_ic, 1);
110
111 t3.clearInterestFilter(Name("/t3"));
112 usleep(100000);
113 t1->sendInterest(Name("/t3/hello-there"), innerClosure);
114 usleep(100000);
115 BOOST_CHECK_EQUAL(g_dc_i, 1);
116 BOOST_CHECK_EQUAL(g_ic, 1);
Zhenkai Zhu7b3a7302013-01-07 19:50:41 -0800117
118}
119
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -0800120
121BOOST_AUTO_TEST_SUITE_END()