blob: 2ee249dd7a00f04885ea73e90e9099e5fbc906e4 [file] [log] [blame]
Bob Badour6dd00352021-10-01 15:21:58 -07001// 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 main
16
17import (
18 "bytes"
19 "strings"
20 "testing"
21)
22
23func 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 Badour103eb0f2022-01-10 13:50:57 -0800101 conditions: []string{"reciprocal"},
Bob Badour6dd00352021-10-01 15:21:58 -0700102 },
103 {
104 project: "static/library",
105 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800106 "reciprocal",
Bob Badour6dd00352021-10-01 15:21:58 -0700107 },
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 Badour103eb0f2022-01-10 13:50:57 -0800118 conditions: []string{"reciprocal"},
Bob Badour6dd00352021-10-01 15:21:58 -0700119 },
120 {
121 project: "static/library",
122 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800123 "reciprocal",
Bob Badour6dd00352021-10-01 15:21:58 -0700124 },
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 Badour103eb0f2022-01-10 13:50:57 -0800135 conditions: []string{"reciprocal"},
Bob Badour6dd00352021-10-01 15:21:58 -0700136 },
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 Badour103eb0f2022-01-10 13:50:57 -0800147 "reciprocal",
Bob Badour6dd00352021-10-01 15:21:58 -0700148 },
149 },
150 {
151 project: "static/library",
152 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800153 "reciprocal",
Bob Badour6dd00352021-10-01 15:21:58 -0700154 },
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 Badour103eb0f2022-01-10 13:50:57 -0800171 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700172 },
173 {
174 project: "device/library",
Bob Badour103eb0f2022-01-10 13:50:57 -0800175 conditions: []string{"restricted_allows_dynamic_linking"},
Bob Badour6dd00352021-10-01 15:21:58 -0700176 },
177 {
178 project: "dynamic/binary",
Bob Badour103eb0f2022-01-10 13:50:57 -0800179 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700180 },
181 {
182 project: "highest/apex",
183 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800184 "restricted",
185 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700186 },
187 },
188 {
189 project: "static/binary",
190 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800191 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700192 },
193 },
194 {
195 project: "static/library",
196 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800197 "reciprocal",
198 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700199 },
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 Badour103eb0f2022-01-10 13:50:57 -0800210 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700211 },
212 {
213 project: "container/zip",
214 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800215 "restricted",
216 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700217 },
218 },
219 {
220 project: "device/library",
Bob Badour103eb0f2022-01-10 13:50:57 -0800221 conditions: []string{"restricted_allows_dynamic_linking"},
Bob Badour6dd00352021-10-01 15:21:58 -0700222 },
223 {
224 project: "dynamic/binary",
Bob Badour103eb0f2022-01-10 13:50:57 -0800225 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700226 },
227 {
228 project: "static/binary",
229 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800230 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700231 },
232 },
233 {
234 project: "static/library",
235 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800236 "reciprocal",
237 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700238 },
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 Badour103eb0f2022-01-10 13:50:57 -0800250 "restricted",
251 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700252 },
253 },
254 {
255 project: "distributable/application",
256 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800257 "restricted",
258 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700259 },
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 Badour103eb0f2022-01-10 13:50:57 -0800271 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700272 },
273 },
274 {
275 project: "static/binary",
276 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800277 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700278 },
279 },
280 {
281 project: "static/library",
282 conditions: []string{
Bob Badour103eb0f2022-01-10 13:50:57 -0800283 "reciprocal",
284 "restricted_allows_dynamic_linking",
Bob Badour6dd00352021-10-01 15:21:58 -0700285 },
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 Badour103eb0f2022-01-10 13:50:57 -0800302 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700303 },
304 {
305 project: "dynamic/binary",
Bob Badour103eb0f2022-01-10 13:50:57 -0800306 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700307 },
308 {
309 project: "highest/apex",
Bob Badour103eb0f2022-01-10 13:50:57 -0800310 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700311 },
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 Badour103eb0f2022-01-10 13:50:57 -0800321 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700322 },
323 {
324 project: "container/zip",
Bob Badour103eb0f2022-01-10 13:50:57 -0800325 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700326 },
327 {
328 project: "dynamic/binary",
Bob Badour103eb0f2022-01-10 13:50:57 -0800329 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700330 },
331 },
332 },
333 {
334 condition: "proprietary",
335 name: "application",
336 roots: []string{"application.meta_lic"},
337 expectedOut: []projectShare{
338 {
339 project: "device/library",
Bob Badour103eb0f2022-01-10 13:50:57 -0800340 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700341 },
342 {
343 project: "distributable/application",
Bob Badour103eb0f2022-01-10 13:50:57 -0800344 conditions: []string{"restricted"},
Bob Badour6dd00352021-10-01 15:21:58 -0700345 },
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 Badour6dd00352021-10-01 15:21:58 -0700368 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}