1*9e94795aSAndroid Build Coastguard Worker// Copyright 2021 Google LLC 2*9e94795aSAndroid Build Coastguard Worker// 3*9e94795aSAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License"); 4*9e94795aSAndroid Build Coastguard Worker// you may not use this file except in compliance with the License. 5*9e94795aSAndroid Build Coastguard Worker// You may obtain a copy of the License at 6*9e94795aSAndroid Build Coastguard Worker// 7*9e94795aSAndroid Build Coastguard Worker// http://www.apache.org/licenses/LICENSE-2.0 8*9e94795aSAndroid Build Coastguard Worker// 9*9e94795aSAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software 10*9e94795aSAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS, 11*9e94795aSAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9e94795aSAndroid Build Coastguard Worker// See the License for the specific language governing permissions and 13*9e94795aSAndroid Build Coastguard Worker// limitations under the License. 14*9e94795aSAndroid Build Coastguard Worker 15*9e94795aSAndroid Build Coastguard Workerpackage compliance 16*9e94795aSAndroid Build Coastguard Worker 17*9e94795aSAndroid Build Coastguard Workerimport ( 18*9e94795aSAndroid Build Coastguard Worker "sort" 19*9e94795aSAndroid Build Coastguard Worker "testing" 20*9e94795aSAndroid Build Coastguard Worker) 21*9e94795aSAndroid Build Coastguard Worker 22*9e94795aSAndroid Build Coastguard Workervar ( 23*9e94795aSAndroid Build Coastguard Worker // bottomUp describes the bottom-up resolve of a hypothetical graph 24*9e94795aSAndroid Build Coastguard Worker // the graph has a container image, a couple binaries, and a couple 25*9e94795aSAndroid Build Coastguard Worker // libraries. bin1 statically links lib1 and dynamically links lib2; 26*9e94795aSAndroid Build Coastguard Worker // bin2 dynamically links lib1 and statically links lib2. 27*9e94795aSAndroid Build Coastguard Worker // binc represents a compiler or other toolchain binary used for 28*9e94795aSAndroid Build Coastguard Worker // building the other binaries. 29*9e94795aSAndroid Build Coastguard Worker bottomUp = []res{ 30*9e94795aSAndroid Build Coastguard Worker {"image", "image", "notice|restricted"}, 31*9e94795aSAndroid Build Coastguard Worker {"image", "bin1", "reciprocal"}, 32*9e94795aSAndroid Build Coastguard Worker {"image", "bin2", "restricted"}, 33*9e94795aSAndroid Build Coastguard Worker {"image", "lib1", "notice"}, 34*9e94795aSAndroid Build Coastguard Worker {"image", "lib2", "notice"}, 35*9e94795aSAndroid Build Coastguard Worker {"binc", "binc", "proprietary"}, 36*9e94795aSAndroid Build Coastguard Worker {"bin1", "bin1", "reciprocal"}, 37*9e94795aSAndroid Build Coastguard Worker {"bin1", "lib1", "notice"}, 38*9e94795aSAndroid Build Coastguard Worker {"bin2", "bin2", "restricted"}, 39*9e94795aSAndroid Build Coastguard Worker {"bin2", "lib2", "notice"}, 40*9e94795aSAndroid Build Coastguard Worker {"lib1", "lib1", "notice"}, 41*9e94795aSAndroid Build Coastguard Worker {"lib2", "lib2", "notice"}, 42*9e94795aSAndroid Build Coastguard Worker } 43*9e94795aSAndroid Build Coastguard Worker 44*9e94795aSAndroid Build Coastguard Worker // notice describes bottomUp after a top-down notice resolve. 45*9e94795aSAndroid Build Coastguard Worker notice = []res{ 46*9e94795aSAndroid Build Coastguard Worker {"image", "image", "notice|restricted"}, 47*9e94795aSAndroid Build Coastguard Worker {"image", "bin1", "reciprocal"}, 48*9e94795aSAndroid Build Coastguard Worker {"image", "bin2", "restricted"}, 49*9e94795aSAndroid Build Coastguard Worker {"image", "lib1", "notice"}, 50*9e94795aSAndroid Build Coastguard Worker {"image", "lib2", "notice|restricted"}, 51*9e94795aSAndroid Build Coastguard Worker {"bin1", "bin1", "reciprocal"}, 52*9e94795aSAndroid Build Coastguard Worker {"bin1", "lib1", "notice"}, 53*9e94795aSAndroid Build Coastguard Worker {"bin2", "bin2", "restricted"}, 54*9e94795aSAndroid Build Coastguard Worker {"bin2", "lib2", "notice|restricted"}, 55*9e94795aSAndroid Build Coastguard Worker {"lib1", "lib1", "notice"}, 56*9e94795aSAndroid Build Coastguard Worker {"lib2", "lib2", "notice"}, 57*9e94795aSAndroid Build Coastguard Worker } 58*9e94795aSAndroid Build Coastguard Worker 59*9e94795aSAndroid Build Coastguard Worker // share describes bottomUp after a top-down share resolve. 60*9e94795aSAndroid Build Coastguard Worker share = []res{ 61*9e94795aSAndroid Build Coastguard Worker {"image", "image", "restricted"}, 62*9e94795aSAndroid Build Coastguard Worker {"image", "bin1", "reciprocal"}, 63*9e94795aSAndroid Build Coastguard Worker {"image", "bin2", "restricted"}, 64*9e94795aSAndroid Build Coastguard Worker {"image", "lib2", "restricted"}, 65*9e94795aSAndroid Build Coastguard Worker {"bin1", "bin1", "reciprocal"}, 66*9e94795aSAndroid Build Coastguard Worker {"bin2", "bin2", "restricted"}, 67*9e94795aSAndroid Build Coastguard Worker {"bin2", "lib2", "restricted"}, 68*9e94795aSAndroid Build Coastguard Worker } 69*9e94795aSAndroid Build Coastguard Worker 70*9e94795aSAndroid Build Coastguard Worker // proprietary describes bottomUp after a top-down proprietary resolve. 71*9e94795aSAndroid Build Coastguard Worker // Note that the proprietary binc is not reachable through the toolchain 72*9e94795aSAndroid Build Coastguard Worker // dependency. 73*9e94795aSAndroid Build Coastguard Worker proprietary = []res{} 74*9e94795aSAndroid Build Coastguard Worker) 75*9e94795aSAndroid Build Coastguard Worker 76*9e94795aSAndroid Build Coastguard Workerfunc TestResolutionSet_AttachesTo(t *testing.T) { 77*9e94795aSAndroid Build Coastguard Worker lg := newLicenseGraph() 78*9e94795aSAndroid Build Coastguard Worker 79*9e94795aSAndroid Build Coastguard Worker rsShare := toResolutionSet(lg, share) 80*9e94795aSAndroid Build Coastguard Worker 81*9e94795aSAndroid Build Coastguard Worker t.Logf("checking resolution set %s", rsShare.String()) 82*9e94795aSAndroid Build Coastguard Worker 83*9e94795aSAndroid Build Coastguard Worker actual := rsShare.AttachesTo().Names() 84*9e94795aSAndroid Build Coastguard Worker sort.Strings(actual) 85*9e94795aSAndroid Build Coastguard Worker 86*9e94795aSAndroid Build Coastguard Worker expected := []string{"bin1", "bin2", "image"} 87*9e94795aSAndroid Build Coastguard Worker 88*9e94795aSAndroid Build Coastguard Worker t.Logf("actual rsShare: %v", actual) 89*9e94795aSAndroid Build Coastguard Worker t.Logf("expected rsShare: %v", expected) 90*9e94795aSAndroid Build Coastguard Worker 91*9e94795aSAndroid Build Coastguard Worker if len(actual) != len(expected) { 92*9e94795aSAndroid Build Coastguard Worker t.Errorf("rsShare: wrong number of targets: got %d, want %d", len(actual), len(expected)) 93*9e94795aSAndroid Build Coastguard Worker return 94*9e94795aSAndroid Build Coastguard Worker } 95*9e94795aSAndroid Build Coastguard Worker for i := 0; i < len(actual); i++ { 96*9e94795aSAndroid Build Coastguard Worker if actual[i] != expected[i] { 97*9e94795aSAndroid Build Coastguard Worker t.Errorf("rsShare: unexpected target at index %d: got %s, want %s", i, actual[i], expected[i]) 98*9e94795aSAndroid Build Coastguard Worker } 99*9e94795aSAndroid Build Coastguard Worker } 100*9e94795aSAndroid Build Coastguard Worker 101*9e94795aSAndroid Build Coastguard Worker rsPrivate := toResolutionSet(lg, proprietary) 102*9e94795aSAndroid Build Coastguard Worker actual = rsPrivate.AttachesTo().Names() 103*9e94795aSAndroid Build Coastguard Worker expected = []string{} 104*9e94795aSAndroid Build Coastguard Worker 105*9e94795aSAndroid Build Coastguard Worker t.Logf("actual rsPrivate: %v", actual) 106*9e94795aSAndroid Build Coastguard Worker t.Logf("expected rsPrivate: %v", expected) 107*9e94795aSAndroid Build Coastguard Worker 108*9e94795aSAndroid Build Coastguard Worker if len(actual) != len(expected) { 109*9e94795aSAndroid Build Coastguard Worker t.Errorf("rsPrivate: wrong number of targets: got %d, want %d", len(actual), len(expected)) 110*9e94795aSAndroid Build Coastguard Worker return 111*9e94795aSAndroid Build Coastguard Worker } 112*9e94795aSAndroid Build Coastguard Worker for i := 0; i < len(actual); i++ { 113*9e94795aSAndroid Build Coastguard Worker if actual[i] != expected[i] { 114*9e94795aSAndroid Build Coastguard Worker t.Errorf("rsPrivate: unexpected target at index %d: got %s, want %s", i, actual[i], expected[i]) 115*9e94795aSAndroid Build Coastguard Worker } 116*9e94795aSAndroid Build Coastguard Worker } 117*9e94795aSAndroid Build Coastguard Worker} 118*9e94795aSAndroid Build Coastguard Worker 119*9e94795aSAndroid Build Coastguard Workerfunc TestResolutionSet_AttachesToTarget(t *testing.T) { 120*9e94795aSAndroid Build Coastguard Worker lg := newLicenseGraph() 121*9e94795aSAndroid Build Coastguard Worker 122*9e94795aSAndroid Build Coastguard Worker rsShare := toResolutionSet(lg, share) 123*9e94795aSAndroid Build Coastguard Worker 124*9e94795aSAndroid Build Coastguard Worker t.Logf("checking resolution set %s", rsShare.String()) 125*9e94795aSAndroid Build Coastguard Worker 126*9e94795aSAndroid Build Coastguard Worker if rsShare.AttachesToTarget(newTestNode(lg, "binc")) { 127*9e94795aSAndroid Build Coastguard Worker t.Errorf("actual.AttachesToTarget(\"binc\"): got true, want false") 128*9e94795aSAndroid Build Coastguard Worker } 129*9e94795aSAndroid Build Coastguard Worker if !rsShare.AttachesToTarget(newTestNode(lg, "image")) { 130*9e94795aSAndroid Build Coastguard Worker t.Errorf("actual.AttachesToTarget(\"image\"): got false want true") 131*9e94795aSAndroid Build Coastguard Worker } 132*9e94795aSAndroid Build Coastguard Worker} 133