File: smartdatatest.cpp

package info (click to toggle)
plasma-disks 6.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,056 kB
  • sloc: cpp: 1,603; xml: 299; makefile: 5; sh: 2
file content (120 lines) | stat: -rw-r--r-- 4,499 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
// SPDX-FileCopyrightText: 2020-2021 Harald Sitter <sitter@kde.org>

#include <QDebug>
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QObject>
#include <QStandardPaths>
#include <QTest>

#include <KSycoca>
#include <smartdata.h>

class SMARTDataTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void testPass()
    {
        QFile file(QFINDTESTDATA("fixtures/pass.json"));
        QVERIFY(file.open(QFile::ReadOnly));
        auto doc = QJsonDocument::fromJson(file.readAll());
        SMARTData data(doc);
        QCOMPARE(data.m_device, "/dev/testfoobarpass");
        QCOMPARE(data.m_status.m_passed, true);
        QVERIFY(!data.m_smartctl.failure());
        QVERIFY(data.m_valid);
    }

    void testFail()
    {
        // NB: fixture isn't actually of a failure, so fields need tweaking as necessary
        QFile file(QFINDTESTDATA("fixtures/fail.json"));
        QVERIFY(file.open(QFile::ReadOnly));
        auto doc = QJsonDocument::fromJson(file.readAll());
        SMARTData data(doc);
        QCOMPARE(data.m_device, "/dev/testfoobarfail");
        QCOMPARE(data.m_status.m_passed, false);
        QVERIFY(!data.m_smartctl.failure());
        QVERIFY(data.m_valid);
    }

    void testBroken()
    {
        // Actual broken device provided by ahiemstra@heimr.nl
        QFile file(QFINDTESTDATA("fixtures/broken.json"));
        QVERIFY(file.open(QFile::ReadOnly));
        auto doc = QJsonDocument::fromJson(file.readAll());
        SMARTData data(doc);
        QCOMPARE(data.m_device, "/dev/sdc");
        QCOMPARE(data.m_status.m_passed, false);
        QCOMPARE(data.m_smartctl.failure(),
                 SMART::Failures({SMART::Failure::Disk, SMART::Failure::Prefail, SMART::Failure::ErrorsRecorded, SMART::Failure::SelfTestErrors}));
        QVERIFY(data.m_smartctl.failure());
        QVERIFY(!!data.m_smartctl.failure());
        QVERIFY(data.m_valid);
    }

    void testTimeout()
    {
        // Query timed out but overall status is available and success.
        // From https://bugs.kde.org/show_bug.cgi?id=428844
        QFile file(QFINDTESTDATA("fixtures/error-info-log-failed.json"));
        QVERIFY(file.open(QFile::ReadOnly));
        auto doc = QJsonDocument::fromJson(file.readAll());
        SMARTData data(doc);
        QCOMPARE(data.m_device, "/dev/nvme0n1");
        QCOMPARE(data.m_status.m_passed, true);
        QCOMPARE(data.m_smartctl.failure(), SMART::Failures({SMART::Failure::InternalCommand}));
        QVERIFY(data.m_smartctl.failure());
        QVERIFY(!!data.m_smartctl.failure());
        QVERIFY(data.m_valid);
    }

    void testFailingSectorsButPassingStatus()
    {
        // SMART status is a pass but there are problems.
        QFile file(QFINDTESTDATA("fixtures/failing-sectors-passing-status.json"));
        QVERIFY(file.open(QFile::ReadOnly));
        auto doc = QJsonDocument::fromJson(file.readAll());
        SMARTData data(doc);
        QCOMPARE(data.m_device, "/dev/sdb");
        QCOMPARE(data.m_status.m_passed, true);
        QCOMPARE(data.m_smartctl.failure(), SMART::Failures({SMART::Failure::ErrorsRecorded | SMART::Failure::SelfTestErrors}));
        QVERIFY(data.m_valid);
    }

    void testVBoxDrive()
    {
        // VBox virtual drives fail with bit2 and have no status
        QFile file(QFINDTESTDATA("fixtures/invalid-vbox.json"));
        QVERIFY(file.open(QFile::ReadOnly));
        auto doc = QJsonDocument::fromJson(file.readAll());
        SMARTData data(doc);
        QCOMPARE(data.m_device, "/dev/sda");
        QCOMPARE(data.m_status.m_passed, false);
        QCOMPARE(data.m_smartctl.failure(), SMART::Failures({SMART::Failure::InternalCommand}));
        QVERIFY(!data.m_valid);
    }

    void testNoSmartStatusNoError()
    {
        // When SMART is disabled we get no smart_status but also no error. Ought to be invalid all the same and
        // ignored.
        // https://bugs.kde.org/show_bug.cgi?id=435699
        QFile file(QFINDTESTDATA("fixtures/pass-without-status.json"));
        QVERIFY(file.open(QFile::ReadOnly));
        auto doc = QJsonDocument::fromJson(file.readAll());
        SMARTData data(doc);
        QCOMPARE(data.m_device, "/dev/sdb");
        QCOMPARE(data.m_status.m_passed, false);
        QCOMPARE(data.m_smartctl.failure(), SMART::Failures());
        QVERIFY(!data.m_valid);
    }
};

QTEST_MAIN(SMARTDataTest)

#include "smartdatatest.moc"