Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 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 sdk |
| 16 | |
| 17 | import ( |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 21 | "path/filepath" |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 22 | "strings" |
| 23 | "testing" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | "android/soong/apex" |
| 27 | "android/soong/cc" |
| 28 | "android/soong/java" |
| 29 | ) |
| 30 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 31 | func testSdkContext(bp string, fs map[string][]byte) (*android.TestContext, android.Config) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 32 | bp = bp + ` |
| 33 | apex_key { |
| 34 | name: "myapex.key", |
| 35 | public_key: "myapex.avbpubkey", |
| 36 | private_key: "myapex.pem", |
| 37 | } |
| 38 | |
| 39 | android_app_certificate { |
| 40 | name: "myapex.cert", |
| 41 | certificate: "myapex", |
| 42 | } |
| 43 | ` + cc.GatherRequiredDepsForTest(android.Android) |
| 44 | |
| 45 | mockFS := map[string][]byte{ |
| 46 | "build/make/target/product/security": nil, |
| 47 | "apex_manifest.json": nil, |
| 48 | "system/sepolicy/apex/myapex-file_contexts": nil, |
| 49 | "system/sepolicy/apex/myapex2-file_contexts": nil, |
| 50 | "myapex.avbpubkey": nil, |
| 51 | "myapex.pem": nil, |
| 52 | "myapex.x509.pem": nil, |
| 53 | "myapex.pk8": nil, |
| 54 | } |
| 55 | |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 56 | cc.GatherRequiredFilesForTest(mockFS) |
| 57 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 58 | for k, v := range fs { |
| 59 | mockFS[k] = v |
| 60 | } |
| 61 | |
| 62 | config := android.TestArchConfig(buildDir, nil, bp, mockFS) |
| 63 | |
Paul Duffin | 08798aa | 2020-02-27 13:12:46 +0000 | [diff] [blame] | 64 | // Add windows as a default disable OS to test behavior when some OS variants |
| 65 | // are disabled. |
| 66 | config.Targets[android.Windows] = []android.Target{ |
| 67 | {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", ""}, |
| 68 | } |
| 69 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 70 | ctx := android.NewTestArchContext() |
| 71 | |
| 72 | // from android package |
Paul Duffin | c132742 | 2020-01-14 12:15:29 +0000 | [diff] [blame] | 73 | android.RegisterPackageBuildComponents(ctx) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 74 | ctx.PreArchMutators(android.RegisterVisibilityRuleChecker) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 75 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 76 | ctx.PreArchMutators(android.RegisterVisibilityRuleGatherer) |
| 77 | ctx.PostDepsMutators(android.RegisterVisibilityRuleEnforcer) |
| 78 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 79 | // from java package |
Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 80 | java.RegisterJavaBuildComponents(ctx) |
| 81 | java.RegisterAppBuildComponents(ctx) |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 82 | java.RegisterStubsBuildComponents(ctx) |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 83 | java.RegisterSystemModulesBuildComponents(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 84 | |
| 85 | // from cc package |
Paul Duffin | 77980a8 | 2019-12-19 16:01:36 +0000 | [diff] [blame] | 86 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 87 | |
| 88 | // from apex package |
| 89 | ctx.RegisterModuleType("apex", apex.BundleFactory) |
| 90 | ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory) |
| 91 | ctx.PostDepsMutators(apex.RegisterPostDepsMutators) |
| 92 | |
| 93 | // from this package |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 94 | ctx.RegisterModuleType("sdk", SdkModuleFactory) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 95 | ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory) |
Paul Duffin | 8150da6 | 2019-12-16 17:21:27 +0000 | [diff] [blame] | 96 | ctx.RegisterModuleType("module_exports", ModuleExportsFactory) |
| 97 | ctx.RegisterModuleType("module_exports_snapshot", ModuleExportsSnapshotsFactory) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 98 | ctx.PreDepsMutators(RegisterPreDepsMutators) |
| 99 | ctx.PostDepsMutators(RegisterPostDepsMutators) |
| 100 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 101 | ctx.Register(config) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 102 | |
| 103 | return ctx, config |
| 104 | } |
| 105 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 106 | func testSdkWithFs(t *testing.T, bp string, fs map[string][]byte) *testSdkResult { |
| 107 | t.Helper() |
| 108 | ctx, config := testSdkContext(bp, fs) |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 109 | _, errs := ctx.ParseBlueprintsFiles(".") |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 110 | android.FailIfErrored(t, errs) |
| 111 | _, errs = ctx.PrepareBuildActions(config) |
| 112 | android.FailIfErrored(t, errs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 113 | return &testSdkResult{ |
| 114 | TestHelper: TestHelper{t: t}, |
| 115 | ctx: ctx, |
| 116 | config: config, |
| 117 | } |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | func testSdkError(t *testing.T, pattern, bp string) { |
| 121 | t.Helper() |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 122 | ctx, config := testSdkContext(bp, nil) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 123 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 124 | if len(errs) > 0 { |
| 125 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 126 | return |
| 127 | } |
| 128 | _, errs = ctx.PrepareBuildActions(config) |
| 129 | if len(errs) > 0 { |
| 130 | android.FailIfNoMatchingErrors(t, pattern, errs) |
| 131 | return |
| 132 | } |
| 133 | |
| 134 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
| 135 | } |
| 136 | |
| 137 | func ensureListContains(t *testing.T, result []string, expected string) { |
| 138 | t.Helper() |
| 139 | if !android.InList(expected, result) { |
| 140 | t.Errorf("%q is not found in %v", expected, result) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | func pathsToStrings(paths android.Paths) []string { |
| 145 | var ret []string |
| 146 | for _, p := range paths { |
| 147 | ret = append(ret, p.String()) |
| 148 | } |
| 149 | return ret |
| 150 | } |
| 151 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 152 | // Provides general test support. |
| 153 | type TestHelper struct { |
| 154 | t *testing.T |
| 155 | } |
| 156 | |
| 157 | func (h *TestHelper) AssertStringEquals(message string, expected string, actual string) { |
| 158 | h.t.Helper() |
| 159 | if actual != expected { |
| 160 | h.t.Errorf("%s: expected %s, actual %s", message, expected, actual) |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 164 | func (h *TestHelper) AssertTrimmedStringEquals(message string, expected string, actual string) { |
| 165 | h.t.Helper() |
| 166 | h.AssertStringEquals(message, strings.TrimSpace(expected), strings.TrimSpace(actual)) |
| 167 | } |
| 168 | |
| 169 | // Encapsulates result of processing an SDK definition. Provides support for |
| 170 | // checking the state of the build structures. |
| 171 | type testSdkResult struct { |
| 172 | TestHelper |
| 173 | ctx *android.TestContext |
| 174 | config android.Config |
| 175 | } |
| 176 | |
| 177 | // Analyse the sdk build rules to extract information about what it is doing. |
| 178 | |
| 179 | // e.g. find the src/dest pairs from each cp command, the various zip files |
| 180 | // generated, etc. |
| 181 | func (r *testSdkResult) getSdkSnapshotBuildInfo(sdk *sdk) *snapshotBuildInfo { |
| 182 | androidBpContents := strings.NewReplacer("\\n", "\n").Replace(sdk.GetAndroidBpContentsForTests()) |
| 183 | |
| 184 | info := &snapshotBuildInfo{ |
| 185 | r: r, |
| 186 | androidBpContents: androidBpContents, |
| 187 | } |
| 188 | |
| 189 | buildParams := sdk.BuildParamsForTests() |
| 190 | copyRules := &strings.Builder{} |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 191 | snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/" |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 192 | for _, bp := range buildParams { |
| 193 | switch bp.Rule.String() { |
| 194 | case android.Cp.String(): |
Paul Duffin | e1ddcc9 | 2020-03-03 16:01:26 +0000 | [diff] [blame] | 195 | output := bp.Output |
| 196 | // Only check copies into the snapshot directory. |
| 197 | if strings.HasPrefix(output.String(), snapshotDirPrefix) { |
| 198 | // Get source relative to build directory. |
| 199 | src := android.NormalizePathForTesting(bp.Input) |
| 200 | // Get destination relative to the snapshot root |
| 201 | dest := output.Rel() |
| 202 | _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest) |
| 203 | info.snapshotContents = append(info.snapshotContents, dest) |
| 204 | } |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 205 | |
| 206 | case repackageZip.String(): |
| 207 | // Add the destdir to the snapshot contents as that is effectively where |
| 208 | // the content of the repackaged zip is copied. |
| 209 | dest := bp.Args["destdir"] |
| 210 | info.snapshotContents = append(info.snapshotContents, dest) |
| 211 | |
| 212 | case zipFiles.String(): |
| 213 | // This could be an intermediate zip file and not the actual output zip. |
| 214 | // In that case this will be overridden when the rule to merge the zips |
| 215 | // is processed. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 216 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 217 | |
| 218 | case mergeZips.String(): |
| 219 | // Copy the current outputZip to the intermediateZip. |
| 220 | info.intermediateZip = info.outputZip |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 221 | mergeInput := android.NormalizePathForTesting(bp.Input) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 222 | if info.intermediateZip != mergeInput { |
| 223 | r.t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead", |
| 224 | info.intermediateZip, mergeInput) |
| 225 | } |
| 226 | |
| 227 | // Override output zip (which was actually the intermediate zip file) with the actual |
| 228 | // output zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 229 | info.outputZip = android.NormalizePathForTesting(bp.Output) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 230 | |
| 231 | // Save the zips to be merged into the intermediate zip. |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 232 | info.mergeZips = android.NormalizePathsForTesting(bp.Inputs) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | info.copyRules = copyRules.String() |
| 237 | |
| 238 | return info |
| 239 | } |
| 240 | |
| 241 | func (r *testSdkResult) Module(name string, variant string) android.Module { |
| 242 | return r.ctx.ModuleForTests(name, variant).Module() |
| 243 | } |
| 244 | |
| 245 | func (r *testSdkResult) ModuleForTests(name string, variant string) android.TestingModule { |
| 246 | return r.ctx.ModuleForTests(name, variant) |
| 247 | } |
| 248 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 249 | // Check the snapshot build rules. |
| 250 | // |
| 251 | // Takes a list of functions which check different facets of the snapshot build rules. |
| 252 | // Allows each test to customize what is checked without duplicating lots of code |
| 253 | // or proliferating check methods of different flavors. |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 254 | func (r *testSdkResult) CheckSnapshot(name string, dir string, checkers ...snapshotBuildInfoChecker) { |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 255 | r.t.Helper() |
| 256 | |
Paul Duffin | 1356d8c | 2020-02-25 19:26:33 +0000 | [diff] [blame] | 257 | // The sdk CommonOS variant is always responsible for generating the snapshot. |
| 258 | variant := android.CommonOS.Name |
| 259 | |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 260 | sdk := r.Module(name, variant).(*sdk) |
| 261 | |
| 262 | snapshotBuildInfo := r.getSdkSnapshotBuildInfo(sdk) |
| 263 | |
| 264 | // Check state of the snapshot build. |
| 265 | for _, checker := range checkers { |
| 266 | checker(snapshotBuildInfo) |
| 267 | } |
| 268 | |
| 269 | // Make sure that the generated zip file is in the correct place. |
| 270 | actual := snapshotBuildInfo.outputZip |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 271 | if dir != "" { |
| 272 | dir = filepath.Clean(dir) + "/" |
| 273 | } |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 274 | r.AssertStringEquals("Snapshot zip file in wrong place", |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 275 | fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual) |
Paul Duffin | c3c5d5e | 2019-11-29 20:45:22 +0000 | [diff] [blame] | 276 | |
| 277 | // Populate a mock filesystem with the files that would have been copied by |
| 278 | // the rules. |
| 279 | fs := make(map[string][]byte) |
| 280 | for _, dest := range snapshotBuildInfo.snapshotContents { |
| 281 | fs[dest] = nil |
| 282 | } |
| 283 | |
| 284 | // Process the generated bp file to make sure it is valid. |
| 285 | testSdkWithFs(r.t, snapshotBuildInfo.androidBpContents, fs) |
| 286 | } |
| 287 | |
| 288 | type snapshotBuildInfoChecker func(info *snapshotBuildInfo) |
| 289 | |
| 290 | // Check that the snapshot's generated Android.bp is correct. |
| 291 | // |
| 292 | // Both the expected and actual string are both trimmed before comparing. |
| 293 | func checkAndroidBpContents(expected string) snapshotBuildInfoChecker { |
| 294 | return func(info *snapshotBuildInfo) { |
| 295 | info.r.t.Helper() |
| 296 | info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Check that the snapshot's copy rules are correct. |
| 301 | // |
| 302 | // The copy rules are formatted as <src> -> <dest>, one per line and then compared |
| 303 | // to the supplied expected string. Both the expected and actual string are trimmed |
| 304 | // before comparing. |
| 305 | func checkAllCopyRules(expected string) snapshotBuildInfoChecker { |
| 306 | return func(info *snapshotBuildInfo) { |
| 307 | info.r.t.Helper() |
| 308 | info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Check that the specified path is in the list of zips to merge with the intermediate zip. |
| 313 | func checkMergeZip(expected string) snapshotBuildInfoChecker { |
| 314 | return func(info *snapshotBuildInfo) { |
| 315 | info.r.t.Helper() |
| 316 | if info.intermediateZip == "" { |
| 317 | info.r.t.Errorf("No intermediate zip file was created") |
| 318 | } |
| 319 | ensureListContains(info.r.t, info.mergeZips, expected) |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Encapsulates information about the snapshot build structure in order to insulate tests from |
| 324 | // knowing too much about internal structures. |
| 325 | // |
| 326 | // All source/input paths are relative either the build directory. All dest/output paths are |
| 327 | // relative to the snapshot root directory. |
| 328 | type snapshotBuildInfo struct { |
| 329 | r *testSdkResult |
| 330 | |
| 331 | // The contents of the generated Android.bp file |
| 332 | androidBpContents string |
| 333 | |
| 334 | // The paths, relative to the snapshot root, of all files and directories copied into the |
| 335 | // snapshot. |
| 336 | snapshotContents []string |
| 337 | |
| 338 | // A formatted representation of the src/dest pairs, one pair per line, of the format |
| 339 | // src -> dest |
| 340 | copyRules string |
| 341 | |
| 342 | // The path to the intermediate zip, which is a zip created from the source files copied |
| 343 | // into the snapshot directory and which will be merged with other zips to form the final output. |
| 344 | // Is am empty string if there is no intermediate zip because there are no zips to merge in. |
| 345 | intermediateZip string |
| 346 | |
| 347 | // The paths to the zips to merge into the output zip, does not include the intermediate |
| 348 | // zip. |
| 349 | mergeZips []string |
| 350 | |
| 351 | // The final output zip. |
| 352 | outputZip string |
| 353 | } |
| 354 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 355 | var buildDir string |
| 356 | |
| 357 | func setUp() { |
| 358 | var err error |
| 359 | buildDir, err = ioutil.TempDir("", "soong_sdk_test") |
| 360 | if err != nil { |
| 361 | panic(err) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | func tearDown() { |
| 366 | _ = os.RemoveAll(buildDir) |
| 367 | } |
| 368 | |
| 369 | func runTestWithBuildDir(m *testing.M) { |
| 370 | run := func() int { |
| 371 | setUp() |
| 372 | defer tearDown() |
| 373 | |
| 374 | return m.Run() |
| 375 | } |
| 376 | |
| 377 | os.Exit(run()) |
| 378 | } |
| 379 | |
| 380 | func SkipIfNotLinux(t *testing.T) { |
| 381 | t.Helper() |
| 382 | if android.BuildOs != android.Linux { |
| 383 | t.Skipf("Skipping as sdk snapshot generation is only supported on %s not %s", android.Linux, android.BuildOs) |
| 384 | } |
| 385 | } |