-
Notifications
You must be signed in to change notification settings - Fork 0
/
ositransport-test.cpp
340 lines (265 loc) · 12.6 KB
/
ositransport-test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "ositransport-test.h"
const char OsiTransportTest::testData[] = {
(char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f, (char) 0x00, (char) 0x01, (char) 0x02, (char) 0x03, (char) 0x04, (char) 0x05, (char) 0x06,
(char) 0x07, (char) 0x08, (char) 0x09, (char) 0x0a, (char) 0x0b, (char) 0x0c, (char) 0x0d, (char) 0x0e,
(char) 0x0f
};
void OsiTransportTest::startServer()
{
checkServerConnected = false;
checkServerTransportConnected = false;
checkServerErrorConnected = false;
checkServerErrorTransfer = false;
checkIllegalArg = false;
checkIllegalClassMbr = false;
// Create server and start listening
qint32 port = 18982;
pServer = new CServerTSAP(port);
pServer->setMessageTimeout(500);
pServer->setMessageFragmentTimeout(500);
pConnectionListener = pServer->createServer();
// server slots
connect(pConnectionListener, SIGNAL(signalConnected(const CConnection*)), this, SLOT(slotServerClientConnected(const CConnection*)));
connect(pConnectionListener, SIGNAL(signalDisconnected(const CConnection*)), this, SLOT(slotServerClientDisconnected(const CConnection*)));
connect(pConnectionListener, SIGNAL(signalTSduReady(const CConnection*)), this, SLOT(slotServerTSduReady(const CConnection*)));
connect(pConnectionListener, SIGNAL(signalCRReady(const CConnection*)), this, SLOT(slotServerCRReady(const CConnection*)));
connect(pConnectionListener, SIGNAL(signalIOError(QString)), this, SLOT(slotServerIOError(QString)));
pServer->startListening();
}
void OsiTransportTest::startClient()
{
checkClientConnected = false;
checkClientErrorConnected = false;
checkClientErrorTransfer = false;
// Create client and start connection
pClient = new CClientTSAP(*CSocketFactory::getSocketFactory());
// client's error slots
connect(pClient, SIGNAL(signalIllegalArgument(QString)), this, SLOT(slotIllegalArgument(QString)));
connect(pClient, SIGNAL(signalConnectError(QString)), this, SLOT(slotConnectError(QString)));
connect(pClient, SIGNAL(signalIllegalClassMember(QString)), this, SLOT(slotIllegalClassMember(QString)));
pClient->setMaxTPDUSizeParam(7);
pClient->setMessageTimeout(5000);
pClient->setMessageFragmentTimeout(5000);
QHostAddress address("127.0.0.1");
qint32 port = 18982;
pConnection = pClient->createConnection(address, port);
// connection slots
connect(pConnection, SIGNAL(signalConnectionReady(const CConnection*)), this, SLOT(slotConnectionReady(const CConnection*)));
connect(pConnection, SIGNAL(signalConnectionClosed(const CConnection*)), this, SLOT(slotConnectionClosed(const CConnection*)));
connect(pConnection, SIGNAL(signalTSduReady(const CConnection*)), this, SLOT(slotClientTSduReady(const CConnection*)));
connect(pConnection, SIGNAL(signalIOError(QString)), this, SLOT(slotClientIOError(QString)));
pConnection->startConnection();
}
void OsiTransportTest::sendTestData(CConnection* that)
{
QByteArray qdata;
qdata.reserve(sizeof(testData)/sizeof(testData[0]));
for (char c: testData)
{
qdata.push_back(c);
}
that->send(qdata, (quint32)0, qdata.size());
}
void OsiTransportTest::Test1::runTest()
{
qDebug() << "Test1: Check connection";
OsiTransportTest* pTest = OsiTransportTest::getMainTest();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test1: checkClientConnected wrong", true, pTest->checkClientConnected);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test1: checkClientErrorConnected wrong", false, pTest->checkClientErrorConnected);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test1: checkServerConnected wrong", true, pTest->checkServerConnected);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test1: checkServerErrorConnected wrong", false, pTest->checkServerErrorConnected);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test1: checkIllegalArg wrong", false, pTest->checkIllegalArg);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test1: checkIllegalClassMbr wrong", false, pTest->checkIllegalClassMbr);
}
void OsiTransportTest::Test2::runTest()
{
qDebug() << "Test2: Check server side received data";
OsiTransportTest* pTest = OsiTransportTest::getMainTest();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test2: Server side has error transfer", false, pTest->checkServerErrorTransfer);
qint32 expectedSize = sizeof(testData)/sizeof(testData[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test2: Check server size of received data wrong", expectedSize, pTest->m_serverRcvData.size());
QByteArray::Iterator dataIt = pTest->m_serverRcvData.begin();
for (char c: testData)
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test2: Check server side received data wrong", c, *dataIt);
dataIt++;
}
}
void OsiTransportTest::Test3::runTest()
{
qDebug() << "Test3: Check client side received data";
OsiTransportTest* pTest = OsiTransportTest::getMainTest();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test3: Client side has error transfer", false, pTest->checkClientErrorTransfer);
qint32 expectedSize = sizeof(testData)/sizeof(testData[0]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test3: Check client size of received data wrong", expectedSize, pTest->m_clientRcvData.size());
QByteArray::Iterator dataIt = pTest->m_clientRcvData.begin();
for (char c: testData)
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("Test3: Check client side received data wrong", c, *dataIt);
dataIt++;
}
}
// server slots
void OsiTransportTest::slotServerClientConnected(const CConnection*)
{
qDebug() << "OsiTransportTest::slotServerClientConnected";
checkServerConnected = true;
}
void OsiTransportTest::slotServerClientDisconnected(const CConnection*)
{
qDebug() << "OsiTransportTest::slotServerClientDisconnected";
checkServerConnected = false;
checkServerTransportConnected = false;
}
void OsiTransportTest::slotServerCRReady(const CConnection*)
{
qDebug() << "OsiTransportTest::slotServerCRReady";
checkServerTransportConnected = true;
}
void OsiTransportTest::slotServerTSduReady(const CConnection* pConnection)
{
qDebug() << "OsiTransportTest::slotServerTSduReady, checkServerConnected = " << checkServerConnected
<< "; checkServerTransportConnected = " << checkServerTransportConnected;
if (checkServerConnected)
{
if (!checkServerTransportConnected)
{
(const_cast<CConnection*>(pConnection))->listenForCR();
}
else
{
// received data parsing
CConnection* myconn = const_cast<CConnection*>(pConnection);
if ( myconn->receive(m_serverRcvData) == true)
{
qDebug() << "OsiTransportTest::slotServerTSduReady: server data ready";
// Обработка данных и сброс буфера по окончании
sendTestData(myconn);
}
else
{
qDebug() << "OsiTransportTest::slotServerTSduReady: server data is still not ready";
}
}
}
}
void OsiTransportTest::slotServerIOError(QString str)
{
qDebug() << "OsiTransportTest::slotServerIOError: " << str;
checkServerErrorTransfer = true;
}
// client slots
void OsiTransportTest::slotConnectionReady(const CConnection* pconn)
{
qDebug() << "OsiTransportTest::slotConnectionReady";
checkClientConnected = true;
sendTestData( const_cast<CConnection*>(pconn) );
}
void OsiTransportTest::slotConnectionClosed(const CConnection*)
{
qDebug() << "OsiTransportTest::slotConnectionClosed";
checkClientConnected = false;
}
void OsiTransportTest::slotClientTSduReady(const CConnection* pconn)
{
qDebug() << "OsiTransportTest::slotClientTSduReady";
if (!checkClientConnected)
{
(const_cast<CConnection*>(pconn))->parseServerAnswer();
}
else
{
if ( (const_cast<CConnection*>(pconn))->receive(m_clientRcvData) == true)
{
// Обработка данных и сброс буфера по окончании
}
}
}
void OsiTransportTest::slotClientIOError(QString str)
{
qDebug() << "OsiTransportTest::slotClientIOError: " << str;
checkClientErrorTransfer = true;
}
// Client Errors
void OsiTransportTest::slotIllegalArgument(QString strErr)
{
qDebug() << "OsiTransportTest::slotIllegalArgument: " << strErr;
checkIllegalArg = true;
}
void OsiTransportTest::slotConnectError(QString strErr)
{
qDebug() << "OsiTransportTest::slotConnectError: " << strErr;
checkClientErrorConnected = true;
}
void OsiTransportTest::slotIllegalClassMember(QString strErr)
{
qDebug() << "OsiTransportTest::slotIllegalClassMember: " << strErr;
checkIllegalClassMbr = true;
}
void OsiTransportTest::prepare()
{
test1 = new OsiTransportTest::Test1("Connection test");
test2 = new OsiTransportTest::Test2("Send test");
test3 = new OsiTransportTest::Test3("Receive test");
CServer* srv = new CServer(this);
CClient* clt = new CClient(this);
srv->start();
QThread::usleep(500);
clt->start();
}
void OsiTransportTest::run()
{
// Do processing here
CppUnit::TextTestRunner runner;
runner.addTest(test1);
runner.addTest(test2);
runner.addTest(test3);
runner.run();
std::ofstream outFile("testResult.xml");
CppUnit::XmlOutputter outputer(&runner.result(), outFile);
outputer.write();
emit finished();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
OsiTransportTest *test = OsiTransportTest::getMainTest(&a);
QObject::connect(test, SIGNAL(finished()), &a, SLOT(quit()));
QTimer::singleShot(0, test, SLOT(prepare()));
QTimer::singleShot(1000, test, SLOT(run()));
return a.exec();
}