2016-01-18 15:33:29 +00:00
|
|
|
// Copyright (c) 2011-2016 The Cryptonote developers
|
2015-09-18 11:55:31 +00:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
#include <System/Dispatcher.h>
|
2015-07-30 15:22:07 +00:00
|
|
|
#include <System/ContextGroup.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <System/Event.h>
|
|
|
|
#include <System/InterruptedException.h>
|
|
|
|
#include <System/Ipv4Address.h>
|
|
|
|
#include <System/TcpConnection.h>
|
|
|
|
#include <System/TcpConnector.h>
|
|
|
|
#include <System/TcpListener.h>
|
|
|
|
#include <System/Timer.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
using namespace System;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
class TcpConnectorTests : public testing::Test {
|
|
|
|
public:
|
|
|
|
TcpConnectorTests() : event(dispatcher), listener(dispatcher, Ipv4Address("127.0.0.1"), 6666), contextGroup(dispatcher) {
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
Dispatcher dispatcher;
|
2015-07-30 15:22:07 +00:00
|
|
|
Event event;
|
|
|
|
TcpListener listener;
|
|
|
|
ContextGroup contextGroup;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TcpConnectorTests, tcpConnector1) {
|
|
|
|
contextGroup.spawn([&]() {
|
2015-05-27 12:08:46 +00:00
|
|
|
listener.accept();
|
|
|
|
event.set();
|
|
|
|
});
|
|
|
|
|
|
|
|
TcpConnector connector(dispatcher);
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.spawn([&] {
|
|
|
|
connector.connect(Ipv4Address("127.0.0.1"), 6666);
|
|
|
|
});
|
2015-05-27 12:08:46 +00:00
|
|
|
event.wait();
|
|
|
|
dispatcher.yield();
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TEST_F(TcpConnectorTests, tcpConnectorInterruptAfterStart) {
|
|
|
|
contextGroup.spawn([&] {
|
|
|
|
ASSERT_THROW(TcpConnector(dispatcher).connect(Ipv4Address("127.0.0.1"), 6666), InterruptedException);
|
|
|
|
});
|
|
|
|
contextGroup.interrupt();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TcpConnectorTests, tcpConnectorInterrupt) {
|
2015-05-27 12:08:46 +00:00
|
|
|
TcpConnector connector(dispatcher);
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.spawn([&]() {
|
|
|
|
Timer(dispatcher).sleep(std::chrono::milliseconds(10));
|
|
|
|
contextGroup.interrupt();
|
|
|
|
event.set();
|
|
|
|
});
|
|
|
|
|
|
|
|
contextGroup.spawn([&] {
|
|
|
|
ASSERT_THROW(connector.connect(Ipv4Address("10.255.255.1"), 6666), InterruptedException);
|
|
|
|
});
|
|
|
|
contextGroup.wait();
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TEST_F(TcpConnectorTests, tcpConnectorUseAfterInterrupt) {
|
2015-05-27 12:08:46 +00:00
|
|
|
TcpConnector connector(dispatcher);
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.spawn([&]() {
|
2015-05-27 12:08:46 +00:00
|
|
|
Timer(dispatcher).sleep(std::chrono::milliseconds(10));
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.interrupt();
|
2015-05-27 12:08:46 +00:00
|
|
|
event.set();
|
|
|
|
});
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
contextGroup.spawn([&] {
|
|
|
|
ASSERT_THROW(connector.connect(Ipv4Address("10.255.255.1"), 6666), InterruptedException);
|
|
|
|
});
|
|
|
|
contextGroup.wait();
|
|
|
|
contextGroup.spawn([&] {
|
|
|
|
ASSERT_NO_THROW(connector.connect(Ipv4Address("127.0.0.1"), 6666));
|
|
|
|
});
|
|
|
|
contextGroup.wait();
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TEST_F(TcpConnectorTests, bindToTheSameAddressFails) {
|
2015-05-27 12:08:46 +00:00
|
|
|
ASSERT_THROW(TcpListener listener2(dispatcher, Ipv4Address("127.0.0.1"), 6666), std::runtime_error);
|
|
|
|
}
|