blob: 162c1fe3c906c21f54f3a744ba86a6aaadce999f [file] [log] [blame]
Bob Badour9ee7d032021-10-25 16:51:48 -07001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package compliance
16
17import (
18 "bytes"
19 "sort"
20 "testing"
21)
22
23// byConflict orders conflicts by target then share then privacy
24type byConflict []SourceSharePrivacyConflict
25
26// Len returns the count of elements in the slice.
27func (l byConflict) Len() int { return len(l) }
28
29// Swap rearranged 2 elements so that each occupies the other's former
30// position.
31func (l byConflict) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
32
33// Less returns true when the `i`th element is lexicographically less than
34// the `j`th element.
35func (l byConflict) Less(i, j int) bool {
36 if l[i].SourceNode.name == l[j].SourceNode.name {
37 if l[i].ShareCondition.origin.name == l[j].ShareCondition.origin.name {
38 if l[i].ShareCondition.name == l[j].ShareCondition.name {
39 if l[i].PrivacyCondition.origin.name == l[j].PrivacyCondition.origin.name {
40 return l[i].PrivacyCondition.name < l[j].PrivacyCondition.name
41 }
42 return l[i].PrivacyCondition.origin.name < l[j].PrivacyCondition.origin.name
43 }
44 return l[i].ShareCondition.name < l[j].ShareCondition.name
45 }
46 return l[i].ShareCondition.origin.name < l[j].ShareCondition.origin.name
47 }
48 return l[i].SourceNode.name < l[j].SourceNode.name
49}
50
51func TestConflictingSharedPrivateSource(t *testing.T) {
52 tests := []struct {
53 name string
54 roots []string
55 edges []annotated
56 expectedConflicts []confl
57 }{
58 {
59 name: "firstparty",
60 roots: []string{"apacheBin.meta_lic"},
61 edges: []annotated{
62 {"apacheBin.meta_lic", "apacheLib.meta_lic", []string{"static"}},
63 },
64 expectedConflicts: []confl{},
65 },
66 {
67 name: "notice",
68 roots: []string{"mitBin.meta_lic"},
69 edges: []annotated{
70 {"mitBin.meta_lic", "mitLib.meta_lic", []string{"static"}},
71 },
72 expectedConflicts: []confl{},
73 },
74 {
75 name: "lgpl",
76 roots: []string{"lgplBin.meta_lic"},
77 edges: []annotated{
78 {"lgplBin.meta_lic", "apacheLib.meta_lic", []string{"static"}},
79 },
80 expectedConflicts: []confl{},
81 },
82 {
83 name: "proprietaryonrestricted",
84 roots: []string{"proprietary.meta_lic"},
85 edges: []annotated{
86 {"proprietary.meta_lic", "gplLib.meta_lic", []string{"static"}},
87 },
88 expectedConflicts: []confl{
89 {"proprietary.meta_lic", "gplLib.meta_lic:restricted", "proprietary.meta_lic:proprietary"},
90 },
91 },
92 {
93 name: "restrictedonproprietary",
94 roots: []string{"gplBin.meta_lic"},
95 edges: []annotated{
96 {"gplBin.meta_lic", "proprietary.meta_lic", []string{"static"}},
97 },
98 expectedConflicts: []confl{
99 {"proprietary.meta_lic", "gplBin.meta_lic:restricted", "proprietary.meta_lic:proprietary"},
100 },
101 },
102 }
103 for _, tt := range tests {
104 t.Run(tt.name, func(t *testing.T) {
105 stderr := &bytes.Buffer{}
106 lg, err := toGraph(stderr, tt.roots, tt.edges)
107 if err != nil {
108 t.Errorf("unexpected test data error: got %w, want no error", err)
109 return
110 }
111 expectedConflicts := toConflictList(lg, tt.expectedConflicts)
112 actualConflicts := ConflictingSharedPrivateSource(lg)
113 sort.Sort(byConflict(expectedConflicts))
114 sort.Sort(byConflict(actualConflicts))
115 if len(expectedConflicts) != len(actualConflicts) {
116 t.Errorf("unexpected number of share/privacy conflicts: got %v with %d conflicts, want %v with %d conflicts",
117 actualConflicts, len(actualConflicts), expectedConflicts, len(expectedConflicts))
118 } else {
119 for i := 0; i < len(actualConflicts); i++ {
120 if !actualConflicts[i].IsEqualTo(expectedConflicts[i]) {
121 t.Errorf("unexpected share/privacy conflict at element %d: got %q, want %q",
122 i, actualConflicts[i].Error(), expectedConflicts[i].Error())
123 }
124 }
125 }
126
127 })
128 }
129}