Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 7 | #include "face/tcp-factory.hpp" |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 8 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 9 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 10 | #include "tests/test-common.hpp" |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 11 | #include "tests/core/limited-io.hpp" |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 12 | |
| 13 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 14 | namespace tests { |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 15 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 16 | BOOST_FIXTURE_TEST_SUITE(FaceTcp, BaseFixture) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 17 | |
| 18 | BOOST_AUTO_TEST_CASE(ChannelMap) |
| 19 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 20 | TcpFactory factory; |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 22 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 23 | shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 24 | BOOST_CHECK_EQUAL(channel1, channel1a); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 25 | BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 27 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 28 | BOOST_CHECK_NE(channel1, channel2); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 29 | |
| 30 | shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071"); |
| 31 | BOOST_CHECK_NE(channel2, channel3); |
| 32 | BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | class EndToEndFixture : protected BaseFixture |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 39 | channel1_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 40 | { |
| 41 | BOOST_CHECK(!static_cast<bool>(m_face1)); |
| 42 | m_face1 = newFace; |
| 43 | m_face1->onReceiveInterest += |
| 44 | bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
| 45 | m_face1->onReceiveData += |
| 46 | bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 47 | m_face1->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 48 | bind(&EndToEndFixture::face1_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 50 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
| 54 | channel1_onConnectFailed(const std::string& reason) |
| 55 | { |
| 56 | BOOST_CHECK_MESSAGE(false, reason); |
| 57 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 58 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 59 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 60 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 61 | void |
| 62 | face1_onReceiveInterest(const Interest& interest) |
| 63 | { |
| 64 | m_face1_receivedInterests.push_back(interest); |
| 65 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 66 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 67 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 68 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 69 | void |
| 70 | face1_onReceiveData(const Data& data) |
| 71 | { |
| 72 | m_face1_receivedDatas.push_back(data); |
| 73 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 74 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 78 | face1_onFail() |
| 79 | { |
| 80 | m_face1.reset(); |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 81 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 85 | channel2_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 86 | { |
| 87 | BOOST_CHECK(!static_cast<bool>(m_face2)); |
| 88 | m_face2 = newFace; |
| 89 | m_face2->onReceiveInterest += |
| 90 | bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
| 91 | m_face2->onReceiveData += |
| 92 | bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 93 | m_face2->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 94 | bind(&EndToEndFixture::face2_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 96 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void |
| 100 | channel2_onConnectFailed(const std::string& reason) |
| 101 | { |
| 102 | BOOST_CHECK_MESSAGE(false, reason); |
| 103 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 104 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 105 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 106 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 107 | void |
| 108 | face2_onReceiveInterest(const Interest& interest) |
| 109 | { |
| 110 | m_face2_receivedInterests.push_back(interest); |
| 111 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 112 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 113 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 114 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 115 | void |
| 116 | face2_onReceiveData(const Data& data) |
| 117 | { |
| 118 | m_face2_receivedDatas.push_back(data); |
| 119 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 120 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 124 | face2_onFail() |
| 125 | { |
| 126 | m_face2.reset(); |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 127 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 131 | channel_onFaceCreated(const shared_ptr<Face>& newFace) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 132 | { |
| 133 | m_faces.push_back(newFace); |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 134 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
| 138 | channel_onConnectFailed(const std::string& reason) |
| 139 | { |
| 140 | BOOST_CHECK_MESSAGE(false, reason); |
| 141 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 142 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
| 146 | checkFaceList(size_t shouldBe) |
| 147 | { |
| 148 | BOOST_CHECK_EQUAL(m_faces.size(), shouldBe); |
| 149 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 150 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 151 | public: |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 152 | LimitedIo m_limitedIo; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 154 | shared_ptr<Face> m_face1; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 155 | std::vector<Interest> m_face1_receivedInterests; |
| 156 | std::vector<Data> m_face1_receivedDatas; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 157 | shared_ptr<Face> m_face2; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 158 | std::vector<Interest> m_face2_receivedInterests; |
| 159 | std::vector<Data> m_face2_receivedDatas; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 160 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 161 | std::list< shared_ptr<Face> > m_faces; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 165 | BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 166 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 167 | TcpFactory factory; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 168 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 169 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 170 | factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(channel1->isListening(), false); |
| 173 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 174 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 175 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | 53a6fd3 | 2014-03-23 00:00:04 -0700 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(channel1->isListening(), true); |
| 178 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 179 | factory.createFace(FaceUri("tcp://127.0.0.1:20070"), |
| 180 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 181 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 182 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 183 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 184 | "TcpChannel error: cannot connect or cannot accept connection"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 185 | |
| 186 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 187 | BOOST_REQUIRE(static_cast<bool>(m_face2)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | 355c066 | 2014-03-20 18:08:17 -0700 | [diff] [blame] | 189 | BOOST_CHECK(m_face1->isOnDemand()); |
| 190 | BOOST_CHECK(!m_face2->isOnDemand()); |
| 191 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 192 | BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "tcp4://127.0.0.1:20070"); |
| 193 | // face1 has an unknown URI, since the source port is automatically chosen by OS |
| 194 | |
| 195 | BOOST_CHECK_EQUAL(m_face1->isLocal(), true); |
| 196 | BOOST_CHECK_EQUAL(m_face2->isLocal(), true); |
| 197 | |
| 198 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face1)), true); |
| 199 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true); |
| 200 | |
| 201 | // integrated tests needs to check that TcpFace for non-loopback fails these tests... |
| 202 | |
| 203 | Interest interest1("ndn:/TpnzGvW9R"); |
| 204 | Data data1 ("ndn:/KfczhUqVix"); |
| 205 | data1.setContent(0, 0); |
| 206 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 207 | Data data2 ("ndn:/XNBV796f"); |
| 208 | data2.setContent(0, 0); |
| 209 | |
| 210 | ndn::SignatureSha256WithRsa fakeSignature; |
| 211 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 212 | |
| 213 | // set fake signature on data1 and data2 |
| 214 | data1.setSignature(fakeSignature); |
| 215 | data2.setSignature(fakeSignature); |
| 216 | |
| 217 | m_face1->sendInterest(interest1); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 218 | m_face1->sendInterest(interest1); |
| 219 | m_face1->sendInterest(interest1); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 220 | m_face1->sendData (data1 ); |
| 221 | m_face2->sendInterest(interest2); |
| 222 | m_face2->sendData (data2 ); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 223 | m_face2->sendData (data2 ); |
| 224 | m_face2->sendData (data2 ); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 225 | |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 226 | BOOST_CHECK_MESSAGE(m_limitedIo.run(8, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 227 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 228 | |
| 229 | |
| 230 | BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 231 | BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 3); |
| 232 | BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 3); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 233 | BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
| 234 | |
| 235 | BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 236 | BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 237 | BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 238 | BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
Alexander Afanasyev | 7e698e6 | 2014-03-07 16:48:35 +0000 | [diff] [blame] | 239 | |
| 240 | const FaceCounters& counters1 = m_face1->getCounters(); |
| 241 | BOOST_CHECK_EQUAL(counters1.getInInterest() , 1); |
| 242 | BOOST_CHECK_EQUAL(counters1.getInData() , 3); |
| 243 | BOOST_CHECK_EQUAL(counters1.getOutInterest(), 3); |
| 244 | BOOST_CHECK_EQUAL(counters1.getOutData() , 1); |
| 245 | |
| 246 | const FaceCounters& counters2 = m_face2->getCounters(); |
| 247 | BOOST_CHECK_EQUAL(counters2.getInInterest() , 3); |
| 248 | BOOST_CHECK_EQUAL(counters2.getInData() , 1); |
| 249 | BOOST_CHECK_EQUAL(counters2.getOutInterest(), 1); |
| 250 | BOOST_CHECK_EQUAL(counters2.getOutData() , 3); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | BOOST_FIXTURE_TEST_CASE(EndToEnd6, EndToEndFixture) |
| 254 | { |
| 255 | TcpFactory factory; |
| 256 | |
| 257 | shared_ptr<TcpChannel> channel1 = factory.createChannel("::1", "20070"); |
| 258 | shared_ptr<TcpChannel> channel2 = factory.createChannel("::1", "20071"); |
| 259 | |
| 260 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 261 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 262 | |
| 263 | factory.createFace(FaceUri("tcp://[::1]:20070"), |
| 264 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 265 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1)); |
| 266 | |
| 267 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 268 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 269 | |
| 270 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 271 | BOOST_REQUIRE(static_cast<bool>(m_face2)); |
| 272 | |
| 273 | BOOST_CHECK_EQUAL(m_face2->getUri().toString(), "tcp6://[::1]:20070"); |
| 274 | // face1 has an unknown URI, since the source port is automatically chosen by OS |
| 275 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 276 | BOOST_CHECK_EQUAL(m_face1->isLocal(), true); |
| 277 | BOOST_CHECK_EQUAL(m_face2->isLocal(), true); |
| 278 | |
| 279 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face1)), true); |
| 280 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true); |
| 281 | |
| 282 | // integrated tests needs to check that TcpFace for non-loopback fails these tests... |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 283 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 284 | Interest interest1("ndn:/TpnzGvW9R"); |
| 285 | Data data1 ("ndn:/KfczhUqVix"); |
| 286 | data1.setContent(0, 0); |
| 287 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 288 | Data data2 ("ndn:/XNBV796f"); |
| 289 | data2.setContent(0, 0); |
| 290 | |
| 291 | ndn::SignatureSha256WithRsa fakeSignature; |
| 292 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 293 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 294 | // set fake signature on data1 and data2 |
| 295 | data1.setSignature(fakeSignature); |
| 296 | data2.setSignature(fakeSignature); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 297 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 298 | m_face1->sendInterest(interest1); |
| 299 | m_face1->sendData (data1 ); |
| 300 | m_face2->sendInterest(interest2); |
| 301 | m_face2->sendData (data2 ); |
| 302 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 303 | BOOST_CHECK_MESSAGE(m_limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 304 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 305 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 306 | |
| 307 | BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 308 | BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 309 | BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); |
| 310 | BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 311 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 312 | BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 313 | BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 314 | BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 315 | BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
| 316 | } |
| 317 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 318 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 319 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 320 | TcpFactory factory; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 321 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 322 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 323 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 324 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 325 | channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 326 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 327 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 328 | channel2->connect("127.0.0.1", "20070", |
| 329 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 330 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 331 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 332 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 333 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 334 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 335 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 336 | |
| 337 | BOOST_CHECK_EQUAL(m_faces.size(), 2); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 338 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 339 | shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 340 | channel3->connect("127.0.0.1", "20070", |
| 341 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 342 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 343 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 344 | |
| 345 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 346 | shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 347 | |
| 348 | BOOST_CHECK_NE(channel3, channel4); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 349 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 350 | scheduler::schedule(time::seconds(1), |
| 351 | bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070", |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 352 | // does not work without static_cast |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 353 | static_cast<TcpChannel::FaceCreatedCallback>( |
| 354 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1)), |
| 355 | static_cast<TcpChannel::ConnectFailedCallback>( |
| 356 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)), |
| 357 | time::seconds(4))); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 358 | |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 359 | scheduler::schedule(time::milliseconds(500), |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 360 | bind(&EndToEndFixture::checkFaceList, this, 4)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 361 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 362 | BOOST_CHECK_MESSAGE(m_limitedIo.run(4,// 2 connects and 2 accepts |
| 363 | time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 364 | "TcpChannel error: cannot connect or cannot accept multiple connections"); |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 365 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 366 | BOOST_CHECK_EQUAL(m_faces.size(), 6); |
| 367 | } |
| 368 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 369 | |
| 370 | BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture) |
| 371 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 372 | TcpFactory factory; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 373 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 374 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 375 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 376 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 377 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 378 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 379 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 380 | channel2->connect("127.0.0.1", "20070", |
| 381 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 382 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 383 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 384 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 385 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 386 | "TcpChannel error: cannot connect or cannot accept connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 387 | |
| 388 | BOOST_CHECK_EQUAL(channel1->size(), 1); |
| 389 | BOOST_CHECK_EQUAL(channel2->size(), 1); |
| 390 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 391 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 392 | BOOST_CHECK(static_cast<bool>(m_face2)); |
| 393 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 394 | // Face::close must be invoked during io run to be counted as an op |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 395 | scheduler::schedule(time::milliseconds(100), bind(&Face::close, m_face1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 396 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 397 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 398 | "FaceClosing error: cannot properly close faces"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 399 | |
| 400 | // both faces should get closed |
| 401 | BOOST_CHECK(!static_cast<bool>(m_face1)); |
| 402 | BOOST_CHECK(!static_cast<bool>(m_face2)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 403 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 404 | BOOST_CHECK_EQUAL(channel1->size(), 0); |
| 405 | BOOST_CHECK_EQUAL(channel2->size(), 0); |
| 406 | } |
| 407 | |
| 408 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 409 | BOOST_AUTO_TEST_SUITE_END() |
| 410 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 411 | } // namespace tests |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 412 | } // namespace nfd |