Changeset View
Changeset View
Standalone View
Standalone View
autotests/filesystem_table_parser_test.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by David Hallas <david@davidhallas.dk> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public 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, write to the * | ||||
16 | * Free Software Foundation, Inc., * | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
18 | ***************************************************************************/ | ||||
19 | | ||||
20 | #include "filesystem_table_parser_test.h" | ||||
21 | #include <solid/devices/backends/fstab/filesystem_entry.h> | ||||
22 | #include <solid/devices/backends/fstab/filesystem_table_parser.h> | ||||
23 | | ||||
24 | #include <QTemporaryFile> | ||||
25 | #include <QTest> | ||||
26 | | ||||
27 | namespace Fstab = Solid::Backends::Fstab; | ||||
28 | | ||||
29 | const char exampleFstab[] = | ||||
30 | "/dev/sda1 /boot ext2 noauto,noatime 1 2\n" | ||||
31 | "/dev/sda2 / ext4 noatime 0 1\n" ; | ||||
32 | | ||||
33 | void FilesystemTableParserTest::testHappyDay() | ||||
34 | { | ||||
35 | QTemporaryFile testFstabFile; | ||||
36 | QVERIFY(testFstabFile.open()); | ||||
37 | testFstabFile.write(exampleFstab); | ||||
38 | testFstabFile.close(); | ||||
39 | QList<Fstab::FilesystemEntry> entries = Fstab::FilesystemTableParser::parseFilesystemTable(testFstabFile.fileName()); | ||||
40 | QCOMPARE(entries.size(), 2); | ||||
41 | { | ||||
42 | QCOMPARE(entries.at(0).device(), QLatin1String("/dev/sda1")); | ||||
43 | QCOMPARE(entries.at(0).mountPath(), QLatin1String("/boot")); | ||||
44 | QCOMPARE(entries.at(0).type(), QLatin1String("ext2")); | ||||
45 | const QStringList expectedOptions{QLatin1String("noauto"), QLatin1String("noatime")}; | ||||
46 | QCOMPARE(entries.at(0).mountOptions(), expectedOptions); | ||||
47 | QCOMPARE(entries.at(0).id(), QLatin1String("/dev/sda1")); | ||||
48 | } | ||||
49 | QCOMPARE(entries.at(1).device(), QLatin1String("/dev/sda2")); | ||||
50 | QCOMPARE(entries.at(1).mountPath(), QLatin1String("/")); | ||||
51 | QCOMPARE(entries.at(1).type(), QLatin1String("ext4")); | ||||
52 | QCOMPARE(entries.at(1).mountOptions(), QStringList(QLatin1String("noatime"))); | ||||
53 | QCOMPARE(entries.at(1).id(), QLatin1String("/dev/sda2")); | ||||
54 | } | ||||
55 | | ||||
56 | void FilesystemTableParserTest::testWithFilterFunction() | ||||
57 | { | ||||
58 | QTemporaryFile testFstabFile; | ||||
59 | QVERIFY(testFstabFile.open()); | ||||
60 | testFstabFile.write(exampleFstab); | ||||
61 | testFstabFile.close(); | ||||
62 | const auto filterFunction = | ||||
63 | [](const QString& device, const QString& type) -> Fstab::FilesystemTableParser::FilterReturn | ||||
64 | { | ||||
65 | if (device == QLatin1String("/dev/sda2") && type == QLatin1String("ext4")) { | ||||
66 | return Fstab::FilesystemTableParser::FilterReturn::Keep; | ||||
67 | } | ||||
68 | return Fstab::FilesystemTableParser::FilterReturn::Discard; | ||||
69 | }; | ||||
70 | QList<Fstab::FilesystemEntry> entries = Fstab::FilesystemTableParser::parseFilesystemTable( | ||||
71 | testFstabFile.fileName(), filterFunction); | ||||
72 | QCOMPARE(entries.size(), 1); | ||||
73 | QCOMPARE(entries.at(0).device(), QLatin1String("/dev/sda2")); | ||||
74 | QCOMPARE(entries.at(0).mountPath(), QLatin1String("/")); | ||||
75 | QCOMPARE(entries.at(0).type(), QLatin1String("ext4")); | ||||
76 | QCOMPARE(entries.at(0).mountOptions(), QStringList(QLatin1String("noatime"))); | ||||
77 | QCOMPARE(entries.at(0).id(), QLatin1String("/dev/sda2")); | ||||
78 | } | ||||
79 | | ||||
80 | void FilesystemTableParserTest::testFileNotFound() | ||||
81 | { | ||||
82 | QList<Fstab::FilesystemEntry> entries = Fstab::FilesystemTableParser::parseFilesystemTable(QLatin1String("/non_existing_file")); | ||||
83 | QCOMPARE(entries.size(), 0); | ||||
84 | } | ||||
85 | | ||||
86 | const char exampleMtabWithSpecialCharacters[] = | ||||
87 | "/home/dha/some.iso /home/dha/mn\\\\t\\0401 fuseiso defaults 0 0\n" | ||||
88 | "/home/dha/tab_character\\011_in_file_name.iso /home/dha/space_\\040_in_dir_name fuseiso defaults 0 0\n"; | ||||
89 | | ||||
90 | void FilesystemTableParserTest::testParseSpecialCharacters() | ||||
91 | { | ||||
92 | QTemporaryFile testFstabFile; | ||||
93 | QVERIFY(testFstabFile.open()); | ||||
94 | testFstabFile.write(exampleMtabWithSpecialCharacters); | ||||
95 | testFstabFile.close(); | ||||
96 | QList<Fstab::FilesystemEntry> entries = Fstab::FilesystemTableParser::parseFilesystemTable(testFstabFile.fileName()); | ||||
97 | QCOMPARE(entries.size(), 2); | ||||
98 | QCOMPARE(entries.at(0).device(), QLatin1String("/home/dha/some.iso")); | ||||
99 | QCOMPARE(entries.at(0).mountPath(), QLatin1String("/home/dha/mn\\t 1")); | ||||
100 | QCOMPARE(entries.at(0).type(), QLatin1String("fuseiso")); | ||||
101 | QCOMPARE(entries.at(0).mountOptions(), QStringList(QLatin1String("defaults"))); | ||||
102 | QCOMPARE(entries.at(0).id(), QLatin1String("/home/dha/some.iso")); | ||||
103 | QCOMPARE(entries.at(1).device(), QLatin1String("/home/dha/tab_character\t_in_file_name.iso")); | ||||
104 | QCOMPARE(entries.at(1).mountPath(), QLatin1String("/home/dha/space_ _in_dir_name")); | ||||
105 | QCOMPARE(entries.at(1).type(), QLatin1String("fuseiso")); | ||||
106 | QCOMPARE(entries.at(1).mountOptions(), QStringList(QLatin1String("defaults"))); | ||||
107 | QCOMPARE(entries.at(1).id(), QLatin1String("/home/dha/tab_character\t_in_file_name.iso")); | ||||
108 | } | ||||
109 | | ||||
110 | QTEST_MAIN(FilesystemTableParserTest) |