Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 1 | #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 | |
| 10 | using namespace Ccnx; |
| 11 | using namespace std; |
| 12 | using namespace boost; |
| 13 | |
| 14 | BOOST_AUTO_TEST_SUITE(CcnxTunnelTests) |
| 15 | |
| 16 | class DummyTunnel : public CcnxTunnel |
| 17 | { |
| 18 | public: |
| 19 | DummyTunnel(); |
| 20 | virtual ~DummyTunnel() {} |
| 21 | |
| 22 | virtual Name |
| 23 | queryRoutableName(const Name &name); |
| 24 | |
Zhenkai Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 25 | void |
| 26 | overridePrefix(); |
| 27 | |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 28 | }; |
| 29 | |
Zhenkai Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 30 | DummyTunnel::DummyTunnel() : CcnxTunnel() |
| 31 | { |
| 32 | m_localPrefix = Name("/local"); |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | DummyTunnel::overridePrefix() |
| 37 | { |
| 38 | CcnxWrapper::setInterestFilter(m_localPrefix, bind(&DummyTunnel::handleTunneledInterest, this, _1)); |
| 39 | } |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 40 | |
| 41 | Name |
| 42 | DummyTunnel::queryRoutableName (const Name &name) |
| 43 | { |
| 44 | return Name("/local") + name; |
| 45 | } |
| 46 | |
| 47 | CcnxWrapperPtr t1(new DummyTunnel()); |
| 48 | CcnxWrapperPtr t2(new DummyTunnel()); |
| 49 | CcnxWrapperPtr c1(new CcnxWrapper()); |
| 50 | |
Zhenkai Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 51 | DummyTunnel t3; |
| 52 | |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 53 | // global data callback counter; |
| 54 | int g_dc_i = 0; |
| 55 | int g_dc_o = 0; |
Zhenkai Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 56 | int g_ic = 0; |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 57 | |
| 58 | void 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 | |
| 65 | void outerCallback(const Name &name, const Bytes &content) |
| 66 | { |
| 67 | g_dc_o ++; |
| 68 | } |
| 69 | |
Zhenkai Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 70 | void 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 Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 76 | |
| 77 | BOOST_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); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame^] | 85 | |
| 86 | c1->sendInterest(Name("/local/hello"), Closure(bind(outerCallback, _1, _2))); |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 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; |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame^] | 92 | t2->sendInterest(Name(inner), Closure(bind(innerCallback, _1, _2))); |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 93 | usleep(100000); |
| 94 | BOOST_CHECK_EQUAL(g_dc_i, 1); |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Zhenkai Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE (CcnxTunnelRegister) |
| 98 | { |
| 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 Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame^] | 105 | Closure innerClosure (bind(innerCallback, _1, _2)); |
Zhenkai Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 106 | 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 Zhu | 7b3a730 | 2013-01-07 19:50:41 -0800 | [diff] [blame] | 117 | |
| 118 | } |
| 119 | |
Zhenkai Zhu | 0ccc9ec | 2013-01-04 19:24:09 -0800 | [diff] [blame] | 120 | |
| 121 | BOOST_AUTO_TEST_SUITE_END() |