diff --git a/src/CopyrighFixer/CopyrighFixer/ifilemanager.cpp b/src/CopyrighFixer/CopyrighFixer/ifilemanager.cpp index 5a5c260..507bb92 100644 --- a/src/CopyrighFixer/CopyrighFixer/ifilemanager.cpp +++ b/src/CopyrighFixer/CopyrighFixer/ifilemanager.cpp @@ -16,35 +16,35 @@ bool IFileManager::isSupport(const Extension &curExt) const { Extension IFileManager::toExtension(const QString &curExt) const { - if (".cpp" == curExt) { + if ("cpp" == curExt) { return Extension::CPP; } - if (".hpp" == curExt) { + if ("hpp" == curExt) { return Extension::HPP; } - if (".h" == curExt) { + if ("h" == curExt) { return Extension::H; } - if (".cc" == curExt) { + if ("cc" == curExt) { return Extension::CC; } - if (".qml" == curExt) { + if ("qml" == curExt) { return Extension::QML; } - if (".pro" == curExt) { + if ("pro" == curExt) { return Extension::PRO; } - if (".txt" == curExt) { + if ("txt" == curExt) { return Extension::TXT; } - if (".py" == curExt) { + if ("py" == curExt) { return Extension::PY; } diff --git a/src/CopyrighFixer/CopyrighFixer/signer.cpp b/src/CopyrighFixer/CopyrighFixer/signer.cpp index 0628739..284a69f 100644 --- a/src/CopyrighFixer/CopyrighFixer/signer.cpp +++ b/src/CopyrighFixer/CopyrighFixer/signer.cpp @@ -5,27 +5,151 @@ //# of this license document, but changing it is not allowed. //# +#include <time.h> #include "signer.h" #include "CopyrighFixer/ifilemanager.h" +#include <QDir> +#include <quasarapp.h> namespace CopyrighFixer { Signer::Signer() { } -bool Signer::checkSign(const Config &objConf) { + +bool Signer::processSign(const QString &pathToFile, const Config &objConf) const { + + QDir currentFolder(pathToFile); + currentFolder.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); + + QFileInfoList folderItems(currentFolder.entryInfoList()); + + for (auto i_file: folderItems) { + + if (i_file.isDir()) { + processSign(i_file.filePath(), objConf); + + } else { + IFileManager *currFM = searchFileByExt(i_file.suffix()); + Signature signFromFile; + Signature resultSign; + + if (currFM == nullptr) { + QuasarAppUtils::Params::log( + QString("The CFixer tool are not support files with sufix: %1").arg(i_file.suffix()), + QuasarAppUtils::VerboseLvl::Error); + return false; + } + + if (!currFM->read(i_file.filePath(), signFromFile)) { + QuasarAppUtils::Params::log(QString("Failed to load file - %1").arg(i_file.fileName()), + QuasarAppUtils::VerboseLvl::Error); + return false; + } + + resultSign = mergeSign(objConf.getSignVal(), signFromFile); + + if (!currFM->write(i_file.filePath(), resultSign)) { + QuasarAppUtils::Params::log(QString("Failed to write signature in file - %1").arg(i_file.fileName()), + QuasarAppUtils::VerboseLvl::Error); + return false; + } + } + } + return true; } -IFileManager *Signer::searchFileByExt(const QString &extension) { +bool Signer::checkSign(const Config &objConf) { + + if (!processSign(objConf.getSrcDir(), objConf)) { + return false; + } + + return true; +} + +const Signature Signer::upgradeOwner(const Signature &signConf, const Signature &fileSign) const { + + int unixTime = time(0); + + Signature signForSing = fileSign; + QMap<int, CopyrighFixer::Owner> mapOwnersFile = signForSing.getMapOwn(); + + CopyrighFixer::Owner newOwner = mapOwnersFile.take(fileSign.getMapOwn().cbegin().key()); + newOwner.setName(signConf.getMapOwn().cbegin().value().getOwnerName()); + newOwner.setTimePoint(unixTime); + + mapOwnersFile.insert(unixTime, newOwner); + signForSing.setMapOwners(mapOwnersFile); + + return signForSing; + +} + +const Signature Signer::appendOwner(const Signature &signConf, const Signature &fileSign) const { + + int unixTime = time(0); + + Signature signForSing = fileSign; + QMap<int, CopyrighFixer::Owner> mapOwnersFile = signForSing.getMapOwn(); + + CopyrighFixer::Owner newOwners; + newOwners.setName(signConf.getMapOwn().cbegin().value().getOwnerName()); + newOwners.setTimePoint(unixTime); + + mapOwnersFile.insert(unixTime, newOwners); + signForSing.setMapOwners(mapOwnersFile); + + return signForSing; +} + +const Signature Signer::mergeSign(const Signature &userSign, const Signature &fileSign) const { + + bool fLastOwnerEqual = userSign.getMapOwn().cbegin().value().getOwnerName() != fileSign.getMapOwn().cbegin().value().getOwnerName(); + bool fLastOwnerDiff = userSign.getMapOwn().cbegin().value().getOwnerName() == fileSign.getMapOwn().cbegin().value().getOwnerName(); + bool fNumOwnsMore = userSign.getMapOwn().size() > 1; + bool fEqualLicTitle = userSign.getLicenseTitle() != fileSign.getLicenseTitle(); + + if (!fileSign.isValid()) { + return userSign; + } + + if (fEqualLicTitle) { + QuasarAppUtils::Params::log("The signature in the file is different from the config signature.", + QuasarAppUtils::VerboseLvl::Warning); + return fileSign; + } + + if (fNumOwnsMore) { + QuasarAppUtils::Params::log("Config signature contains more owners.", + QuasarAppUtils::VerboseLvl::Warning); + + if (fLastOwnerEqual) { + return appendOwner(userSign, fileSign); + } + } + + + if (fLastOwnerDiff) { + return upgradeOwner(userSign, fileSign); + } + + if (fLastOwnerEqual) { + return appendOwner(userSign, fileSign); + } + + return fileSign; + +} + +IFileManager *Signer::searchFileByExt(const QString &extension) const { for (auto itemFM: _fileManager) { if (itemFM && itemFM->isSupport(itemFM->toExtension(extension))) { return itemFM; } - - } return nullptr; } diff --git a/src/CopyrighFixer/CopyrighFixer/signer.h b/src/CopyrighFixer/CopyrighFixer/signer.h index 9b53858..8e08a9b 100644 --- a/src/CopyrighFixer/CopyrighFixer/signer.h +++ b/src/CopyrighFixer/CopyrighFixer/signer.h @@ -10,6 +10,7 @@ #include "CopyrighFixer_global.h" #include "config.h" +#include <QFileInfo> namespace CopyrighFixer { @@ -22,22 +23,47 @@ class CopyrighFixer_EXPORT Signer { public: Signer(); + bool processSign(const QString &pathToFile, const Config &objConf) const; + /** * @brief checkSign The method that add copyright to all sources files. * @param objConf This is a configuration object. */ bool checkSign(const Config &objConf); + /** + * @brief mergeSign The method compares two signatures and merges into one. + * @param userSign The signature that is pulled from the configuration. + * @param fileSign The signature that is read from the file. + * @return Returns the signature for signing a filereturn false;. + */ + const Signature mergeSign(const Signature &userSign, const Signature &fileSign) const; + /** * @brief searchFileByExt The method that searches for files with the desired extension. * @param extension This is the file extension to search. * @return Returns a pointer to the FileManager of the found extension, or nullptr if the file extension was not found. */ - IFileManager *searchFileByExt(const QString &extension); + IFileManager *searchFileByExt(const QString &extension) const; private: QList<IFileManager*> _fileManager; + /** + * @brief upgradeOwner + * @param signConf The signature that is pulled from the configuration. + * @param fileSign The signature that is read from the file. + * @return Returns a signature with the last owner updated. + */ + const Signature upgradeOwner(const Signature &signConf, const Signature &fileSign) const; + + /** + * @brief appendOwner + * @param signConf The signature that is pulled from the configuration. + * @param fileSign The signature that is read from the file. + * @return Returns a signature with the most recently updated owner list. + */ + const Signature appendOwner(const Signature &signConf, const Signature &fileSign) const; }; } diff --git a/tests/tstMain.cpp b/tests/tstMain.cpp index d8d4c07..953871b 100644 --- a/tests/tstMain.cpp +++ b/tests/tstMain.cpp @@ -8,6 +8,7 @@ #include <QtTest> #include "cfixertest.h" #include "signtest.h" +#include "signertest.h" #include "configparsertest.h" // Use This macros for initialize your own test classes. @@ -34,6 +35,7 @@ private slots: // BEGIN TESTS CASES TestCase(exampleTest, ExampleTest); TestCase(signTest, SignTest); + TestCase(signerTest, SignerTest); TestCase(configParserTest, ConfigParserTest) // END TEST CASES diff --git a/tests/units/signertest.cpp b/tests/units/signertest.cpp index 1438074..737d439 100644 --- a/tests/units/signertest.cpp +++ b/tests/units/signertest.cpp @@ -5,6 +5,7 @@ //# of this license document, but changing it is not allowed. //# +#include <time.h> #include "signertest.h" #include <QDebug> @@ -20,7 +21,150 @@ void SignerTest::test() { testSigner(); } -void SignerTest::testSigner() { +CopyrighFixer::Signature SignerTest::genSign(const QString licen, + const QString nOwn, + int tP) const { + + CopyrighFixer::Owner ownerObj; + ownerObj.setName(nOwn); + ownerObj.setTimePoint(tP); + + QMap<int, CopyrighFixer::Owner> OwnerMap; + OwnerMap.insert(tP, ownerObj); + + CopyrighFixer::Signature signTest; + signTest.setLicenseTitle(licen); + signTest.setMessage("Distributed under the lgplv3 software license, see the accompany."); + signTest.setMapOwners(OwnerMap); + + return signTest; +} + +CopyrighFixer::Signature SignerTest::genSign(QList<SignerTest::dataOwns> numOwns, const QString licen) const { + + QMap<int, CopyrighFixer::Owner> OwnerMap; + for (auto itemOwn: numOwns) { + CopyrighFixer::Owner ownerObj; + ownerObj.setName(itemOwn.name); + ownerObj.setTimePoint(itemOwn.timePoint); + + OwnerMap.insert(itemOwn.timePoint, ownerObj); + } + + CopyrighFixer::Signature signTest; + signTest.setLicenseTitle(licen); + signTest.setMessage("Distributed under the lgplv3 software license, see the accompany."); + signTest.setMapOwners(OwnerMap); + + return signTest; +} + + +QList<SignerTest::Signers> SignerTest::lstSing() const { + + int unixTime = time(0); + // 1 + // The case when the signatures in the config and the file match. + SignerTest::Signers equalSign; + equalSign.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + equalSign.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + equalSign.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", unixTime); + + // 2 + // The Different licenses. + SignerTest::Signers signDiffLic; + signDiffLic.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + signDiffLic.signFormFile = genSign("MIT License Copyright (C) 2019-2020 QuasarApp.", "QuasarApp", 1622657022); + signDiffLic.signAfterMerge = genSign("MIT License Copyright (C) 2019-2020 QuasarApp.", "QuasarApp", 1622657022); + + // 3 + // The Different owners. + SignerTest::Signers signDiffOwn; + signDiffOwn.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000); + signDiffOwn.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarAppSdfdf", 1622657022); + signDiffOwn.signAfterMerge = genSign({{"QuasarAppSdfdf", 1622657022}, + {"QuasarApp", unixTime}}, + "MIT License Copyright (C) 2020-2021 QuasarApp."); + + // 4 + // The equal owners. + SignerTest::Signers signEqualOwn; + signEqualOwn.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000); + signEqualOwn.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + signEqualOwn.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", unixTime); + + + // 5 + // The signature is missing from the file. + SignerTest::Signers signMissingInFile; + signMissingInFile.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + signMissingInFile.signFormFile = genSign(); + signMissingInFile.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + + // 6 + // Here are no owners in the file. + SignerTest::Signers signMissOwnInFile; + signMissOwnInFile.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + signMissOwnInFile.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp."); + signMissOwnInFile.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1622657022); + + // 7 + // Generating multiple owners config. + QList<SignerTest::dataOwns> lstOwnsForConf = { + {"QuasarApp", unixTime}, + {"QuasarApp1", unixTime+1}, + {"QuasarApp2", unixTime+2} + }; + + // The number of owners differs. + SignerTest::Signers signDiffNumOwnConf; + signDiffNumOwnConf.signFormConf = genSign(lstOwnsForConf, "MIT License Copyright (C) 2020-2021 QuasarApp."); + signDiffNumOwnConf.signFormFile = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000); + signDiffNumOwnConf.signAfterMerge = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", unixTime); + + // 8 + // Generating multiple owners file. + QList<SignerTest::dataOwns> lstOwnsForFile = { + {"QuasarApp", unixTime}, + {"QuasarApp1", unixTime+1}, + {"QuasarApp2", unixTime+2}, + {"QuasarApp3", unixTime+3} + }; + + // The number of owners differs. + SignerTest::Signers signDiffNumOwnFile; + signDiffNumOwnFile.signFormConf = genSign("MIT License Copyright (C) 2020-2021 QuasarApp.", "QuasarApp", 1700000000); + signDiffNumOwnFile.signFormFile = genSign(lstOwnsForFile, "MIT License Copyright (C) 2020-2021 QuasarApp."); + signDiffNumOwnFile.signAfterMerge = genSign(lstOwnsForFile, "MIT License Copyright (C) 2020-2021 QuasarApp."); + + return {equalSign, signDiffLic, signDiffOwn, signEqualOwn, signMissingInFile, signMissOwnInFile, signDiffNumOwnConf}; + +} + +void SignerTest::testMergeSign() const { + + QList<SignerTest::Signers> dfd = lstSing(); + + for (auto itemSign: lstSing()) { + + CopyrighFixer::Signer resSign; + CopyrighFixer::Signature mergeSignUF = resSign.mergeSign(itemSign.signFormConf, + itemSign.signFormFile); + + QVERIFY(mergeSignUF == itemSign.signAfterMerge); + + } + +} + +void SignerTest::testCheckSign() const { qWarning() << "The SignerTest class is not implemented"; QVERIFY(true); } + +void SignerTest::testSigner() { + + testMergeSign(); + testCheckSign(); + +} diff --git a/tests/units/signertest.h b/tests/units/signertest.h index ab8fbff..85e49b5 100644 --- a/tests/units/signertest.h +++ b/tests/units/signertest.h @@ -19,6 +19,26 @@ public: ~SignerTest(); void test(); + + struct dataOwns{ + QString name; + int timePoint; + }; + + struct Signers { + CopyrighFixer::Signature signFormConf; + CopyrighFixer::Signature signFormFile; + CopyrighFixer::Signature signAfterMerge; + }; + + CopyrighFixer::Signature genSign(const QString licen = "", + const QString nOwn = "", + int tP = 0) const; + CopyrighFixer::Signature genSign(QList<dataOwns> numOwns, const QString licen = "") const; + + QList<Signers> lstSing() const; + void testMergeSign() const; + void testCheckSign() const; void testSigner(); };