File indexing completed on 2024-04-28 05:41:05

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include <dwarf/dwarfexpression.h>
0019 
0020 #include <QtTest/qtest.h>
0021 #include <QObject>
0022 
0023 class DwarfExpressionTest : public QObject
0024 {
0025     Q_OBJECT
0026 private slots:
0027     void testDisplayString_data()
0028     {
0029         QTest::addColumn<QByteArray>("block");
0030         QTest::addColumn<QString>("display");
0031 
0032         // real-world examples
0033         QTest::newRow("empty") << QByteArray() << QStringLiteral("<empty>");
0034         QTest::newRow("virtual inheritance 24") << QByteArray("\x12\x06\x48\x1c\x06\x22") << QStringLiteral("DW_OP_dup DW_OP_deref DW_OP_lit24 DW_OP_minus DW_OP_deref DW_OP_plus");
0035         QTest::newRow("virtual inheritance 32") << QByteArray("\x12\x06\x08\x20\x1c\x06\x22") << QStringLiteral("DW_OP_dup DW_OP_deref DW_OP_const1u 32 DW_OP_minus DW_OP_deref DW_OP_plus");
0036         QTest::newRow("virtual inheritance 112") << QByteArray("\x12\x06\x08\x70\x1c\x06\x22") << QStringLiteral("DW_OP_dup DW_OP_deref DW_OP_const1u 112 DW_OP_minus DW_OP_deref DW_OP_plus");
0037         QTest::newRow("vtable elem offset 0") << QByteArray("\x10\x00", 2) << QStringLiteral("DW_OP_constu 0");
0038         QTest::newRow("vtable elem offset 4") << QByteArray("\x10\x04") << QStringLiteral("DW_OP_constu 4");
0039         QTest::newRow("address") << QByteArray("\x03\x34\x08\x40\x00\x00\x00\x00\x00", 9) << QStringLiteral("DW_OP_addr 0x400834");
0040         QTest::newRow("argument location") << QByteArray("\x91\x88\x7f") << QStringLiteral("DW_OP_fbreg -120");
0041 
0042         // artificial
0043         QTest::newRow("OP_const1s") << QByteArray("\x09\xfe") << QStringLiteral("DW_OP_const1s -2");
0044         QTest::newRow("OP_const2u") << QByteArray("\x0a\x2a\x00", 3) << QStringLiteral("DW_OP_const2u 42");
0045         QTest::newRow("OP_const4s") << QByteArray("\x0d\xfd\xff\xff\xff") << QStringLiteral("DW_OP_const4s -3");
0046         QTest::newRow("OP_const8u") << QByteArray("\x0e\x34\x08\x40\x00\x00\x00\x00\x00", 9) << QStringLiteral("DW_OP_const8u 4196404");
0047     }
0048 
0049     void testDisplayString()
0050     {
0051         QFETCH(QByteArray, block);
0052         QFETCH(QString, display);
0053 
0054         DwarfExpression exp((void*)block.constData(), block.size(), 8);
0055         QCOMPARE(exp.displayString(), display);
0056     }
0057 
0058     void testSimpleEval_data()
0059     {
0060         QTest::addColumn<QByteArray>("block");
0061         QTest::addColumn<int>("result");
0062 
0063         QTest::newRow("OP_addr") << QByteArray("\x03\x34\x08\x40\x00\x00\x00\x00\x00", 9) << 0x400834;
0064         QTest::newRow("OP_const1s") << QByteArray("\x09\xfe") << -2;
0065         QTest::newRow("OP_const2u") << QByteArray("\x0a\x2a\x00", 3) << 42;
0066         QTest::newRow("OP_const4s") << QByteArray("\x0d\xfd\xff\xff\xff") << -3;
0067         QTest::newRow("OP_const8u") << QByteArray("\x0e\x34\x08\x40\x00\x00\x00\x00\x00", 9) << 4196404;
0068         QTest::newRow("OP_constu") << QByteArray("\x10\x00", 2) << 0;
0069         QTest::newRow("OP_constu") << QByteArray("\x10\x04") << 4;
0070 
0071         QTest::newRow("OP_lit23 OP_dup OP_plus") << QByteArray("\x47\x12\x22") << 46;
0072         QTest::newRow("OP_const1s 42 OP_lit20 OP_minus") << QByteArray("\x09\x2a\x44\x1c") << 22;
0073     }
0074 
0075     void testSimpleEval()
0076     {
0077         QFETCH(QByteArray, block);
0078         QFETCH(int, result);
0079 
0080         DwarfExpression exp((void*)block.constData(), block.size(), 8);
0081         QVERIFY(exp.evaluateSimple());
0082         QCOMPARE(exp.top(), (uint64_t)result);
0083     }
0084 };
0085 
0086 QTEST_MAIN(DwarfExpressionTest)
0087 
0088 #include "dwarfexpressiontest.moc"