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 | "strings" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestConditionNames(t *testing.T) { |
| 24 | impliesShare := ConditionNames([]string{"restricted", "reciprocal"}) |
| 25 | |
| 26 | if impliesShare.Contains("notice") { |
| 27 | t.Errorf("impliesShare.Contains(\"notice\") got true, want false") |
| 28 | } |
| 29 | |
| 30 | if !impliesShare.Contains("restricted") { |
| 31 | t.Errorf("impliesShare.Contains(\"restricted\") got false, want true") |
| 32 | } |
| 33 | |
| 34 | if !impliesShare.Contains("reciprocal") { |
| 35 | t.Errorf("impliesShare.Contains(\"reciprocal\") got false, want true") |
| 36 | } |
| 37 | |
| 38 | if impliesShare.Contains("") { |
| 39 | t.Errorf("impliesShare.Contains(\"\") got true, want false") |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestConditionList(t *testing.T) { |
| 44 | tests := []struct { |
| 45 | name string |
| 46 | conditions map[string][]string |
| 47 | byName map[string][]string |
| 48 | byOrigin map[string][]string |
| 49 | }{ |
| 50 | { |
| 51 | name: "noticeonly", |
| 52 | conditions: map[string][]string{ |
| 53 | "notice": []string{"bin1", "lib1"}, |
| 54 | }, |
| 55 | byName: map[string][]string{ |
| 56 | "notice": []string{"bin1", "lib1"}, |
| 57 | "restricted": []string{}, |
| 58 | }, |
| 59 | byOrigin: map[string][]string{ |
| 60 | "bin1": []string{"notice"}, |
| 61 | "lib1": []string{"notice"}, |
| 62 | "bin2": []string{}, |
| 63 | "lib2": []string{}, |
| 64 | }, |
| 65 | }, |
| 66 | { |
| 67 | name: "empty", |
| 68 | conditions: map[string][]string{}, |
| 69 | byName: map[string][]string{ |
| 70 | "notice": []string{}, |
| 71 | "restricted": []string{}, |
| 72 | }, |
| 73 | byOrigin: map[string][]string{ |
| 74 | "bin1": []string{}, |
| 75 | "lib1": []string{}, |
| 76 | "bin2": []string{}, |
| 77 | "lib2": []string{}, |
| 78 | }, |
| 79 | }, |
| 80 | { |
| 81 | name: "everything", |
| 82 | conditions: map[string][]string{ |
| 83 | "notice": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 84 | "reciprocal": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 85 | "restricted": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 86 | "by_exception_only": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 87 | }, |
| 88 | byName: map[string][]string{ |
| 89 | "permissive": []string{}, |
| 90 | "notice": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 91 | "reciprocal": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 92 | "restricted": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 93 | "by_exception_only": []string{"bin1", "bin2", "lib1", "lib2"}, |
| 94 | }, |
| 95 | byOrigin: map[string][]string{ |
| 96 | "bin1": []string{"notice", "reciprocal", "restricted", "by_exception_only"}, |
| 97 | "bin2": []string{"notice", "reciprocal", "restricted", "by_exception_only"}, |
| 98 | "lib1": []string{"notice", "reciprocal", "restricted", "by_exception_only"}, |
| 99 | "lib2": []string{"notice", "reciprocal", "restricted", "by_exception_only"}, |
| 100 | "other": []string{}, |
| 101 | }, |
| 102 | }, |
| 103 | { |
| 104 | name: "allbutoneeach", |
| 105 | conditions: map[string][]string{ |
| 106 | "notice": []string{"bin2", "lib1", "lib2"}, |
| 107 | "reciprocal": []string{"bin1", "lib1", "lib2"}, |
| 108 | "restricted": []string{"bin1", "bin2", "lib2"}, |
| 109 | "by_exception_only": []string{"bin1", "bin2", "lib1"}, |
| 110 | }, |
| 111 | byName: map[string][]string{ |
| 112 | "permissive": []string{}, |
| 113 | "notice": []string{"bin2", "lib1", "lib2"}, |
| 114 | "reciprocal": []string{"bin1", "lib1", "lib2"}, |
| 115 | "restricted": []string{"bin1", "bin2", "lib2"}, |
| 116 | "by_exception_only": []string{"bin1", "bin2", "lib1"}, |
| 117 | }, |
| 118 | byOrigin: map[string][]string{ |
| 119 | "bin1": []string{"reciprocal", "restricted", "by_exception_only"}, |
| 120 | "bin2": []string{"notice", "restricted", "by_exception_only"}, |
| 121 | "lib1": []string{"notice", "reciprocal", "by_exception_only"}, |
| 122 | "lib2": []string{"notice", "reciprocal", "restricted"}, |
| 123 | "other": []string{}, |
| 124 | }, |
| 125 | }, |
| 126 | { |
| 127 | name: "oneeach", |
| 128 | conditions: map[string][]string{ |
| 129 | "notice": []string{"bin1"}, |
| 130 | "reciprocal": []string{"bin2"}, |
| 131 | "restricted": []string{"lib1"}, |
| 132 | "by_exception_only": []string{"lib2"}, |
| 133 | }, |
| 134 | byName: map[string][]string{ |
| 135 | "permissive": []string{}, |
| 136 | "notice": []string{"bin1"}, |
| 137 | "reciprocal": []string{"bin2"}, |
| 138 | "restricted": []string{"lib1"}, |
| 139 | "by_exception_only": []string{"lib2"}, |
| 140 | }, |
| 141 | byOrigin: map[string][]string{ |
| 142 | "bin1": []string{"notice"}, |
| 143 | "bin2": []string{"reciprocal"}, |
| 144 | "lib1": []string{"restricted"}, |
| 145 | "lib2": []string{"by_exception_only"}, |
| 146 | "other": []string{}, |
| 147 | }, |
| 148 | }, |
| 149 | } |
| 150 | for _, tt := range tests { |
| 151 | t.Run(tt.name, func(t *testing.T) { |
| 152 | lg := newLicenseGraph() |
| 153 | cl := toConditionList(lg, tt.conditions) |
| 154 | for names, expected := range tt.byName { |
| 155 | name := ConditionNames(strings.Split(names, ":")) |
| 156 | if cl.HasByName(name) { |
| 157 | if len(expected) == 0 { |
| 158 | t.Errorf("unexpected ConditionList.HasByName(%q): got true, want false", name) |
| 159 | } |
| 160 | } else { |
| 161 | if len(expected) != 0 { |
| 162 | t.Errorf("unexpected ConditionList.HasByName(%q): got false, want true", name) |
| 163 | } |
| 164 | } |
| 165 | if len(expected) != cl.CountByName(name) { |
| 166 | t.Errorf("unexpected ConditionList.CountByName(%q): got %d, want %d", name, cl.CountByName(name), len(expected)) |
| 167 | } |
| 168 | byName := cl.ByName(name) |
| 169 | if len(expected) != len(byName) { |
| 170 | t.Errorf("unexpected ConditionList.ByName(%q): got %v, want %v", name, byName, expected) |
| 171 | } else { |
| 172 | sort.Strings(expected) |
| 173 | actual := make([]string, 0, len(byName)) |
| 174 | for _, lc := range byName { |
| 175 | actual = append(actual, lc.Origin().Name()) |
| 176 | } |
| 177 | sort.Strings(actual) |
| 178 | for i := 0; i < len(expected); i++ { |
| 179 | if expected[i] != actual[i] { |
| 180 | t.Errorf("unexpected ConditionList.ByName(%q) index %d in %v: got %s, want %s", name, i, actual, actual[i], expected[i]) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | for origin, expected := range tt.byOrigin { |
| 186 | onode := newTestNode(lg, origin) |
| 187 | if cl.HasByOrigin(onode) { |
| 188 | if len(expected) == 0 { |
| 189 | t.Errorf("unexpected ConditionList.HasByOrigin(%q): got true, want false", origin) |
| 190 | } |
| 191 | } else { |
| 192 | if len(expected) != 0 { |
| 193 | t.Errorf("unexpected ConditionList.HasByOrigin(%q): got false, want true", origin) |
| 194 | } |
| 195 | } |
| 196 | if len(expected) != cl.CountByOrigin(onode) { |
| 197 | t.Errorf("unexpected ConditionList.CountByOrigin(%q): got %d, want %d", origin, cl.CountByOrigin(onode), len(expected)) |
| 198 | } |
| 199 | byOrigin := cl.ByOrigin(onode) |
| 200 | if len(expected) != len(byOrigin) { |
| 201 | t.Errorf("unexpected ConditionList.ByOrigin(%q): got %v, want %v", origin, byOrigin, expected) |
| 202 | } else { |
| 203 | sort.Strings(expected) |
| 204 | actual := make([]string, 0, len(byOrigin)) |
| 205 | for _, lc := range byOrigin { |
| 206 | actual = append(actual, lc.Name()) |
| 207 | } |
| 208 | sort.Strings(actual) |
| 209 | for i := 0; i < len(expected); i++ { |
| 210 | if expected[i] != actual[i] { |
| 211 | t.Errorf("unexpected ConditionList.ByOrigin(%q) index %d in %v: got %s, want %s", origin, i, actual, actual[i], expected[i]) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | }) |
| 217 | } |
| 218 | } |