Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [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 | "bufio" |
| 19 | "bytes" |
Colin Cross | d0f05c9 | 2022-01-27 15:40:29 -0800 | [diff] [blame] | 20 | "fmt" |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 21 | "os" |
| 22 | "strings" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
Colin Cross | d0f05c9 | 2022-01-27 15:40:29 -0800 | [diff] [blame] | 26 | func 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 Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 36 | func 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 45 | condition: "firstparty", |
| 46 | name: "apex", |
| 47 | roots: []string{"highest.apex.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 48 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 58 | condition: "firstparty", |
| 59 | name: "container", |
| 60 | roots: []string{"container.zip.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 61 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 71 | condition: "firstparty", |
| 72 | name: "application", |
| 73 | roots: []string{"application.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 74 | stripPrefix: "out/target/product/fictional/bin/", |
| 75 | expectedOut: []string{"application"}, |
| 76 | }, |
| 77 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 78 | condition: "firstparty", |
| 79 | name: "binary", |
| 80 | roots: []string{"bin/bin1.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 81 | stripPrefix: "out/target/product/fictional/system/", |
| 82 | expectedOut: []string{"bin/bin1"}, |
| 83 | }, |
| 84 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 85 | condition: "firstparty", |
| 86 | name: "library", |
| 87 | roots: []string{"lib/libd.so.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 88 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 116 | condition: "notice", |
| 117 | name: "application", |
| 118 | roots: []string{"application.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 119 | expectedOut: []string{"out/target/product/fictional/bin/application"}, |
| 120 | }, |
| 121 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 122 | condition: "notice", |
| 123 | name: "binary", |
| 124 | roots: []string{"bin/bin1.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 125 | expectedOut: []string{"out/target/product/fictional/system/bin/bin1"}, |
| 126 | }, |
| 127 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 128 | condition: "notice", |
| 129 | name: "library", |
| 130 | roots: []string{"lib/libd.so.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 131 | expectedOut: []string{"out/target/product/fictional/system/lib/libd.so"}, |
| 132 | }, |
| 133 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 134 | condition: "reciprocal", |
| 135 | name: "apex", |
| 136 | roots: []string{"highest.apex.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 137 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 147 | condition: "reciprocal", |
| 148 | name: "container", |
| 149 | roots: []string{"container.zip.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 150 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 160 | condition: "reciprocal", |
| 161 | name: "application", |
| 162 | roots: []string{"application.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 163 | stripPrefix: "out/target/product/fictional/bin/", |
| 164 | expectedOut: []string{"application"}, |
| 165 | }, |
| 166 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 167 | condition: "reciprocal", |
| 168 | name: "binary", |
| 169 | roots: []string{"bin/bin1.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 170 | stripPrefix: "out/target/product/fictional/system/", |
| 171 | expectedOut: []string{"bin/bin1"}, |
| 172 | }, |
| 173 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 174 | condition: "reciprocal", |
| 175 | name: "library", |
| 176 | roots: []string{"lib/libd.so.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 177 | stripPrefix: "out/target/product/fictional/system/", |
| 178 | expectedOut: []string{"lib/libd.so"}, |
| 179 | }, |
| 180 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 181 | condition: "restricted", |
| 182 | name: "apex", |
| 183 | roots: []string{"highest.apex.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 184 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 194 | condition: "restricted", |
| 195 | name: "container", |
| 196 | roots: []string{"container.zip.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 197 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 207 | condition: "restricted", |
| 208 | name: "application", |
| 209 | roots: []string{"application.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 210 | stripPrefix: "out/target/product/fictional/bin/", |
| 211 | expectedOut: []string{"application"}, |
| 212 | }, |
| 213 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 214 | condition: "restricted", |
| 215 | name: "binary", |
| 216 | roots: []string{"bin/bin1.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 217 | stripPrefix: "out/target/product/fictional/system/", |
| 218 | expectedOut: []string{"bin/bin1"}, |
| 219 | }, |
| 220 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 221 | condition: "restricted", |
| 222 | name: "library", |
| 223 | roots: []string{"lib/libd.so.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 224 | stripPrefix: "out/target/product/fictional/system/", |
| 225 | expectedOut: []string{"lib/libd.so"}, |
| 226 | }, |
| 227 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 228 | condition: "proprietary", |
| 229 | name: "apex", |
| 230 | roots: []string{"highest.apex.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 231 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 241 | condition: "proprietary", |
| 242 | name: "container", |
| 243 | roots: []string{"container.zip.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 244 | 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 Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 254 | condition: "proprietary", |
| 255 | name: "application", |
| 256 | roots: []string{"application.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 257 | stripPrefix: "out/target/product/fictional/bin/", |
| 258 | expectedOut: []string{"application"}, |
| 259 | }, |
| 260 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 261 | condition: "proprietary", |
| 262 | name: "binary", |
| 263 | roots: []string{"bin/bin1.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 264 | stripPrefix: "out/target/product/fictional/system/", |
| 265 | expectedOut: []string{"bin/bin1"}, |
| 266 | }, |
| 267 | { |
Colin Cross | 35f79c3 | 2022-01-27 15:18:52 -0800 | [diff] [blame] | 268 | condition: "proprietary", |
| 269 | name: "library", |
| 270 | roots: []string{"lib/libd.so.meta_lic"}, |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 271 | 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 Badour | 682e1ba | 2022-02-02 15:15:56 -0800 | [diff] [blame] | 285 | ctx := context{stdout, stderr, os.DirFS("."), []string{tt.stripPrefix}} |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 286 | |
| 287 | err := billOfMaterials(&ctx, rootFiles...) |
| 288 | if err != nil { |
Colin Cross | 179ec3e | 2022-01-27 15:47:09 -0800 | [diff] [blame] | 289 | t.Fatalf("bom: error = %v, stderr = %v", err, stderr) |
Bob Badour | 2546feb | 2022-01-26 20:58:24 -0800 | [diff] [blame] | 290 | 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 | } |