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