File indexing completed on 2024-04-21 05:44:40
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Andreas Cord-Landwehr <cordlandwehr@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "test_licenseconvert.h" 0008 #include "../licenseregistry.h" 0009 #include "../directoryparser.h" 0010 #include <QTest> 0011 #include <QVector> 0012 #include <QDebug> 0013 #include <QDir> 0014 0015 static const QString licensesRootPath(":/licenses_templates/%1"); 0016 0017 void TestLicenseConvert::greedyLicenseTextConversion() 0018 { 0019 // use non-correctly converted license as test data 0020 QFile file(licensesRootPath.arg("LGPL-2.0-or-later/kservice")); 0021 file.open(QIODevice::ReadOnly); 0022 const QString expression{ "LGPL-2.0-or-later" }; 0023 const QString fileContent = file.readAll(); 0024 0025 LicenseRegistry registry; 0026 DirectoryParser parser; 0027 bool result = false; 0028 for (auto regexp: registry.headerTextRegExps(expression)) { 0029 result = fileContent.contains(regexp); 0030 if (result) { 0031 break; 0032 } 0033 } 0034 QVERIFY(result); 0035 QCOMPARE(parser.replaceHeaderText(fileContent, expression), "SPDX-License-Identifier: LGPL-2.0-or-later\n"); 0036 } 0037 0038 void TestLicenseConvert::basicConvertOrExpressions() 0039 { 0040 // use non-correctly converted license as test data 0041 QFile file(licensesRootPath.arg("LGPL-2.1-only_OR_LGPL-3.0-only/kio_httpserver_p")); 0042 file.open(QIODevice::ReadOnly); 0043 const QString expression{ "LGPL-2.1-only_OR_LGPL-3.0-only" }; 0044 const QString fileContent = file.readAll(); 0045 0046 LicenseRegistry registry; 0047 DirectoryParser parser; 0048 0049 bool result = false; 0050 for (auto regexp: registry.headerTextRegExps(expression)) { 0051 result = fileContent.contains(regexp); 0052 if (result) { 0053 break; 0054 } 0055 } 0056 QVERIFY(result); 0057 QCOMPARE(parser.replaceHeaderText(fileContent, expression), "SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only\n"); 0058 } 0059 0060 void TestLicenseConvert::doNotModifyFileWithoutDetectedLicense() 0061 { 0062 QFile fileOrig(":/testdata_conversionexamples/file_without_unrecognized_statement.h.origfile"); 0063 fileOrig.open(QIODevice::ReadOnly); 0064 const QString fileContentOrig = fileOrig.readAll(); 0065 0066 LicenseRegistry registry; 0067 DirectoryParser parser; 0068 0069 QCOMPARE(parser.detectSpdxLicenseStatement(fileContentOrig), QString()); 0070 QCOMPARE(parser.replaceHeaderText(fileContentOrig, QString()), fileContentOrig); 0071 } 0072 0073 void TestLicenseConvert::pruneLicenseList() 0074 { 0075 LicenseRegistry registry; 0076 DirectoryParser parser; 0077 0078 // prune multiple detections of same license 0079 { 0080 QVector<LicenseRegistry::SpdxExpression> licenses{ "LGPL-2.0-only", "LGPL-2.0-only"}; 0081 auto prunedLicenses = parser.pruneLicenseList(licenses); 0082 QCOMPARE(prunedLicenses.length(), 1); 0083 QCOMPARE(prunedLicenses.first(), "LGPL-2.0-only"); 0084 } 0085 0086 // prune licenses that are detected both "LIC-A" as well as "LIC-A OR LIC-B" 0087 { 0088 QVector<LicenseRegistry::SpdxExpression> licenses{ "LGPL-2.1-only", "LGPL-2.1-only_OR_LGPL-3.0-only"}; 0089 auto prunedLicenses = parser.pruneLicenseList(licenses); 0090 QCOMPARE(prunedLicenses.length(), 1); 0091 QCOMPARE(prunedLicenses.first(), "LGPL-2.1-only_OR_LGPL-3.0-only"); 0092 } 0093 0094 // prune licenses that are detected both "LIC-A" as well as "LIC-A OR LIC-B" 0095 { 0096 QVector<LicenseRegistry::SpdxExpression> licenses{ "LGPL-2.1-only", "LGPL-2.1-only_WITH_Qt-Commercial-exception-1.0"}; 0097 auto prunedLicenses = parser.pruneLicenseList(licenses); 0098 QCOMPARE(prunedLicenses.length(), 1); 0099 QCOMPARE(prunedLicenses.first(), "LGPL-2.1-only_WITH_Qt-Commercial-exception-1.0"); 0100 } 0101 } 0102 0103 void TestLicenseConvert::exampleFileConversion() 0104 { 0105 QVector<std::pair<QString, QString>> testFiles; 0106 0107 testFiles.append(std::make_pair("LGPL-2.0-only", ":/testdata_conversionexamples/fake_notifications_server.h")); 0108 testFiles.append(std::make_pair("GPL-2.0-or-later", ":/testdata_conversionexamples/blur.h")); 0109 0110 for (auto iter = testFiles.constBegin(); iter != testFiles.constEnd(); ++iter) { 0111 const QString targetSpdxMarker = iter->first; 0112 const QString baseFileName = iter->second; 0113 0114 qDebug() << "Testing:" << baseFileName; 0115 0116 QFile fileOrig(baseFileName + ".origfile"); 0117 fileOrig.open(QIODevice::ReadOnly); 0118 const QString fileContentOrig = fileOrig.readAll(); 0119 0120 QFile fileSpdx(baseFileName + ".spdx"); 0121 fileSpdx.open(QIODevice::ReadOnly); 0122 const QString fileContentSpdx = fileSpdx.readAll(); 0123 0124 LicenseRegistry registry; 0125 DirectoryParser parser; 0126 0127 QVERIFY(registry.expressions().contains(targetSpdxMarker)); 0128 QVector<LicenseRegistry::SpdxExpression> licenses = parser.detectLicenses(fileContentOrig); 0129 // something is broken in DirectoryParser::detectLicenses(), triggered by 0130 // new kdevelop_ColorPicker & kdevelop_kdevformatfile being longer versions of kemoticontest, 0131 // which again is a longer version of kwin_blur as well as so far being expected here 0132 if (baseFileName == QLatin1String(":/testdata_conversionexamples/blur.h")) { 0133 QEXPECT_FAIL("", "DirectoryParser::detectLicenses() needs fixing for multiple longer versions of the same template", Continue); 0134 } 0135 QCOMPARE(licenses.count(), 1); 0136 QCOMPARE(licenses.first(), targetSpdxMarker); 0137 const QString result = parser.replaceHeaderText(fileContentOrig, targetSpdxMarker); 0138 QCOMPARE(result, fileContentSpdx); 0139 } 0140 } 0141 0142 QTEST_GUILESS_MAIN(TestLicenseConvert);