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); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 26 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 27 | BOOST_CHECK_NE(channel1, channel2); |
| 28 | } |
| 29 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | class EndToEndFixture : protected BaseFixture |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 31 | { |
| 32 | public: |
| 33 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 34 | channel1_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 35 | { |
| 36 | BOOST_CHECK(!static_cast<bool>(m_face1)); |
| 37 | m_face1 = newFace; |
| 38 | m_face1->onReceiveInterest += |
| 39 | bind(&EndToEndFixture::face1_onReceiveInterest, this, _1); |
| 40 | m_face1->onReceiveData += |
| 41 | bind(&EndToEndFixture::face1_onReceiveData, this, _1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 42 | m_face1->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 43 | bind(&EndToEndFixture::face1_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 44 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 45 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void |
| 49 | channel1_onConnectFailed(const std::string& reason) |
| 50 | { |
| 51 | BOOST_CHECK_MESSAGE(false, reason); |
| 52 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 53 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 54 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 55 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 56 | void |
| 57 | face1_onReceiveInterest(const Interest& interest) |
| 58 | { |
| 59 | m_face1_receivedInterests.push_back(interest); |
| 60 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 61 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 62 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 63 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 64 | void |
| 65 | face1_onReceiveData(const Data& data) |
| 66 | { |
| 67 | m_face1_receivedDatas.push_back(data); |
| 68 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 69 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 73 | face1_onFail() |
| 74 | { |
| 75 | m_face1.reset(); |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 76 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 80 | channel2_onFaceCreated(const shared_ptr<Face>& newFace) |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 81 | { |
| 82 | BOOST_CHECK(!static_cast<bool>(m_face2)); |
| 83 | m_face2 = newFace; |
| 84 | m_face2->onReceiveInterest += |
| 85 | bind(&EndToEndFixture::face2_onReceiveInterest, this, _1); |
| 86 | m_face2->onReceiveData += |
| 87 | bind(&EndToEndFixture::face2_onReceiveData, this, _1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 88 | m_face2->onFail += |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 89 | bind(&EndToEndFixture::face2_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 91 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void |
| 95 | channel2_onConnectFailed(const std::string& reason) |
| 96 | { |
| 97 | BOOST_CHECK_MESSAGE(false, reason); |
| 98 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 99 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 100 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 101 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 102 | void |
| 103 | face2_onReceiveInterest(const Interest& interest) |
| 104 | { |
| 105 | m_face2_receivedInterests.push_back(interest); |
| 106 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 107 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 108 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 109 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 110 | void |
| 111 | face2_onReceiveData(const Data& data) |
| 112 | { |
| 113 | m_face2_receivedDatas.push_back(data); |
| 114 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 115 | m_limitedIo.afterOp(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 119 | face2_onFail() |
| 120 | { |
| 121 | m_face2.reset(); |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 122 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 126 | channel_onFaceCreated(const shared_ptr<Face>& newFace) |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 127 | { |
| 128 | m_faces.push_back(newFace); |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 129 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void |
| 133 | channel_onConnectFailed(const std::string& reason) |
| 134 | { |
| 135 | BOOST_CHECK_MESSAGE(false, reason); |
| 136 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 137 | m_limitedIo.afterOp(); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void |
| 141 | checkFaceList(size_t shouldBe) |
| 142 | { |
| 143 | BOOST_CHECK_EQUAL(m_faces.size(), shouldBe); |
| 144 | } |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 145 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 146 | public: |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 147 | LimitedIo m_limitedIo; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 149 | shared_ptr<Face> m_face1; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 150 | std::vector<Interest> m_face1_receivedInterests; |
| 151 | std::vector<Data> m_face1_receivedDatas; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 152 | shared_ptr<Face> m_face2; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 153 | std::vector<Interest> m_face2_receivedInterests; |
| 154 | std::vector<Data> m_face2_receivedDatas; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 155 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 156 | std::list< shared_ptr<Face> > m_faces; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | |
| 160 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 161 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 162 | TcpFactory factory; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 164 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 165 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 166 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 167 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 168 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 169 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 170 | channel2->connect("127.0.0.1", "20070", |
| 171 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 172 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 173 | time::seconds(4)); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 174 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 175 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 176 | "TcpChannel error: cannot connect or cannot accept connection"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 177 | |
| 178 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 179 | BOOST_REQUIRE(static_cast<bool>(m_face2)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 180 | |
| 181 | BOOST_CHECK_EQUAL(m_face1->isLocal(), true); |
| 182 | BOOST_CHECK_EQUAL(m_face2->isLocal(), true); |
| 183 | |
| 184 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face1)), true); |
| 185 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true); |
| 186 | |
| 187 | // 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] | 188 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 189 | Interest interest1("ndn:/TpnzGvW9R"); |
| 190 | Data data1 ("ndn:/KfczhUqVix"); |
| 191 | data1.setContent(0, 0); |
| 192 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 193 | Data data2 ("ndn:/XNBV796f"); |
| 194 | data2.setContent(0, 0); |
| 195 | |
| 196 | ndn::SignatureSha256WithRsa fakeSignature; |
| 197 | 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] | 198 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 199 | // set fake signature on data1 and data2 |
| 200 | data1.setSignature(fakeSignature); |
| 201 | data2.setSignature(fakeSignature); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 202 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 203 | m_face1->sendInterest(interest1); |
| 204 | m_face1->sendData (data1 ); |
| 205 | m_face2->sendInterest(interest2); |
| 206 | m_face2->sendData (data2 ); |
| 207 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 208 | BOOST_CHECK_MESSAGE(m_limitedIo.run(4, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 209 | "TcpChannel error: cannot send or receive Interest/Data packets"); |
| 210 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 211 | |
| 212 | BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 213 | BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 214 | BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); |
| 215 | BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 216 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 218 | BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 219 | BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 220 | BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
| 221 | } |
| 222 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 223 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 224 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 225 | TcpFactory factory; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 226 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 227 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 228 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 229 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 230 | channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 231 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 232 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 233 | channel2->connect("127.0.0.1", "20070", |
| 234 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 235 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 236 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 237 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 238 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 239 | "TcpChannel error: cannot connect or cannot accept connection"); |
| 240 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 241 | |
| 242 | BOOST_CHECK_EQUAL(m_faces.size(), 2); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 243 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 244 | shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 245 | channel3->connect("127.0.0.1", "20070", |
| 246 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 247 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 248 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 249 | |
| 250 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 251 | shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 252 | |
| 253 | BOOST_CHECK_NE(channel3, channel4); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 254 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 255 | scheduler::schedule(time::seconds(1), |
| 256 | bind(&TcpChannel::connect, channel4, "127.0.0.1", "20070", |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 257 | // does not work without static_cast |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 258 | static_cast<TcpChannel::FaceCreatedCallback>( |
| 259 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1)), |
| 260 | static_cast<TcpChannel::ConnectFailedCallback>( |
| 261 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)), |
| 262 | time::seconds(4))); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 263 | |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 264 | scheduler::schedule(time::seconds(0.5), |
| 265 | bind(&EndToEndFixture::checkFaceList, this, 4)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 266 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 267 | BOOST_CHECK_MESSAGE(m_limitedIo.run(4,// 2 connects and 2 accepts |
| 268 | time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 269 | "TcpChannel error: cannot connect or cannot accept multiple connections"); |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 270 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(m_faces.size(), 6); |
| 272 | } |
| 273 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 274 | |
| 275 | BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture) |
| 276 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 277 | TcpFactory factory; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 278 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 279 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 280 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 281 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 282 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 283 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 284 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 285 | channel2->connect("127.0.0.1", "20070", |
| 286 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 287 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 288 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 289 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 290 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 291 | "TcpChannel error: cannot connect or cannot accept connection"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 292 | |
| 293 | BOOST_CHECK_EQUAL(channel1->size(), 1); |
| 294 | BOOST_CHECK_EQUAL(channel2->size(), 1); |
| 295 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 296 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 297 | BOOST_CHECK(static_cast<bool>(m_face2)); |
| 298 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 299 | // Face::close must be invoked during io run to be counted as an op |
| 300 | scheduler::schedule(time::seconds(0.1), bind(&Face::close, m_face1)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 301 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 302 | BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(10)) == LimitedIo::EXCEED_OPS, |
| 303 | "FaceClosing error: cannot properly close faces"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 304 | |
| 305 | // both faces should get closed |
| 306 | BOOST_CHECK(!static_cast<bool>(m_face1)); |
| 307 | BOOST_CHECK(!static_cast<bool>(m_face2)); |
Alexander Afanasyev | 5f1ec25 | 2014-02-28 10:59:17 -0800 | [diff] [blame] | 308 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 309 | BOOST_CHECK_EQUAL(channel1->size(), 0); |
| 310 | BOOST_CHECK_EQUAL(channel2->size(), 0); |
| 311 | } |
| 312 | |
| 313 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 314 | BOOST_AUTO_TEST_SUITE_END() |
| 315 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 316 | } // namespace tests |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 317 | } // namespace nfd |