blob: 6df402cff0c406f984c793f24a6dd30093f7254c [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 Duffin4a2a29c2021-03-09 22:27:13 +000030var sdkFixtureFactory = android.NewFixtureFactory(
Paul Duffinabbf63d2021-03-18 01:47:31 +000031 nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000032 apex.PrepareForTestWithApexBuildComponents,
33 cc.PrepareForTestWithCcDefaultModules,
34 genrule.PrepareForTestWithGenRuleBuildComponents,
35 java.PrepareForTestWithJavaBuildComponents,
36 PrepareForTestWithSdkBuildComponents,
Martin Stjernholm7feceb22020-07-11 04:33:29 +010037
Paul Duffin4a2a29c2021-03-09 22:27:13 +000038 android.FixtureAddTextFile("sdk/tests/Android.bp", `
Colin Cross98be1bb2019-12-13 20:41:13 -080039 apex_key {
40 name: "myapex.key",
41 public_key: "myapex.avbpubkey",
42 private_key: "myapex.pem",
43 }
44
45 android_app_certificate {
46 name: "myapex.cert",
47 certificate: "myapex",
48 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000049 `),
Colin Cross98be1bb2019-12-13 20:41:13 -080050
Paul Duffin4a2a29c2021-03-09 22:27:13 +000051 android.FixtureMergeMockFs(map[string][]byte{
Martin Stjernholmcc776012020-07-07 03:22:21 +010052 "build/make/target/product/security": nil,
53 "apex_manifest.json": nil,
54 "system/sepolicy/apex/myapex-file_contexts": nil,
55 "system/sepolicy/apex/myapex2-file_contexts": nil,
56 "system/sepolicy/apex/mysdkapex-file_contexts": nil,
57 "myapex.avbpubkey": nil,
58 "myapex.pem": nil,
59 "myapex.x509.pem": nil,
60 "myapex.pk8": nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000061 }),
Colin Cross98be1bb2019-12-13 20:41:13 -080062
Paul Duffin4a2a29c2021-03-09 22:27:13 +000063 cc.PrepareForTestOnWindows,
64 android.FixtureModifyConfig(func(config android.Config) {
65 // Add windows as a default disable OS to test behavior when some OS variants
66 // are disabled.
67 config.Targets[android.Windows] = []android.Target{
68 {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
Martin Stjernholm7feceb22020-07-11 04:33:29 +010069 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000070 }),
71)
Martin Stjernholm7feceb22020-07-11 04:33:29 +010072
Paul Duffin4a2a29c2021-03-09 22:27:13 +000073var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
74 android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
75 android.FixtureRegisterWithContext(registerSdkBuildComponents),
76)
Paul Duffin82d90432019-11-30 09:24:33 +000077
Paul Duffin4a2a29c2021-03-09 22:27:13 +000078func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000079 t.Helper()
Paul Duffin4a2a29c2021-03-09 22:27:13 +000080 return sdkFixtureFactory.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp))
Martin Stjernholm7feceb22020-07-11 04:33:29 +010081}
82
Paul Duffin82d90432019-11-30 09:24:33 +000083func testSdkError(t *testing.T, pattern, bp string) {
84 t.Helper()
Paul Duffin4a2a29c2021-03-09 22:27:13 +000085 sdkFixtureFactory.
86 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
87 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +000088}
89
90func ensureListContains(t *testing.T, result []string, expected string) {
91 t.Helper()
92 if !android.InList(expected, result) {
93 t.Errorf("%q is not found in %v", expected, result)
94 }
95}
96
97func pathsToStrings(paths android.Paths) []string {
98 var ret []string
99 for _, p := range paths {
100 ret = append(ret, p.String())
101 }
102 return ret
103}
104
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000105// Analyse the sdk build rules to extract information about what it is doing.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000106//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000107// e.g. find the src/dest pairs from each cp command, the various zip files
108// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000109func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000110 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000111 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000112 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000113 androidBpContents: sdk.GetAndroidBpContentsForTests(),
114 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
115 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000116 }
117
118 buildParams := sdk.BuildParamsForTests()
119 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000120 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000121 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000122 for _, bp := range buildParams {
123 switch bp.Rule.String() {
124 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000125 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000126 // Get destination relative to the snapshot root
127 dest := output.Rel()
128 src := android.NormalizePathForTesting(bp.Input)
129 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000130 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
131 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000132 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
133 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000134 } else {
135 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000136 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000137
138 case repackageZip.String():
139 // Add the destdir to the snapshot contents as that is effectively where
140 // the content of the repackaged zip is copied.
141 dest := bp.Args["destdir"]
142 info.snapshotContents = append(info.snapshotContents, dest)
143
144 case zipFiles.String():
145 // This could be an intermediate zip file and not the actual output zip.
146 // In that case this will be overridden when the rule to merge the zips
147 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000148 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000149
150 case mergeZips.String():
151 // Copy the current outputZip to the intermediateZip.
152 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000153 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000154 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000155 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000156 info.intermediateZip, mergeInput)
157 }
158
159 // Override output zip (which was actually the intermediate zip file) with the actual
160 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000161 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000162
163 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000164 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000165 }
166 }
167
168 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000169 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000170
171 return info
172}
173
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000174// Check the snapshot build rules.
175//
176// Takes a list of functions which check different facets of the snapshot build rules.
177// Allows each test to customize what is checked without duplicating lots of code
178// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000179func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
180 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000181
Paul Duffin1356d8c2020-02-25 19:26:33 +0000182 // The sdk CommonOS variant is always responsible for generating the snapshot.
183 variant := android.CommonOS.Name
184
Paul Duffin981b94b2021-03-11 12:32:12 +0000185 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000186
Paul Duffin36474d32021-03-12 12:19:43 +0000187 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000188
189 // Check state of the snapshot build.
190 for _, checker := range checkers {
191 checker(snapshotBuildInfo)
192 }
193
194 // Make sure that the generated zip file is in the correct place.
195 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000196 if dir != "" {
197 dir = filepath.Clean(dir) + "/"
198 }
Paul Duffin36474d32021-03-12 12:19:43 +0000199 android.AssertStringEquals(t, "Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000200 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000201
202 // Populate a mock filesystem with the files that would have been copied by
203 // the rules.
204 fs := make(map[string][]byte)
205 for _, dest := range snapshotBuildInfo.snapshotContents {
206 fs[dest] = nil
207 }
208
209 // Process the generated bp file to make sure it is valid.
Paul Duffin36474d32021-03-12 12:19:43 +0000210 testSdkWithFs(t, snapshotBuildInfo.androidBpContents, fs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000211}
212
213type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
214
215// Check that the snapshot's generated Android.bp is correct.
216//
217// Both the expected and actual string are both trimmed before comparing.
218func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
219 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000220 info.t.Helper()
221 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000222 }
223}
224
Paul Duffind0759072021-02-17 11:23:00 +0000225// Check that the snapshot's unversioned generated Android.bp is correct.
226//
227// This func should be used to check the general snapshot generation code.
228//
229// Both the expected and actual string are both trimmed before comparing.
230func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
231 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000232 info.t.Helper()
233 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000234 }
235}
236
237// Check that the snapshot's versioned generated Android.bp is correct.
238//
239// This func should only be used to check the version specific snapshot generation code,
240// i.e. the encoding of version into module names and the generation of the _snapshot module. The
241// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
242// func.
243//
244// Both the expected and actual string are both trimmed before comparing.
245func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
246 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000247 info.t.Helper()
248 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000249 }
250}
251
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000252// Check that the snapshot's copy rules are correct.
253//
254// The copy rules are formatted as <src> -> <dest>, one per line and then compared
255// to the supplied expected string. Both the expected and actual string are trimmed
256// before comparing.
257func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
258 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000259 info.t.Helper()
260 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000261 }
262}
263
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000264func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
265 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000266 info.t.Helper()
267 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000268 }
269}
270
Paul Duffin3d1248c2020-04-09 00:10:17 +0100271// Check that the specified paths match the list of zips to merge with the intermediate zip.
272func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000273 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000274 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000275 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000276 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000277 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100278
Paul Duffin36474d32021-03-12 12:19:43 +0000279 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000280 }
281}
282
283// Encapsulates information about the snapshot build structure in order to insulate tests from
284// knowing too much about internal structures.
285//
286// All source/input paths are relative either the build directory. All dest/output paths are
287// relative to the snapshot root directory.
288type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000289 t *testing.T
290
291 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000292 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000293
294 // The contents of the generated Android.bp file
295 androidBpContents string
296
Paul Duffind0759072021-02-17 11:23:00 +0000297 // The contents of the unversioned Android.bp file
298 androidUnversionedBpContents string
299
300 // The contents of the versioned Android.bp file
301 androidVersionedBpContents string
302
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000303 // The paths, relative to the snapshot root, of all files and directories copied into the
304 // snapshot.
305 snapshotContents []string
306
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000307 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
308 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000309 copyRules string
310
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000311 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
312 // per line, of the format src -> dest
313 otherCopyRules string
314
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000315 // The path to the intermediate zip, which is a zip created from the source files copied
316 // into the snapshot directory and which will be merged with other zips to form the final output.
317 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
318 intermediateZip string
319
320 // The paths to the zips to merge into the output zip, does not include the intermediate
321 // zip.
322 mergeZips []string
323
324 // The final output zip.
325 outputZip string
326}