blob: b0d61e1bf9e8595414fa5d7e9b7cf22b05b36e97 [file] [log] [blame]
Bob Badour2546feb2022-01-26 20:58:24 -08001// 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 "bufio"
19 "bytes"
20 "os"
21 "strings"
22 "testing"
23)
24
25func Test(t *testing.T) {
26 tests := []struct {
27 condition string
28 name string
29 roots []string
30 stripPrefix string
31 expectedOut []string
32 }{
33 {
34 condition: "firstparty",
35 name: "apex",
36 roots: []string{"highest.apex.meta_lic"},
37 stripPrefix: "out/target/product/fictional",
38 expectedOut: []string{
39 "/system/apex/highest.apex",
40 "/system/apex/highest.apex/bin/bin1",
41 "/system/apex/highest.apex/bin/bin2",
42 "/system/apex/highest.apex/lib/liba.so",
43 "/system/apex/highest.apex/lib/libb.so",
44 },
45 },
46 {
47 condition: "firstparty",
48 name: "container",
49 roots: []string{"container.zip.meta_lic"},
50 stripPrefix: "out/target/product/fictional/data/",
51 expectedOut: []string{
52 "container.zip",
53 "container.zip/bin1",
54 "container.zip/bin2",
55 "container.zip/liba.so",
56 "container.zip/libb.so",
57 },
58 },
59 {
60 condition: "firstparty",
61 name: "application",
62 roots: []string{"application.meta_lic"},
63 stripPrefix: "out/target/product/fictional/bin/",
64 expectedOut: []string{"application"},
65 },
66 {
67 condition: "firstparty",
68 name: "binary",
69 roots: []string{"bin/bin1.meta_lic"},
70 stripPrefix: "out/target/product/fictional/system/",
71 expectedOut: []string{"bin/bin1"},
72 },
73 {
74 condition: "firstparty",
75 name: "library",
76 roots: []string{"lib/libd.so.meta_lic"},
77 stripPrefix: "out/target/product/fictional/system/",
78 expectedOut: []string{"lib/libd.so"},
79 },
80 {
81 condition: "notice",
82 name: "apex",
83 roots: []string{"highest.apex.meta_lic"},
84 expectedOut: []string{
85 "out/target/product/fictional/system/apex/highest.apex",
86 "out/target/product/fictional/system/apex/highest.apex/bin/bin1",
87 "out/target/product/fictional/system/apex/highest.apex/bin/bin2",
88 "out/target/product/fictional/system/apex/highest.apex/lib/liba.so",
89 "out/target/product/fictional/system/apex/highest.apex/lib/libb.so",
90 },
91 },
92 {
93 condition: "notice",
94 name: "container",
95 roots: []string{"container.zip.meta_lic"},
96 expectedOut: []string{
97 "out/target/product/fictional/data/container.zip",
98 "out/target/product/fictional/data/container.zip/bin1",
99 "out/target/product/fictional/data/container.zip/bin2",
100 "out/target/product/fictional/data/container.zip/liba.so",
101 "out/target/product/fictional/data/container.zip/libb.so",
102 },
103 },
104 {
105 condition: "notice",
106 name: "application",
107 roots: []string{"application.meta_lic"},
108 expectedOut: []string{"out/target/product/fictional/bin/application"},
109 },
110 {
111 condition: "notice",
112 name: "binary",
113 roots: []string{"bin/bin1.meta_lic"},
114 expectedOut: []string{"out/target/product/fictional/system/bin/bin1"},
115 },
116 {
117 condition: "notice",
118 name: "library",
119 roots: []string{"lib/libd.so.meta_lic"},
120 expectedOut: []string{"out/target/product/fictional/system/lib/libd.so"},
121 },
122 {
123 condition: "reciprocal",
124 name: "apex",
125 roots: []string{"highest.apex.meta_lic"},
126 stripPrefix: "out/target/product/fictional/system/apex/",
127 expectedOut: []string{
128 "highest.apex",
129 "highest.apex/bin/bin1",
130 "highest.apex/bin/bin2",
131 "highest.apex/lib/liba.so",
132 "highest.apex/lib/libb.so",
133 },
134 },
135 {
136 condition: "reciprocal",
137 name: "container",
138 roots: []string{"container.zip.meta_lic"},
139 stripPrefix: "out/target/product/fictional/data/",
140 expectedOut: []string{
141 "container.zip",
142 "container.zip/bin1",
143 "container.zip/bin2",
144 "container.zip/liba.so",
145 "container.zip/libb.so",
146 },
147 },
148 {
149 condition: "reciprocal",
150 name: "application",
151 roots: []string{"application.meta_lic"},
152 stripPrefix: "out/target/product/fictional/bin/",
153 expectedOut: []string{"application"},
154 },
155 {
156 condition: "reciprocal",
157 name: "binary",
158 roots: []string{"bin/bin1.meta_lic"},
159 stripPrefix: "out/target/product/fictional/system/",
160 expectedOut: []string{"bin/bin1"},
161 },
162 {
163 condition: "reciprocal",
164 name: "library",
165 roots: []string{"lib/libd.so.meta_lic"},
166 stripPrefix: "out/target/product/fictional/system/",
167 expectedOut: []string{"lib/libd.so"},
168 },
169 {
170 condition: "restricted",
171 name: "apex",
172 roots: []string{"highest.apex.meta_lic"},
173 stripPrefix: "out/target/product/fictional/system/apex/",
174 expectedOut: []string{
175 "highest.apex",
176 "highest.apex/bin/bin1",
177 "highest.apex/bin/bin2",
178 "highest.apex/lib/liba.so",
179 "highest.apex/lib/libb.so",
180 },
181 },
182 {
183 condition: "restricted",
184 name: "container",
185 roots: []string{"container.zip.meta_lic"},
186 stripPrefix: "out/target/product/fictional/data/",
187 expectedOut: []string{
188 "container.zip",
189 "container.zip/bin1",
190 "container.zip/bin2",
191 "container.zip/liba.so",
192 "container.zip/libb.so",
193 },
194 },
195 {
196 condition: "restricted",
197 name: "application",
198 roots: []string{"application.meta_lic"},
199 stripPrefix: "out/target/product/fictional/bin/",
200 expectedOut: []string{"application"},
201 },
202 {
203 condition: "restricted",
204 name: "binary",
205 roots: []string{"bin/bin1.meta_lic"},
206 stripPrefix: "out/target/product/fictional/system/",
207 expectedOut: []string{"bin/bin1"},
208 },
209 {
210 condition: "restricted",
211 name: "library",
212 roots: []string{"lib/libd.so.meta_lic"},
213 stripPrefix: "out/target/product/fictional/system/",
214 expectedOut: []string{"lib/libd.so"},
215 },
216 {
217 condition: "proprietary",
218 name: "apex",
219 roots: []string{"highest.apex.meta_lic"},
220 stripPrefix: "out/target/product/fictional/system/apex/",
221 expectedOut: []string{
222 "highest.apex",
223 "highest.apex/bin/bin1",
224 "highest.apex/bin/bin2",
225 "highest.apex/lib/liba.so",
226 "highest.apex/lib/libb.so",
227 },
228 },
229 {
230 condition: "proprietary",
231 name: "container",
232 roots: []string{"container.zip.meta_lic"},
233 stripPrefix: "out/target/product/fictional/data/",
234 expectedOut: []string{
235 "container.zip",
236 "container.zip/bin1",
237 "container.zip/bin2",
238 "container.zip/liba.so",
239 "container.zip/libb.so",
240 },
241 },
242 {
243 condition: "proprietary",
244 name: "application",
245 roots: []string{"application.meta_lic"},
246 stripPrefix: "out/target/product/fictional/bin/",
247 expectedOut: []string{"application"},
248 },
249 {
250 condition: "proprietary",
251 name: "binary",
252 roots: []string{"bin/bin1.meta_lic"},
253 stripPrefix: "out/target/product/fictional/system/",
254 expectedOut: []string{"bin/bin1"},
255 },
256 {
257 condition: "proprietary",
258 name: "library",
259 roots: []string{"lib/libd.so.meta_lic"},
260 stripPrefix: "out/target/product/fictional/system/",
261 expectedOut: []string{"lib/libd.so"},
262 },
263 }
264 for _, tt := range tests {
265 t.Run(tt.condition+" "+tt.name, func(t *testing.T) {
266 stdout := &bytes.Buffer{}
267 stderr := &bytes.Buffer{}
268
269 rootFiles := make([]string, 0, len(tt.roots))
270 for _, r := range tt.roots {
271 rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r)
272 }
273
274 ctx := context{stdout, stderr, os.DirFS("."), tt.stripPrefix}
275
276 err := billOfMaterials(&ctx, rootFiles...)
277 if err != nil {
278 t.Fatalf("bom: error = %w, stderr = %v", err, stderr)
279 return
280 }
281 if stderr.Len() > 0 {
282 t.Errorf("bom: gotStderr = %v, want none", stderr)
283 }
284
285 t.Logf("got stdout: %s", stdout.String())
286
287 t.Logf("want stdout: %s", strings.Join(tt.expectedOut, "\n"))
288
289 out := bufio.NewScanner(stdout)
290 lineno := 0
291 for out.Scan() {
292 line := out.Text()
293 if strings.TrimLeft(line, " ") == "" {
294 continue
295 }
296 if len(tt.expectedOut) <= lineno {
297 t.Errorf("bom: unexpected output at line %d: got %q, want nothing (wanted %d lines)", lineno+1, line, len(tt.expectedOut))
298 } else if tt.expectedOut[lineno] != line {
299 t.Errorf("bom: unexpected output at line %d: got %q, want %q", lineno+1, line, tt.expectedOut[lineno])
300 }
301 lineno++
302 }
303 for ; lineno < len(tt.expectedOut); lineno++ {
304 t.Errorf("bom: missing output line %d: ended early, want %q", lineno+1, tt.expectedOut[lineno])
305 }
306 })
307 }
308}