• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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
80func TestResolutionSet_AttachesTo(t *testing.T) {
81	lg := newLicenseGraph()
82
83	rsShare := toResolutionSet(lg, share)
84
85	t.Logf("checking resolution set %s", rsShare.String())
86
87	actual := rsShare.AttachesTo().Names()
88	sort.Strings(actual)
89
90	expected := []string{"bin1", "bin2", "image"}
91
92	t.Logf("actual rsShare: %v", actual)
93	t.Logf("expected rsShare: %v", expected)
94
95	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	}
104
105	rsPrivate := toResolutionSet(lg, proprietary)
106	actual = rsPrivate.AttachesTo().Names()
107	expected = []string{}
108
109	t.Logf("actual rsPrivate: %v", actual)
110	t.Logf("expected rsPrivate: %v", expected)
111
112	if len(actual) != len(expected) {
113		t.Errorf("rsPrivate: wrong number of targets: got %d, want %d", len(actual), len(expected))
114		return
115	}
116	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		}
120	}
121}
122
123func TestResolutionSet_AttachesToTarget(t *testing.T) {
124	lg := newLicenseGraph()
125
126	rsShare := toResolutionSet(lg, share)
127
128	t.Logf("checking resolution set %s", rsShare.String())
129
130	if rsShare.AttachesToTarget(newTestNode(lg, "binc")) {
131		t.Errorf("actual.AttachesToTarget(\"binc\"): got true, want false")
132	}
133	if !rsShare.AttachesToTarget(newTestNode(lg, "image")) {
134		t.Errorf("actual.AttachesToTarget(\"image\"): got false want true")
135	}
136}
137