瀏覽代碼

tests for basic connection, handshake errors, timeout

master
Robin Thoni 11 年之前
父節點
當前提交
b4ab172144
共有 5 個檔案被更改,包括 452 行新增47 行删除
  1. 104
    8
      libptsocket/ptsocket.cpp
  2. 23
    1
      libptsocket/ptsocket.h
  3. 3
    4
      tests/main.cpp
  4. 274
    22
      tests/test.cpp
  5. 48
    12
      tests/test.h

+ 104
- 8
libptsocket/ptsocket.cpp 查看文件

@@ -3,9 +3,15 @@
3 3
 
4 4
 PTSocket::PTSocket(QObject* p) : QTcpSocket(p)
5 5
 {
6
+#ifdef PT_DEBUG
7
+    m_willPing = true;
8
+    dbgSetWillHandshake(true);
9
+#endif
6 10
     setSocketOption(QAbstractSocket::LowDelayOption, 1);
7 11
     m_packetSize = 0;
12
+    m_timeoutTimer.setSingleShot(true);
8 13
     setTimeout(5000);
14
+    m_pingTimer.setSingleShot(true);
9 15
     setPingInterval(1000 * 7);
10 16
     m_state = Disconnected;
11 17
     m_isServerSide = false;
@@ -31,6 +37,11 @@ bool PTSocket::isServerSide() const
31 37
     return m_isServerSide;
32 38
 }
33 39
 
40
+QString PTSocket::getError() const
41
+{
42
+    return (m_error.isEmpty()? errorString() : m_error);
43
+}
44
+
34 45
 QByteArray PTSocket::handshakeData()
35 46
 {
36 47
     return "LibPTSocketv1";
@@ -64,6 +75,51 @@ void PTSocket::send(int pktType, QByteArray data)
64 75
     send(false, pktType, data);
65 76
 }
66 77
 
78
+void PTSocket::disconnectFromServer()
79
+{
80
+    disconnectFromHost();
81
+}
82
+
83
+void PTSocket::dbgSetWillPing(bool w)
84
+{
85
+#ifdef PT_DEBUG
86
+    m_willPing = w;
87
+#endif
88
+}
89
+
90
+void PTSocket::dbgSetWillHandshake(bool w)
91
+{
92
+#ifdef PT_DEBUG
93
+    m_willHandshake = w;
94
+    m_willHandshakeFake1 = false;
95
+    m_willHandshakeFake2 = false;
96
+#endif
97
+}
98
+
99
+void PTSocket::dbgSetWillHandshakeFake1(bool w)
100
+{
101
+#ifdef PT_DEBUG
102
+    m_willHandshakeFake1 = w;
103
+    if(!w)
104
+        m_willHandshake = true;
105
+    else
106
+        m_willHandshake = false;
107
+    m_willHandshakeFake2 = false;
108
+#endif
109
+}
110
+
111
+void PTSocket::dbgSetWillHandshakeFake2(bool w)
112
+{
113
+#ifdef PT_DEBUG
114
+    m_willHandshakeFake2 = w;
115
+    if(!w)
116
+        m_willHandshake = true;
117
+    else
118
+        m_willHandshake = false;
119
+    m_willHandshakeFake1 = false;
120
+#endif
121
+}
122
+
67 123
 void PTSocket::send(bool sys, int pktType, QByteArray data)
68 124
 {
69 125
     if(data.isEmpty())
@@ -79,7 +135,13 @@ void PTSocket::m_ping()
79 135
     m_pingTimer.stop();
80 136
     if(m_timeoutTimer.isActive())
81 137
         return;
138
+
139
+#ifdef PT_DEBUG
140
+    if(m_willPing)
141
+        send(true, PingRequest);
142
+#else
82 143
     send(true, PingRequest);
144
+#endif
83 145
     m_timeoutTimer.start();
84 146
 }
85 147
 
@@ -107,7 +169,14 @@ void PTSocket::m_readyRead()
107 169
                 if(sys)
108 170
                 {
109 171
                     if(pktType == PingRequest)
172
+                    {
173
+#ifdef PT_DEBUG
174
+                        if(m_willPing)
175
+                            send(true, PingAnswer);
176
+#else
110 177
                         send(true, PingAnswer);
178
+#endif
179
+                    }
111 180
                     //else if(pktType == PingAnswer)
112 181
                 }
113 182
                 else
@@ -169,7 +238,16 @@ void PTSocket::m_stateChanged(QAbstractSocket::SocketState s)
169 238
     {
170 239
         m_packetSize = 0;
171 240
         setState(Connected);
241
+#ifdef PT_DEBUG
242
+        if(m_willHandshakeFake1)
243
+            write("LibPTSocketv");
244
+        else if(m_willHandshakeFake2)
245
+            write("42LibPTSocketv4242");
246
+        else if(m_willHandshake)
247
+            write(handshakeData());
248
+#else
172 249
         write(handshakeData());
250
+#endif
173 251
     }
174 252
     else if(s == QAbstractSocket::ClosingState)
175 253
         setState(Disconnecting);
@@ -181,32 +259,43 @@ void PTSocket::m_stateChanged(QAbstractSocket::SocketState s)
181 259
     }
182 260
 }
183 261
 
184
-void PTSocket::m_socketError(QAbstractSocket::SocketError)
262
+void PTSocket::m_socketError(QAbstractSocket::SocketError s)
185 263
 {
186
-    setState(Error);
264
+    if(s != QAbstractSocket::RemoteHostClosedError)
265
+    {
266
+        qDebug()<<"Socket error:"<<s;
267
+        setState(Error);
268
+    }
187 269
 }
188 270
 
189 271
 void PTSocket::setState(PTSocket::State s)
190 272
 {
191
-    if(s == m_state || (s == Error && (m_state == Disconnected || m_state == Disconnecting)))
273
+    if(s == m_state)
274
+        return;
275
+    if(s == Error && (m_state == Disconnected || m_state == Disconnecting))
192 276
         return;
277
+    if(m_state == Error && (s == Disconnected || s == Disconnecting))
278
+        return;
279
+
280
+    m_state = s;
281
+
193 282
     if(s == Error)
194 283
         abort();
284
+    else
285
+        m_error.clear();
195 286
     if(s == Handshaked)
196 287
         m_pingTimer.start();
197 288
     else
198 289
         m_pingTimer.stop();
199
-    if(m_state == Error && (s != HostLookUp && s != Connecting))
200
-        return;
201
-    m_state = s;
290
+
202 291
     emit stateChanged(m_state);
203 292
 }
204 293
 
205 294
 void PTSocket::setError(QString e)
206 295
 {
207
-    if(m_state == Error)
296
+    if(m_state == Error || m_state == Disconnected || m_state == Disconnecting)
208 297
         return;
209
-    setErrorString(e);
298
+    m_error = e;
210 299
     setState(Error);
211 300
 }
212 301
 
@@ -225,3 +314,10 @@ int PTSocket::byteArrayToInt(QByteArray d)
225 314
         a |= (unsigned int)((unsigned char)d.at(i) << i * 8);
226 315
     return a;
227 316
 }
317
+
318
+QDebug operator <<(QDebug dbg, const PTSocket::State s)
319
+{
320
+    const QMetaObject & mo = PTSocket::staticMetaObject;
321
+    QMetaEnum me = mo.enumerator(mo.indexOfEnumerator("State"));
322
+    return dbg<<me.valueToKey(s);
323
+}

+ 23
- 1
libptsocket/ptsocket.h 查看文件

@@ -6,6 +6,9 @@
6 6
 #include <QTcpSocket>
7 7
 #include <QHostAddress>
8 8
 #include <QTimer>
9
+#include <QMetaEnum>
10
+
11
+#define PT_DEBUG
9 12
 
10 13
 class LIBPTSOCKETSHARED_EXPORT PTSocket : public QTcpSocket
11 14
 {
@@ -21,10 +24,12 @@ public:
21 24
         Disconnecting,
22 25
         Error
23 26
     };
27
+    Q_ENUMS(State)
24 28
 	PTSocket(QObject* p);
25 29
 	int getTimeout() const;
26 30
     int getPingInterval() const;
27 31
     bool isServerSide() const;
32
+    QString getError() const;
28 33
 
29 34
     static QByteArray handshakeData();
30 35
 
@@ -40,6 +45,12 @@ public slots:
40 45
     void setPingInterval(int p);
41 46
     void setIsServerSide(bool s);
42 47
     void send(int pktType, QByteArray data);
48
+    void disconnectFromServer();
49
+
50
+    void dbgSetWillPing(bool w);
51
+    void dbgSetWillHandshake(bool w);
52
+    void dbgSetWillHandshakeFake1(bool w);
53
+    void dbgSetWillHandshakeFake2(bool w);
43 54
 
44 55
 private slots:
45 56
     void m_ping();
@@ -47,7 +58,7 @@ private slots:
47 58
     void m_timedout();
48 59
     void m_handshakeError();
49 60
     void m_stateChanged(QAbstractSocket::SocketState s);
50
-    void m_socketError(QAbstractSocket::SocketError);
61
+    void m_socketError(QAbstractSocket::SocketError s);
51 62
     void setState(State s);
52 63
     void setError(QString e);
53 64
     void send(bool sys, int pktType, QByteArray data = "");
@@ -60,11 +71,22 @@ private:
60 71
     State m_state;
61 72
     bool m_isServerSide;
62 73
     unsigned int m_packetSize;
74
+    QString m_error;
63 75
     enum PacketType
64 76
     {
65 77
         PingRequest = 1,
66 78
         PingAnswer = 2
67 79
     };
80
+#ifdef PT_DEBUG
81
+    bool m_willPing;
82
+    bool m_willHandshake;
83
+    bool m_willHandshakeFake1;
84
+    bool m_willHandshakeFake2;
85
+#endif
86
+#ifdef PT_DEBUG
87
+#endif
68 88
 };
69 89
 
90
+QDebug operator <<(QDebug dbg, const PTSocket::State s);
91
+
70 92
 #endif // PTSOCKET_H

+ 3
- 4
tests/main.cpp 查看文件

@@ -5,10 +5,9 @@
5 5
 int main(int argc, char *argv[])
6 6
 {
7 7
 	QCoreApplication a(argc, argv);
8
-	Test t;
9
-    t.testIntConvert();
10
-	t.testListen(6950);
11
-	t.testNewConnection();
8
+    Test t;
9
+    t.testListen(6950);
10
+    t.test0IntConvert();
12 11
 	
13 12
 	return a.exec();
14 13
 }

+ 274
- 22
tests/test.cpp 查看文件

@@ -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
 }

+ 48
- 12
tests/test.h 查看文件

@@ -2,33 +2,69 @@
2 2
 #define TEST_H
3 3
 
4 4
 #include <QObject>
5
+#include <QCoreApplication>
6
+#include <QPair>
5 7
 #include "ptserver.h"
6 8
 #include "ptsocket.h"
7 9
 #include <QDebug>
8 10
 
9 11
 #define ASSERT_LISTEN Q_ASSERT(m_serv->isListening())
10 12
 
13
+typedef QPair<int, PTSocket::State> TestState;
14
+
11 15
 class Test : public QObject
12 16
 {
13 17
 	Q_OBJECT
14 18
 public:
15
-	explicit Test(QObject *parent = 0);
16
-	
17
-signals:
18
-	
19
+    explicit Test(QObject *parent = 0);
20
+
19 21
 public slots:
20
-    void testIntConvert();
21
-	void testListen(quint16 p);
22
-	void testNewConnection();
22
+    void testListen(quint16 p);
23
+    void test0IntConvert();
24
+    void test1Basic();
25
+    void test2Ping();
26
+    void test3Handshake();
27
+    void test4HandshakeFake1();
28
+    void test5HandshakeFake2();
23 29
 
24 30
 private slots:
25
-	void m_newConnection();
26
-	void m_newClient(PTSocket* s);
27
-    void m_clientStateChanged(PTSocket::State s);
31
+    void timeout(int i = 15000);
32
+    void stopTimeout();
33
+    void timedout();
34
+    void m_clientStateChangedTest(PTSocket::State s);
35
+    void m_newClientTest(PTSocket* s);
36
+
37
+    void preTest();
38
+
39
+	void m_newClient1(PTSocket* s);
40
+    void m_clientStateChanged1(PTSocket::State s);
41
+
42
+    void m_newClient2(PTSocket* s);
43
+    void m_clientStateChanged2(PTSocket::State s);
44
+
45
+    void m_newClient3(PTSocket* s);
46
+    void m_clientStateChanged3(PTSocket::State s);
47
+
48
+    void m_newClient4(PTSocket* s);
49
+    void m_clientStateChanged4(PTSocket::State s);
50
+
51
+    void m_newClient5(PTSocket* s);
52
+    void m_clientStateChanged5(PTSocket::State s);
53
+
28 54
 
29 55
 private:
30
-	PTServer* m_serv;
31
-	QList<PTSocket*> m_clients;
56
+    PTSocket* newSocket();
57
+    QTimer m_timeoutTimer;
58
+    PTServer* m_serv;
59
+    QMap<PTSocket*, int> m_socketsIds;
60
+    QList<QList<TestState> > m_testsStates;
61
+    int m_currentTest;
62
+    int m_currentState;
63
+
32 64
 };
33 65
 
66
+bool operator ==(TestState t1, TestState t2);
67
+bool operator !=(TestState t1, TestState t2);
68
+QDebug operator <<(QDebug dbg, TestState t);
69
+
34 70
 #endif // TEST_H

Loading…
取消
儲存