blob: 89cdfebb8f1d87de82093601ac333c9262be21ac [file] [log] [blame]
Bob Badoura99ac622021-10-25 16:21:00 -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 "sort"
19 "testing"
20)
21
22var (
23 // bottomUp describes the bottom-up resolve of a hypothetical graph
24 // the graph has a container image, a couple binaries, and a couple
25 // libraries. bin1 statically links lib1 and dynamically links lib2;
26 // bin2 dynamically links lib1 and statically links lib2.
27 // binc represents a compiler or other toolchain binary used for
28 // building the other binaries.
29 bottomUp = []res{
30 {"image", "image", "image", "notice"},
31 {"image", "image", "bin2", "restricted"},
32 {"image", "bin1", "bin1", "reciprocal"},
33 {"image", "bin2", "bin2", "restricted"},
34 {"image", "lib1", "lib1", "notice"},
35 {"image", "lib2", "lib2", "notice"},
36 {"binc", "binc", "binc", "proprietary"},
37 {"bin1", "bin1", "bin1", "reciprocal"},
38 {"bin1", "lib1", "lib1", "notice"},
39 {"bin2", "bin2", "bin2", "restricted"},
40 {"bin2", "lib2", "lib2", "notice"},
41 {"lib1", "lib1", "lib1", "notice"},
42 {"lib2", "lib2", "lib2", "notice"},
43 }
44
45 // notice describes bottomUp after a top-down notice resolve.
46 notice = []res{
47 {"image", "image", "image", "notice"},
48 {"image", "image", "bin2", "restricted"},
49 {"image", "bin1", "bin1", "reciprocal"},
50 {"image", "bin2", "bin2", "restricted"},
51 {"image", "lib1", "lib1", "notice"},
52 {"image", "lib2", "bin2", "restricted"},
53 {"image", "lib2", "lib2", "notice"},
54 {"bin1", "bin1", "bin1", "reciprocal"},
55 {"bin1", "lib1", "lib1", "notice"},
56 {"bin2", "bin2", "bin2", "restricted"},
57 {"bin2", "lib2", "bin2", "restricted"},
58 {"bin2", "lib2", "lib2", "notice"},
59 {"lib1", "lib1", "lib1", "notice"},
60 {"lib2", "lib2", "lib2", "notice"},
61 }
62
63 // share describes bottomUp after a top-down share resolve.
64 share = []res{
65 {"image", "image", "bin2", "restricted"},
66 {"image", "bin1", "bin1", "reciprocal"},
67 {"image", "bin2", "bin2", "restricted"},
68 {"image", "lib2", "bin2", "restricted"},
69 {"bin1", "bin1", "bin1", "reciprocal"},
70 {"bin2", "bin2", "bin2", "restricted"},
71 {"bin2", "lib2", "bin2", "restricted"},
72 }
73
74 // proprietary describes bottomUp after a top-down proprietary resolve.
75 // Note that the proprietary binc is not reachable through the toolchain
76 // dependency.
77 proprietary = []res{}
78)
79
Bob Badour103eb0f2022-01-10 13:50:57 -080080func TestResolutionSet_AttachesTo(t *testing.T) {
Bob Badoura99ac622021-10-25 16:21:00 -070081 lg := newLicenseGraph()
82
83 rsShare := toResolutionSet(lg, share)
84
Bob Badour103eb0f2022-01-10 13:50:57 -080085 t.Logf("checking resolution set %s", rsShare.String())
Bob Badoura99ac622021-10-25 16:21:00 -070086
Bob Badour103eb0f2022-01-10 13:50:57 -080087 actual := rsShare.AttachesTo().Names()
88 sort.Strings(actual)
Bob Badoura99ac622021-10-25 16:21:00 -070089
Bob Badour103eb0f2022-01-10 13:50:57 -080090 expected := []string{"bin1", "bin2", "image"}
Bob Badoura99ac622021-10-25 16:21:00 -070091
Bob Badour103eb0f2022-01-10 13:50:57 -080092 t.Logf("actual rsShare: %v", actual)
93 t.Logf("expected rsShare: %v", expected)
Bob Badoura99ac622021-10-25 16:21:00 -070094
Bob Badour103eb0f2022-01-10 13:50:57 -080095 if len(actual) != len(expected) {
96 t.Errorf("rsShare: wrong number of targets: got %d, want %d", len(actual), len(expected))
97 return
98 }
99 for i := 0; i < len(actual); i++ {
100 if actual[i] != expected[i] {
101 t.Errorf("rsShare: unexpected target at index %d: got %s, want %s", i, actual[i], expected[i])
102 }
103 }
Bob Badoura99ac622021-10-25 16:21:00 -0700104
Bob Badour103eb0f2022-01-10 13:50:57 -0800105 rsPrivate := toResolutionSet(lg, proprietary)
106 actual = rsPrivate.AttachesTo().Names()
107 expected = []string{}
Bob Badoura99ac622021-10-25 16:21:00 -0700108
Bob Badour103eb0f2022-01-10 13:50:57 -0800109 t.Logf("actual rsPrivate: %v", actual)
110 t.Logf("expected rsPrivate: %v", expected)
Bob Badoura99ac622021-10-25 16:21:00 -0700111
Bob Badour103eb0f2022-01-10 13:50:57 -0800112 if len(actual) != len(expected) {
113 t.Errorf("rsPrivate: wrong number of targets: got %d, want %d", len(actual), len(expected))
114 return
Bob Badoura99ac622021-10-25 16:21:00 -0700115 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800116 for i := 0; i < len(actual); i++ {
117 if actual[i] != expected[i] {
118 t.Errorf("rsPrivate: unexpected target at index %d: got %s, want %s", i, actual[i], expected[i])
119 }
Bob Badoura99ac622021-10-25 16:21:00 -0700120 }
121}
122
123func TestResolutionSet_AttachesToTarget(t *testing.T) {
124 lg := newLicenseGraph()
125
126 rsShare := toResolutionSet(lg, share)
127
Bob Badour103eb0f2022-01-10 13:50:57 -0800128 t.Logf("checking resolution set %s", rsShare.String())
129
Bob Badoura99ac622021-10-25 16:21:00 -0700130 if rsShare.AttachesToTarget(newTestNode(lg, "binc")) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800131 t.Errorf("actual.AttachesToTarget(\"binc\"): got true, want false")
Bob Badoura99ac622021-10-25 16:21:00 -0700132 }
133 if !rsShare.AttachesToTarget(newTestNode(lg, "image")) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800134 t.Errorf("actual.AttachesToTarget(\"image\"): got false want true")
Bob Badoura99ac622021-10-25 16:21:00 -0700135 }
136}