blob: 307b6de3b640a73c19c3b04b60821687df1437be [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 Duffin89648f92021-03-20 00:36:55 +000030var prepareForSdkTest = android.GroupFixturePreparers(
Paul Duffin4a2a29c2021-03-09 22:27:13 +000031 apex.PrepareForTestWithApexBuildComponents,
32 cc.PrepareForTestWithCcDefaultModules,
33 genrule.PrepareForTestWithGenRuleBuildComponents,
34 java.PrepareForTestWithJavaBuildComponents,
35 PrepareForTestWithSdkBuildComponents,
Martin Stjernholm7feceb22020-07-11 04:33:29 +010036
Paul Duffin4a2a29c2021-03-09 22:27:13 +000037 android.FixtureAddTextFile("sdk/tests/Android.bp", `
Colin Cross98be1bb2019-12-13 20:41:13 -080038 apex_key {
39 name: "myapex.key",
40 public_key: "myapex.avbpubkey",
41 private_key: "myapex.pem",
42 }
43
44 android_app_certificate {
45 name: "myapex.cert",
46 certificate: "myapex",
47 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000048 `),
Colin Cross98be1bb2019-12-13 20:41:13 -080049
Paul Duffin4a2a29c2021-03-09 22:27:13 +000050 android.FixtureMergeMockFs(map[string][]byte{
Martin Stjernholmcc776012020-07-07 03:22:21 +010051 "build/make/target/product/security": nil,
52 "apex_manifest.json": nil,
53 "system/sepolicy/apex/myapex-file_contexts": nil,
54 "system/sepolicy/apex/myapex2-file_contexts": nil,
55 "system/sepolicy/apex/mysdkapex-file_contexts": nil,
56 "myapex.avbpubkey": nil,
57 "myapex.pem": nil,
58 "myapex.x509.pem": nil,
59 "myapex.pk8": nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000060 }),
Colin Cross98be1bb2019-12-13 20:41:13 -080061
Paul Duffin4a2a29c2021-03-09 22:27:13 +000062 cc.PrepareForTestOnWindows,
63 android.FixtureModifyConfig(func(config android.Config) {
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, "", "", true},
Martin Stjernholm7feceb22020-07-11 04:33:29 +010068 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000069 }),
70)
Martin Stjernholm7feceb22020-07-11 04:33:29 +010071
Paul Duffin4a2a29c2021-03-09 22:27:13 +000072var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
73 android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
74 android.FixtureRegisterWithContext(registerSdkBuildComponents),
75)
Paul Duffin82d90432019-11-30 09:24:33 +000076
Paul Duffin4a2a29c2021-03-09 22:27:13 +000077func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000078 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +000079 return prepareForSdkTest.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp))
Martin Stjernholm7feceb22020-07-11 04:33:29 +010080}
81
Paul Duffin82d90432019-11-30 09:24:33 +000082func testSdkError(t *testing.T, pattern, bp string) {
83 t.Helper()
Paul Duffin89648f92021-03-20 00:36:55 +000084 prepareForSdkTest.
Paul Duffin4a2a29c2021-03-09 22:27:13 +000085 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
86 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +000087}
88
89func ensureListContains(t *testing.T, result []string, expected string) {
90 t.Helper()
91 if !android.InList(expected, result) {
92 t.Errorf("%q is not found in %v", expected, result)
93 }
94}
95
96func pathsToStrings(paths android.Paths) []string {
97 var ret []string
98 for _, p := range paths {
99 ret = append(ret, p.String())
100 }
101 return ret
102}
103
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000104// Analyse the sdk build rules to extract information about what it is doing.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000105//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000106// e.g. find the src/dest pairs from each cp command, the various zip files
107// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000108func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000109 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000110 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000111 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000112 androidBpContents: sdk.GetAndroidBpContentsForTests(),
113 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
114 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000115 }
116
117 buildParams := sdk.BuildParamsForTests()
118 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000119 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000120 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000121 for _, bp := range buildParams {
122 switch bp.Rule.String() {
123 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000124 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000125 // Get destination relative to the snapshot root
126 dest := output.Rel()
127 src := android.NormalizePathForTesting(bp.Input)
128 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000129 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
130 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000131 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
132 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000133 } else {
134 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000135 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000136
137 case repackageZip.String():
138 // Add the destdir to the snapshot contents as that is effectively where
139 // the content of the repackaged zip is copied.
140 dest := bp.Args["destdir"]
141 info.snapshotContents = append(info.snapshotContents, dest)
142
143 case zipFiles.String():
144 // This could be an intermediate zip file and not the actual output zip.
145 // In that case this will be overridden when the rule to merge the zips
146 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000147 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000148
149 case mergeZips.String():
150 // Copy the current outputZip to the intermediateZip.
151 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000152 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000153 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000154 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000155 info.intermediateZip, mergeInput)
156 }
157
158 // Override output zip (which was actually the intermediate zip file) with the actual
159 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000160 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000161
162 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000163 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000164 }
165 }
166
167 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000168 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000169
170 return info
171}
172
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000173// Check the snapshot build rules.
174//
175// Takes a list of functions which check different facets of the snapshot build rules.
176// Allows each test to customize what is checked without duplicating lots of code
177// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000178func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
179 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000180
Paul Duffin1356d8c2020-02-25 19:26:33 +0000181 // The sdk CommonOS variant is always responsible for generating the snapshot.
182 variant := android.CommonOS.Name
183
Paul Duffin981b94b2021-03-11 12:32:12 +0000184 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000185
Paul Duffin36474d32021-03-12 12:19:43 +0000186 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000187
188 // Check state of the snapshot build.
189 for _, checker := range checkers {
190 checker(snapshotBuildInfo)
191 }
192
193 // Make sure that the generated zip file is in the correct place.
194 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000195 if dir != "" {
196 dir = filepath.Clean(dir) + "/"
197 }
Paul Duffin36474d32021-03-12 12:19:43 +0000198 android.AssertStringEquals(t, "Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000199 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000200
201 // Populate a mock filesystem with the files that would have been copied by
202 // the rules.
Paul Duffinc93c98e2021-03-20 01:32:50 +0000203 fs := android.MockFS{}
204 snapshotSubDir := "snapshot"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000205 for _, dest := range snapshotBuildInfo.snapshotContents {
Paul Duffinc93c98e2021-03-20 01:32:50 +0000206 fs[filepath.Join(snapshotSubDir, dest)] = nil
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000207 }
Paul Duffinc93c98e2021-03-20 01:32:50 +0000208 fs[filepath.Join(snapshotSubDir, "Android.bp")] = []byte(snapshotBuildInfo.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000209
Paul Duffinc93c98e2021-03-20 01:32:50 +0000210 preparer := result.Preparer()
211
212 // Process the generated bp file to make sure it is valid. Use the same preparer as was used to
213 // produce this result.
214 t.Run("snapshot without source", func(t *testing.T) {
215 android.GroupFixturePreparers(
216 preparer,
217 // TODO(b/183184375): Set Config.TestAllowNonExistentPaths = false to verify that all the
218 // files the snapshot needs are actually copied into the snapshot.
219
220 // Add the files (including bp) created for this snapshot to the test fixture.
221 fs.AddToFixture(),
222
223 // Remove the source Android.bp file to make sure it works without.
224 // TODO(b/183184375): Add a test with the source.
225 android.FixtureModifyMockFS(func(fs android.MockFS) {
226 delete(fs, "Android.bp")
227 }),
228 ).RunTest(t)
229 })
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000230}
231
232type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
233
234// Check that the snapshot's generated Android.bp is correct.
235//
236// Both the expected and actual string are both trimmed before comparing.
237func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
238 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000239 info.t.Helper()
240 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000241 }
242}
243
Paul Duffind0759072021-02-17 11:23:00 +0000244// Check that the snapshot's unversioned generated Android.bp is correct.
245//
246// This func should be used to check the general snapshot generation code.
247//
248// Both the expected and actual string are both trimmed before comparing.
249func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
250 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000251 info.t.Helper()
252 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000253 }
254}
255
256// Check that the snapshot's versioned generated Android.bp is correct.
257//
258// This func should only be used to check the version specific snapshot generation code,
259// i.e. the encoding of version into module names and the generation of the _snapshot module. The
260// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
261// func.
262//
263// Both the expected and actual string are both trimmed before comparing.
264func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
265 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000266 info.t.Helper()
267 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000268 }
269}
270
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000271// Check that the snapshot's copy rules are correct.
272//
273// The copy rules are formatted as <src> -> <dest>, one per line and then compared
274// to the supplied expected string. Both the expected and actual string are trimmed
275// before comparing.
276func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
277 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000278 info.t.Helper()
279 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000280 }
281}
282
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000283func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
284 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000285 info.t.Helper()
286 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000287 }
288}
289
Paul Duffin3d1248c2020-04-09 00:10:17 +0100290// Check that the specified paths match the list of zips to merge with the intermediate zip.
291func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000292 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000293 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000294 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000295 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000296 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100297
Paul Duffin36474d32021-03-12 12:19:43 +0000298 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000299 }
300}
301
302// Encapsulates information about the snapshot build structure in order to insulate tests from
303// knowing too much about internal structures.
304//
305// All source/input paths are relative either the build directory. All dest/output paths are
306// relative to the snapshot root directory.
307type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000308 t *testing.T
309
310 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000311 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000312
313 // The contents of the generated Android.bp file
314 androidBpContents string
315
Paul Duffind0759072021-02-17 11:23:00 +0000316 // The contents of the unversioned Android.bp file
317 androidUnversionedBpContents string
318
319 // The contents of the versioned Android.bp file
320 androidVersionedBpContents string
321
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000322 // The paths, relative to the snapshot root, of all files and directories copied into the
323 // snapshot.
324 snapshotContents []string
325
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000326 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
327 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000328 copyRules string
329
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000330 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
331 // per line, of the format src -> dest
332 otherCopyRules string
333
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000334 // The path to the intermediate zip, which is a zip created from the source files copied
335 // into the snapshot directory and which will be merged with other zips to form the final output.
336 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
337 intermediateZip string
338
339 // The paths to the zips to merge into the output zip, does not include the intermediate
340 // zip.
341 mergeZips []string
342
343 // The final output zip.
344 outputZip string
345}