Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -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 main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func Test(t *testing.T) { |
| 24 | type projectShare struct { |
| 25 | project string |
| 26 | conditions []string |
| 27 | } |
| 28 | tests := []struct { |
| 29 | condition string |
| 30 | name string |
| 31 | roots []string |
| 32 | expectedOut []projectShare |
| 33 | }{ |
| 34 | { |
| 35 | condition: "firstparty", |
| 36 | name: "apex", |
| 37 | roots: []string{"highest.apex.meta_lic"}, |
| 38 | expectedOut: []projectShare{}, |
| 39 | }, |
| 40 | { |
| 41 | condition: "firstparty", |
| 42 | name: "container", |
| 43 | roots: []string{"container.zip.meta_lic"}, |
| 44 | expectedOut: []projectShare{}, |
| 45 | }, |
| 46 | { |
| 47 | condition: "firstparty", |
| 48 | name: "application", |
| 49 | roots: []string{"application.meta_lic"}, |
| 50 | expectedOut: []projectShare{}, |
| 51 | }, |
| 52 | { |
| 53 | condition: "firstparty", |
| 54 | name: "binary", |
| 55 | roots: []string{"bin/bin1.meta_lic"}, |
| 56 | expectedOut: []projectShare{}, |
| 57 | }, |
| 58 | { |
| 59 | condition: "firstparty", |
| 60 | name: "library", |
| 61 | roots: []string{"lib/libd.so.meta_lic"}, |
| 62 | expectedOut: []projectShare{}, |
| 63 | }, |
| 64 | { |
| 65 | condition: "notice", |
| 66 | name: "apex", |
| 67 | roots: []string{"highest.apex.meta_lic"}, |
| 68 | expectedOut: []projectShare{}, |
| 69 | }, |
| 70 | { |
| 71 | condition: "notice", |
| 72 | name: "container", |
| 73 | roots: []string{"container.zip.meta_lic"}, |
| 74 | expectedOut: []projectShare{}, |
| 75 | }, |
| 76 | { |
| 77 | condition: "notice", |
| 78 | name: "application", |
| 79 | roots: []string{"application.meta_lic"}, |
| 80 | expectedOut: []projectShare{}, |
| 81 | }, |
| 82 | { |
| 83 | condition: "notice", |
| 84 | name: "binary", |
| 85 | roots: []string{"bin/bin1.meta_lic"}, |
| 86 | expectedOut: []projectShare{}, |
| 87 | }, |
| 88 | { |
| 89 | condition: "notice", |
| 90 | name: "library", |
| 91 | roots: []string{"lib/libd.so.meta_lic"}, |
| 92 | expectedOut: []projectShare{}, |
| 93 | }, |
| 94 | { |
| 95 | condition: "reciprocal", |
| 96 | name: "apex", |
| 97 | roots: []string{"highest.apex.meta_lic"}, |
| 98 | expectedOut: []projectShare{ |
| 99 | { |
| 100 | project: "device/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 101 | conditions: []string{"reciprocal"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 102 | }, |
| 103 | { |
| 104 | project: "static/library", |
| 105 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 106 | "reciprocal", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 107 | }, |
| 108 | }, |
| 109 | }, |
| 110 | }, |
| 111 | { |
| 112 | condition: "reciprocal", |
| 113 | name: "container", |
| 114 | roots: []string{"container.zip.meta_lic"}, |
| 115 | expectedOut: []projectShare{ |
| 116 | { |
| 117 | project: "device/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 118 | conditions: []string{"reciprocal"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 119 | }, |
| 120 | { |
| 121 | project: "static/library", |
| 122 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 123 | "reciprocal", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 124 | }, |
| 125 | }, |
| 126 | }, |
| 127 | }, |
| 128 | { |
| 129 | condition: "reciprocal", |
| 130 | name: "application", |
| 131 | roots: []string{"application.meta_lic"}, |
| 132 | expectedOut: []projectShare{ |
| 133 | { |
| 134 | project: "device/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 135 | conditions: []string{"reciprocal"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 136 | }, |
| 137 | }, |
| 138 | }, |
| 139 | { |
| 140 | condition: "reciprocal", |
| 141 | name: "binary", |
| 142 | roots: []string{"bin/bin1.meta_lic"}, |
| 143 | expectedOut: []projectShare{ |
| 144 | { |
| 145 | project: "device/library", |
| 146 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 147 | "reciprocal", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 148 | }, |
| 149 | }, |
| 150 | { |
| 151 | project: "static/library", |
| 152 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 153 | "reciprocal", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 154 | }, |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | { |
| 159 | condition: "reciprocal", |
| 160 | name: "library", |
| 161 | roots: []string{"lib/libd.so.meta_lic"}, |
| 162 | expectedOut: []projectShare{}, |
| 163 | }, |
| 164 | { |
| 165 | condition: "restricted", |
| 166 | name: "apex", |
| 167 | roots: []string{"highest.apex.meta_lic"}, |
| 168 | expectedOut: []projectShare{ |
| 169 | { |
| 170 | project: "base/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 171 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 172 | }, |
| 173 | { |
| 174 | project: "device/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 175 | conditions: []string{"restricted_allows_dynamic_linking"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 176 | }, |
| 177 | { |
| 178 | project: "dynamic/binary", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 179 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 180 | }, |
| 181 | { |
| 182 | project: "highest/apex", |
| 183 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 184 | "restricted", |
| 185 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 186 | }, |
| 187 | }, |
| 188 | { |
| 189 | project: "static/binary", |
| 190 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 191 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 192 | }, |
| 193 | }, |
| 194 | { |
| 195 | project: "static/library", |
| 196 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 197 | "reciprocal", |
| 198 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 199 | }, |
| 200 | }, |
| 201 | }, |
| 202 | }, |
| 203 | { |
| 204 | condition: "restricted", |
| 205 | name: "container", |
| 206 | roots: []string{"container.zip.meta_lic"}, |
| 207 | expectedOut: []projectShare{ |
| 208 | { |
| 209 | project: "base/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 210 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 211 | }, |
| 212 | { |
| 213 | project: "container/zip", |
| 214 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 215 | "restricted", |
| 216 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 217 | }, |
| 218 | }, |
| 219 | { |
| 220 | project: "device/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 221 | conditions: []string{"restricted_allows_dynamic_linking"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 222 | }, |
| 223 | { |
| 224 | project: "dynamic/binary", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 225 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 226 | }, |
| 227 | { |
| 228 | project: "static/binary", |
| 229 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 230 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 231 | }, |
| 232 | }, |
| 233 | { |
| 234 | project: "static/library", |
| 235 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 236 | "reciprocal", |
| 237 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 238 | }, |
| 239 | }, |
| 240 | }, |
| 241 | }, |
| 242 | { |
| 243 | condition: "restricted", |
| 244 | name: "application", |
| 245 | roots: []string{"application.meta_lic"}, |
| 246 | expectedOut: []projectShare{ |
| 247 | { |
| 248 | project: "device/library", |
| 249 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 250 | "restricted", |
| 251 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 252 | }, |
| 253 | }, |
| 254 | { |
| 255 | project: "distributable/application", |
| 256 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 257 | "restricted", |
| 258 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 259 | }, |
| 260 | }, |
| 261 | }, |
| 262 | }, |
| 263 | { |
| 264 | condition: "restricted", |
| 265 | name: "binary", |
| 266 | roots: []string{"bin/bin1.meta_lic"}, |
| 267 | expectedOut: []projectShare{ |
| 268 | { |
| 269 | project: "device/library", |
| 270 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 271 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 272 | }, |
| 273 | }, |
| 274 | { |
| 275 | project: "static/binary", |
| 276 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 277 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 278 | }, |
| 279 | }, |
| 280 | { |
| 281 | project: "static/library", |
| 282 | conditions: []string{ |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 283 | "reciprocal", |
| 284 | "restricted_allows_dynamic_linking", |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 285 | }, |
| 286 | }, |
| 287 | }, |
| 288 | }, |
| 289 | { |
| 290 | condition: "restricted", |
| 291 | name: "library", |
| 292 | roots: []string{"lib/libd.so.meta_lic"}, |
| 293 | expectedOut: []projectShare{}, |
| 294 | }, |
| 295 | { |
| 296 | condition: "proprietary", |
| 297 | name: "apex", |
| 298 | roots: []string{"highest.apex.meta_lic"}, |
| 299 | expectedOut: []projectShare{ |
| 300 | { |
| 301 | project: "base/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 302 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 303 | }, |
| 304 | { |
| 305 | project: "dynamic/binary", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 306 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 307 | }, |
| 308 | { |
| 309 | project: "highest/apex", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 310 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 311 | }, |
| 312 | }, |
| 313 | }, |
| 314 | { |
| 315 | condition: "proprietary", |
| 316 | name: "container", |
| 317 | roots: []string{"container.zip.meta_lic"}, |
| 318 | expectedOut: []projectShare{ |
| 319 | { |
| 320 | project: "base/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 321 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 322 | }, |
| 323 | { |
| 324 | project: "container/zip", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 325 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 326 | }, |
| 327 | { |
| 328 | project: "dynamic/binary", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 329 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 330 | }, |
| 331 | }, |
| 332 | }, |
| 333 | { |
| 334 | condition: "proprietary", |
| 335 | name: "application", |
| 336 | roots: []string{"application.meta_lic"}, |
| 337 | expectedOut: []projectShare{ |
| 338 | { |
| 339 | project: "device/library", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 340 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 341 | }, |
| 342 | { |
| 343 | project: "distributable/application", |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 344 | conditions: []string{"restricted"}, |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 345 | }, |
| 346 | }, |
| 347 | }, |
| 348 | { |
| 349 | condition: "proprietary", |
| 350 | name: "binary", |
| 351 | roots: []string{"bin/bin1.meta_lic"}, |
| 352 | expectedOut: []projectShare{}, |
| 353 | }, |
| 354 | { |
| 355 | condition: "proprietary", |
| 356 | name: "library", |
| 357 | roots: []string{"lib/libd.so.meta_lic"}, |
| 358 | expectedOut: []projectShare{}, |
| 359 | }, |
| 360 | } |
| 361 | for _, tt := range tests { |
| 362 | t.Run(tt.condition+" "+tt.name, func(t *testing.T) { |
| 363 | expectedOut := &bytes.Buffer{} |
| 364 | for _, p := range tt.expectedOut { |
| 365 | expectedOut.WriteString(p.project) |
| 366 | for _, lc := range p.conditions { |
| 367 | expectedOut.WriteString(",") |
Bob Badour | 6dd0035 | 2021-10-01 15:21:58 -0700 | [diff] [blame] | 368 | expectedOut.WriteString(lc) |
| 369 | } |
| 370 | expectedOut.WriteString("\n") |
| 371 | } |
| 372 | |
| 373 | stdout := &bytes.Buffer{} |
| 374 | stderr := &bytes.Buffer{} |
| 375 | |
| 376 | rootFiles := make([]string, 0, len(tt.roots)) |
| 377 | for _, r := range tt.roots { |
| 378 | rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r) |
| 379 | } |
| 380 | err := listShare(stdout, stderr, rootFiles...) |
| 381 | if err != nil { |
| 382 | t.Fatalf("listshare: error = %v, stderr = %v", err, stderr) |
| 383 | return |
| 384 | } |
| 385 | if stderr.Len() > 0 { |
| 386 | t.Errorf("listshare: gotStderr = %v, want none", stderr) |
| 387 | } |
| 388 | out := stdout.String() |
| 389 | expected := expectedOut.String() |
| 390 | if out != expected { |
| 391 | outList := strings.Split(out, "\n") |
| 392 | expectedList := strings.Split(expected, "\n") |
| 393 | startLine := 0 |
| 394 | for len(outList) > startLine && len(expectedList) > startLine && outList[startLine] == expectedList[startLine] { |
| 395 | startLine++ |
| 396 | } |
| 397 | t.Errorf("listshare: gotStdout = %v, want %v, somewhere near line %d Stdout = %v, want %v", |
| 398 | out, expected, startLine+1, outList[startLine], expectedList[startLine]) |
| 399 | } |
| 400 | }) |
| 401 | } |
| 402 | } |