File indexing completed on 2024-05-05 05:48:55

0001 /*
0002     SPDX-FileCopyrightText: 2023 Arjen Hiemstra <ahiemstra@heimr.nl>
0003 
0004     SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 
0009 #include <QtTest>
0010 
0011 #include "core/fstab.h"
0012 
0013 using namespace Qt::StringLiterals;
0014 
0015 struct ExpectedEntry {
0016     bool comment = false;
0017     QString device = QString();
0018     QString mountPoint = QString();
0019     QString type = QString();
0020 };
0021 
0022 class FsTabTest : public QObject
0023 {
0024     Q_OBJECT
0025 
0026 private Q_SLOTS:
0027 
0028     void initTestCase()
0029     {
0030 
0031     }
0032 
0033     void testReadFstab()
0034     {
0035         auto fstabFile = QFINDTESTDATA("test_fstab_data/fstab");
0036         auto entries = readFstabEntries(fstabFile);
0037 
0038         std::array<ExpectedEntry, 13> expected_entries = {{
0039             {.comment = true},
0040             {.comment = true},
0041             {.comment = true},
0042             {.comment = true},
0043             {.device = u"/dev/sda"_s, .mountPoint = u"/"_s, .type = u"ext4"_s },
0044             {.comment = true},
0045             {.device = u""_s, .mountPoint = u"/test"_s, .type = u"btrfs"_s},
0046             {.comment = true},
0047             {.device = u"127.0.0.1:/nfs/export"_s, .mountPoint = u"/nfs"_s, .type = u"nfs"_s},
0048             {.comment = true},
0049             {.device = u"none"_s, .mountPoint = u"/tmp"_s, .type = u"tmpfs"_s},
0050             {.comment = true},
0051             {.device = u"/dev/mapper/swap"_s, .mountPoint = u"none"_s, .type = u"swap"_s},
0052         }};
0053 
0054         QCOMPARE(entries.size(), expected_entries.size());
0055 
0056         for (int i = 0; i < entries.size(); ++i) {
0057             auto entry = entries.at(i);
0058             auto expected = expected_entries.at(i);
0059 
0060             if (expected.comment) {
0061                 QVERIFY(!entry.comment().isNull());
0062                 continue;
0063             }
0064 
0065             QCOMPARE(entry.deviceNode(), expected.device);
0066             QCOMPARE(entry.mountPoint(), expected.mountPoint);
0067             QCOMPARE(entry.type(), expected.type);
0068         }
0069     }
0070 
0071     void testWriteFstab()
0072     {
0073         auto entries = FstabEntryList{{
0074             {{}, {}, {}, {}, {}, {}, u"# Static information about the filesystems."_s},
0075             {{}, {}, {}, {}, {}, {}, u"# See fstab(5) for details."_s},
0076             {{}, {}, {}, {}, {}, {}, {}},
0077             {{}, {}, {}, {}, {}, {}, u"# <file system> <dir> <type> <options> <dump> <pass>"_s},
0078             {u"/dev/sda"_s, u"/"_s, u"ext4"_s, u"rw,relatime,discard"_s, 0, 1},
0079             {{}, {}, {}, {}, {}, {}, {}},
0080             {u"UUID=0491f5bc-487c-4797-b118-78add1e9cfb0"_s, u"/test"_s, u"btrfs"_s, u"defaults"_s, 0, 2},
0081             {{}, {}, {}, {}, {}, {}, {}},
0082             {u"127.0.0.1:/nfs/export"_s, u"/nfs"_s, u"nfs"_s, u"defaults"_s, 0, 2},
0083             {{}, {}, {}, {}, {}, {}, {}},
0084             {u"none"_s, u"/tmp"_s, u"tmpfs"_s, u"defaults"_s, 0, 0},
0085             {{}, {}, {}, {}, {}, {}, {}},
0086             {u"/dev/mapper/swap"_s, u"none"_s, u"swap"_s, u"defaults"_s, 0, 0},
0087         }};
0088 
0089         auto fstab = generateFstab(entries).toUtf8().split('\n');
0090 
0091         auto fstabFilePath = QFINDTESTDATA("test_fstab_data/fstab");
0092         QFile fstabFile(fstabFilePath);
0093         QVERIFY(fstabFile.open(QIODevice::OpenModeFlag::ReadOnly));
0094         auto fileData = fstabFile.readAll().split('\n');
0095         fstabFile.close();
0096 
0097         QCOMPARE(fstab, fileData);
0098 
0099         // Test whether an empty mount point for not-swap is properly ignored.
0100         entries.append({u"/dev/sda"_s, u"none"_s, u"ext4"_s, u"defaults"_s});
0101         entries.append({u"/dev/sda"_s, QString(), u"ext4"_s, u"defaults"_s});
0102         fstab = generateFstab(entries).toUtf8().split('\n');
0103 
0104         QCOMPARE(fstab, fileData);
0105     }
0106 };
0107 
0108 QTEST_GUILESS_MAIN(FsTabTest)
0109 
0110 
0111 #include "test_fstab.moc"