Warning, file /utilities/okteta/kasten/controllers/test/customtostringtest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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