diff --git a/src/core/smartparser.h b/src/core/smartparser.h index 3438e8d..ace6135 100644 --- a/src/core/smartparser.h +++ b/src/core/smartparser.h @@ -1,63 +1,65 @@ /************************************************************************* * Copyright (C) 2018 by Caio Carvalho * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 3 of * * the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see .* *************************************************************************/ #if !defined(KPMCORE_SMARTPARSER_H) #define KPMCORE_SMARTPARSER_H +#include "util/libpartitionmanagerexport.h" + #include #include class SmartDiskInformation; /** A parser to SMART JSON output. Responsible to execute smartctl and parse its output. @author Caio Carvalho */ -class SmartParser +class LIBKPMCORE_EXPORT SmartParser { public: SmartParser(const QString &device_path); public: bool init(); public: const QString &devicePath() const { return m_DevicePath; /**< @return the device path that SMART must analyse */ } SmartDiskInformation *diskInformation() const { return m_DiskInformation; /**< @return a reference to parsed disk information */ } protected: void loadSmartOutput(); void loadAttributes(); private: const QString m_DevicePath; QJsonDocument m_SmartOutput; SmartDiskInformation *m_DiskInformation; }; #endif // SMARTPARSER_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e81581e..67b9cea 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,54 +1,58 @@ # Tests for KPMcore # # These are not so much "tests" as "small example programs". They illustrate # how to use the library, how to perform common tasks. set(CMAKE_SKIP_BUILD_RPATH FALSE) SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) include_directories(${CMAKE_SOURCE_DIR}/src) # To get at KPMcore headers add_compile_options(-fPIC) ### # # Helper macro to link to the helper (for initialization of kpmcore) # and to add a test with the given name. # add_library(testhelpers OBJECT helpers.cpp) macro (kpm_test name) add_executable(${name} ${ARGN} $) target_link_libraries(${name} kpmcore) endmacro() ### # # Tests of initialization: try explicitly loading some backends kpm_test(testinit testinit.cpp) # Default backend if(TARGET pmdummybackendplugin) add_test(NAME testinit-dummy COMMAND testinit $) endif() if(TARGET pmlibpartedbackendplugin) add_test(NAME testinit-parted COMMAND testinit $) else() return() # All the rest really needs a working backend endif() set(BACKEND $) ### # # Listing devices, partitions kpm_test(testlist testlist.cpp) add_test(NAME testlist COMMAND testlist ${BACKEND}) kpm_test(testdevicescanner testdevicescanner.cpp) add_test(NAME testdevicescanner COMMAND testdevicescanner ${BACKEND}) find_package (Threads) ### # # Execute external commands as root kpm_test(testexternalcommand testexternalcommand.cpp) add_test(NAME testexternalcommand COMMAND testexternalcommand ${BACKEND}) + +# Test SMART support +kpm_test(testsmart testsmart.cpp) +add_test(NAME testsmart COMMAND testsmart ${BACKEND}) diff --git a/test/testsmart.cpp b/test/testsmart.cpp new file mode 100644 index 0000000..ea5aa72 --- /dev/null +++ b/test/testsmart.cpp @@ -0,0 +1,102 @@ +#include "helpers.h" + +#include "util/externalcommand.h" +#include "backend/corebackend.h" +#include "backend/corebackendmanager.h" +#include "core/smartstatus.h" +#include "core/smartparser.h" + +#include +#include + +static QString getDefaultDevicePath(); +static bool testSmartStatus(); +static bool testSmartParser(); + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + KPMCoreInitializer i; + + if (argc == 2) + i = KPMCoreInitializer(argv[1]); + + if (!i.isValid()) + return 1; + + CoreBackend *backend = CoreBackendManager::self()->backend(); + + if (!backend) + { + qWarning() << "Couldn't get backend."; + return 1; + } + + if (!testSmartStatus() || !testSmartParser()) + return 1; + + return app.exec(); +} + +static QString getDefaultDevicePath() +{ + // Getting default home partition using 'df -P /home | awk 'END{print $1}'' command + ExternalCommand command(QStringLiteral("df"), { QStringLiteral("-P"), QStringLiteral("/home"), QStringLiteral("|"), + QStringLiteral("awk"), QStringLiteral("\'END{print $1}\'") }); + + if (command.run() && command.exitCode() == 0) { + QString output = command.output(); + return output; + } + + return QString(); +} + +static bool testSmartStatus() +{ + QString devicePath = getDefaultDevicePath(); + + SmartStatus smart(devicePath); + + if (smart.devicePath() != devicePath) + return false; + + if (!smart.status()) + return false; + + if (smart.modelName() == QString()) + return false; + + if (smart.firmware() == QString()) + return false; + + if (smart.serial() == QString()) + return false; + + if (smart.selfTestStatus()) + return false; + + if (!smart.isValid()) + return false; + + return true; +} + +static bool testSmartParser() +{ + QString devicePath = getDefaultDevicePath(); + + SmartParser parser(devicePath); + + if (!parser.init()) + return false; + + if (parser.devicePath() != devicePath) + return false; + + if (!parser.diskInformation()) + return false; + + return true; +}