blob: efdff82bc56c61f543d3dec5bde148dc1af35126 [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{
Bob Badoura6ee6d52022-12-16 13:50:41 -080030 {"image", "image", "notice|restricted"},
31 {"image", "bin1", "reciprocal"},
32 {"image", "bin2", "restricted"},
33 {"image", "lib1", "notice"},
34 {"image", "lib2", "notice"},
35 {"binc", "binc", "proprietary"},
36 {"bin1", "bin1", "reciprocal"},
37 {"bin1", "lib1", "notice"},
38 {"bin2", "bin2", "restricted"},
39 {"bin2", "lib2", "notice"},
40 {"lib1", "lib1", "notice"},
41 {"lib2", "lib2", "notice"},
Bob Badoura99ac622021-10-25 16:21:00 -070042 }
43
44 // notice describes bottomUp after a top-down notice resolve.
45 notice = []res{
Bob Badoura6ee6d52022-12-16 13:50:41 -080046 {"image", "image", "notice|restricted"},
47 {"image", "bin1", "reciprocal"},
48 {"image", "bin2", "restricted"},
49 {"image", "lib1", "notice"},
50 {"image", "lib2", "notice|restricted"},
51 {"bin1", "bin1", "reciprocal"},
52 {"bin1", "lib1", "notice"},
53 {"bin2", "bin2", "restricted"},
54 {"bin2", "lib2", "notice|restricted"},
55 {"lib1", "lib1", "notice"},
56 {"lib2", "lib2", "notice"},
Bob Badoura99ac622021-10-25 16:21:00 -070057 }
58
59 // share describes bottomUp after a top-down share resolve.
60 share = []res{
Bob Badoura6ee6d52022-12-16 13:50:41 -080061 {"image", "image", "restricted"},
62 {"image", "bin1", "reciprocal"},
63 {"image", "bin2", "restricted"},
64 {"image", "lib2", "restricted"},
65 {"bin1", "bin1", "reciprocal"},
66 {"bin2", "bin2", "restricted"},
67 {"bin2", "lib2", "restricted"},
Bob Badoura99ac622021-10-25 16:21:00 -070068 }
69
70 // proprietary describes bottomUp after a top-down proprietary resolve.
71 // Note that the proprietary binc is not reachable through the toolchain
72 // dependency.
73 proprietary = []res{}
74)
75
Bob Badour103eb0f2022-01-10 13:50:57 -080076func TestResolutionSet_AttachesTo(t *testing.T) {
Bob Badoura99ac622021-10-25 16:21:00 -070077 lg := newLicenseGraph()
78
79 rsShare := toResolutionSet(lg, share)
80
Bob Badour103eb0f2022-01-10 13:50:57 -080081 t.Logf("checking resolution set %s", rsShare.String())
Bob Badoura99ac622021-10-25 16:21:00 -070082
Bob Badour103eb0f2022-01-10 13:50:57 -080083 actual := rsShare.AttachesTo().Names()
84 sort.Strings(actual)
Bob Badoura99ac622021-10-25 16:21:00 -070085
Bob Badour103eb0f2022-01-10 13:50:57 -080086 expected := []string{"bin1", "bin2", "image"}
Bob Badoura99ac622021-10-25 16:21:00 -070087
Bob Badour103eb0f2022-01-10 13:50:57 -080088 t.Logf("actual rsShare: %v", actual)
89 t.Logf("expected rsShare: %v", expected)
Bob Badoura99ac622021-10-25 16:21:00 -070090
Bob Badour103eb0f2022-01-10 13:50:57 -080091 if len(actual) != len(expected) {
92 t.Errorf("rsShare: wrong number of targets: got %d, want %d", len(actual), len(expected))
93 return
94 }
95 for i := 0; i < len(actual); i++ {
96 if actual[i] != expected[i] {
97 t.Errorf("rsShare: unexpected target at index %d: got %s, want %s", i, actual[i], expected[i])
98 }
99 }
Bob Badoura99ac622021-10-25 16:21:00 -0700100
Bob Badour103eb0f2022-01-10 13:50:57 -0800101 rsPrivate := toResolutionSet(lg, proprietary)
102 actual = rsPrivate.AttachesTo().Names()
103 expected = []string{}
Bob Badoura99ac622021-10-25 16:21:00 -0700104
Bob Badour103eb0f2022-01-10 13:50:57 -0800105 t.Logf("actual rsPrivate: %v", actual)
106 t.Logf("expected rsPrivate: %v", expected)
Bob Badoura99ac622021-10-25 16:21:00 -0700107
Bob Badour103eb0f2022-01-10 13:50:57 -0800108 if len(actual) != len(expected) {
109 t.Errorf("rsPrivate: wrong number of targets: got %d, want %d", len(actual), len(expected))
110 return
Bob Badoura99ac622021-10-25 16:21:00 -0700111 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800112 for i := 0; i < len(actual); i++ {
113 if actual[i] != expected[i] {
114 t.Errorf("rsPrivate: unexpected target at index %d: got %s, want %s", i, actual[i], expected[i])
115 }
Bob Badoura99ac622021-10-25 16:21:00 -0700116 }
117}
118
119func TestResolutionSet_AttachesToTarget(t *testing.T) {
120 lg := newLicenseGraph()
121
122 rsShare := toResolutionSet(lg, share)
123
Bob Badour103eb0f2022-01-10 13:50:57 -0800124 t.Logf("checking resolution set %s", rsShare.String())
125
Bob Badoura99ac622021-10-25 16:21:00 -0700126 if rsShare.AttachesToTarget(newTestNode(lg, "binc")) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800127 t.Errorf("actual.AttachesToTarget(\"binc\"): got true, want false")
Bob Badoura99ac622021-10-25 16:21:00 -0700128 }
129 if !rsShare.AttachesToTarget(newTestNode(lg, "image")) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800130 t.Errorf("actual.AttachesToTarget(\"image\"): got false want true")
Bob Badoura99ac622021-10-25 16:21:00 -0700131 }
132}