123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #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<TestState>());
- m_testsStates<<(QList<TestState>()//Basic
- <<TestState(1, PTSocket::HostLookUp)
- <<TestState(1, PTSocket::Connecting)
- <<TestState(1, PTSocket::Connected)
- <<TestState(1, PTSocket::Handshaked)
- <<TestState(1, PTSocket::Disconnecting)
- <<TestState(1, PTSocket::Disconnected)
- <<TestState(2, PTSocket::Disconnecting)
- <<TestState(2, PTSocket::Disconnected));
- m_testsStates<<(QList<TestState>()//Ping
- <<TestState(1, PTSocket::HostLookUp)
- <<TestState(1, PTSocket::Connecting)
- <<TestState(1, PTSocket::Connected)
- <<TestState(1, PTSocket::Handshaked)
- <<TestState(2, PTSocket::Error)
- <<TestState(1, PTSocket::Disconnecting)
- <<TestState(1, PTSocket::Disconnected));
- m_testsStates<<(QList<TestState>()//Handshake
- <<TestState(1, PTSocket::HostLookUp)
- <<TestState(1, PTSocket::Connecting)
- <<TestState(1, PTSocket::Connected)
- <<TestState(1, PTSocket::Handshaked)
- <<TestState(1, PTSocket::Disconnecting)
- <<TestState(1, PTSocket::Disconnected));
- m_testsStates<<(QList<TestState>()//Handshake Fake 1
- <<TestState(1, PTSocket::HostLookUp)
- <<TestState(1, PTSocket::Connecting)
- <<TestState(1, PTSocket::Connected)
- <<TestState(1, PTSocket::Handshaked)
- <<TestState(1, PTSocket::Disconnecting)
- <<TestState(1, PTSocket::Disconnected));
- m_testsStates<<(QList<TestState>()//Handshake Fake 2
- <<TestState(1, PTSocket::HostLookUp)
- <<TestState(1, PTSocket::Connecting)
- <<TestState(1, PTSocket::Connected)
- <<TestState(1, PTSocket::Handshaked)
- <<TestState(1, PTSocket::Disconnecting)
- <<TestState(1, PTSocket::Disconnected));
- m_testsStates<<(QList<TestState>()//Error Invalid Host
- <<TestState(1, PTSocket::HostLookUp)
- <<TestState(1, PTSocket::Error));
- m_testsStates<<(QList<TestState>()//Error Invalid Port
- <<TestState(1, PTSocket::HostLookUp)
- <<TestState(1, PTSocket::Connecting)
- <<TestState(1, PTSocket::Error));
- m_currentState = 0;
- m_currentTest = -1;
- }
-
- void Test::test0IntConvert()
- {
- preTest();
- QList<int> 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()<<tests[i]<<PTSocket::intToByteArray(tests[i])<<PTSocket::byteArrayToInt(PTSocket::intToByteArray(tests[i]));
- Q_ASSERT(false);
- }
- }
- timeout(0);
- }
-
- void Test::testListen(quint16 p)
- {
- Q_ASSERT(m_serv->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"<<m_currentTest<<"OK\n\n";
-
- switch(m_currentTest)
- {
- case 0:
- test1Basic();
- break;
- case 1:
- test2Ping();
- break;
- case 2:
- test3Handshake();
- break;
- case 3:
- test4HandshakeFake1();
- break;
- case 4:
- test5HandshakeFake2();
- break;
- case 5:
- test6Error1();
- break;
- case 6:
- test7Error2();
- break;
- default:
- qApp->quit();
- }
-
- }
- else
- {
- qDebug()<<"Timedout\n\n\n";
- qDebug()<<m_currentState+1<<m_testsStates.at(m_currentTest).at(m_currentState)<<m_testsStates.at(m_currentTest);
- Q_ASSERT(false);
- }
- }
-
- void Test::m_clientStateChangedTest(PTSocket::State s)
- {
- PTSocket* sock = qobject_cast<PTSocket*>(sender());
- Q_ASSERT(sock != 0);
- qDebug()<<"State"<<(sock->isServerSide()? "server": "client")<<sock<<s;
- if(s == PTSocket::Error)
- qDebug()<<"\t"<<sock->getError();
- if(m_currentState >= m_testsStates.at(m_currentTest).size())
- {
- qDebug()<<"Out of TestState\n\n\n"<<m_socketsIds[sock]<<s;
- Q_ASSERT(false);
- }
- TestState t = m_testsStates.at(m_currentTest).at(m_currentState);
- if(!m_socketsIds.keys().contains(sock) && !m_socketsIds.values().contains(t.first))
- m_socketsIds.insert(sock, t.first);
-
- TestState r(m_socketsIds[sock], s);
-
- if(r != t)
- {
- qDebug()<<"Test not matching\n\n\n"<<"Expected"<<t<<"but got"<<r;
- Q_ASSERT(false);
- }
- ++m_currentState;
- if(m_currentState == m_testsStates.at(m_currentTest).size())
- timeout(1500);
- else
- timeout();
- }
-
- void Test::m_newClientTest(PTSocket *s)
- {
- qDebug()<<"New client:"<<s->peerAddress().toString()<<s->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<PTSocket*>(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("<<t.first<<","<<t.second<<")";
- }
|