File indexing completed on 2024-05-12 05:55:43

0001 /*
0002  *  This file is part of the Okteta Kasten module, made within the KDE community.
0003  *
0004  *  SPDX-FileCopyrightText: 2013 Alex Richardson <alex.richardson@gmx.de>
0005  *
0006  *  SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 
0009 #include <QTest>
0010 #include <QScriptEngine>
0011 
0012 #include "view/structures/script/scriptengineinitializer.hpp"
0013 #include "view/structures/parsers/scriptvalueconverter.hpp"
0014 #include "view/structures/datatypes/topleveldatainformation.hpp"
0015 #include "testutils.hpp"
0016 #include <Okteta/ByteArrayModel>
0017 
0018 class CustomToStringTest : public QObject
0019 {
0020     Q_OBJECT
0021 
0022 private Q_SLOTS:
0023     void initTestCase();
0024     void testUuid_data();
0025     void testUuid();
0026 };
0027 
0028 static uchar uuid1[16] =
0029 {
0030     0x55, 0x0e, 0x84, 0x00, 0xe2, 0x9b, 0x41, 0xd4, 0xa7, 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00
0031 };
0032 
0033 static uchar uuid2[16] =
0034 {
0035     0x3f, 0x25, 0x04, 0xe0, 0x4f, 0x89, 0x11, 0xd3, 0x9a, 0x0c, 0x03, 0x05, 0xe8, 0x2c, 0x33, 0x01
0036 };
0037 
0038 static uchar nullUuid[16] =
0039 {
0040     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
0041 };
0042 
0043 void CustomToStringTest::initTestCase()
0044 {
0045     // needed so that imports can be resolved
0046     QString examples = QFINDTESTDATA("../view/structures/examples");
0047     QVERIFY2(!examples.isEmpty(), "Test data must exist!");
0048     qputenv("XDG_DATA_DIRS", QFile::encodeName(QFileInfo(examples).absoluteFilePath()));
0049 }
0050 
0051 void CustomToStringTest::testUuid_data()
0052 {
0053     QTest::addColumn<bool>("isGUID");
0054     QTest::addColumn<QString>("uuidString");
0055     QTest::addColumn<QByteArray>("data");
0056     QTest::newRow("uuid1") << false << "{550e8400-e29b-41d4-a716-446655440000}"
0057                            << QByteArray::fromRawData(reinterpret_cast<char*>(uuid1), sizeof(uuid1));
0058     QTest::newRow("uuid2") << false << "{3f2504e0-4f89-11d3-9a0c-0305e82c3301}"
0059                            << QByteArray::fromRawData(reinterpret_cast<char*>(uuid2), sizeof(uuid2));
0060     QTest::newRow("null uuid") << false << "{00000000-0000-0000-0000-000000000000}"
0061                                << QByteArray::fromRawData(reinterpret_cast<char*>(nullUuid), sizeof(nullUuid));
0062 
0063     // now the same just as a Microsoft GUID
0064     QTest::newRow("guid1") << true << "{00840e55-9be2-d441-a716-446655440000}"
0065                            << QByteArray::fromRawData(reinterpret_cast<char*>(uuid1), sizeof(uuid1));
0066     QTest::newRow("guid2") << true << "{e004253f-894f-d311-9a0c-0305e82c3301}"
0067                            << QByteArray::fromRawData(reinterpret_cast<char*>(uuid2), sizeof(uuid2));
0068     QTest::newRow("null guid") << true << "{00000000-0000-0000-0000-000000000000}"
0069                                << QByteArray::fromRawData(reinterpret_cast<char*>(nullUuid), sizeof(nullUuid));
0070 }
0071 
0072 void CustomToStringTest::testUuid()
0073 {
0074     QFETCH(QByteArray, data);
0075     QFETCH(QString, uuidString);
0076     QFETCH(bool, isGUID);
0077     QScriptEngine* eng = ScriptEngineInitializer::newEngine();
0078     auto* logger = new ScriptLogger();
0079     logger->setLogToStdOut(true);
0080     DataInformation* structure = nullptr;
0081     if (isGUID) {
0082         structure = Utils::evalAndParse(eng, "var u = importScript('uuid.js'); u.GUID();", logger);
0083     } else {
0084         structure = Utils::evalAndParse(eng, "var u = importScript('uuid.js'); u.UUID();", logger);
0085     }
0086 #if defined(Q_OS_MACOS) || defined(Q_OS_WINDOWS)
0087   QEXPECT_FAIL("", "QStandardPaths::GenericDataLocation can't be modified on macOS/Windows", Abort);
0088 #endif
0089     QVERIFY(structure);
0090     TopLevelDataInformation top(structure, logger, eng);
0091     QCOMPARE(structure->childCount(), 4U);
0092 
0093     QVERIFY(structure->toStringFunction().isFunction());
0094     Okteta::ByteArrayModel model(reinterpret_cast<const uchar*>(data.constData()), data.size());
0095     model.setAutoDelete(false);
0096     top.read(&model, 0, Okteta::ArrayChangeMetricsList(), true);
0097 
0098     QCOMPARE(structure->childAt(0)->effectiveByteOrder(),
0099              isGUID ? QSysInfo::LittleEndian : QSysInfo::BigEndian);
0100     QCOMPARE(structure->childAt(1)->effectiveByteOrder(),
0101              isGUID ? QSysInfo::LittleEndian : QSysInfo::BigEndian);
0102     QCOMPARE(structure->childAt(2)->effectiveByteOrder(),
0103              isGUID ? QSysInfo::LittleEndian : QSysInfo::BigEndian);
0104     bool ok;
0105     quint32 val1 = uuidString.midRef(1, 8).toUInt(&ok, 16);
0106     QVERIFY(ok);
0107     quint16 val2 = uuidString.midRef(10, 4).toUShort(&ok, 16);
0108     QVERIFY(ok);
0109     quint16 val3 = uuidString.midRef(15, 4).toUShort(&ok, 16);
0110     QVERIFY(ok);
0111     qDebug() << Qt::hex << val1 << val2 << val3;
0112     QCOMPARE(structure->childAt(0)->asPrimitive()->value().value<quint32>(), val1);
0113     QCOMPARE(structure->childAt(1)->asPrimitive()->value().value<quint16>(), val2);
0114     QCOMPARE(structure->childAt(2)->asPrimitive()->value().value<quint16>(), val3);
0115 
0116     QString typeStr = isGUID ? QStringLiteral("GUID") : QStringLiteral("UUID");
0117     QCOMPARE(structure->typeName(), typeStr);
0118     QCOMPARE(structure->valueString(), uuidString);
0119 }
0120 
0121 QTEST_GUILESS_MAIN(CustomToStringTest)
0122 
0123 #include "customtostringtest.moc"