blob: d21f425abb47edb80f59fa560a44d55b9fc83537 [file] [log] [blame]
Paul Duffin82d90432019-11-30 09:24:33 +00001// 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
15package sdk
16
17import (
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000018 "fmt"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000019 "path/filepath"
Paul Duffin82d90432019-11-30 09:24:33 +000020 "strings"
21 "testing"
22
23 "android/soong/android"
24 "android/soong/apex"
25 "android/soong/cc"
Paul Duffind6ceb862021-03-04 23:02:31 +000026 "android/soong/genrule"
Paul Duffin82d90432019-11-30 09:24:33 +000027 "android/soong/java"
28)
29
Paul Duffincf3ee2f2021-03-15 13:08:51 +000030// Prepare for running an sdk test with an apex.
31var prepareForSdkTestWithApex = android.GroupFixturePreparers(
Paul Duffin4a2a29c2021-03-09 22:27:13 +000032 apex.PrepareForTestWithApexBuildComponents,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000033 android.FixtureAddTextFile("sdk/tests/Android.bp", `
Colin Cross98be1bb2019-12-13 20:41:13 -080034 apex_key {
35 name: "myapex.key",
36 public_key: "myapex.avbpubkey",
37 private_key: "myapex.pem",
38 }
39
40 android_app_certificate {
41 name: "myapex.cert",
42 certificate: "myapex",
43 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000044 `),
Colin Cross98be1bb2019-12-13 20:41:13 -080045
Paul Duffin4a2a29c2021-03-09 22:27:13 +000046 android.FixtureMergeMockFs(map[string][]byte{
Martin Stjernholmcc776012020-07-07 03:22:21 +010047 "build/make/target/product/security": nil,
48 "apex_manifest.json": nil,
49 "system/sepolicy/apex/myapex-file_contexts": nil,
50 "system/sepolicy/apex/myapex2-file_contexts": nil,
51 "system/sepolicy/apex/mysdkapex-file_contexts": nil,
52 "myapex.avbpubkey": nil,
53 "myapex.pem": nil,
54 "myapex.x509.pem": nil,
55 "myapex.pk8": nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000056 }),
Paul Duffincf3ee2f2021-03-15 13:08:51 +000057)
58
59// Legacy preparer used for running tests within the sdk package.
60//
61// This includes everything that was needed to run any test in the sdk package prior to the
62// introduction of the test fixtures. Tests that are being converted to use fixtures directly
63// rather than through the testSdkError() and testSdkWithFs() methods should avoid using this and
64// instead should use the various preparers directly using android.GroupFixturePreparers(...) to
65// group them when necessary.
66//
67// deprecated
68var prepareForSdkTest = android.GroupFixturePreparers(
69 cc.PrepareForTestWithCcDefaultModules,
70 genrule.PrepareForTestWithGenRuleBuildComponents,
71 java.PrepareForTestWithJavaBuildComponents,
72 PrepareForTestWithSdkBuildComponents,
73
74 prepareForSdkTestWithApex,
Colin Cross98be1bb2019-12-13 20:41:13 -080075
Paul Duffin4a2a29c2021-03-09 22:27:13 +000076 cc.PrepareForTestOnWindows,
77 android.FixtureModifyConfig(func(config android.Config) {
78 // Add windows as a default disable OS to test behavior when some OS variants
79 // are disabled.
80 config.Targets[android.Windows] = []android.Target{
81 {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
Martin Stjernholm7feceb22020-07-11 04:33:29 +010082 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000083 }),
84)
Martin Stjernholm7feceb22020-07-11 04:33:29 +010085
Paul Duffin4a2a29c2021-03-09 22:27:13 +000086var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
87 android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
88 android.FixtureRegisterWithContext(registerSdkBuildComponents),
89)
Paul Duffin82d90432019-11-30 09:24:33 +000090
Paul Duffin4a2a29c2021-03-09 22:27:13 +000091func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000092 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +000093 return prepareForSdkTest.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp))
Martin Stjernholm7feceb22020-07-11 04:33:29 +010094}
95
Paul Duffin82d90432019-11-30 09:24:33 +000096func testSdkError(t *testing.T, pattern, bp string) {
97 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +000098 prepareForSdkTest.
Paul Duffin4a2a29c2021-03-09 22:27:13 +000099 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
100 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +0000101}
102
103func ensureListContains(t *testing.T, result []string, expected string) {
104 t.Helper()
105 if !android.InList(expected, result) {
106 t.Errorf("%q is not found in %v", expected, result)
107 }
108}
109
110func pathsToStrings(paths android.Paths) []string {
111 var ret []string
112 for _, p := range paths {
113 ret = append(ret, p.String())
114 }
115 return ret
116}
117
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000118// Analyse the sdk build rules to extract information about what it is doing.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000119//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000120// e.g. find the src/dest pairs from each cp command, the various zip files
121// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000122func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000123 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000124 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000125 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000126 androidBpContents: sdk.GetAndroidBpContentsForTests(),
127 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
128 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000129 }
130
131 buildParams := sdk.BuildParamsForTests()
132 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000133 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000134 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000135 for _, bp := range buildParams {
136 switch bp.Rule.String() {
137 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000138 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000139 // Get destination relative to the snapshot root
140 dest := output.Rel()
141 src := android.NormalizePathForTesting(bp.Input)
142 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000143 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
144 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000145 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
146 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000147 } else {
148 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000149 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000150
151 case repackageZip.String():
152 // Add the destdir to the snapshot contents as that is effectively where
153 // the content of the repackaged zip is copied.
154 dest := bp.Args["destdir"]
155 info.snapshotContents = append(info.snapshotContents, dest)
156
157 case zipFiles.String():
158 // This could be an intermediate zip file and not the actual output zip.
159 // In that case this will be overridden when the rule to merge the zips
160 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000161 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000162
163 case mergeZips.String():
164 // Copy the current outputZip to the intermediateZip.
165 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000166 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000167 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000168 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000169 info.intermediateZip, mergeInput)
170 }
171
172 // Override output zip (which was actually the intermediate zip file) with the actual
173 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000174 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000175
176 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000177 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000178 }
179 }
180
181 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000182 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000183
184 return info
185}
186
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000187// Check the snapshot build rules.
188//
189// Takes a list of functions which check different facets of the snapshot build rules.
190// Allows each test to customize what is checked without duplicating lots of code
191// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000192func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
193 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000194
Paul Duffin1356d8c2020-02-25 19:26:33 +0000195 // The sdk CommonOS variant is always responsible for generating the snapshot.
196 variant := android.CommonOS.Name
197
Paul Duffin981b94b2021-03-11 12:32:12 +0000198 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000199
Paul Duffin36474d32021-03-12 12:19:43 +0000200 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000201
202 // Check state of the snapshot build.
203 for _, checker := range checkers {
204 checker(snapshotBuildInfo)
205 }
206
207 // Make sure that the generated zip file is in the correct place.
208 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000209 if dir != "" {
210 dir = filepath.Clean(dir) + "/"
211 }
Paul Duffin36474d32021-03-12 12:19:43 +0000212 android.AssertStringEquals(t, "Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000213 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000214
215 // Populate a mock filesystem with the files that would have been copied by
216 // the rules.
Paul Duffinc93c98e2021-03-20 01:32:50 +0000217 fs := android.MockFS{}
218 snapshotSubDir := "snapshot"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000219 for _, dest := range snapshotBuildInfo.snapshotContents {
Paul Duffinc93c98e2021-03-20 01:32:50 +0000220 fs[filepath.Join(snapshotSubDir, dest)] = nil
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000221 }
Paul Duffinc93c98e2021-03-20 01:32:50 +0000222 fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000223
Paul Duffinc93c98e2021-03-20 01:32:50 +0000224 preparer := result.Preparer()
225
226 // Process the generated bp file to make sure it is valid. Use the same preparer as was used to
227 // produce this result.
228 t.Run("snapshot without source", func(t *testing.T) {
229 android.GroupFixturePreparers(
230 preparer,
231 // TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the
232 // files the snapshot needs are actually copied into the snapshot.
233
234 // Add the files (including bp) created for this snapshot to the test fixture.
235 fs.AddToFixture(),
236
237 // Remove the source Android.bp file to make sure it works without.
238 // TODO(b/183184375): Add a test with the source.
239 android.FixtureModifyMockFS(func(fs android.MockFS) {
240 delete(fs, "Android.bp")
241 }),
242 ).RunTest(t)
243 })
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000244}
245
246type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
247
248// Check that the snapshot's generated Android.bp is correct.
249//
250// Both the expected and actual string are both trimmed before comparing.
251func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
252 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000253 info.t.Helper()
254 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000255 }
256}
257
Paul Duffind0759072021-02-17 11:23:00 +0000258// Check that the snapshot's unversioned generated Android.bp is correct.
259//
260// This func should be used to check the general snapshot generation code.
261//
262// Both the expected and actual string are both trimmed before comparing.
263func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
264 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000265 info.t.Helper()
266 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000267 }
268}
269
270// Check that the snapshot's versioned generated Android.bp is correct.
271//
272// This func should only be used to check the version specific snapshot generation code,
273// i.e. the encoding of version into module names and the generation of the _snapshot module. The
274// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
275// func.
276//
277// Both the expected and actual string are both trimmed before comparing.
278func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
279 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000280 info.t.Helper()
281 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000282 }
283}
284
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000285// Check that the snapshot's copy rules are correct.
286//
287// The copy rules are formatted as <src> -> <dest>, one per line and then compared
288// to the supplied expected string. Both the expected and actual string are trimmed
289// before comparing.
290func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
291 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000292 info.t.Helper()
293 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000294 }
295}
296
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000297func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
298 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000299 info.t.Helper()
300 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000301 }
302}
303
Paul Duffin3d1248c2020-04-09 00:10:17 +0100304// Check that the specified paths match the list of zips to merge with the intermediate zip.
305func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000306 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000307 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000308 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000309 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000310 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100311
Paul Duffin36474d32021-03-12 12:19:43 +0000312 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000313 }
314}
315
316// Encapsulates information about the snapshot build structure in order to insulate tests from
317// knowing too much about internal structures.
318//
319// All source/input paths are relative either the build directory. All dest/output paths are
320// relative to the snapshot root directory.
321type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000322 t *testing.T
323
324 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000325 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000326
327 // The contents of the generated Android.bp file
328 androidBpContents string
329
Paul Duffind0759072021-02-17 11:23:00 +0000330 // The contents of the unversioned Android.bp file
331 androidUnversionedBpContents string
332
333 // The contents of the versioned Android.bp file
334 androidVersionedBpContents string
335
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000336 // The paths, relative to the snapshot root, of all files and directories copied into the
337 // snapshot.
338 snapshotContents []string
339
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000340 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
341 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000342 copyRules string
343
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000344 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
345 // per line, of the format src -> dest
346 otherCopyRules string
347
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000348 // The path to the intermediate zip, which is a zip created from the source files copied
349 // into the snapshot directory and which will be merged with other zips to form the final output.
350 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
351 intermediateZip string
352
353 // The paths to the zips to merge into the output zip, does not include the intermediate
354 // zip.
355 mergeZips []string
356
357 // The final output zip.
358 outputZip string
359}