Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "test.h"
  2. Test::Test(QObject *parent) : QObject(parent)
  3. {
  4. m_serv = new PTServer(this);
  5. m_timeoutTimer.setSingleShot(true);
  6. connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(timedout()));
  7. m_testsStates<<(QList<TestState>());
  8. m_testsStates<<(QList<TestState>()//Basic
  9. <<TestState(1, PTSocket::HostLookUp)
  10. <<TestState(1, PTSocket::Connecting)
  11. <<TestState(1, PTSocket::Connected)
  12. <<TestState(1, PTSocket::Handshaked)
  13. <<TestState(1, PTSocket::Disconnecting)
  14. <<TestState(1, PTSocket::Disconnected)
  15. <<TestState(2, PTSocket::Disconnecting)
  16. <<TestState(2, PTSocket::Disconnected));
  17. m_testsStates<<(QList<TestState>()//Ping
  18. <<TestState(1, PTSocket::HostLookUp)
  19. <<TestState(1, PTSocket::Connecting)
  20. <<TestState(1, PTSocket::Connected)
  21. <<TestState(1, PTSocket::Handshaked)
  22. <<TestState(2, PTSocket::Error)
  23. <<TestState(1, PTSocket::Disconnecting)
  24. <<TestState(1, PTSocket::Disconnected));
  25. m_testsStates<<(QList<TestState>()//Handshake
  26. <<TestState(1, PTSocket::HostLookUp)
  27. <<TestState(1, PTSocket::Connecting)
  28. <<TestState(1, PTSocket::Connected)
  29. <<TestState(1, PTSocket::Handshaked)
  30. <<TestState(1, PTSocket::Disconnecting)
  31. <<TestState(1, PTSocket::Disconnected));
  32. m_testsStates<<(QList<TestState>()//Handshake Fake 1
  33. <<TestState(1, PTSocket::HostLookUp)
  34. <<TestState(1, PTSocket::Connecting)
  35. <<TestState(1, PTSocket::Connected)
  36. <<TestState(1, PTSocket::Handshaked)
  37. <<TestState(1, PTSocket::Disconnecting)
  38. <<TestState(1, PTSocket::Disconnected));
  39. m_testsStates<<(QList<TestState>()//Handshake Fake 2
  40. <<TestState(1, PTSocket::HostLookUp)
  41. <<TestState(1, PTSocket::Connecting)
  42. <<TestState(1, PTSocket::Connected)
  43. <<TestState(1, PTSocket::Handshaked)
  44. <<TestState(1, PTSocket::Disconnecting)
  45. <<TestState(1, PTSocket::Disconnected));
  46. m_testsStates<<(QList<TestState>()//Error Invalid Host
  47. <<TestState(1, PTSocket::HostLookUp)
  48. <<TestState(1, PTSocket::Error));
  49. m_testsStates<<(QList<TestState>()//Error Invalid Port
  50. <<TestState(1, PTSocket::HostLookUp)
  51. <<TestState(1, PTSocket::Connecting)
  52. <<TestState(1, PTSocket::Error));
  53. m_currentState = 0;
  54. m_currentTest = -1;
  55. }
  56. void Test::test0IntConvert()
  57. {
  58. preTest();
  59. QList<int> tests;
  60. tests << 0 << 1 << 255 << 256 << 65535 << 65536 << -1 << -65535 << -65536 << 123456789 << -123456789;
  61. for(int i = 0; i < tests.size(); ++i)
  62. {
  63. if(PTSocket::byteArrayToInt(PTSocket::intToByteArray(tests[i])) != tests[i])
  64. {
  65. qDebug()<<tests[i]<<PTSocket::intToByteArray(tests[i])<<PTSocket::byteArrayToInt(PTSocket::intToByteArray(tests[i]));
  66. Q_ASSERT(false);
  67. }
  68. }
  69. timeout(0);
  70. }
  71. void Test::testListen(quint16 p)
  72. {
  73. Q_ASSERT(m_serv->listen(QHostAddress::Any, p));
  74. }
  75. void Test::test1Basic()
  76. {
  77. preTest();
  78. connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient1(PTSocket*)));
  79. PTSocket* sock = newSocket();
  80. connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged1(PTSocket::State)));
  81. m_socketsIds.insert(sock, 1);
  82. sock->connectToHost("127.0.0.1", m_serv->serverPort());
  83. }
  84. void Test::test2Ping()
  85. {
  86. preTest();
  87. connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient2(PTSocket*)));
  88. PTSocket* sock = newSocket();
  89. sock->dbgSetWillPing(false);
  90. connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged2(PTSocket::State)));
  91. m_socketsIds.insert(sock, 1);
  92. sock->connectToHost("127.0.0.1", m_serv->serverPort());
  93. }
  94. void Test::test3Handshake()
  95. {
  96. preTest();
  97. connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient3(PTSocket*)));
  98. PTSocket* sock = newSocket();
  99. sock->dbgSetWillHandshake(false);
  100. connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged3(PTSocket::State)));
  101. m_socketsIds.insert(sock, 1);
  102. sock->connectToHost("127.0.0.1", m_serv->serverPort());
  103. }
  104. void Test::test4HandshakeFake1()
  105. {
  106. preTest();
  107. connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient4(PTSocket*)));
  108. PTSocket* sock = newSocket();
  109. sock->dbgSetWillHandshakeFake1(true);
  110. connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged4(PTSocket::State)));
  111. m_socketsIds.insert(sock, 1);
  112. sock->connectToHost("127.0.0.1", m_serv->serverPort());
  113. }
  114. void Test::test5HandshakeFake2()
  115. {
  116. preTest();
  117. connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient5(PTSocket*)));
  118. PTSocket* sock = newSocket();
  119. sock->dbgSetWillHandshakeFake2(true);
  120. connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged5(PTSocket::State)));
  121. m_socketsIds.insert(sock, 1);
  122. sock->connectToHost("127.0.0.1", m_serv->serverPort());
  123. }
  124. void Test::test6Error1()
  125. {
  126. preTest();
  127. PTSocket* sock = newSocket();
  128. m_socketsIds.insert(sock, 1);
  129. sock->connectToHost("", m_serv->serverPort());
  130. }
  131. void Test::test7Error2()
  132. {
  133. preTest();
  134. PTSocket* sock = newSocket();
  135. m_socketsIds.insert(sock, 1);
  136. sock->connectToHost("127.0.0.1", 15963);
  137. }
  138. void Test::timeout(int i)
  139. {
  140. m_timeoutTimer.start(i);
  141. }
  142. void Test::stopTimeout()
  143. {
  144. m_timeoutTimer.stop();
  145. }
  146. void Test::timedout()
  147. {
  148. if(m_currentState == m_testsStates.at(m_currentTest).size())
  149. {
  150. qDebug()<<"\n\nTest"<<m_currentTest<<"OK\n\n";
  151. switch(m_currentTest)
  152. {
  153. case 0:
  154. test1Basic();
  155. break;
  156. case 1:
  157. test2Ping();
  158. break;
  159. case 2:
  160. test3Handshake();
  161. break;
  162. case 3:
  163. test4HandshakeFake1();
  164. break;
  165. case 4:
  166. test5HandshakeFake2();
  167. break;
  168. case 5:
  169. test6Error1();
  170. break;
  171. case 6:
  172. test7Error2();
  173. break;
  174. default:
  175. qApp->quit();
  176. }
  177. }
  178. else
  179. {
  180. qDebug()<<"Timedout\n\n\n";
  181. qDebug()<<m_currentState+1<<m_testsStates.at(m_currentTest).at(m_currentState)<<m_testsStates.at(m_currentTest);
  182. Q_ASSERT(false);
  183. }
  184. }
  185. void Test::m_clientStateChangedTest(PTSocket::State s)
  186. {
  187. PTSocket* sock = qobject_cast<PTSocket*>(sender());
  188. Q_ASSERT(sock != 0);
  189. qDebug()<<"State"<<(sock->isServerSide()? "server": "client")<<sock<<s;
  190. if(s == PTSocket::Error)
  191. qDebug()<<"\t"<<sock->getError();
  192. if(m_currentState >= m_testsStates.at(m_currentTest).size())
  193. {
  194. qDebug()<<"Out of TestState\n\n\n"<<m_socketsIds[sock]<<s;
  195. Q_ASSERT(false);
  196. }
  197. TestState t = m_testsStates.at(m_currentTest).at(m_currentState);
  198. if(!m_socketsIds.keys().contains(sock) && !m_socketsIds.values().contains(t.first))
  199. m_socketsIds.insert(sock, t.first);
  200. TestState r(m_socketsIds[sock], s);
  201. if(r != t)
  202. {
  203. qDebug()<<"Test not matching\n\n\n"<<"Expected"<<t<<"but got"<<r;
  204. Q_ASSERT(false);
  205. }
  206. ++m_currentState;
  207. if(m_currentState == m_testsStates.at(m_currentTest).size())
  208. timeout(1500);
  209. else
  210. timeout();
  211. }
  212. void Test::m_newClientTest(PTSocket *s)
  213. {
  214. qDebug()<<"New client:"<<s->peerAddress().toString()<<s->peerPort();
  215. timeout();
  216. connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChangedTest(PTSocket::State)));
  217. }
  218. void Test::preTest()
  219. {
  220. ASSERT_LISTEN;
  221. ++m_currentTest;
  222. m_currentState = 0;
  223. m_socketsIds.clear();
  224. m_serv->disconnect(this);
  225. connect(m_serv, SIGNAL(newConnection()), this, SLOT(timeout()));
  226. connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClientTest(PTSocket*)));
  227. }
  228. void Test::m_newClient1(PTSocket* s)
  229. {
  230. connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged1(PTSocket::State)));
  231. }
  232. void Test::m_clientStateChanged1(PTSocket::State s)
  233. {
  234. PTSocket* sock = qobject_cast<PTSocket*>(sender());
  235. if(s == PTSocket::Handshaked)
  236. {
  237. qDebug()<<"";
  238. QTimer::singleShot(1500, sock, SLOT(disconnectFromServer()));
  239. }
  240. }
  241. void Test::m_newClient2(PTSocket* s)
  242. {
  243. connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged2(PTSocket::State)));
  244. }
  245. void Test::m_clientStateChanged2(PTSocket::State s)
  246. {
  247. if(s == PTSocket::Handshaked)
  248. qDebug()<<"";
  249. }
  250. void Test::m_newClient3(PTSocket* s)
  251. {
  252. connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged3(PTSocket::State)));
  253. }
  254. void Test::m_clientStateChanged3(PTSocket::State s)
  255. {
  256. if(s == PTSocket::Handshaked)
  257. qDebug()<<"";
  258. }
  259. void Test::m_newClient4(PTSocket* s)
  260. {
  261. connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged4(PTSocket::State)));
  262. }
  263. void Test::m_clientStateChanged4(PTSocket::State s)
  264. {
  265. if(s == PTSocket::Handshaked)
  266. qDebug()<<"";
  267. }
  268. void Test::m_newClient5(PTSocket* s)
  269. {
  270. connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged5(PTSocket::State)));
  271. }
  272. void Test::m_clientStateChanged5(PTSocket::State s)
  273. {
  274. if(s == PTSocket::Handshaked)
  275. qDebug()<<"";
  276. }
  277. PTSocket *Test::newSocket()
  278. {
  279. PTSocket* sock = new PTSocket(this);
  280. connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChangedTest(PTSocket::State)));
  281. return sock;
  282. }
  283. bool operator ==(TestState t1, TestState t2)
  284. {
  285. return t1.first == t2.first && t1.second == t2.second;
  286. }
  287. bool operator !=(TestState t1, TestState t2)
  288. {
  289. return !(t1 == t2);
  290. }
  291. QDebug operator <<(QDebug dbg, TestState t)
  292. {
  293. return dbg<<"TestState("<<t.first<<","<<t.second<<")";
  294. }