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", "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"}, 42 } 43 44 // notice describes bottomUp after a top-down notice resolve. 45 notice = []res{ 46 {"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"}, 57 } 58 59 // share describes bottomUp after a top-down share resolve. 60 share = []res{ 61 {"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"}, 68 } 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 76func TestResolutionSet_AttachesTo(t *testing.T) { 77 lg := newLicenseGraph() 78 79 rsShare := toResolutionSet(lg, share) 80 81 t.Logf("checking resolution set %s", rsShare.String()) 82 83 actual := rsShare.AttachesTo().Names() 84 sort.Strings(actual) 85 86 expected := []string{"bin1", "bin2", "image"} 87 88 t.Logf("actual rsShare: %v", actual) 89 t.Logf("expected rsShare: %v", expected) 90 91 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 } 100 101 rsPrivate := toResolutionSet(lg, proprietary) 102 actual = rsPrivate.AttachesTo().Names() 103 expected = []string{} 104 105 t.Logf("actual rsPrivate: %v", actual) 106 t.Logf("expected rsPrivate: %v", expected) 107 108 if len(actual) != len(expected) { 109 t.Errorf("rsPrivate: wrong number of targets: got %d, want %d", len(actual), len(expected)) 110 return 111 } 112 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 } 116 } 117} 118 119func TestResolutionSet_AttachesToTarget(t *testing.T) { 120 lg := newLicenseGraph() 121 122 rsShare := toResolutionSet(lg, share) 123 124 t.Logf("checking resolution set %s", rsShare.String()) 125 126 if rsShare.AttachesToTarget(newTestNode(lg, "binc")) { 127 t.Errorf("actual.AttachesToTarget(\"binc\"): got true, want false") 128 } 129 if !rsShare.AttachesToTarget(newTestNode(lg, "image")) { 130 t.Errorf("actual.AttachesToTarget(\"image\"): got false want true") 131 } 132} 133