Skip to content

Commit 98f8b4e

Browse files
[packchk] Add a validation step issuing a warning in case no license information is contained in a pack #1304 (#1149) (#1974)
* [packchk] Add a validation step issuing a warning in case no license information is contained in a pack #1304 Co-authored-by: Thorsten de Buhr <[email protected]>
1 parent 55ba25e commit 98f8b4e

File tree

8 files changed

+138
-21
lines changed

8 files changed

+138
-21
lines changed

tools/packchk/include/ValidateSyntax.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class ValidateSyntax : public Validate
6363
bool CheckRequirements(RtePackage* pKg);
6464
bool CheckRequirements_Packages(RteItem* requirement);
6565
bool CheckLicense(RtePackage* pKg, CheckFilesVisitor& fileVisitor);
66+
bool CheckLicenseFile(RteItem* item, const std::string& path);
6667
bool CheckFeatureDevice(RteDeviceProperty* prop, const std::string& devName);
6768
bool CheckFeatureBoard(RteItem* prop, const std::string& boardName);
6869
bool CheckDeviceProperties(RtePackage* pKg);

tools/packchk/src/PackChk_Msgs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ const MsgTable PackChk::msgTable = {
233233

234234
// 600... further PackCHk Errors
235235
{ "M600", { MsgLevel::LEVEL_WARNING, CRLF_B, "Attribute '%ATTR%' on '%TAG%' is deprecated." } },
236-
{ "M601", { MsgLevel::LEVEL_WARNING, CRLF_B, "" } },
237-
{ "M602", { MsgLevel::LEVEL_WARNING, CRLF_B, "" } },
238-
{ "M603", { MsgLevel::LEVEL_WARNING, CRLF_B, "" } },
236+
{ "M601", { MsgLevel::LEVEL_WARNING, CRLF_B, "'%TAG%' missing on '%TAG2%'" } },
237+
{ "M602", { MsgLevel::LEVEL_WARNING, CRLF_B, "Attribute '%ATTR%' already set, see Line %LINE%" } },
238+
{ "M603", { MsgLevel::LEVEL_WARNING, CRLF_B, "No license information found" } },
239239
{ "M604", { MsgLevel::LEVEL_WARNING, CRLF_B, "" } },
240240
{ "M605", { MsgLevel::LEVEL_WARNING, CRLF_B, "" } },
241241
{ "M606", { MsgLevel::LEVEL_WARNING, CRLF_B, "" } },

tools/packchk/src/ValidateSyntax.cpp

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,27 @@ bool ValidateSyntax::CheckAllFiles(RtePackage* pKg)
152152
return(true);
153153
}
154154

155+
bool ValidateSyntax::CheckLicenseFile(RteItem* item, const string& path) {
156+
bool bOk = false;
157+
const auto lineNo = item->GetLineNumber();
158+
159+
if(!path.empty()) {
160+
if(XmlValueAdjuster::IsAbsolute(path)) {
161+
LogMsg("M326", PATH(path), lineNo); // error : absolute paths are not permitted
162+
}
163+
else if(path.find('\\') != string::npos) {
164+
if(XmlValueAdjuster::IsURL(path)) {
165+
LogMsg("M370", URL(path), lineNo); // error : backslash are non permitted in URL
166+
}
167+
else {
168+
LogMsg("M327", PATH(path), lineNo); // error : backslash are not recommended
169+
}
170+
}
171+
}
172+
173+
return bOk;
174+
}
175+
155176
/**
156177
* @brief check (for) license file
157178
* @param pKg package under test
@@ -160,23 +181,82 @@ bool ValidateSyntax::CheckAllFiles(RtePackage* pKg)
160181
*/
161182
bool ValidateSyntax::CheckLicense(RtePackage* pKg, CheckFilesVisitor& fileVisitor)
162183
{
184+
bool bLicFound = false;
163185
const string& licPath = pKg->GetItemValue("license");
164-
if(licPath.empty()) {
165-
return true;
186+
if(!licPath.empty()) {
187+
bLicFound = true;
188+
CheckLicenseFile(pKg, licPath);
166189
}
167190

168-
if(XmlValueAdjuster::IsAbsolute(licPath)) {
169-
LogMsg("M326", PATH(licPath)); // error : absolute paths are not permitted
170-
}
171-
else if(licPath.find('\\') != string::npos) {
172-
if(XmlValueAdjuster::IsURL(licPath)) {
173-
LogMsg("M370", URL(licPath)); // error : backslash are non permitted in URL
191+
const auto licenseSets = pKg->GetLicenseSets();
192+
if(licenseSets) {
193+
const auto& children = licenseSets->GetChildren();
194+
if(children.empty()) {
195+
LogMsg("M601", TAG("licenseSet"), TAG2("licenseSets")); // license set must be defined if <licenseSets>
174196
}
175-
else {
176-
LogMsg("M327", PATH(licPath)); // error : backslash are not recommended
197+
198+
map<string, RteItem*> licenseSetIds;
199+
RteItem* defaultLicenseSet = nullptr;
200+
201+
for(const auto& child : children) {
202+
bLicFound = true;
203+
const auto& id = child->GetID();
204+
if(id.empty()) {
205+
LogMsg("M308", TAG("id"), TAG2("licenseSet")); // Attribute '%TAG%' missing on '%TAG2%'
206+
continue;
207+
}
208+
209+
const auto& found = licenseSetIds.find(id);
210+
if(found != licenseSetIds.end()) {
211+
const auto& foundId = found->first;
212+
const auto foundChild = found->second;
213+
LogMsg("M367", TYP("id"), NAME(foundId), LINE(foundChild->GetLineNumber()), child->GetLineNumber()); // Redefined %TYPE% '%NAME%' found, see Line %LINE%
214+
}
215+
else {
216+
licenseSetIds[id] = child;
217+
}
218+
219+
if(child->GetAttribute("default") == "1") {
220+
if(defaultLicenseSet) {
221+
LogMsg("M602", VAL("ATTR", "default")); // Attribute '%ATTR%' already set, see Line %LINE%
222+
}
223+
else {
224+
defaultLicenseSet = child;
225+
}
226+
}
227+
228+
const auto& lincenseChilds = child->GetChildren();
229+
if(lincenseChilds.empty()) {
230+
LogMsg("M601", TAG("license"), TAG2("licenseSet")); // '%TAG%' missing on '%TAG2%'
231+
}
232+
else {
233+
for(const auto lincenseChild : lincenseChilds) {
234+
const auto& licensePath = lincenseChild->GetAttribute("name");
235+
if(licensePath.empty()) {
236+
LogMsg("M601", TAG("name"), TAG2("license")); // '%TAG%' missing on '%TAG2%'
237+
}
238+
else {
239+
CheckLicenseFile(lincenseChild, licensePath);
240+
}
241+
242+
const auto& title = lincenseChild->GetAttribute("title");
243+
if(!title.length()) {
244+
LogMsg("M601", TAG("title"), TAG2("license")); // '%TAG%' missing on '%TAG2%'
245+
}
246+
247+
const auto& url = lincenseChild->GetAttribute("url");
248+
if(!url.empty()) {
249+
CheckLicenseFile(lincenseChild, url);
250+
}
251+
}
252+
}
177253
}
178254
}
179255

256+
if(!bLicFound) {
257+
LogMsg("M603"); // no license information found
258+
}
259+
180260
CheckFiles& checkFiles = fileVisitor.GetCheckFiles();
181261
if(!checkFiles.CheckFileExists(licPath, -1)) {
182262
return false;

tools/packchk/test/data/TestLicense/TestVendor.TestPackLicense.pdsc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313
</release>
1414
</releases>
1515

16+
<licenseSets>
17+
<licenseSet id="all" default="true" gating="true">
18+
<license name="./licenses/license1.txt" title="BSD-3 Clause License for components" spdx="BSD-3-Clause"/>
19+
<license name="./licenses/license2.txt" title="MIT License for device support" spdx="MIT"/>
20+
</licenseSet>
21+
<licenseSet id="Implementation">
22+
<license name="./licenses/myProprietaryLicense.txt" title="ProprietaryLicense" url="https://myvendor.com/licenses/myProprietaryLicense.html"/>
23+
</licenseSet>
24+
<licenseSet id="Implementation" default="true">
25+
<license name="./licenses/myProprietaryLicense.txt" title="ProprietaryLicense" url="https://myvendor.com/licenses/myProprietaryLicense.html"/>
26+
</licenseSet>
27+
<licenseSet>
28+
<license title="ProprietaryLicense" url="https://myvendor.com/licenses/myProprietaryLicense.html"/>
29+
</licenseSet>
30+
</licenseSets>
31+
1632
<keywords>
1733
<keyword>TestPackLicense</keyword>
1834
</keywords>
@@ -25,7 +41,7 @@
2541
</conditions>
2642

2743
<components>
28-
<component Cclass="TestClass" Cgroup="TestGlobal" Cversion="1.0.0" condition="Test_Condition">
44+
<component Cclass="TestClass" Cgroup="TestGlobal" Cversion="1.0.0" condition="Test_Condition" licenseSet="Implementation">
2945
<description>TestGlobal</description>
3046
</component>
3147
</components>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BSD-3 Clause License for components
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MIT License for device support
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
My Proprietary License
2+

tools/packchk/test/integtests/src/PackChkIntegTests.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,24 +419,39 @@ TEST_F(PackChkIntegTests, CheckPackLicense) {
419419
argv[2] = (char*)"--disable-validation";
420420

421421
PackChk packChk;
422-
EXPECT_EQ(0, packChk.Check(3, argv, nullptr));
422+
EXPECT_EQ(1, packChk.Check(3, argv, nullptr));
423423

424424
auto errMsgs = ErrLog::Get()->GetLogMessages();
425-
bool bFound = false;
425+
int M300_foundCnt = 0;
426+
int M327_foundCnt = 0;
427+
int M367_foundCnt = 0;
428+
int M601_foundCnt = 0;
429+
int M602_foundCnt = 0;
426430
for (const string& msg : errMsgs) {
427431
size_t s;
432+
if ((s = msg.find("M300")) != string::npos) {
433+
M300_foundCnt++;
434+
}
428435
if ((s = msg.find("M327")) != string::npos) {
429-
bFound = true;
430-
break;
436+
M327_foundCnt++;
437+
}
438+
if ((s = msg.find("M367")) != string::npos) {
439+
M367_foundCnt++;
440+
}
441+
if ((s = msg.find("M601")) != string::npos) {
442+
M601_foundCnt++;
443+
}
444+
if ((s = msg.find("M602")) != string::npos) {
445+
M602_foundCnt++;
431446
}
432447
}
433448

434-
if (!bFound) {
435-
FAIL() << "error: missing warning M327";
449+
if (M300_foundCnt != 1 || M327_foundCnt != 1 || M367_foundCnt != 1 || M601_foundCnt != 1 || M602_foundCnt != 1) {
450+
FAIL() << "error: missing license check warnings";
436451
}
437452
}
438453

439-
// Validate license path
454+
// Validate CPU feature SON
440455
TEST_F(PackChkIntegTests, CheckFeatureSON) {
441456
const char* argv[3];
442457

0 commit comments

Comments
 (0)