blob: 4a9889f3f71753dd74adfb990ba47be33226556a [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"
Colin Crossd0f05c92022-01-27 15:40:29 -080020 "fmt"
Bob Badour2546feb2022-01-26 20:58:24 -080021 "os"
22 "strings"
23 "testing"
24)
25
Colin Crossd0f05c92022-01-27 15:40:29 -080026func TestMain(m *testing.M) {
27 // Change into the parent directory before running the tests
28 // so they can find the testdata directory.
29 if err := os.Chdir(".."); err != nil {
30 fmt.Printf("failed to change to testdata directory: %s\n", err)
31 os.Exit(1)
32 }
33 os.Exit(m.Run())
34}
35
Bob Badour2546feb2022-01-26 20:58:24 -080036func Test(t *testing.T) {
37 tests := []struct {
38 condition string
39 name string
40 roots []string
41 stripPrefix string
42 expectedOut []string
43 }{
44 {
Colin Cross35f79c32022-01-27 15:18:52 -080045 condition: "firstparty",
46 name: "apex",
47 roots: []string{"highest.apex.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -080048 stripPrefix: "out/target/product/fictional",
49 expectedOut: []string{
50 "/system/apex/highest.apex",
51 "/system/apex/highest.apex/bin/bin1",
52 "/system/apex/highest.apex/bin/bin2",
53 "/system/apex/highest.apex/lib/liba.so",
54 "/system/apex/highest.apex/lib/libb.so",
55 },
56 },
57 {
Colin Cross35f79c32022-01-27 15:18:52 -080058 condition: "firstparty",
59 name: "container",
60 roots: []string{"container.zip.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -080061 stripPrefix: "out/target/product/fictional/data/",
62 expectedOut: []string{
63 "container.zip",
64 "container.zip/bin1",
65 "container.zip/bin2",
66 "container.zip/liba.so",
67 "container.zip/libb.so",
68 },
69 },
70 {
Colin Cross35f79c32022-01-27 15:18:52 -080071 condition: "firstparty",
72 name: "application",
73 roots: []string{"application.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -080074 stripPrefix: "out/target/product/fictional/bin/",
75 expectedOut: []string{"application"},
76 },
77 {
Colin Cross35f79c32022-01-27 15:18:52 -080078 condition: "firstparty",
79 name: "binary",
80 roots: []string{"bin/bin1.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -080081 stripPrefix: "out/target/product/fictional/system/",
82 expectedOut: []string{"bin/bin1"},
83 },
84 {
Colin Cross35f79c32022-01-27 15:18:52 -080085 condition: "firstparty",
86 name: "library",
87 roots: []string{"lib/libd.so.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -080088 stripPrefix: "out/target/product/fictional/system/",
89 expectedOut: []string{"lib/libd.so"},
90 },
91 {
92 condition: "notice",
93 name: "apex",
94 roots: []string{"highest.apex.meta_lic"},
95 expectedOut: []string{
96 "out/target/product/fictional/system/apex/highest.apex",
97 "out/target/product/fictional/system/apex/highest.apex/bin/bin1",
98 "out/target/product/fictional/system/apex/highest.apex/bin/bin2",
99 "out/target/product/fictional/system/apex/highest.apex/lib/liba.so",
100 "out/target/product/fictional/system/apex/highest.apex/lib/libb.so",
101 },
102 },
103 {
104 condition: "notice",
105 name: "container",
106 roots: []string{"container.zip.meta_lic"},
107 expectedOut: []string{
108 "out/target/product/fictional/data/container.zip",
109 "out/target/product/fictional/data/container.zip/bin1",
110 "out/target/product/fictional/data/container.zip/bin2",
111 "out/target/product/fictional/data/container.zip/liba.so",
112 "out/target/product/fictional/data/container.zip/libb.so",
113 },
114 },
115 {
Colin Cross35f79c32022-01-27 15:18:52 -0800116 condition: "notice",
117 name: "application",
118 roots: []string{"application.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800119 expectedOut: []string{"out/target/product/fictional/bin/application"},
120 },
121 {
Colin Cross35f79c32022-01-27 15:18:52 -0800122 condition: "notice",
123 name: "binary",
124 roots: []string{"bin/bin1.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800125 expectedOut: []string{"out/target/product/fictional/system/bin/bin1"},
126 },
127 {
Colin Cross35f79c32022-01-27 15:18:52 -0800128 condition: "notice",
129 name: "library",
130 roots: []string{"lib/libd.so.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800131 expectedOut: []string{"out/target/product/fictional/system/lib/libd.so"},
132 },
133 {
Colin Cross35f79c32022-01-27 15:18:52 -0800134 condition: "reciprocal",
135 name: "apex",
136 roots: []string{"highest.apex.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800137 stripPrefix: "out/target/product/fictional/system/apex/",
138 expectedOut: []string{
139 "highest.apex",
140 "highest.apex/bin/bin1",
141 "highest.apex/bin/bin2",
142 "highest.apex/lib/liba.so",
143 "highest.apex/lib/libb.so",
144 },
145 },
146 {
Colin Cross35f79c32022-01-27 15:18:52 -0800147 condition: "reciprocal",
148 name: "container",
149 roots: []string{"container.zip.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800150 stripPrefix: "out/target/product/fictional/data/",
151 expectedOut: []string{
152 "container.zip",
153 "container.zip/bin1",
154 "container.zip/bin2",
155 "container.zip/liba.so",
156 "container.zip/libb.so",
157 },
158 },
159 {
Colin Cross35f79c32022-01-27 15:18:52 -0800160 condition: "reciprocal",
161 name: "application",
162 roots: []string{"application.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800163 stripPrefix: "out/target/product/fictional/bin/",
164 expectedOut: []string{"application"},
165 },
166 {
Colin Cross35f79c32022-01-27 15:18:52 -0800167 condition: "reciprocal",
168 name: "binary",
169 roots: []string{"bin/bin1.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800170 stripPrefix: "out/target/product/fictional/system/",
171 expectedOut: []string{"bin/bin1"},
172 },
173 {
Colin Cross35f79c32022-01-27 15:18:52 -0800174 condition: "reciprocal",
175 name: "library",
176 roots: []string{"lib/libd.so.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800177 stripPrefix: "out/target/product/fictional/system/",
178 expectedOut: []string{"lib/libd.so"},
179 },
180 {
Colin Cross35f79c32022-01-27 15:18:52 -0800181 condition: "restricted",
182 name: "apex",
183 roots: []string{"highest.apex.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800184 stripPrefix: "out/target/product/fictional/system/apex/",
185 expectedOut: []string{
186 "highest.apex",
187 "highest.apex/bin/bin1",
188 "highest.apex/bin/bin2",
189 "highest.apex/lib/liba.so",
190 "highest.apex/lib/libb.so",
191 },
192 },
193 {
Colin Cross35f79c32022-01-27 15:18:52 -0800194 condition: "restricted",
195 name: "container",
196 roots: []string{"container.zip.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800197 stripPrefix: "out/target/product/fictional/data/",
198 expectedOut: []string{
199 "container.zip",
200 "container.zip/bin1",
201 "container.zip/bin2",
202 "container.zip/liba.so",
203 "container.zip/libb.so",
204 },
205 },
206 {
Colin Cross35f79c32022-01-27 15:18:52 -0800207 condition: "restricted",
208 name: "application",
209 roots: []string{"application.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800210 stripPrefix: "out/target/product/fictional/bin/",
211 expectedOut: []string{"application"},
212 },
213 {
Colin Cross35f79c32022-01-27 15:18:52 -0800214 condition: "restricted",
215 name: "binary",
216 roots: []string{"bin/bin1.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800217 stripPrefix: "out/target/product/fictional/system/",
218 expectedOut: []string{"bin/bin1"},
219 },
220 {
Colin Cross35f79c32022-01-27 15:18:52 -0800221 condition: "restricted",
222 name: "library",
223 roots: []string{"lib/libd.so.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800224 stripPrefix: "out/target/product/fictional/system/",
225 expectedOut: []string{"lib/libd.so"},
226 },
227 {
Colin Cross35f79c32022-01-27 15:18:52 -0800228 condition: "proprietary",
229 name: "apex",
230 roots: []string{"highest.apex.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800231 stripPrefix: "out/target/product/fictional/system/apex/",
232 expectedOut: []string{
233 "highest.apex",
234 "highest.apex/bin/bin1",
235 "highest.apex/bin/bin2",
236 "highest.apex/lib/liba.so",
237 "highest.apex/lib/libb.so",
238 },
239 },
240 {
Colin Cross35f79c32022-01-27 15:18:52 -0800241 condition: "proprietary",
242 name: "container",
243 roots: []string{"container.zip.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800244 stripPrefix: "out/target/product/fictional/data/",
245 expectedOut: []string{
246 "container.zip",
247 "container.zip/bin1",
248 "container.zip/bin2",
249 "container.zip/liba.so",
250 "container.zip/libb.so",
251 },
252 },
253 {
Colin Cross35f79c32022-01-27 15:18:52 -0800254 condition: "proprietary",
255 name: "application",
256 roots: []string{"application.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800257 stripPrefix: "out/target/product/fictional/bin/",
258 expectedOut: []string{"application"},
259 },
260 {
Colin Cross35f79c32022-01-27 15:18:52 -0800261 condition: "proprietary",
262 name: "binary",
263 roots: []string{"bin/bin1.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800264 stripPrefix: "out/target/product/fictional/system/",
265 expectedOut: []string{"bin/bin1"},
266 },
267 {
Colin Cross35f79c32022-01-27 15:18:52 -0800268 condition: "proprietary",
269 name: "library",
270 roots: []string{"lib/libd.so.meta_lic"},
Bob Badour2546feb2022-01-26 20:58:24 -0800271 stripPrefix: "out/target/product/fictional/system/",
272 expectedOut: []string{"lib/libd.so"},
273 },
274 }
275 for _, tt := range tests {
276 t.Run(tt.condition+" "+tt.name, func(t *testing.T) {
277 stdout := &bytes.Buffer{}
278 stderr := &bytes.Buffer{}
279
280 rootFiles := make([]string, 0, len(tt.roots))
281 for _, r := range tt.roots {
282 rootFiles = append(rootFiles, "testdata/"+tt.condition+"/"+r)
283 }
284
Bob Badour682e1ba2022-02-02 15:15:56 -0800285 ctx := context{stdout, stderr, os.DirFS("."), []string{tt.stripPrefix}}
Bob Badour2546feb2022-01-26 20:58:24 -0800286
287 err := billOfMaterials(&ctx, rootFiles...)
288 if err != nil {
Colin Cross179ec3e2022-01-27 15:47:09 -0800289 t.Fatalf("bom: error = %v, stderr = %v", err, stderr)
Bob Badour2546feb2022-01-26 20:58:24 -0800290 return
291 }
292 if stderr.Len() > 0 {
293 t.Errorf("bom: gotStderr = %v, want none", stderr)
294 }
295
296 t.Logf("got stdout: %s", stdout.String())
297
298 t.Logf("want stdout: %s", strings.Join(tt.expectedOut, "\n"))
299
300 out := bufio.NewScanner(stdout)
301 lineno := 0
302 for out.Scan() {
303 line := out.Text()
304 if strings.TrimLeft(line, " ") == "" {
305 continue
306 }
307 if len(tt.expectedOut) <= lineno {
308 t.Errorf("bom: unexpected output at line %d: got %q, want nothing (wanted %d lines)", lineno+1, line, len(tt.expectedOut))
309 } else if tt.expectedOut[lineno] != line {
310 t.Errorf("bom: unexpected output at line %d: got %q, want %q", lineno+1, line, tt.expectedOut[lineno])
311 }
312 lineno++
313 }
314 for ; lineno < len(tt.expectedOut); lineno++ {
315 t.Errorf("bom: missing output line %d: ended early, want %q", lineno+1, tt.expectedOut[lineno])
316 }
317 })
318 }
319}