blob: b4847e39c7a0a428393012bd7c3c3be30bf1989b [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",
101 conditions: []string{"lib/liba.so.meta_lic:reciprocal"},
102 },
103 {
104 project: "static/library",
105 conditions: []string{
106 "lib/libc.a.meta_lic:reciprocal",
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",
118 conditions: []string{"lib/liba.so.meta_lic:reciprocal"},
119 },
120 {
121 project: "static/library",
122 conditions: []string{
123 "lib/libc.a.meta_lic:reciprocal",
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",
135 conditions: []string{"lib/liba.so.meta_lic:reciprocal"},
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{
147 "lib/liba.so.meta_lic:reciprocal",
148 },
149 },
150 {
151 project: "static/library",
152 conditions: []string{
153 "lib/libc.a.meta_lic:reciprocal",
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",
171 conditions: []string{"lib/libb.so.meta_lic:restricted"},
172 },
173 {
174 project: "device/library",
175 conditions: []string{"lib/liba.so.meta_lic:restricted"},
176 },
177 {
178 project: "dynamic/binary",
179 conditions: []string{"lib/libb.so.meta_lic:restricted"},
180 },
181 {
182 project: "highest/apex",
183 conditions: []string{
184 "lib/liba.so.meta_lic:restricted",
185 "lib/libb.so.meta_lic:restricted",
186 },
187 },
188 {
189 project: "static/binary",
190 conditions: []string{
191 "lib/liba.so.meta_lic:restricted",
192 },
193 },
194 {
195 project: "static/library",
196 conditions: []string{
197 "lib/liba.so.meta_lic:restricted",
198 "lib/libc.a.meta_lic:reciprocal",
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",
210 conditions: []string{"lib/libb.so.meta_lic:restricted"},
211 },
212 {
213 project: "container/zip",
214 conditions: []string{
215 "lib/liba.so.meta_lic:restricted",
216 "lib/libb.so.meta_lic:restricted",
217 },
218 },
219 {
220 project: "device/library",
221 conditions: []string{"lib/liba.so.meta_lic:restricted"},
222 },
223 {
224 project: "dynamic/binary",
225 conditions: []string{"lib/libb.so.meta_lic:restricted"},
226 },
227 {
228 project: "static/binary",
229 conditions: []string{
230 "lib/liba.so.meta_lic:restricted",
231 },
232 },
233 {
234 project: "static/library",
235 conditions: []string{
236 "lib/liba.so.meta_lic:restricted",
237 "lib/libc.a.meta_lic:reciprocal",
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{
250 "lib/liba.so.meta_lic:restricted",
251 "lib/libb.so.meta_lic:restricted",
252 },
253 },
254 {
255 project: "distributable/application",
256 conditions: []string{
257 "lib/liba.so.meta_lic:restricted",
258 "lib/libb.so.meta_lic:restricted",
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{
271 "lib/liba.so.meta_lic:restricted",
272 },
273 },
274 {
275 project: "static/binary",
276 conditions: []string{
277 "lib/liba.so.meta_lic:restricted",
278 },
279 },
280 {
281 project: "static/library",
282 conditions: []string{
283 "lib/liba.so.meta_lic:restricted",
284 "lib/libc.a.meta_lic:reciprocal",
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",
302 conditions: []string{"lib/libb.so.meta_lic:restricted"},
303 },
304 {
305 project: "dynamic/binary",
306 conditions: []string{"lib/libb.so.meta_lic:restricted"},
307 },
308 {
309 project: "highest/apex",
310 conditions: []string{"lib/libb.so.meta_lic:restricted"},
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",
321 conditions: []string{"lib/libb.so.meta_lic:restricted"},
322 },
323 {
324 project: "container/zip",
325 conditions: []string{"lib/libb.so.meta_lic:restricted"},
326 },
327 {
328 project: "dynamic/binary",
329 conditions: []string{"lib/libb.so.meta_lic:restricted"},
330 },
331 },
332 },
333 {
334 condition: "proprietary",
335 name: "application",
336 roots: []string{"application.meta_lic"},
337 expectedOut: []projectShare{
338 {
339 project: "device/library",
340 conditions: []string{"lib/libb.so.meta_lic:restricted"},
341 },
342 {
343 project: "distributable/application",
344 conditions: []string{"lib/libb.so.meta_lic:restricted"},
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(",")
368 expectedOut.WriteString("testdata/")
369 expectedOut.WriteString(tt.condition)
370 expectedOut.WriteString("/")
371 expectedOut.WriteString(lc)
372 }
373 expectedOut.WriteString("\n")
374 }
375
376 stdout := &bytes.Buffer{}
377 stderr := &bytes.Buffer{}
378
379 rootFiles := make([]string, 0, len(tt.roots))
380 for _, r := range tt.roots {
381 rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r)
382 }
383 err := listShare(stdout, stderr, rootFiles...)
384 if err != nil {
385 t.Fatalf("listshare: error = %v, stderr = %v", err, stderr)
386 return
387 }
388 if stderr.Len() > 0 {
389 t.Errorf("listshare: gotStderr = %v, want none", stderr)
390 }
391 out := stdout.String()
392 expected := expectedOut.String()
393 if out != expected {
394 outList := strings.Split(out, "\n")
395 expectedList := strings.Split(expected, "\n")
396 startLine := 0
397 for len(outList) > startLine && len(expectedList) > startLine && outList[startLine] == expectedList[startLine] {
398 startLine++
399 }
400 t.Errorf("listshare: gotStdout = %v, want %v, somewhere near line %d Stdout = %v, want %v",
401 out, expected, startLine+1, outList[startLine], expectedList[startLine])
402 }
403 })
404 }
405}