#include "test.h" Test::Test(QObject *parent) : QObject(parent) { m_serv = new PTServer(this); m_timeoutTimer.setSingleShot(true); connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(timedout())); m_testsStates<<(QList()); m_testsStates<<(QList()//Basic <()//Ping <()//Handshake <()//Handshake Fake 1 <()//Handshake Fake 2 <()//Error Invalid Host <()//Error Invalid Port < tests; tests << 0 << 1 << 255 << 256 << 65535 << 65536 << -1 << -65535 << -65536 << 123456789 << -123456789; for(int i = 0; i < tests.size(); ++i) { if(PTSocket::byteArrayToInt(PTSocket::intToByteArray(tests[i])) != tests[i]) { qDebug()<listen(QHostAddress::Any, p)); } void Test::test1Basic() { preTest(); connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient1(PTSocket*))); PTSocket* sock = newSocket(); connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged1(PTSocket::State))); m_socketsIds.insert(sock, 1); sock->connectToHost("127.0.0.1", m_serv->serverPort()); } void Test::test2Ping() { preTest(); connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient2(PTSocket*))); PTSocket* sock = newSocket(); sock->dbgSetWillPing(false); connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged2(PTSocket::State))); m_socketsIds.insert(sock, 1); sock->connectToHost("127.0.0.1", m_serv->serverPort()); } void Test::test3Handshake() { preTest(); connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient3(PTSocket*))); PTSocket* sock = newSocket(); sock->dbgSetWillHandshake(false); connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged3(PTSocket::State))); m_socketsIds.insert(sock, 1); sock->connectToHost("127.0.0.1", m_serv->serverPort()); } void Test::test4HandshakeFake1() { preTest(); connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient4(PTSocket*))); PTSocket* sock = newSocket(); sock->dbgSetWillHandshakeFake1(true); connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged4(PTSocket::State))); m_socketsIds.insert(sock, 1); sock->connectToHost("127.0.0.1", m_serv->serverPort()); } void Test::test5HandshakeFake2() { preTest(); connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient5(PTSocket*))); PTSocket* sock = newSocket(); sock->dbgSetWillHandshakeFake2(true); connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged5(PTSocket::State))); m_socketsIds.insert(sock, 1); sock->connectToHost("127.0.0.1", m_serv->serverPort()); } void Test::test6Error1() { preTest(); PTSocket* sock = newSocket(); m_socketsIds.insert(sock, 1); sock->connectToHost("", m_serv->serverPort()); } void Test::test7Error2() { preTest(); PTSocket* sock = newSocket(); m_socketsIds.insert(sock, 1); sock->connectToHost("127.0.0.1", 15963); } void Test::timeout(int i) { m_timeoutTimer.start(i); } void Test::stopTimeout() { m_timeoutTimer.stop(); } void Test::timedout() { if(m_currentState == m_testsStates.at(m_currentTest).size()) { qDebug()<<"\n\nTest"<quit(); } } else { qDebug()<<"Timedout\n\n\n"; qDebug()<(sender()); Q_ASSERT(sock != 0); qDebug()<<"State"<<(sock->isServerSide()? "server": "client")<getError(); if(m_currentState >= m_testsStates.at(m_currentTest).size()) { qDebug()<<"Out of TestState\n\n\n"<peerAddress().toString()<peerPort(); timeout(); connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChangedTest(PTSocket::State))); } void Test::preTest() { ASSERT_LISTEN; ++m_currentTest; m_currentState = 0; m_socketsIds.clear(); m_serv->disconnect(this); connect(m_serv, SIGNAL(newConnection()), this, SLOT(timeout())); connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClientTest(PTSocket*))); } void Test::m_newClient1(PTSocket* s) { connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged1(PTSocket::State))); } void Test::m_clientStateChanged1(PTSocket::State s) { PTSocket* sock = qobject_cast(sender()); if(s == PTSocket::Handshaked) { qDebug()<<""; QTimer::singleShot(1500, sock, SLOT(disconnectFromServer())); } } void Test::m_newClient2(PTSocket* s) { connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged2(PTSocket::State))); } void Test::m_clientStateChanged2(PTSocket::State s) { if(s == PTSocket::Handshaked) qDebug()<<""; } void Test::m_newClient3(PTSocket* s) { connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged3(PTSocket::State))); } void Test::m_clientStateChanged3(PTSocket::State s) { if(s == PTSocket::Handshaked) qDebug()<<""; } void Test::m_newClient4(PTSocket* s) { connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged4(PTSocket::State))); } void Test::m_clientStateChanged4(PTSocket::State s) { if(s == PTSocket::Handshaked) qDebug()<<""; } void Test::m_newClient5(PTSocket* s) { connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged5(PTSocket::State))); } void Test::m_clientStateChanged5(PTSocket::State s) { if(s == PTSocket::Handshaked) qDebug()<<""; } PTSocket *Test::newSocket() { PTSocket* sock = new PTSocket(this); connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChangedTest(PTSocket::State))); return sock; } bool operator ==(TestState t1, TestState t2) { return t1.first == t2.first && t1.second == t2.second; } bool operator !=(TestState t1, TestState t2) { return !(t1 == t2); } QDebug operator <<(QDebug dbg, TestState t) { return dbg<<"TestState("<