Skip to content

Commit

Permalink
add support for licenseRefs processing in Satisfies
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Oct 4, 2022
1 parent 96f5a7c commit e9a7844
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
13 changes: 13 additions & 0 deletions spdxexp/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ func (nodes *nodePair) licensesAreCompatible() bool {
return nodes.licensesExactlyEqual()
}

func (nodes *nodePair) licenseRefsAreCompatible() bool {
if !nodes.firstNode.isLicenseRef() || !nodes.secondNode.isLicenseRef() {
return false
}

compatible := *nodes.firstNode.licenseRef() == *nodes.secondNode.licenseRef()
compatible = compatible && (nodes.firstNode.hasDocumentRef() == nodes.secondNode.hasDocumentRef())
if compatible && nodes.firstNode.hasDocumentRef() {
compatible = compatible && (*nodes.firstNode.documentRef() == *nodes.secondNode.documentRef())
}
return compatible
}

// Return true if two licenses are compatible in the context of their ranges; otherwise, false.
func (nodes *nodePair) rangesAreCompatible() bool {
if nodes.licensesExactlyEqual() {
Expand Down
4 changes: 2 additions & 2 deletions spdxexp/satisfies.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func isCompatible(expressionPart, allowed []*node) bool {
compatible := false
for _, allowedLicense := range allowed {
nodes := &nodePair{firstNode: expLicense, secondNode: allowedLicense}
if nodes.licensesAreCompatible() {
if nodes.licensesAreCompatible() || nodes.licenseRefsAreCompatible() {
compatible = true
break
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func (n *node) expandAnd() [][]*node {
// expandAndTerm expands the terms of an AND expression.
func expandAndTerm(term *node) [][]*node {
var result [][]*node
if term.isLicense() {
if term.isLicense() || term.isLicenseRef() {
result = append(result, []*node{term})
} else if term.isExpression() {
if term.isAndExpression() {
Expand Down
26 changes: 26 additions & 0 deletions spdxexp/satisfies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ func TestSatisfies(t *testing.T) {
"MIT AND (GPL-2.0 OR ISC)", []string{"MIT"}, false, nil},
{"! (MIT OR Apache-2.0) AND (ISC OR GPL-2.0) satisfies [MIT]",
"(MIT OR Apache-2.0) AND (ISC OR GPL-2.0)", []string{"MIT"}, false, nil},
{"licenseRef is expression",
"LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
{"licenseRef in expression",
"MIT AND LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
{"licenseRef not in expression",
"MIT AND Apache-2.0", []string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
{"licenseRef not allowed",
"MIT AND LicenseRef-X-BSD-3-Clause-Golang", []string{"MIT", "Apache-2.0"}, false, nil},
{"licenseRef with documentRef is expression",
"DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
{"licenseRef with documentRef in expression",
"MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
{"licenseRef with documentRef not in expression",
"MIT AND Apache-2.0",
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, true, nil},
{"licenseRef with documentRef not allowed",
"MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
[]string{"MIT", "Apache-2.0"}, false, nil},
{"licenseRef allowed, but documentRef not allowed",
"MIT AND DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang",
[]string{"MIT", "Apache-2.0", "LicenseRef-X-BSD-3-Clause-Golang"}, false, nil},
{"licenseRef alone not allowed, but with documentRef allowed",
"MIT AND LicenseRef-X-BSD-3-Clause-Golang",
[]string{"MIT", "Apache-2.0", "DocumentRef-spdx-tool-1.2:LicenseRef-X-BSD-3-Clause-Golang"}, false, nil},
}

for _, test := range tests {
Expand Down

0 comments on commit e9a7844

Please sign in to comment.