diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 67f63a2..6d8f0a5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,64 +1,68 @@ # 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 STATIC helpers.cpp) target_link_libraries(testhelpers) macro (kpm_test name) add_executable(${name} ${ARGN}) target_link_libraries(${name} testhelpers kpmcore Qt5::Core) 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 pmsfdiskbackendplugin) add_test(NAME testinit-sfdisk 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}) # Including SMART files reference set(SMARTPARSER ${CMAKE_SOURCE_DIR}/src/core/smartdiskinformation.cpp ${CMAKE_SOURCE_DIR}/src/core/smartattributeparseddata.cpp ${CMAKE_SOURCE_DIR}/src/core/smartparser.cpp) # Test SMART support kpm_test(testsmart testsmart.cpp ${SMARTPARSER}) add_test(NAME testsmart COMMAND testsmart ${BACKEND}) + +# Test Device +kpm_test(testdevice testdevice.cpp) +add_test(NAME testdevice COMMAND testdevice ${BACKEND}) diff --git a/test/testdevice.cpp b/test/testdevice.cpp new file mode 100644 index 0000000..1bfb29f --- /dev/null +++ b/test/testdevice.cpp @@ -0,0 +1,130 @@ + /************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * 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 .* + *************************************************************************/ + + // SPDX-License-Identifier: GPL-3.0+ + +#include "helpers.h" +#include "testdevice.h" + +#include "backend/corebackend.h" +#include "backend/corebackendmanager.h" + +#include +#include +#include + +int main(int argc, char **argv) +{ + QCoreApplication app(argc, argv); + + KPMCoreInitializer init; + + if (argc == 2) + init = KPMCoreInitializer(argv[1]); + + return init.isValid() ? true : false; + + CoreBackend *backend = CoreBackendManager::self()->backend(); + + if (!backend) { + qWarning() << "Failed to load backend plugin"; + return false; + } + + TestDevice device; + + if (!device.testDeviceName() || !device.testDeviceNode() || !device.testDeviceSize() || !device.testDeviceTotalSectors()) + return false; + + return app.exec(); +} + +TestDevice::TestDevice() +{ + operationStack = new OperationStack(); + deviceScanner = new DeviceScanner(nullptr, *operationStack); + deviceScanner->scan(); + + // Get list of available devices on the system + devices = operationStack->previewDevices(); +} + +TestDevice::~TestDevice() +{ + delete operationStack; + delete deviceScanner; + + // Delete the list of devices + qDeleteAll(devices.begin(), devices.end()); + devices.clear(); +} + +bool TestDevice::testDeviceName() +{ + if (devices.isEmpty()) { + return false; + } else { + for (const auto &device : devices) { + if (device->name() == QString()) + return false; + } + } + + return true; +} + +bool TestDevice::testDeviceNode() +{ + if (devices.isEmpty()) { + return false; + } else { + for (const auto &device : devices) { + if (device->deviceNode() == QString()) + return false; + } + } + + return true; +} + +bool TestDevice::testDeviceSize() +{ + if (devices.isEmpty()) { + return false; + } else { + for (const auto &device : devices) { + if (device->logicalSize() < 0) + return false; + } + } + + return true; +} + +bool TestDevice::testDeviceTotalSectors() +{ + if (devices.isEmpty()) { + return false; + } else { + for (const auto &device : devices) { + if (device->totalLogical() < 0) + return false; + } + } + + return true; +} diff --git a/test/testdevice.h b/test/testdevice.h new file mode 100644 index 0000000..07b9d65 --- /dev/null +++ b/test/testdevice.h @@ -0,0 +1,46 @@ + /************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * 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 .* +**************************************************************************/ + +// SPDX-License-Identifier: GPL-3.0+ + +#ifndef TESTDEVICE_H +#define TESTDEVICE_H + +#include "core/device.h" +#include "core/devicescanner.h" +#include "core/operationstack.h" + +#include + +class TestDevice +{ +public: + TestDevice(); + ~TestDevice(); + + bool testDeviceName(); + bool testDeviceNode(); + bool testDeviceSize(); + bool testDeviceTotalSectors(); + +private: + OperationStack *operationStack; + DeviceScanner *deviceScanner; + QList devices; +}; + +#endif // TESTDEVICE_H