Ensure the profile path is pointing to a directory
[qBittorrent.git] / test / testutilsio.cpp
blobfdc1358b1355590b156321b835199af0d9017072
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include <QtGlobal>
30 #include <QTest>
32 #include "base/global.h"
33 #include "base/path.h"
34 #include "base/utils/io.h"
36 class TestUtilsIO final : public QObject
38 Q_OBJECT
39 Q_DISABLE_COPY_MOVE(TestUtilsIO)
41 public:
42 TestUtilsIO() = default;
44 private slots:
45 void testReadFile() const
47 const Path testFolder = Path(QString::fromUtf8(__FILE__)).parentPath() / Path(u"testdata"_s);
49 const Path size10File = testFolder / Path(u"size10.txt"_s);
50 const QByteArray size10Data = QByteArrayLiteral("123456789\n");
53 const auto readResult = Utils::IO::readFile(size10File, 0);
54 QCOMPARE(readResult.has_value(), false);
55 QCOMPARE(readResult.error().status, Utils::IO::ReadError::ExceedSize);
56 QCOMPARE(readResult.error().message.isEmpty(), false);
59 const auto readResult = Utils::IO::readFile(size10File, 9);
60 QCOMPARE(readResult.has_value(), false);
61 QCOMPARE(readResult.error().status, Utils::IO::ReadError::ExceedSize);
62 QCOMPARE(readResult.error().message.isEmpty(), false);
65 const auto readResult = Utils::IO::readFile(size10File, 10);
66 QCOMPARE(readResult.has_value(), true);
67 QCOMPARE(readResult.value(), size10Data);
70 const auto readResult = Utils::IO::readFile(size10File, 11);
71 QCOMPARE(readResult.has_value(), true);
72 QCOMPARE(readResult.value(), size10Data);
75 const auto readResult = Utils::IO::readFile(size10File, -1);
76 QCOMPARE(readResult.has_value(), true);
77 QCOMPARE(readResult.value(), size10Data);
81 const Path crlfFile = testFolder / Path(u"crlf.txt"_s);
82 const auto readResult = Utils::IO::readFile(crlfFile, -1, QIODevice::Text);
83 QCOMPARE(readResult.has_value(), true);
84 QCOMPARE(readResult.value(), "\n\n");
88 const Path nonExistFile = testFolder / Path(u".non_existent_file_1234"_s);
89 const auto readResult = Utils::IO::readFile(nonExistFile, 1);
90 QCOMPARE(readResult.has_value(), false);
91 QCOMPARE(readResult.error().status, Utils::IO::ReadError::NotExist);
92 QCOMPARE(readResult.error().message.isEmpty(), false);
95 #ifdef Q_OS_UNIX
97 const auto readResult = Utils::IO::readFile(Path(u"/dev/null"_s), 10);
98 QCOMPARE(readResult.has_value(), true);
99 QCOMPARE(readResult.value().length(), 0);
102 const auto readResult = Utils::IO::readFile(Path(u"/dev/null"_s), -1);
103 QCOMPARE(readResult.has_value(), true);
104 QCOMPARE(readResult.value().length(), 0);
108 const auto readResult = Utils::IO::readFile(Path(u"/dev/zero"_s), 10);
109 QCOMPARE(readResult.has_value(), true);
110 QCOMPARE(readResult.value().length(), 0);
113 const auto readResult = Utils::IO::readFile(Path(u"/dev/zero"_s), -1);
114 QCOMPARE(readResult.has_value(), true);
115 QCOMPARE(readResult.value().length(), 0);
117 #endif
121 QTEST_APPLESS_MAIN(TestUtilsIO)
122 #include "testutilsio.moc"