1 /*
2     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
3 
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU Library General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or (at your
7     option) any later version.
8 
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
12     License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 #include <elf/elffile.h>
19 #include <elf/elfnotesection.h>
20 #include <elf/elfnoteentry.h>
21 
22 #include <QtTest/qtest.h>
23 #include <QObject>
24 
25 #include <elf.h>
26 
27 class ElfNoteSectionTest : public QObject
28 {
29     Q_OBJECT
30 
checkLinuxABISection(const ElfFile & f)31     void checkLinuxABISection(const ElfFile& f)
32     {
33         const auto index = f.indexOfSection(".note.ABI-tag");
34         QVERIFY(index > 0);
35 
36         ElfNoteSection *section = f.section<ElfNoteSection>(index);
37         QVERIFY(section);
38 
39         QCOMPARE(section->entryCount(), 1);
40         const auto entry = section->entry(0);
41 
42         QVERIFY(entry);
43         QCOMPARE(entry->section(), section);
44         QCOMPARE(entry->size(), section->size());
45         QCOMPARE(entry->type(), (uint64_t)NT_GNU_ABI_TAG);
46         QCOMPARE(entry->name(), "GNU");
47         QVERIFY(entry->isGNUVendorNote());
48         QCOMPARE(entry->descriptionSize(), (uint64_t)16);
49     }
50 
checkFreeBSDABISection(const ElfFile & f)51     void checkFreeBSDABISection(const ElfFile& f)
52     {
53         const auto index = f.indexOfSection(".note.tag");
54         QVERIFY(index > 0);
55 
56         ElfNoteSection *section = f.section<ElfNoteSection>(index);
57         QVERIFY(section);
58 
59         QVERIFY(section->entryCount() > 0);
60         const auto entry = section->entry(0);
61 
62         QVERIFY(entry);
63         QCOMPARE(entry->section(), section);
64         QVERIFY(entry->size() <= section->size());
65         QCOMPARE(entry->type(), (uint64_t)NT_GNU_ABI_TAG);
66         QCOMPARE(entry->name(), "FreeBSD");
67         QVERIFY(!entry->isGNUVendorNote());
68         QCOMPARE(entry->descriptionSize(), (uint64_t)4);
69     }
70 
71 private slots:
testABISection()72     void testABISection()
73     {
74         ElfFile f(QStringLiteral(BINDIR "single-executable"));
75         QVERIFY(f.open(QFile::ReadOnly));
76         QCOMPARE(f.isValid(), true);
77 
78 #ifdef Q_OS_FREEBSD
79         checkFreeBSDABISection(f);
80 #else
81         checkLinuxABISection(f);
82 #endif
83     }
84 };
85 
86 QTEST_MAIN(ElfNoteSectionTest)
87 
88 #include "elfnotesectiontest.moc"
89