| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com> |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include <QtTest/QtTest> |
| 30 | #include <QStorageInfo> |
| 31 | |
| 32 | #include <stdarg.h> |
| 33 | |
| 34 | #include "../../../../manual/qstorageinfo/printvolumes.cpp" |
| 35 | |
| 36 | class tst_QStorageInfo : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | private slots: |
| 40 | void defaultValues(); |
| 41 | void dump(); |
| 42 | void operatorEqual(); |
| 43 | #ifndef Q_OS_WINRT |
| 44 | void operatorNotEqual(); |
| 45 | void root(); |
| 46 | void currentStorage(); |
| 47 | void storageList(); |
| 48 | void tempFile(); |
| 49 | void caching(); |
| 50 | #endif |
| 51 | }; |
| 52 | |
| 53 | void tst_QStorageInfo::defaultValues() |
| 54 | { |
| 55 | QStorageInfo storage; |
| 56 | |
| 57 | QVERIFY(!storage.isValid()); |
| 58 | QVERIFY(!storage.isReady()); |
| 59 | QVERIFY(storage.rootPath().isEmpty()); |
| 60 | QVERIFY(!storage.isRoot()); |
| 61 | QVERIFY(storage.device().isEmpty()); |
| 62 | QVERIFY(storage.fileSystemType().isEmpty()); |
| 63 | QCOMPARE(storage.bytesTotal(), -1); |
| 64 | QCOMPARE(storage.bytesFree(), -1); |
| 65 | QCOMPARE(storage.bytesAvailable(), -1); |
| 66 | } |
| 67 | |
| 68 | static int qInfoPrinter(const char *format, ...) |
| 69 | { |
| 70 | static char buf[1024]; |
| 71 | static size_t bufuse = 0; |
| 72 | |
| 73 | va_list ap; |
| 74 | va_start(ap, format); // use variable arg list |
| 75 | int n = qvsnprintf(str: buf + bufuse, n: sizeof(buf) - bufuse, fmt: format, ap); |
| 76 | va_end(ap); |
| 77 | |
| 78 | bufuse += n; |
| 79 | if (bufuse >= sizeof(buf) - 1 || format[strlen(s: format) - 1] == '\n') { |
| 80 | // flush |
| 81 | QtMessageHandler qt_message_print = qInstallMessageHandler(0); |
| 82 | qInstallMessageHandler(qt_message_print); // restore the handler |
| 83 | qt_message_print(QtInfoMsg, QMessageLogContext(), QString::fromLocal8Bit(str: buf).trimmed()); |
| 84 | bufuse = 0; |
| 85 | } |
| 86 | |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | void tst_QStorageInfo::dump() |
| 91 | { |
| 92 | printVolumes(volumes: QStorageInfo::mountedVolumes(), printer: qInfoPrinter); |
| 93 | } |
| 94 | |
| 95 | void tst_QStorageInfo::operatorEqual() |
| 96 | { |
| 97 | { |
| 98 | QStorageInfo storage1 = QStorageInfo::root(); |
| 99 | QStorageInfo storage2(QDir::rootPath()); |
| 100 | QCOMPARE(storage1, storage2); |
| 101 | } |
| 102 | |
| 103 | { |
| 104 | QStorageInfo storage1(QCoreApplication::applicationDirPath()); |
| 105 | QStorageInfo storage2(QCoreApplication::applicationFilePath()); |
| 106 | QCOMPARE(storage1, storage2); |
| 107 | } |
| 108 | |
| 109 | { |
| 110 | QStorageInfo storage1; |
| 111 | QStorageInfo storage2; |
| 112 | QCOMPARE(storage1, storage2); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #ifndef Q_OS_WINRT |
| 117 | void tst_QStorageInfo::operatorNotEqual() |
| 118 | { |
| 119 | QStorageInfo storage1 = QStorageInfo::root(); |
| 120 | QStorageInfo storage2; |
| 121 | QVERIFY(storage1 != storage2); |
| 122 | } |
| 123 | |
| 124 | void tst_QStorageInfo::root() |
| 125 | { |
| 126 | QStorageInfo storage = QStorageInfo::root(); |
| 127 | |
| 128 | QVERIFY(storage.isValid()); |
| 129 | QVERIFY(storage.isReady()); |
| 130 | QCOMPARE(storage.rootPath(), QDir::rootPath()); |
| 131 | QVERIFY(storage.isRoot()); |
| 132 | QVERIFY(!storage.device().isEmpty()); |
| 133 | QVERIFY(!storage.fileSystemType().isEmpty()); |
| 134 | #ifndef Q_OS_HAIKU |
| 135 | QVERIFY(storage.bytesTotal() >= 0); |
| 136 | QVERIFY(storage.bytesFree() >= 0); |
| 137 | QVERIFY(storage.bytesAvailable() >= 0); |
| 138 | #endif |
| 139 | } |
| 140 | |
| 141 | void tst_QStorageInfo::currentStorage() |
| 142 | { |
| 143 | QString appPath = QCoreApplication::applicationFilePath(); |
| 144 | QStorageInfo storage(appPath); |
| 145 | QVERIFY(storage.isValid()); |
| 146 | QVERIFY(storage.isReady()); |
| 147 | QVERIFY(appPath.startsWith(storage.rootPath(), Qt::CaseInsensitive)); |
| 148 | QVERIFY(!storage.device().isEmpty()); |
| 149 | QVERIFY(!storage.fileSystemType().isEmpty()); |
| 150 | QVERIFY(storage.bytesTotal() >= 0); |
| 151 | QVERIFY(storage.bytesFree() >= 0); |
| 152 | QVERIFY(storage.bytesAvailable() >= 0); |
| 153 | } |
| 154 | |
| 155 | void tst_QStorageInfo::storageList() |
| 156 | { |
| 157 | QStorageInfo root = QStorageInfo::root(); |
| 158 | |
| 159 | QList<QStorageInfo> volumes = QStorageInfo::mountedVolumes(); |
| 160 | |
| 161 | // at least, root storage should be present |
| 162 | QVERIFY(volumes.contains(root)); |
| 163 | volumes.removeOne(t: root); |
| 164 | QVERIFY(!volumes.contains(root)); |
| 165 | |
| 166 | foreach (const QStorageInfo &storage, volumes) { |
| 167 | if (!storage.isReady()) |
| 168 | continue; |
| 169 | |
| 170 | QVERIFY(storage.isValid()); |
| 171 | QVERIFY(!storage.isRoot()); |
| 172 | #ifndef Q_OS_WIN |
| 173 | QVERIFY(!storage.device().isEmpty()); |
| 174 | QVERIFY(!storage.fileSystemType().isEmpty()); |
| 175 | #endif |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void tst_QStorageInfo::tempFile() |
| 180 | { |
| 181 | QTemporaryFile file; |
| 182 | QVERIFY2(file.open(), qPrintable(file.errorString())); |
| 183 | |
| 184 | QStorageInfo storage1(file.fileName()); |
| 185 | #ifdef Q_OS_LINUX |
| 186 | if (storage1.fileSystemType() == "btrfs" ) |
| 187 | QSKIP("This test doesn't work on btrfs, probably due to a btrfs bug" ); |
| 188 | #endif |
| 189 | |
| 190 | qint64 free = storage1.bytesFree(); |
| 191 | QVERIFY(free != -1); |
| 192 | |
| 193 | file.write(data: QByteArray(1024*1024, '1')); |
| 194 | file.flush(); |
| 195 | file.close(); |
| 196 | |
| 197 | QStorageInfo storage2(file.fileName()); |
| 198 | if (free == storage2.bytesFree() && storage2.fileSystemType() == "apfs" ) { |
| 199 | QEXPECT_FAIL("" , "This test is likely to fail on APFS" , Continue); |
| 200 | } |
| 201 | |
| 202 | QVERIFY(free != storage2.bytesFree()); |
| 203 | } |
| 204 | |
| 205 | void tst_QStorageInfo::caching() |
| 206 | { |
| 207 | QTemporaryFile file; |
| 208 | QVERIFY2(file.open(), qPrintable(file.errorString())); |
| 209 | |
| 210 | QStorageInfo storage1(file.fileName()); |
| 211 | #ifdef Q_OS_LINUX |
| 212 | if (storage1.fileSystemType() == "btrfs" ) |
| 213 | QSKIP("This test doesn't work on btrfs, probably due to a btrfs bug" ); |
| 214 | #endif |
| 215 | |
| 216 | qint64 free = storage1.bytesFree(); |
| 217 | QStorageInfo storage2(storage1); |
| 218 | QCOMPARE(free, storage2.bytesFree()); |
| 219 | QVERIFY(free != -1); |
| 220 | |
| 221 | file.write(data: QByteArray(1024*1024, '\0')); |
| 222 | file.flush(); |
| 223 | |
| 224 | QCOMPARE(free, storage1.bytesFree()); |
| 225 | QCOMPARE(free, storage2.bytesFree()); |
| 226 | storage2.refresh(); |
| 227 | QCOMPARE(storage1, storage2); |
| 228 | if (free == storage2.bytesFree() && storage2.fileSystemType() == "apfs" ) { |
| 229 | QEXPECT_FAIL("" , "This test is likely to fail on APFS" , Continue); |
| 230 | } |
| 231 | QVERIFY(free != storage2.bytesFree()); |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | QTEST_MAIN(tst_QStorageInfo) |
| 236 | |
| 237 | #include "tst_qstorageinfo.moc" |
| 238 | |