File indexing completed on 2024-05-12 05:17:29

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "asn1/berelement.h"
0008 
0009 #include <QObject>
0010 #include <QTest>
0011 
0012 using namespace KItinerary;
0013 
0014 class BerDecoderTest : public QObject
0015 {
0016     Q_OBJECT
0017 private Q_SLOTS:
0018     void testBerElementInvalid_data()
0019     {
0020         QTest::addColumn<QByteArray>("input");
0021         QTest::newRow("empty") << QByteArray();
0022         QTest::newRow("only type") << QByteArray::fromHex("02");
0023         QTest::newRow("truncated extended type") << QByteArray::fromHex("1F");
0024         QTest::newRow("no content") << QByteArray::fromHex("0201");
0025         QTest::newRow("too large") << QByteArray::fromHex("020242");
0026         QTest::newRow("variable length no terminator") << QByteArray::fromHex("028042");
0027     }
0028 
0029     void testBerElementInvalid()
0030     {
0031         QFETCH(QByteArray, input);
0032         BER::Element e(input);
0033         QVERIFY(!e.isValid());
0034     }
0035 
0036     void testBerElementType_data()
0037     {
0038         QTest::addColumn<QByteArray>("input");
0039         QTest::addColumn<uint32_t>("type");
0040 
0041         QTest::newRow("primitive type") << QByteArray::fromHex("020142") << 0x02u;
0042         QTest::newRow("extended type") << QByteArray::fromHex("1F420142") << 0x1F42u;
0043         QTest::newRow("zero size") << QByteArray::fromHex("0200") << 0x02u;
0044     }
0045 
0046     void testBerElementType()
0047     {
0048         QFETCH(QByteArray, input);
0049         QFETCH(uint32_t, type);
0050         BER::Element e(input);
0051         QVERIFY(e.isValid());
0052         QCOMPARE(e.type(), type);
0053         QCOMPARE(e.size(), input.size());
0054         QVERIFY(e.contentData());
0055     }
0056 
0057     void testBerElementContentSize_data()
0058     {
0059         QTest::addColumn<QByteArray>("input");
0060         QTest::addColumn<int>("size");
0061 
0062         QTest::newRow("basic size") << QByteArray::fromHex("020142") << 1;
0063         QTest::newRow("1 byte extended size") << QByteArray::fromHex("02810142") << 1;
0064 
0065         QByteArray b(256, 42);
0066         b.prepend(QByteArray::fromHex("02820100"));
0067         QTest::newRow("2 byte extended size") << b << 256;
0068 
0069         QTest::newRow("variable length") << QByteArray::fromHex("0280420000") << 1;
0070 
0071         QTest::newRow("zero size") << QByteArray::fromHex("0200") << 0;
0072     }
0073 
0074     void testBerElementContentSize()
0075     {
0076         QFETCH(QByteArray, input);
0077         QFETCH(int, size);
0078         BER::Element e(input);
0079         QVERIFY(e.isValid());
0080         QCOMPARE(e.contentSize(), size);
0081         QCOMPARE(e.size(), input.size());
0082         QVERIFY(e.contentData());
0083     }
0084 
0085     void testBerRecursionInvalid_data()
0086     {
0087         QTest::addColumn<QByteArray>("input");
0088         QTest::newRow("incomplete content") << QByteArray::fromHex("1E020201");
0089         QTest::newRow("wrong child size") << QByteArray::fromHex("1E03020242");
0090         QTest::newRow("wrong child size with trailing data") << QByteArray::fromHex("1E03020242020142");
0091         QTest::newRow("variable length child") << QByteArray::fromHex("1E0402804242420000");
0092     }
0093 
0094     void testBerRecursionInvalid()
0095     {
0096         QFETCH(QByteArray, input);
0097         BER::Element e(input);
0098         QVERIFY(e.isValid());
0099         QVERIFY(!e.first().isValid());
0100     }
0101 
0102     void testBerChildCount_data()
0103     {
0104         QTest::addColumn<QByteArray>("input");
0105         QTest::addColumn<int>("childCount");
0106         QTest::newRow("one child") << QByteArray::fromHex("1E03020142") << 1;
0107         QTest::newRow("two children") << QByteArray::fromHex("1E06020142020123") << 2;
0108         QTest::newRow("one child, trailing element") << QByteArray::fromHex("1E03020142020123") << 1;
0109     }
0110 
0111     void testBerChildCount()
0112     {
0113         QFETCH(QByteArray, input);
0114         QFETCH(int, childCount);
0115 
0116         BER::Element e(input);
0117         QVERIFY(e.isValid());
0118 
0119         BER::Element c = e.first();
0120         QVERIFY(c.isValid());
0121         int i = 0;
0122         while (c.isValid()) {
0123             c = c.next();
0124             ++i;
0125         }
0126         QCOMPARE(i, childCount);
0127     }
0128 
0129     void testBerFindChild_data()
0130     {
0131         QTest::addColumn<QByteArray>("input");
0132         QTest::addColumn<uint32_t>("type");
0133         QTest::addColumn<bool>("found");
0134         QTest::newRow("no children") << QByteArray::fromHex("020142") << 0x02u << false;
0135         QTest::newRow("children, not found") << QByteArray::fromHex("1E06020142020123") << 0x04u << false;
0136         QTest::newRow("children, first hit") << QByteArray::fromHex("1E06040142020123") << 0x04u << true;
0137         QTest::newRow("children, second hit") << QByteArray::fromHex("1E06020142040123") << 0x04u << true;
0138     }
0139 
0140     void testBerFindChild()
0141     {
0142         QFETCH(QByteArray, input);
0143         QFETCH(uint32_t, type);
0144         QFETCH(bool, found);
0145 
0146         BER::Element e(input);
0147         QVERIFY(e.isValid());
0148         auto c = e.find(type);
0149         QCOMPARE(c.isValid(), found);
0150     }
0151 };
0152 
0153 QTEST_APPLESS_MAIN(BerDecoderTest)
0154 
0155 #include "berdecodertest.moc"