summaryrefslogtreecommitdiff
path: root/tests/auto/qndefmessage/tst_qndefmessage.cpp
blob: 738be9275a4094340c43ab740a4ab5f0c40ed5df (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtTest/QtTest>

#include <qndefrecord.h>
#include <qndefmessage.h>
#include <qndefnfctextrecord.h>
#include <qndefnfcurirecord.h>

QT_USE_NAMESPACE

Q_DECLARE_METATYPE(QNdefRecord)

class tst_QNdefMessage : public QObject
{
    Q_OBJECT

public:
    tst_QNdefMessage();
    ~tst_QNdefMessage();

private slots:
    void equality();
    void equality_data();
    void parseSingleRecordMessage_data();
    void parseSingleRecordMessage();
    void parseCorruptedMessage();
    void parseCorruptedMessage_data();
    void parseComplexMessage();
};

tst_QNdefMessage::tst_QNdefMessage()
{
}

tst_QNdefMessage::~tst_QNdefMessage()
{
}

void tst_QNdefMessage::equality()
{
    QFETCH(QNdefMessage, lhs);
    QFETCH(QNdefMessage, rhs);
    QFETCH(bool, result);

    QCOMPARE(lhs == rhs, result);
    QCOMPARE(rhs == lhs, result);
}

void tst_QNdefMessage::equality_data()
{
    QTest::addColumn<QNdefMessage>("lhs");
    QTest::addColumn<QNdefMessage>("rhs");
    QTest::addColumn<bool>("result");

    QTest::newRow("empty vs empty") << QNdefMessage() << QNdefMessage() << true;
    {
        QNdefRecord rec;
        rec.setTypeNameFormat(QNdefRecord::Empty);

        QNdefMessage message;
        message.push_back(rec);

        QTest::newRow("empty vs empty record") << QNdefMessage() << message << true;
    }
    {
        QNdefMessage message;
        message.push_back(QNdefNfcTextRecord());

        QTest::newRow("empty vs non-empty record") << QNdefMessage() << message << false;
    }
    {
        QNdefMessage lhs;
        lhs.push_back(QNdefNfcTextRecord());

        QNdefMessage rhs;
        rhs.push_back(QNdefNfcUriRecord());

        QTest::newRow("different records") << lhs << rhs << false;
    }
    {
        QNdefMessage lhs;
        lhs.push_back(QNdefNfcTextRecord());
        lhs.push_back(QNdefNfcUriRecord());

        QNdefMessage rhs;
        rhs.push_back(QNdefNfcUriRecord());

        QTest::newRow("different amount of records") << lhs << rhs << false;
    }
    {
        QNdefMessage lhs;
        lhs.push_back(QNdefNfcTextRecord());
        lhs.push_back(QNdefNfcUriRecord());

        QNdefMessage rhs;
        rhs.push_back(QNdefNfcUriRecord());
        rhs.push_back(QNdefNfcTextRecord());

        QTest::newRow("same records, different order") << lhs << rhs << false;
    }
    {
        QNdefMessage lhs;
        lhs.push_back(QNdefNfcTextRecord());
        lhs.push_back(QNdefNfcUriRecord());

        QNdefMessage rhs;
        rhs.push_back(QNdefNfcTextRecord());
        rhs.push_back(QNdefNfcUriRecord());

        QTest::newRow("same records, same order") << lhs << rhs << true;
    }
}

void tst_QNdefMessage::parseSingleRecordMessage_data()
{
    QTest::addColumn<QByteArray>("data");
    QTest::addColumn<QNdefMessage>("message");
    QTest::addColumn<QVariantList>("expectedData");

    // empty message
    {
        QByteArray data;
        data.append(char(0xc0));   // MB=1, ME=1
        data.append(char(0));      // TYPE LENGTH
        data.append(char(0));      // PAYLOAD LENGTH 3
        data.append(char(0));      // PAYLOAD LENGTH 2
        data.append(char(0));      // PAYLOAD LENGTH 1
        data.append(char(0));      // PAYLOAD LENGTH 0
        QTest::newRow("empty") << data << QNdefMessage() << QVariantList();

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::Empty);
        QTest::newRow("empty record") << data
                                      << QNdefMessage(record)
                                      << QVariantList();
    }

    // empty short message
    {
        QByteArray data;
        data.append(char(0xd0));   // MB=1, ME=1, SR=1
        data.append(char(0));      // TYPE LENGTH
        data.append(char(0));      // PAYLOAD LENGTH
        QTest::newRow("empty") << data << QNdefMessage() << QVariantList();

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::Empty);
        QTest::newRow("empty record") << data
                                      << QNdefMessage(record)
                                      << QVariantList();
    }

    // unknown message
    {
        QByteArray type("type");
        QByteArray id("id");
        QByteArray payload("payload");

        QByteArray data;
        data.append(char(0xcd));                            // MB=1, ME=1, IL=1, TNF=5
        data.append(char(type.length()));                   // TYPE LENGTH
        data.append(char((payload.length() >> 24) & 0xff)); // PAYLOAD LENGTH 3
        data.append(char((payload.length() >> 16) & 0xff)); // PAYLOAD LENGTH 2
        data.append(char((payload.length() >> 8) & 0xff));  // PAYLOAD LENGTH 1
        data.append(char((payload.length() >> 0) & 0xff));  // PAYLOAD LENGTH 0
        data.append(char(id.length()));                     // ID LENGTH
        data.append(type);
        data.append(id);
        data.append(payload);

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::Unknown);
        record.setType(type);
        record.setId(id);
        record.setPayload(payload);
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("unknown") << data << QNdefMessage(recordList) << QVariantList();
    }

    // chunked message
    {
        QByteArray type("type");
        QByteArray id("id");
        QByteArray payload("payload");

        QByteArray data;
        data.append(char(0xbd));            // MB=1, CF=1, SR=1, IL=1, TNF=5
        data.append(char(type.length()));   // TYPE LENGTH
        data.append(char(1));               // PAYLOAD LENGTH
        data.append(char(id.length()));     // ID LENGTH
        data.append(type);                  // TYPE
        data.append(id);                    // ID
        data.append(payload.at(0));         // PAYLOAD[0]

        for (int i = 1; i < payload.length() - 1; ++i) {
            data.append(char(0x36));            // CF=1, SR=1, TNF=6
            data.append(char(0));               // TYPE LENGTH
            data.append(char(1));               // PAYLOAD LENGTH
            data.append(payload.at(i));         // PAYLOAD[i]
        }

        data.append(char(0x56));                        // ME=1, SR=1, TNF=6
        data.append(char(0));                           // TYPE LENGTH
        data.append(char(1));                           // PAYLOAD LENGTH
        data.append(payload.at(payload.length() - 1));  // PAYLOAD[length - 1]

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::Unknown);
        record.setType(type);
        record.setId(id);
        record.setPayload(payload);
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("chunked") << data << QNdefMessage(recordList) << QVariantList();

        const QByteArray recordContent = record.type() + record.id()
                                         + record.payload();
        QCOMPARE(recordContent, QByteArray::fromHex(QByteArray("7479706569647061796c6f6164")));
    }

    // NFC-RTD Text
    {
        QByteArray type("T");
        QByteArray payload;
        payload.append(char(0x02));     // UTF-8, 2 byte language code
        payload.append("en");
        payload.append("Test String");

        QByteArray data;
        data.append(char(0xc1));            // MB=1, ME=1, IL=0, TNF=1
        data.append(char(type.length()));   // TYPE LENGTH
        data.append(char((payload.length() >> 24) & 0xff)); // PAYLOAD LENGTH 3
        data.append(char((payload.length() >> 16) & 0xff)); // PAYLOAD LENGTH 2
        data.append(char((payload.length() >> 8) & 0xff));  // PAYLOAD LENGTH 1
        data.append(char((payload.length() >> 0) & 0xff));  // PAYLOAD LENGTH 0
        data.append(type);
        data.append(payload);

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::NfcRtd);
        record.setType("T");
        record.setPayload("\002enTest String");
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("nfc-rtd text") << data << QNdefMessage(recordList)
                                      << (QVariantList() << QStringLiteral("Test String")
                                                         << QStringLiteral("en"));

        const QByteArray recordContent = record.type() + record.id()
                                         + record.payload();
        QCOMPARE(recordContent,
                 QByteArray::fromHex(QByteArray("5402656e5465737420537472696e67")));
    }

    // NFC-RTD Text
    {
        QByteArray type("T");
        QByteArray payload;
        payload.append(char(0x02));     // UTF-8, 2 byte language code
        payload.append("ja");
        payload.append(QByteArray::fromHex("e38386e382b9e38388e69687e5ad97e58897"));

        QByteArray data;
        data.append(char(0xc1));            // MB=1, ME=1, IL=0, TNF=1
        data.append(char(type.length()));   // TYPE LENGTH
        data.append(char((payload.length() >> 24) & 0xff)); // PAYLOAD LENGTH 3
        data.append(char((payload.length() >> 16) & 0xff)); // PAYLOAD LENGTH 2
        data.append(char((payload.length() >> 8) & 0xff));  // PAYLOAD LENGTH 1
        data.append(char((payload.length() >> 0) & 0xff));  // PAYLOAD LENGTH 0
        data.append(type);
        data.append(payload);

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::NfcRtd);
        record.setType("T");
        record.setPayload("\002ja" + QByteArray::fromHex("e38386e382b9e38388e69687e5ad97e58897"));
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("nfc-rtd text ja")
            << data << QNdefMessage(recordList)
            << (QVariantList() << QString::fromUtf8("\343\203\206\343\202\271\343\203\210\346\226"
                                                    "\207\345\255\227\345\210\227")
                               << QStringLiteral("ja"));

        const QByteArray recordContent = record.type() + record.id()
                                         + record.payload();
        QCOMPARE(recordContent,
                 QByteArray::fromHex(QByteArray("54026a61e38386e382b9e38388e69687e5ad97e58897")));
    }

    // NFC-RTD URI
    {
        QByteArray type("U");
        QByteArray payload;
        payload.append(char(0x00));
        payload.append("http://qt-project.org/");

        QByteArray data;
        data.append(char(0xc1));
        data.append(char(type.length()));
        data.append(char((payload.length() >> 24) & 0xff)); // PAYLOAD LENGTH 3
        data.append(char((payload.length() >> 16) & 0xff)); // PAYLOAD LENGTH 2
        data.append(char((payload.length() >> 8) & 0xff));  // PAYLOAD LENGTH 1
        data.append(char((payload.length() >> 0) & 0xff));  // PAYLOAD LENGTH 0
        data.append(type);
        data.append(payload);

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::NfcRtd);
        record.setType("U");
        record.setPayload(QByteArray("\000http://qt-project.org/", 23));
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("nfc-rtd uri http://qt-project.org/")
            << data << QNdefMessage(recordList)
            << (QVariantList() << QUrl(QStringLiteral("http://qt-project.org/")));

        const QByteArray recordContent = record.type() + record.id()
                                         + record.payload();
        QCOMPARE(recordContent,
                 QByteArray::fromHex(QByteArray("5500687474703a2f2f71742d70726f6a6563742e6f72672f")));
    }

    // NFC-RTD URI
    {
        QByteArray type("U");
        QByteArray payload;
        payload.append(char(0x03));
        payload.append("qt-project.org/");

        QByteArray data;
        data.append(char(0xc1));
        data.append(char(type.length()));
        data.append(char((payload.length() >> 24) & 0xff)); // PAYLOAD LENGTH 3
        data.append(char((payload.length() >> 16) & 0xff)); // PAYLOAD LENGTH 2
        data.append(char((payload.length() >> 8) & 0xff));  // PAYLOAD LENGTH 1
        data.append(char((payload.length() >> 0) & 0xff));  // PAYLOAD LENGTH 0
        data.append(type);
        data.append(payload);

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::NfcRtd);
        record.setType("U");
        record.setPayload(QByteArray("\003qt-project.org/", 16));
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("nfc-rtd uri abbrev http://qt-project.org/")
            << data << QNdefMessage(recordList)
            << (QVariantList() << QUrl(QStringLiteral("http://qt-project.org/")));

        const QByteArray recordContent = record.type() + record.id()
                                         + record.payload();
        QCOMPARE(recordContent,
                 QByteArray::fromHex(QByteArray("550371742d70726f6a6563742e6f72672f")));
    }

    // NFC-RTD URI
    {
        QByteArray type("U");
        QByteArray payload;
        payload.append(char(0x05));
        payload.append("+1234567890");

        QByteArray data;
        data.append(char(0xc1));
        data.append(char(type.length()));
        data.append(char((payload.length() >> 24) & 0xff)); // PAYLOAD LENGTH 3
        data.append(char((payload.length() >> 16) & 0xff)); // PAYLOAD LENGTH 2
        data.append(char((payload.length() >> 8) & 0xff));  // PAYLOAD LENGTH 1
        data.append(char((payload.length() >> 0) & 0xff));  // PAYLOAD LENGTH 0
        data.append(type);
        data.append(payload);

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::NfcRtd);
        record.setType("U");
        record.setPayload(QByteArray("\005+1234567890", 12));
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("nfc-rtd uri tel:+1234567890")
            << data << QNdefMessage(recordList)
            << (QVariantList() << QUrl(QStringLiteral("tel:+1234567890")));

        const QByteArray recordContent = record.type() + record.id()
                                         + record.payload();
        QCOMPARE(recordContent,
                 QByteArray::fromHex(QByteArray("55052b31323334353637383930")));
    }

    // Chunk payload - the NFC-RTD URI split into 3 chunks
    {
        const QByteArray type("U");
        const QByteArray id("test");
        QByteArray payload1;
        payload1.append(char(0x05));
        payload1.append("+123");

        const QByteArray payload2("456");
        const QByteArray payload3("7890");

        QByteArray data;
        data.append(char(0xB9)); // MB=1, ME=0, CF=1, SR=1, IL=1, TNF=1 (NFC-RTD)
        data.append(type.length());
        data.append(payload1.length() & 0xff); // length fits into 1 byte
        data.append(id.length());
        data.append(type);
        data.append(id);
        data.append(payload1);
        data.append(char(0x36)); // MB=0, ME=0, CF=1, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x00));
        data.append(payload2.length());
        data.append(payload2);
        data.append(char(0x56)); // MB=0, ME=1, CF=0, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x00));
        data.append(payload3.length());
        data.append(payload3);

        QNdefRecord record;
        record.setTypeNameFormat(QNdefRecord::NfcRtd);
        record.setType(type);
        record.setId(id);
        record.setPayload(QByteArray("\005+1234567890", 12));
        QList<QNdefRecord> recordList;
        recordList.append(record);
        QTest::newRow("chunk payloads nfc-rtd uri tel:+1234567890")
                << data << QNdefMessage(recordList)
                << (QVariantList() << QUrl(QStringLiteral("tel:+1234567890")));
    }

    // Truncated message
    {
        QByteArray type("U");
        QByteArray id("Test ID");
        QByteArray payload;
        payload.append(char(0x00));
        payload.append("http://qt-project.org/");
        QByteArray data;
        data.append(char(0xc9));   // MB=1, ME=1, IL=1

        QTest::newRow("truncated 1") << data << QNdefMessage() << QVariantList();

        data.append(char(type.length()));   // TYPE LENGTH
        QTest::newRow("truncated 2") << data << QNdefMessage() << QVariantList();

        data.append(char((payload.length() >> 24) & 0xff)); // PAYLOAD LENGTH 3
        QTest::newRow("truncated 3") << data << QNdefMessage() << QVariantList();

        data.append(char((payload.length() >> 16) & 0xff)); // PAYLOAD LENGTH 2
        QTest::newRow("truncated 4") << data << QNdefMessage() << QVariantList();

        data.append(char((payload.length() >> 8) & 0xff));  // PAYLOAD LENGTH 1
        QTest::newRow("truncated 5") << data << QNdefMessage() << QVariantList();

        data.append(char((payload.length() >> 0) & 0xff));  // PAYLOAD LENGTH 0
        QTest::newRow("truncated 6") << data << QNdefMessage() << QVariantList();

        data.append(char(id.length())); // ID LENGTH
        QTest::newRow("truncated 7") << data << QNdefMessage() << QVariantList();

        data.append(type);
        QTest::newRow("truncated 8") << data << QNdefMessage() << QVariantList();

        data.append(id);
        QTest::newRow("truncated 9") << data << QNdefMessage() << QVariantList();

        payload.chop(1);
        data.append(payload);
        QTest::newRow("truncated 10") << data << QNdefMessage() << QVariantList();
    }
}

void tst_QNdefMessage::parseSingleRecordMessage()
{
    QFETCH(QByteArray, data);
    QFETCH(QNdefMessage, message);
    QFETCH(QVariantList, expectedData);

    if (QByteArray(QTest::currentDataTag()).startsWith("truncated "))
        QTest::ignoreMessage(QtWarningMsg, "Unexpected end of message");

    QNdefMessage parsedMessage = QNdefMessage::fromByteArray(data);

    QVERIFY(parsedMessage == message);
    QVERIFY(message == parsedMessage);

    QNdefMessage reparsedMessage = QNdefMessage::fromByteArray(message.toByteArray());

    QVERIFY(message == reparsedMessage);
    QVERIFY(reparsedMessage == message);

    for (qsizetype i = 0; i < message.size(); ++i) {
        const QNdefRecord &record = message.at(i);
        const QNdefRecord &parsedRecord = parsedMessage.at(i);

        QCOMPARE(record.typeNameFormat(), parsedRecord.typeNameFormat());
        QCOMPARE(record.type(), parsedRecord.type());
        QCOMPARE(record.id(), parsedRecord.id());
        QCOMPARE(record.payload(), parsedRecord.payload());
        QCOMPARE(record.isEmpty(), parsedRecord.isEmpty());

        if (record.isRecordType<QNdefNfcTextRecord>()) {
            QNdefNfcTextRecord textRecord(record);
            QNdefNfcTextRecord parsedTextRecord(parsedRecord);

            QCOMPARE(textRecord.text(), parsedTextRecord.text());
            QCOMPARE(textRecord.locale(), parsedTextRecord.locale());

            if (expectedData.size() == 2) {
                QCOMPARE(parsedTextRecord.text(), expectedData.at(0).toString());
                QCOMPARE(parsedTextRecord.locale(), expectedData.at(1).toString());
            }
        } else if (record.isRecordType<QNdefNfcUriRecord>()) {
            QNdefNfcUriRecord uriRecord(record);
            QNdefNfcUriRecord parsedUriRecord(parsedRecord);

            QCOMPARE(uriRecord.uri(), parsedUriRecord.uri());

            if (expectedData.size() == 1)
                QCOMPARE(parsedUriRecord.uri(), expectedData.at(0).toUrl());
        } else if (record.isRecordType<QNdefRecord>()) {
            QVERIFY(record.isEmpty());
        }
    }
}

void tst_QNdefMessage::parseCorruptedMessage()
{
    // This test is needed to check that all the potential errors in the
    // input data are handled correctly.

    QFETCH(QByteArray, data);
    QFETCH(QByteArray, warningMessage);

    if (!warningMessage.isEmpty())
        QTest::ignoreMessage(QtWarningMsg, warningMessage.constData());

    QNdefMessage parsedMessage = QNdefMessage::fromByteArray(data);
    // corrupted data always results in an empty message
    QVERIFY(parsedMessage.isEmpty());
}

void tst_QNdefMessage::parseCorruptedMessage_data()
{
    QTest::addColumn<QByteArray>("data");
    QTest::addColumn<QByteArray>("warningMessage");

    {
        QByteArray data;
        data.append(char(0x50)); // MB=0, ME=1, SR=1
        data.append(char(0)); // TYPE LENGTH
        data.append(char(0)); // PAYLOAD LENGTH

        const QByteArray warningMsg = "Haven't got message begin yet";

        QTest::newRow("No message begin") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0x90)); // MB=1, ME=0, SR=1
        data.append(char(0)); // TYPE LENGTH
        data.append(char(0)); // PAYLOAD LENGTH

        const QByteArray warningMsg = "Malformed NDEF Message, missing begin or end";

        QTest::newRow("No message end") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0x90)); // MB=1, ME=0, SR=1
        data.append(char(0)); // TYPE LENGTH
        data.append(char(0)); // PAYLOAD LENGTH
        data.append(char(0xd0)); // MB=1, ME=1, SR=1
        data.append(char(0)); // TYPE LENGTH
        data.append(char(0)); // PAYLOAD LENGTH

        const QByteArray warningMsg = "Got message begin but already parsed some records";

        QTest::newRow("Multiple message begin") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0xB1)); // MB=1, ME=0, CF=1, SR=1, IL=0, TNF=1 (NFC-RTD)
        data.append(char(0x01)); // type length
        data.append(char(0x01)); // payload length
        data.append('U'); // type
        data.append('a'); // payload
        data.append(char(0x76)); // MB=0, ME=1 (incorrect), CF=1, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x00)); // type length
        data.append(char(0x01)); // payload length
        data.append('b'); // payload
        data.append(char(0x56)); // MB=0, ME=1, CF=0, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x00)); // type length
        data.append(char(0x01)); // payload length
        data.append('c'); // payload

        const QByteArray warningMsg = "Got message end but already parsed final record";

        QTest::newRow("Multiple message end") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0xB1)); // MB=1, ME=0, CF=1, SR=1, IL=0, TNF=1 (NFC-RTD)
        data.append(char(0x01)); // type length
        data.append(char(0x01)); // payload length
        data.append('U'); // type
        data.append('a'); // payload
        data.append(char(0x36)); // MB=0, ME=0, CF=1, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x00)); // type length
        data.append(char(0x01)); // payload length
        data.append('b'); // payload
        data.append(char(0x51)); // MB=0, ME=1, CF=0, SR=1, IL=0, TNF=1 (NFC-RTD - incorrect)
        data.append(char(0x00)); // type length
        data.append(char(0x01)); // payload length
        data.append('c'); // payload

        const QByteArray warningMsg = "Partial chunk not empty, but TNF not 0x06 as expected";

        QTest::newRow("Incorrect TNF in chunk") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0xB1)); // MB=1, ME=0, CF=1, SR=1, IL=0, TNF=1 (NFC-RTD)
        data.append(char(0x01)); // type length
        data.append(char(0x01)); // payload length
        data.append('U'); // type
        data.append('a'); // payload
        data.append(char(0x36)); // MB=0, ME=0, CF=1, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x01)); // type length (incorrect)
        data.append(char(0x01)); // payload length
        data.append('U'); // type (incorrect)
        data.append('b'); // payload
        data.append(char(0x56)); // MB=0, ME=1, CF=0, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x00)); // type length
        data.append(char(0x01)); // payload length
        data.append('c'); // payload

        const QByteArray warningMsg = "Invalid chunked data, TYPE_LENGTH != 0";

        QTest::newRow("Incorrect TYPE_LENGTH in chunk") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0xB1)); // MB=1, ME=0, CF=1, SR=1, IL=0, TNF=1 (NFC-RTD)
        data.append(char(0x01)); // type length
        data.append(char(0x01)); // payload length
        data.append('U'); // type
        data.append('a'); // payload
        data.append(char(0x3E)); // MB=0, ME=0, CF=1, SR=1, IL=1 (incorrect), TNF=6 (Unchanged)
        data.append(char(0x00)); // type length
        data.append(char(0x01)); // payload length
        data.append(char(0x01)); // id length (incorrect)
        data.append('i'); // id (incorrect)
        data.append('b'); // payload
        data.append(char(0x56)); // MB=0, ME=1, CF=0, SR=1, IL=0, TNF=6 (Unchanged)
        data.append(char(0x00)); // type length
        data.append(char(0x01)); // payload length
        data.append('c'); // payload

        const QByteArray warningMsg = "Invalid chunked data, IL != 0";

        QTest::newRow("Incorrect IL in chunk") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0xC8)); // MB=1, ME=1, CF=0, SR=0, IL=1, TNF=0
        data.append(char(0)); // type legnth
        data.append(char(0)); // payload length (incorrect, 4 bytes expected)

        const QByteArray warningMsg = "Unexpected end of message";

        QTest::newRow("Invalid header length") << data << warningMsg;
    }
    {
        QByteArray data;
        data.append(char(0xC1)); // MB=1, ME=1, CF=0, SR=0, IL=0, TNF=1
        data.append(char(0x01)); // type legnth
        data.append(char(0xFF)); // payload length 3
        data.append(char(0xFF)); // payload length 2
        data.append(char(0xFF)); // payload length 1
        data.append(char(0xFF)); // payload length 0
        data.append('U'); // type
        data.append('a'); // payload

        // In case of 32 bit the code path will be different from 64 bit, so
        // the warning message is also different.
        const QByteArray warningMsg = (sizeof(qsizetype) <= sizeof(quint32))
                ? "Payload can't fit into QByteArray"
                : "Unexpected end of message";

        QTest::newRow("Max payload length") << data << warningMsg;
    }
}

void tst_QNdefMessage::parseComplexMessage()
{
    const QByteArray reference("1234567890");
    QNdefMessage message;
    QNdefRecord first;
    QVERIFY(first.isEmpty());
    first.setTypeNameFormat(QNdefRecord::Uri);
    QVERIFY(first.isEmpty());
    first.setPayload(reference);
    QCOMPARE(first.payload(), reference);
    QVERIFY(!first.isEmpty());
    QCOMPARE(first.typeNameFormat(), QNdefRecord::Uri);

    message.append(first);

    QNdefRecord second;

    QCOMPARE(second.payload(), QByteArray());
    QVERIFY(second.isEmpty());
    QCOMPARE(second.typeNameFormat(), QNdefRecord::Empty);

    message.append(second);

    QByteArray result = message.toByteArray();
    QNdefMessage messageCopy = QNdefMessage::fromByteArray(result);
    QCOMPARE(messageCopy.size(), 2);

    first = messageCopy.at(0);
    second = messageCopy.at(1);

    QCOMPARE(first.payload(), reference);
    QVERIFY(!first.isEmpty());
    QCOMPARE(first.typeNameFormat(), QNdefRecord::Uri);
    QCOMPARE(second.payload(), QByteArray());
    QVERIFY(second.isEmpty());
    QCOMPARE(second.typeNameFormat(), QNdefRecord::Empty);

}

QTEST_MAIN(tst_QNdefMessage)

#include "tst_qndefmessage.moc"