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

0001 /*
0002     Copyright (C) 2019 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 <elf/elffile.h>
0019 #include <elf/elfsymboltablesection.h>
0020 #include <elf/elfsymboltableentry.h>
0021 
0022 #include <QDebug>
0023 #include <QtTest/qtest.h>
0024 #include <QObject>
0025 
0026 #include <elf.h>
0027 
0028 class ElfSymbolTableTest : public QObject
0029 {
0030     Q_OBJECT
0031 private slots:
0032     void testSymbolTable_data()
0033     {
0034         QTest::addColumn<QString>("executable");
0035         QTest::newRow("single-executable") << QStringLiteral(BINDIR "single-executable");
0036         QTest::newRow("structures") << QStringLiteral(BINDIR "structures");
0037         QTest::newRow("elf-dissector") << QStringLiteral(BINDIR "elf-dissector");
0038     }
0039 
0040     void testSymbolTable()
0041     {
0042         QFETCH(QString, executable);
0043 
0044         ElfFile f(executable);
0045         QVERIFY(f.open(QFile::ReadOnly));
0046         QCOMPARE(f.isValid(), true);
0047 
0048         const auto symtab = f.symbolTable();
0049         QVERIFY(symtab);
0050         QVERIFY(symtab->header()->entryCount() > 0);
0051         QVERIFY(symtab->importCount() > 0);
0052         QVERIFY(symtab->exportCount() > 0);
0053 
0054         for (uint32_t i = 0; i < symtab->header()->entryCount(); ++i) {
0055             const auto entry = symtab->entry(i);
0056             QVERIFY(entry);
0057             QCOMPARE(i, entry->index());
0058             QCOMPARE(symtab->entry(i), entry);
0059             QVERIFY(entry->name());
0060             if (entry->value() && entry->size()) {
0061                 auto revEntry = symtab->entryWithValue(entry->value());
0062                 QVERIFY(revEntry);
0063                 QCOMPARE(revEntry->value(), entry->value()); // not necessarily entry == revEntry though
0064 
0065                 revEntry = symtab->entryContainingValue(entry->value());
0066                 QVERIFY(revEntry);
0067                 QVERIFY(entry->value() >= revEntry->value());
0068                 QVERIFY(entry->value() < revEntry->value() + revEntry->size());
0069 
0070                 const auto val = entry->value() + entry->size() - 1;
0071                 revEntry = symtab->entryContainingValue(val);
0072                 if (!revEntry) {
0073                     qDebug() << entry->name() << entry->value() << entry->size() << entry->type() << entry->bindType();
0074                 }
0075                 QVERIFY(revEntry);
0076                 QVERIFY(val >= revEntry->value());
0077                 QVERIFY(val < revEntry->value() + revEntry->size());
0078             }
0079         }
0080     }
0081 };
0082 
0083 QTEST_MAIN(ElfSymbolTableTest)
0084 
0085 #include "elfsymboltabletest.moc"