Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 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 | |
| 15 | package compliance |
| 16 | |
| 17 | import ( |
| 18 | "sort" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | var ( |
| 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 Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 80 | func TestResolutionSet_AttachesTo(t *testing.T) { |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 81 | lg := newLicenseGraph() |
| 82 | |
| 83 | rsShare := toResolutionSet(lg, share) |
| 84 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 85 | t.Logf("checking resolution set %s", rsShare.String()) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 86 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 87 | actual := rsShare.AttachesTo().Names() |
| 88 | sort.Strings(actual) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 89 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 90 | expected := []string{"bin1", "bin2", "image"} |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 91 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 92 | t.Logf("actual rsShare: %v", actual) |
| 93 | t.Logf("expected rsShare: %v", expected) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 94 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 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 | } |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 104 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 105 | rsPrivate := toResolutionSet(lg, proprietary) |
| 106 | actual = rsPrivate.AttachesTo().Names() |
| 107 | expected = []string{} |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 108 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 109 | t.Logf("actual rsPrivate: %v", actual) |
| 110 | t.Logf("expected rsPrivate: %v", expected) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 111 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 112 | if len(actual) != len(expected) { |
| 113 | t.Errorf("rsPrivate: wrong number of targets: got %d, want %d", len(actual), len(expected)) |
| 114 | return |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 115 | } |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 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 | } |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestResolutionSet_AttachesToTarget(t *testing.T) { |
| 124 | lg := newLicenseGraph() |
| 125 | |
| 126 | rsShare := toResolutionSet(lg, share) |
| 127 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 128 | t.Logf("checking resolution set %s", rsShare.String()) |
| 129 | |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 130 | if rsShare.AttachesToTarget(newTestNode(lg, "binc")) { |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 131 | t.Errorf("actual.AttachesToTarget(\"binc\"): got true, want false") |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 132 | } |
| 133 | if !rsShare.AttachesToTarget(newTestNode(lg, "image")) { |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 134 | t.Errorf("actual.AttachesToTarget(\"image\"): got false want true") |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 135 | } |
| 136 | } |