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 "core/scheduler.hpp" |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 9 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 10 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 11 | #include "tests/test-common.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; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [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); |
| 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 | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 42 | m_face1->onFail += |
| 43 | bind(&EndToEndFixture::face1_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 44 | |
| 45 | this->afterIo(); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | channel1_onConnectFailed(const std::string& reason) |
| 50 | { |
| 51 | BOOST_CHECK_MESSAGE(false, reason); |
| 52 | |
| 53 | this->afterIo(); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | face1_onReceiveInterest(const Interest& interest) |
| 58 | { |
| 59 | m_face1_receivedInterests.push_back(interest); |
| 60 | |
| 61 | this->afterIo(); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | face1_onReceiveData(const Data& data) |
| 66 | { |
| 67 | m_face1_receivedDatas.push_back(data); |
| 68 | |
| 69 | this->afterIo(); |
| 70 | } |
| 71 | |
| 72 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 73 | face1_onFail() |
| 74 | { |
| 75 | m_face1.reset(); |
| 76 | this->afterIo(); |
| 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 | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 88 | m_face2->onFail += |
| 89 | bind(&EndToEndFixture::face2_onFail, this); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 90 | |
| 91 | this->afterIo(); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | channel2_onConnectFailed(const std::string& reason) |
| 96 | { |
| 97 | BOOST_CHECK_MESSAGE(false, reason); |
| 98 | |
| 99 | this->afterIo(); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | face2_onReceiveInterest(const Interest& interest) |
| 104 | { |
| 105 | m_face2_receivedInterests.push_back(interest); |
| 106 | |
| 107 | this->afterIo(); |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | face2_onReceiveData(const Data& data) |
| 112 | { |
| 113 | m_face2_receivedDatas.push_back(data); |
| 114 | |
| 115 | this->afterIo(); |
| 116 | } |
| 117 | |
| 118 | void |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 119 | face2_onFail() |
| 120 | { |
| 121 | m_face2.reset(); |
| 122 | this->afterIo(); |
| 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); |
| 129 | this->afterIo(); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | channel_onConnectFailed(const std::string& reason) |
| 134 | { |
| 135 | BOOST_CHECK_MESSAGE(false, reason); |
| 136 | |
| 137 | this->afterIo(); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | checkFaceList(size_t shouldBe) |
| 142 | { |
| 143 | BOOST_CHECK_EQUAL(m_faces.size(), shouldBe); |
| 144 | } |
| 145 | |
| 146 | void |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 147 | abortTestCase(const std::string& message) |
| 148 | { |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 149 | g_io.stop(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 150 | BOOST_FAIL(message); |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | void |
| 155 | afterIo() |
| 156 | { |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 157 | if (--m_ioRemaining <= 0) |
| 158 | g_io.stop(); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | public: |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 162 | int m_ioRemaining; |
| 163 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 164 | shared_ptr<Face> m_face1; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 165 | std::vector<Interest> m_face1_receivedInterests; |
| 166 | std::vector<Data> m_face1_receivedDatas; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 167 | shared_ptr<Face> m_face2; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 168 | std::vector<Interest> m_face2_receivedInterests; |
| 169 | std::vector<Data> m_face2_receivedDatas; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 171 | std::list< shared_ptr<Face> > m_faces; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | |
| 175 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 176 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 177 | TcpFactory factory; |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 178 | |
| 179 | EventId abortEvent = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 180 | scheduler::schedule(time::seconds(10), |
| 181 | bind(&EndToEndFixture::abortTestCase, this, |
| 182 | "TcpChannel error: cannot connect or cannot accept connection")); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 184 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 185 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 186 | |
| 187 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 188 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 189 | |
| 190 | channel2->connect("127.0.0.1", "20070", |
| 191 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 192 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 193 | time::seconds(4)); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 194 | |
| 195 | m_ioRemaining = 2; |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 196 | g_io.run(); |
| 197 | g_io.reset(); |
| 198 | scheduler::cancel(abortEvent); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 199 | |
| 200 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 201 | BOOST_REQUIRE(static_cast<bool>(m_face2)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 202 | |
| 203 | BOOST_CHECK_EQUAL(m_face1->isLocal(), true); |
| 204 | BOOST_CHECK_EQUAL(m_face2->isLocal(), true); |
| 205 | |
| 206 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face1)), true); |
| 207 | BOOST_CHECK_EQUAL(static_cast<bool>(dynamic_pointer_cast<LocalFace>(m_face2)), true); |
| 208 | |
| 209 | // integrated tests needs to check that TcpFace for non-loopback fails these tests... |
| 210 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 211 | abortEvent = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 212 | scheduler::schedule(time::seconds(10), |
| 213 | bind(&EndToEndFixture::abortTestCase, this, |
| 214 | "TcpChannel error: cannot send or receive Interest/Data packets")); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 215 | |
| 216 | Interest interest1("ndn:/TpnzGvW9R"); |
| 217 | Data data1 ("ndn:/KfczhUqVix"); |
| 218 | data1.setContent(0, 0); |
| 219 | Interest interest2("ndn:/QWiIMfj5sL"); |
| 220 | Data data2 ("ndn:/XNBV796f"); |
| 221 | data2.setContent(0, 0); |
| 222 | |
| 223 | ndn::SignatureSha256WithRsa fakeSignature; |
| 224 | fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0)); |
| 225 | |
| 226 | // set fake signature on data1 and data2 |
| 227 | data1.setSignature(fakeSignature); |
| 228 | data2.setSignature(fakeSignature); |
| 229 | |
| 230 | m_face1->sendInterest(interest1); |
| 231 | m_face1->sendData (data1 ); |
| 232 | m_face2->sendInterest(interest2); |
| 233 | m_face2->sendData (data2 ); |
| 234 | |
| 235 | m_ioRemaining = 4; |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 236 | g_io.run(); |
| 237 | g_io.reset(); |
| 238 | scheduler::cancel(abortEvent); |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 239 | |
| 240 | BOOST_REQUIRE_EQUAL(m_face1_receivedInterests.size(), 1); |
| 241 | BOOST_REQUIRE_EQUAL(m_face1_receivedDatas .size(), 1); |
| 242 | BOOST_REQUIRE_EQUAL(m_face2_receivedInterests.size(), 1); |
| 243 | BOOST_REQUIRE_EQUAL(m_face2_receivedDatas .size(), 1); |
| 244 | |
| 245 | BOOST_CHECK_EQUAL(m_face1_receivedInterests[0].getName(), interest2.getName()); |
| 246 | BOOST_CHECK_EQUAL(m_face1_receivedDatas [0].getName(), data2.getName()); |
| 247 | BOOST_CHECK_EQUAL(m_face2_receivedInterests[0].getName(), interest1.getName()); |
| 248 | BOOST_CHECK_EQUAL(m_face2_receivedDatas [0].getName(), data1.getName()); |
| 249 | } |
| 250 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 251 | BOOST_FIXTURE_TEST_CASE(MultipleAccepts, EndToEndFixture) |
| 252 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 253 | TcpFactory factory; |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 254 | |
| 255 | EventId abortEvent = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 256 | scheduler::schedule(time::seconds(10), |
| 257 | bind(&EndToEndFixture::abortTestCase, this, |
| 258 | "TcpChannel error: cannot connect or cannot accept connection")); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 259 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 260 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 261 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 262 | |
| 263 | channel1->listen(bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 264 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1)); |
| 265 | |
| 266 | channel2->connect("127.0.0.1", "20070", |
| 267 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 268 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 269 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 270 | |
| 271 | m_ioRemaining = 2; |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 272 | g_io.run(); |
| 273 | g_io.reset(); |
| 274 | scheduler::cancel(abortEvent); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 275 | |
| 276 | BOOST_CHECK_EQUAL(m_faces.size(), 2); |
| 277 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 278 | shared_ptr<TcpChannel> channel3 = factory.createChannel("127.0.0.1", "20072"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 279 | channel3->connect("127.0.0.1", "20070", |
| 280 | bind(&EndToEndFixture::channel_onFaceCreated, this, _1), |
| 281 | bind(&EndToEndFixture::channel_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 282 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 283 | |
| 284 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 285 | shared_ptr<TcpChannel> channel4 = factory.createChannel("127.0.0.1", "20073"); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 286 | |
| 287 | BOOST_CHECK_NE(channel3, channel4); |
| 288 | |
| 289 | scheduler |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 290 | ::schedule |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 291 | (time::seconds(1), |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 292 | bind(&TcpChannel::connect, channel4, |
| 293 | "127.0.0.1", "20070", |
| 294 | // does not work without static_cast |
| 295 | static_cast<TcpChannel::FaceCreatedCallback>(bind(&EndToEndFixture:: |
| 296 | channel_onFaceCreated, this, _1)), |
| 297 | static_cast<TcpChannel::ConnectFailedCallback>(bind(&EndToEndFixture:: |
| 298 | channel_onConnectFailed, this, _1)), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 299 | time::seconds(4))); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 300 | |
| 301 | m_ioRemaining = 4; // 2 connects and 2 accepts |
| 302 | abortEvent = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 303 | scheduler::schedule(time::seconds(10), |
| 304 | bind(&EndToEndFixture::abortTestCase, this, |
| 305 | "TcpChannel error: cannot connect or cannot accept multiple connections")); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 306 | |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 307 | scheduler::schedule(time::seconds(0.5), |
| 308 | bind(&EndToEndFixture::checkFaceList, this, 4)); |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 309 | |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 310 | g_io.run(); |
| 311 | g_io.reset(); |
| 312 | scheduler::cancel(abortEvent); |
| 313 | |
Alexander Afanasyev | a16abd2 | 2014-01-31 18:12:29 -0800 | [diff] [blame] | 314 | BOOST_CHECK_EQUAL(m_faces.size(), 6); |
| 315 | } |
| 316 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 317 | |
| 318 | BOOST_FIXTURE_TEST_CASE(FaceClosing, EndToEndFixture) |
| 319 | { |
Alexander Afanasyev | 0eb7065 | 2014-02-27 18:35:07 -0800 | [diff] [blame] | 320 | TcpFactory factory; |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 321 | |
| 322 | EventId abortEvent = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 323 | scheduler::schedule(time::seconds(10), |
| 324 | bind(&EndToEndFixture::abortTestCase, this, |
| 325 | "TcpChannel error: cannot connect or cannot accept connection")); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 326 | |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame^] | 327 | shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070"); |
| 328 | shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071"); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 329 | |
| 330 | channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated, this, _1), |
| 331 | bind(&EndToEndFixture::channel1_onConnectFailed, this, _1)); |
| 332 | |
| 333 | channel2->connect("127.0.0.1", "20070", |
| 334 | bind(&EndToEndFixture::channel2_onFaceCreated, this, _1), |
| 335 | bind(&EndToEndFixture::channel2_onConnectFailed, this, _1), |
Alexander Afanasyev | c1e2ee0 | 2014-02-25 17:02:07 -0800 | [diff] [blame] | 336 | time::seconds(4)); // very short timeout |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 337 | |
| 338 | m_ioRemaining = 2; |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 339 | g_io.run(); |
| 340 | // BOOST_REQUIRE_NO_THROW(g_io.run()); |
| 341 | g_io.reset(); |
| 342 | scheduler::cancel(abortEvent); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 343 | |
| 344 | BOOST_CHECK_EQUAL(channel1->size(), 1); |
| 345 | BOOST_CHECK_EQUAL(channel2->size(), 1); |
| 346 | |
| 347 | abortEvent = |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 348 | scheduler::schedule(time::seconds(10), |
| 349 | bind(&EndToEndFixture::abortTestCase, this, |
| 350 | "FaceClosing error: cannot properly close faces")); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 351 | |
| 352 | BOOST_REQUIRE(static_cast<bool>(m_face1)); |
| 353 | BOOST_CHECK(static_cast<bool>(m_face2)); |
| 354 | |
| 355 | m_ioRemaining = 2; |
| 356 | // just double check that we are calling the virtual method |
| 357 | static_pointer_cast<Face>(m_face1)->close(); |
| 358 | |
Alexander Afanasyev | 7329e02 | 2014-02-27 14:47:22 -0800 | [diff] [blame] | 359 | BOOST_REQUIRE_NO_THROW(g_io.run()); |
| 360 | g_io.reset(); |
| 361 | scheduler::cancel(abortEvent); |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 362 | |
| 363 | // both faces should get closed |
| 364 | BOOST_CHECK(!static_cast<bool>(m_face1)); |
| 365 | BOOST_CHECK(!static_cast<bool>(m_face2)); |
| 366 | |
| 367 | BOOST_CHECK_EQUAL(channel1->size(), 0); |
| 368 | BOOST_CHECK_EQUAL(channel2->size(), 0); |
| 369 | } |
| 370 | |
| 371 | |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 372 | BOOST_AUTO_TEST_SUITE_END() |
| 373 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 374 | } // namespace tests |
Junxiao Shi | 96dc0c4 | 2014-01-30 23:51:59 -0700 | [diff] [blame] | 375 | } // namespace nfd |