blob: ba40f672be08d1909fd68b76e68c192ab3828a64 [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 "apex_manifest.json": nil,
48 "system/sepolicy/apex/myapex-file_contexts": nil,
49 "system/sepolicy/apex/myapex2-file_contexts": nil,
50 "system/sepolicy/apex/mysdkapex-file_contexts": nil,
51 "myapex.avbpubkey": nil,
52 "myapex.pem": nil,
53 "myapex.x509.pem": nil,
54 "myapex.pk8": nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000055 }),
Paul Duffincf3ee2f2021-03-15 13:08:51 +000056)
57
58// Legacy preparer used for running tests within the sdk package.
59//
60// This includes everything that was needed to run any test in the sdk package prior to the
61// introduction of the test fixtures. Tests that are being converted to use fixtures directly
62// rather than through the testSdkError() and testSdkWithFs() methods should avoid using this and
63// instead should use the various preparers directly using android.GroupFixturePreparers(...) to
64// group them when necessary.
65//
66// deprecated
67var prepareForSdkTest = android.GroupFixturePreparers(
68 cc.PrepareForTestWithCcDefaultModules,
69 genrule.PrepareForTestWithGenRuleBuildComponents,
70 java.PrepareForTestWithJavaBuildComponents,
71 PrepareForTestWithSdkBuildComponents,
72
73 prepareForSdkTestWithApex,
Colin Cross98be1bb2019-12-13 20:41:13 -080074
Paul Duffin4a2a29c2021-03-09 22:27:13 +000075 cc.PrepareForTestOnWindows,
76 android.FixtureModifyConfig(func(config android.Config) {
77 // Add windows as a default disable OS to test behavior when some OS variants
78 // are disabled.
79 config.Targets[android.Windows] = []android.Target{
80 {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
Martin Stjernholm7feceb22020-07-11 04:33:29 +010081 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000082 }),
83)
Martin Stjernholm7feceb22020-07-11 04:33:29 +010084
Paul Duffin4a2a29c2021-03-09 22:27:13 +000085var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
86 android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
87 android.FixtureRegisterWithContext(registerSdkBuildComponents),
88)
Paul Duffin82d90432019-11-30 09:24:33 +000089
Paul Duffin4a2a29c2021-03-09 22:27:13 +000090func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000091 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +000092 return prepareForSdkTest.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp))
Martin Stjernholm7feceb22020-07-11 04:33:29 +010093}
94
Paul Duffin82d90432019-11-30 09:24:33 +000095func testSdkError(t *testing.T, pattern, bp string) {
96 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +000097 prepareForSdkTest.
Paul Duffin4a2a29c2021-03-09 22:27:13 +000098 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
99 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +0000100}
101
102func ensureListContains(t *testing.T, result []string, expected string) {
103 t.Helper()
104 if !android.InList(expected, result) {
105 t.Errorf("%q is not found in %v", expected, result)
106 }
107}
108
109func pathsToStrings(paths android.Paths) []string {
110 var ret []string
111 for _, p := range paths {
112 ret = append(ret, p.String())
113 }
114 return ret
115}
116
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000117// Analyse the sdk build rules to extract information about what it is doing.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000118//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000119// e.g. find the src/dest pairs from each cp command, the various zip files
120// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000121func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000122 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000123 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000124 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000125 androidBpContents: sdk.GetAndroidBpContentsForTests(),
126 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
127 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000128 }
129
130 buildParams := sdk.BuildParamsForTests()
131 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000132 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000133 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000134 for _, bp := range buildParams {
135 switch bp.Rule.String() {
136 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000137 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000138 // Get destination relative to the snapshot root
139 dest := output.Rel()
140 src := android.NormalizePathForTesting(bp.Input)
141 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000142 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
143 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000144 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
145 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000146 } else {
147 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000148 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000149
150 case repackageZip.String():
151 // Add the destdir to the snapshot contents as that is effectively where
152 // the content of the repackaged zip is copied.
153 dest := bp.Args["destdir"]
154 info.snapshotContents = append(info.snapshotContents, dest)
155
156 case zipFiles.String():
157 // This could be an intermediate zip file and not the actual output zip.
158 // In that case this will be overridden when the rule to merge the zips
159 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000160 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000161
162 case mergeZips.String():
163 // Copy the current outputZip to the intermediateZip.
164 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000165 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000166 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000167 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000168 info.intermediateZip, mergeInput)
169 }
170
171 // Override output zip (which was actually the intermediate zip file) with the actual
172 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000173 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000174
175 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000176 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000177 }
178 }
179
180 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000181 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000182
183 return info
184}
185
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000186// Check the snapshot build rules.
187//
188// Takes a list of functions which check different facets of the snapshot build rules.
189// Allows each test to customize what is checked without duplicating lots of code
190// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000191func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
192 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000193
Paul Duffin1356d8c2020-02-25 19:26:33 +0000194 // The sdk CommonOS variant is always responsible for generating the snapshot.
195 variant := android.CommonOS.Name
196
Paul Duffin981b94b2021-03-11 12:32:12 +0000197 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000198
Paul Duffin36474d32021-03-12 12:19:43 +0000199 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000200
201 // Check state of the snapshot build.
202 for _, checker := range checkers {
203 checker(snapshotBuildInfo)
204 }
205
206 // Make sure that the generated zip file is in the correct place.
207 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000208 if dir != "" {
209 dir = filepath.Clean(dir) + "/"
210 }
Paul Duffin36474d32021-03-12 12:19:43 +0000211 android.AssertStringEquals(t, "Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000212 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000213
214 // Populate a mock filesystem with the files that would have been copied by
215 // the rules.
Paul Duffinc93c98e2021-03-20 01:32:50 +0000216 fs := android.MockFS{}
217 snapshotSubDir := "snapshot"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000218 for _, dest := range snapshotBuildInfo.snapshotContents {
Paul Duffinc93c98e2021-03-20 01:32:50 +0000219 fs[filepath.Join(snapshotSubDir, dest)] = nil
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000220 }
Paul Duffinc93c98e2021-03-20 01:32:50 +0000221 fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000222
Paul Duffinc93c98e2021-03-20 01:32:50 +0000223 preparer := result.Preparer()
224
225 // Process the generated bp file to make sure it is valid. Use the same preparer as was used to
226 // produce this result.
227 t.Run("snapshot without source", func(t *testing.T) {
228 android.GroupFixturePreparers(
229 preparer,
230 // TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the
231 // files the snapshot needs are actually copied into the snapshot.
232
233 // Add the files (including bp) created for this snapshot to the test fixture.
234 fs.AddToFixture(),
235
236 // Remove the source Android.bp file to make sure it works without.
237 // TODO(b/183184375): Add a test with the source.
238 android.FixtureModifyMockFS(func(fs android.MockFS) {
239 delete(fs, "Android.bp")
240 }),
241 ).RunTest(t)
242 })
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000243}
244
245type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
246
247// Check that the snapshot's generated Android.bp is correct.
248//
249// Both the expected and actual string are both trimmed before comparing.
250func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
251 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000252 info.t.Helper()
253 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000254 }
255}
256
Paul Duffind0759072021-02-17 11:23:00 +0000257// Check that the snapshot's unversioned generated Android.bp is correct.
258//
259// This func should be used to check the general snapshot generation code.
260//
261// Both the expected and actual string are both trimmed before comparing.
262func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
263 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000264 info.t.Helper()
265 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000266 }
267}
268
269// Check that the snapshot's versioned generated Android.bp is correct.
270//
271// This func should only be used to check the version specific snapshot generation code,
272// i.e. the encoding of version into module names and the generation of the _snapshot module. The
273// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
274// func.
275//
276// Both the expected and actual string are both trimmed before comparing.
277func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
278 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000279 info.t.Helper()
280 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000281 }
282}
283
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000284// Check that the snapshot's copy rules are correct.
285//
286// The copy rules are formatted as <src> -> <dest>, one per line and then compared
287// to the supplied expected string. Both the expected and actual string are trimmed
288// before comparing.
289func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
290 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000291 info.t.Helper()
292 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000293 }
294}
295
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000296func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
297 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000298 info.t.Helper()
299 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000300 }
301}
302
Paul Duffin3d1248c2020-04-09 00:10:17 +0100303// Check that the specified paths match the list of zips to merge with the intermediate zip.
304func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000305 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000306 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000307 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000308 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000309 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100310
Paul Duffin36474d32021-03-12 12:19:43 +0000311 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000312 }
313}
314
315// Encapsulates information about the snapshot build structure in order to insulate tests from
316// knowing too much about internal structures.
317//
318// All source/input paths are relative either the build directory. All dest/output paths are
319// relative to the snapshot root directory.
320type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000321 t *testing.T
322
323 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000324 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000325
326 // The contents of the generated Android.bp file
327 androidBpContents string
328
Paul Duffind0759072021-02-17 11:23:00 +0000329 // The contents of the unversioned Android.bp file
330 androidUnversionedBpContents string
331
332 // The contents of the versioned Android.bp file
333 androidVersionedBpContents string
334
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000335 // The paths, relative to the snapshot root, of all files and directories copied into the
336 // snapshot.
337 snapshotContents []string
338
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000339 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
340 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000341 copyRules string
342
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000343 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
344 // per line, of the format src -> dest
345 otherCopyRules string
346
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000347 // The path to the intermediate zip, which is a zip created from the source files copied
348 // into the snapshot directory and which will be merged with other zips to form the final output.
349 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
350 intermediateZip string
351
352 // The paths to the zips to merge into the output zip, does not include the intermediate
353 // zip.
354 mergeZips []string
355
356 // The final output zip.
357 outputZip string
358}