summaryrefslogtreecommitdiffstats
path: root/tests/nfcsymbianbackend/qllcpsocketremote/tst_qllcpsocketremote.cpp
blob: 3fa6e5bde28db2850d65290bbd9e1f2e9f4f4bfd (plain)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtNfc module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore/QString>
#include <QtTest/QtTest>
#include <QtCore/QCoreApplication>

#include <qllcpsocket.h>
#include <qnearfieldmanager.h>
#include <qnearfieldtarget.h>
#include "qnfctestcommon.h"
#include "qnfctestutil.h"

QTNFC_USE_NAMESPACE

Q_DECLARE_METATYPE(QNearFieldTarget*)
Q_DECLARE_METATYPE(QLlcpSocket::SocketError);

class tst_qllcpsocketremote : public QObject
{
    Q_OBJECT

public:
    tst_qllcpsocketremote();

private Q_SLOTS:

    void mulitpleServer();
    void echoServer();
    void testCase2();

    void dumpServer();
    void multiConnection();

    void initTestCase();
    void cleanupTest();


private:
    void readMessage(QLlcpSocket& remoteSocket,QByteArray& inPayload);
    void verifyString(QString& longStr4k, QByteArray& inPayload);
private:
     QNearFieldTarget *m_target; // Not own
     quint8 m_port;
};

tst_qllcpsocketremote::tst_qllcpsocketremote()
{
    qRegisterMetaType<QNearFieldTarget *>("QNearFieldTarget*");
    qRegisterMetaType<QLlcpSocket::SocketError>("QLlcpSocket::SocketError");
}

/*!
 Description: Init test case for NFC LLCP connection-less mode socket - local peer

 TestScenario:
     Touch a NFC device with LLCP connection-less service actived

 TestExpectedResults:
     Signal of target detected has been found.
*/
void tst_qllcpsocketremote::initTestCase()
{
    QNearFieldManager nfcManager;
    QSignalSpy targetDetectedSpy(&nfcManager, SIGNAL(targetDetected(QNearFieldTarget*)));
    nfcManager.startTargetDetection(QNearFieldTarget::NfcForumDevice);

    QString message("Please touch a NFC device with llcp client enabled");
    QNfcTestUtil::ShowAutoMsg(message, &targetDetectedSpy, 1);
    QTRY_VERIFY(!targetDetectedSpy.isEmpty());

    m_target = targetDetectedSpy.at(targetDetectedSpy.count() - 1).at(0).value<QNearFieldTarget *>();
    QVERIFY(m_target!=NULL);
    QVERIFY(m_target->accessMethods() & QNearFieldTarget::LlcpAccess);
    QVERIFY(m_target->uid() == QByteArray());
    QVERIFY(m_target->type() == QNearFieldTarget::NfcForumDevice);

    m_port = 35;
}


void tst_qllcpsocketremote::readMessage(
                        QLlcpSocket& remoteSocket,
                        QByteArray& inPayload)
{
    // STEP 2: Receive data from the peer which send messages to
   while(remoteSocket.hasPendingDatagrams()) {
       QByteArray tempBuffer;
       tempBuffer.resize(remoteSocket.pendingDatagramSize());
       quint8 remotePort = 0;
       QSignalSpy readyRead(&remoteSocket, SIGNAL(readyRead()));
       qint64 readSize = remoteSocket.readDatagram(tempBuffer.data()
                                                  , tempBuffer.size()
                                                  , &m_target, &remotePort);
       QVERIFY(readSize != -1);
       QVERIFY(remotePort > 0);
       inPayload.append(tempBuffer);
       qDebug() << "Server-- read inPayload size=" << inPayload.size();
       qDebug() << "Server-- read remotePort=" << remotePort;
       if (inPayload.size() >= (int)sizeof(quint16)) {
           break;
       }
       else {
           qDebug() << "Server-- not enough header";
           QTRY_VERIFY(!readyRead.isEmpty());
       }
   }

   QDataStream in(inPayload);
   quint16 headerSize = 0; // size of real echo payload
   in >> headerSize;
   qDebug() << "Server-- read headerSize=" << headerSize;
    while (inPayload.size() < headerSize + (int)sizeof(quint16)){
       QSignalSpy readyRead(&remoteSocket, SIGNAL(readyRead()));
       QTRY_VERIFY(!readyRead.isEmpty());
       if(remoteSocket.hasPendingDatagrams()) {
           QByteArray tempBuffer;
           tempBuffer.resize(remoteSocket.pendingDatagramSize());
           quint8 remotePort = 0;
           qint64 readSize = remoteSocket.readDatagram(tempBuffer.data()
                                                      , tempBuffer.size()
                                                      , &m_target, &remotePort);
           QVERIFY(readSize != -1);
           QVERIFY(remotePort > 0);
           inPayload.append(tempBuffer);
           qDebug() << "Server-- read long blocksize=" << inPayload.size();
       }
   }
}

void tst_qllcpsocketremote::verifyString(
                        QString& longStr,
                        QByteArray& inPayload)
{
     quint16 headerSize;
     QDataStream in2(inPayload);
     in2.setVersion(QDataStream::Qt_4_6);
     in2 >> headerSize;
     QString receivedLongStr;
     in2 >> receivedLongStr;
     qDebug() << "Client-- in received string len:" << receivedLongStr.length();
     qDebug("Client-- in received string = %s", qPrintable(receivedLongStr));
     //test the received string is same as the original string
     QVERIFY(longStr == receivedLongStr);
}


/*!
 Description:  Description: Receive the message and send the acknowledged identical message

 TestScenario: 1. Remote peer binds to local port
               2. Remote peer receives the long message sending from the local peer
               3. Remote peer receives the other long message sending from the local peer

 TestExpectedResults:
               1. Remote peer binds to local port successfully.
               2. The 1st long message has been received from remote peer.
               3. The 2nd long message has been received from remote peer.
*/
void tst_qllcpsocketremote::mulitpleServer()
{
    QLlcpSocket remoteSocket;
    bool ret = remoteSocket.bind(m_port);
    QVERIFY(ret);

    QString longStr1k;
    for (int i = 0; i < 1000; i++)
        longStr1k.append((char)(i%26 + 'a'));

    QString longStr2k;
    for (int i = 0; i < 2000; i++)
        longStr2k.append((char)(i%26 + 'b'));

    QSignalSpy readyReadSpy(&remoteSocket, SIGNAL(readyRead()));
    QString message("mulitpleServer 1: Wait multiple msg coming");
    QNfcTestUtil::ShowAutoMsg(message, &readyReadSpy);

    QByteArray inPayload;
    readMessage(remoteSocket,inPayload);

    QSignalSpy readyReadSpy2(&remoteSocket, SIGNAL(readyRead()));
    QString message2("mulitpleServer 2: Wait multiple msg coming");
    QNfcTestUtil::ShowAutoMsg(message2, &readyReadSpy2);

    QByteArray inPayload2;
    readMessage(remoteSocket,inPayload2);

    verifyString(longStr1k,inPayload);
    verifyString(longStr2k,inPayload2);
}

/*!
 Description:  Description: Receive the message and send the acknowledged identical message

 TestScenario: 1. Remote peer binds to local port
               2. Remote peer receives the message sending from the local peer
               3. Remote peer sends the echoed message to the local peer

 TestExpectedResults:
               1. Remote peer binds to local port successfully.
               2. The message has been received from remote peer.
               3. The message has be sent to local peer.
*/
void tst_qllcpsocketremote::echoServer()
{
    QLlcpSocket remoteSocket;
    // STEP 1:  bind the local port for current socket
    QSignalSpy readyReadSpy(&remoteSocket, SIGNAL(readyRead()));
    bool ret = remoteSocket.bind(m_port);
    QVERIFY(ret);

    QString message("Test 1: Wait client msg coming");
    QNfcTestUtil::ShowAutoMsg(message, &readyReadSpy);

    // STEP 2: Receive data from the peer which send messages to
    QByteArray inPayload;
    readMessage(remoteSocket,inPayload);

    // STEP 3: Send the received message back to the intiated device.
    QSignalSpy errorSpy(&remoteSocket, SIGNAL(error(QLlcpSocket::SocketError)));
    QSignalSpy bytesWrittenSpy(&remoteSocket, SIGNAL(bytesWritten(qint64)));

    qDebug("Server-- write payload length = %d", inPayload.length());
    qint64 val = remoteSocket.writeDatagram(inPayload, m_target, m_port);
    QVERIFY(val != -1);

    QTRY_VERIFY(!bytesWrittenSpy.isEmpty());

    qint64 written = 0;
    qint32 lastSignalCount = 0;
    while(true)
    {
        for(int i = lastSignalCount; i < bytesWrittenSpy.count(); i++) {
            written += bytesWrittenSpy.at(i).at(0).value<qint64>();
        }
        lastSignalCount = bytesWrittenSpy.count();
        qDebug() << "current signal count = " << lastSignalCount;
        qDebug() << "written payload size = " << written;
        if (written < inPayload.size()) {
            QTRY_VERIFY(bytesWrittenSpy.count() > lastSignalCount);
        }
        else {
            break;
        }
    }

    qDebug() << "Overall bytesWritten = " << written;
    qDebug() << "Overall block size = " << inPayload.size();
    QVERIFY(written == inPayload.size());
    // make sure the no error signal emitted
    QCOMPARE(errorSpy.count(), 0);
}

/*!
 Description: This server will all dump data received from client
 To test some negtive case
*/
void tst_qllcpsocketremote::dumpServer()
    {
    QLlcpSocket remoteSocket;
    QSignalSpy readyReadSpy(&remoteSocket, SIGNAL(readyRead()));
    bool ret = remoteSocket.bind(m_port);
    QVERIFY(ret);
    QString message("dumpServer test");
    QNfcTestUtil::ShowAutoMsg(message, &readyReadSpy);

    QTest::qWait(2 * 1000);
    }
/*!
 Description: Unit test for NFC LLCP connection-less mode socket - remote peer (passive)

 TestScenario: 1. Remote peer binds to local port ( already done in testCase1)
               2. Remote peer  receive datagram twice
               3. Remote peer waitForReadyRead

 TestExpectedResults:
               1. Remote peer binds to local port successfully.
               2. Remote peer  receive datagram twice successfully
               3. waitForReadyRead return true as long as readyReadSpy not empty
*/
void tst_qllcpsocketremote::testCase2()
{
    QLlcpSocket remoteSocket;
    quint8 localPort = 38;
    QSignalSpy readyReadSpy(&remoteSocket, SIGNAL(readyRead()));
    bool ret = remoteSocket.bind(localPort);
    QVERIFY(ret);

    // STEP 1:  bind the local port for current socket
    QString expectedMessage1("testcase2 string str1");
    QString expectedMessage2("testcase2 string str2");

    QString boxMessage("handshake 3");
    QNfcTestUtil::ShowAutoMsg(boxMessage,&readyReadSpy);

    QTRY_VERIFY(readyReadSpy.count() == 1);

    QByteArray datagram;
    if (remoteSocket.hasPendingDatagrams())
    {
       datagram.resize(remoteSocket.pendingDatagramSize());
       qint64 readSize = remoteSocket.readDatagram(datagram.data(), datagram.size());
       QVERIFY(readSize != -1);
    }

    QString receivedMessage1 = datagram.data();
    qDebug() << "receivedMessage1: " << receivedMessage1;
    QVERIFY(expectedMessage1 == receivedMessage1);

    QTRY_VERIFY(readyReadSpy.count() == 2);

     QByteArray datagram2;
    if (remoteSocket.hasPendingDatagrams())
    {
       datagram2.resize(remoteSocket.pendingDatagramSize());
       qint64 readSize = remoteSocket.readDatagram(datagram2.data(), datagram2.size());
       QVERIFY(readSize != -1);
    }
    QString receivedMessage2 = datagram2.data();
    qDebug() << "receivedMessage2: " << receivedMessage2;
    QVERIFY(expectedMessage2 == receivedMessage2);

    const int Timeout = 10 * 1000;
    ret = remoteSocket.waitForReadyRead(Timeout);

    QVERIFY(ret == false);
 }
void tst_qllcpsocketremote::multiConnection()
    {
    QLlcpSocket remoteSocket;
    // STEP 1:  bind the local port for current socket
    QSignalSpy readyReadSpy(&remoteSocket, SIGNAL(readyRead()));
    bool ret = remoteSocket.bind(m_port);
    QVERIFY(ret);

    const int KLoopCount = 2;
    int loopCount = 0;

    while(loopCount < KLoopCount)
        {
        QString message("Test 1: Wait client msg coming");
        QNfcTestUtil::ShowAutoMsg(message, &readyReadSpy);

        // STEP 2: Receive data from the peer which send messages to
        QByteArray inPayload;
        while(remoteSocket.hasPendingDatagrams()) {
            QByteArray tempBuffer;
            tempBuffer.resize(remoteSocket.pendingDatagramSize());
            quint8 remotePort = 0;
            QSignalSpy readyRead(&remoteSocket, SIGNAL(readyRead()));
            qint64 readSize = remoteSocket.readDatagram(tempBuffer.data()
                                                       , tempBuffer.size()
                                                       , &m_target, &remotePort);
            QVERIFY(readSize != -1);
            QVERIFY(remotePort > 0);
            inPayload.append(tempBuffer);
            qDebug() << "Server-- read inPayload size=" << inPayload.size();
            qDebug() << "Server-- read remotePort=" << remotePort;
            if (inPayload.size() >= (int)sizeof(quint16)) {
                break;
            }
            else {
                qDebug() << "Server-- not enough header";
                QTRY_VERIFY(!readyRead.isEmpty());
            }
        }

        QDataStream in(inPayload);
        quint16 headerSize = 0; // size of real echo payload
        in >> headerSize;
        qDebug() << "Server-- read headerSize=" << headerSize;
        while (inPayload.size() < headerSize + (int)sizeof(quint16)){
            QSignalSpy readyRead(&remoteSocket, SIGNAL(readyRead()));
            QTRY_VERIFY(!readyRead.isEmpty());
            if(remoteSocket.hasPendingDatagrams()) {
                QByteArray tempBuffer;
                tempBuffer.resize(remoteSocket.pendingDatagramSize());
                quint8 remotePort = 0;
                qint64 readSize = remoteSocket.readDatagram(tempBuffer.data()
                                                           , tempBuffer.size()
                                                           , &m_target, &remotePort);
                QVERIFY(readSize != -1);
                QVERIFY(remotePort > 0);
                inPayload.append(tempBuffer);
                qDebug() << "Server-- read long blocksize=" << inPayload.size();
            }
        }

        // STEP 3: Send the received message back to the intiated device.
        QSignalSpy errorSpy(&remoteSocket, SIGNAL(error(QLlcpSocket::SocketError)));
        QSignalSpy bytesWrittenSpy(&remoteSocket, SIGNAL(bytesWritten(qint64)));

        qDebug("Server-- write payload length = %d", inPayload.length());
        qint64 val = remoteSocket.writeDatagram(inPayload, m_target, m_port);
        QVERIFY(val != -1);

        QTRY_VERIFY(!bytesWrittenSpy.isEmpty());

        qint64 written = 0;
        qint32 lastSignalCount = 0;
        while(true)
        {
            for(int i = lastSignalCount; i < bytesWrittenSpy.count(); i++) {
                written += bytesWrittenSpy.at(i).at(0).value<qint64>();
            }
            lastSignalCount = bytesWrittenSpy.count();
            qDebug() << "current signal count = " << lastSignalCount;
            qDebug() << "written payload size = " << written;
            if (written < inPayload.size()) {
                QTRY_VERIFY(bytesWrittenSpy.count() > lastSignalCount);
            }
            else {
                break;
            }
        }

        qDebug() << "Overall bytesWritten = " << written;
        qDebug() << "Overall block size = " << inPayload.size();
        QVERIFY(written == inPayload.size());
        // make sure the no error signal emitted
        QCOMPARE(errorSpy.count(), 0);
        //wait another socket to send data
        loopCount++;
        readyReadSpy.removeFirst();
        }
    QTest::qWait(1500);//give some time to client to finish

    }
void tst_qllcpsocketremote::cleanupTest()
{
}

QTEST_MAIN(tst_qllcpsocketremote);

#include "tst_qllcpsocketremote.moc"