blob: c319895f531819e3821f8455a65f0bc004489f4c [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.
203 fs := make(map[string][]byte)
204 for _, dest := range snapshotBuildInfo.snapshotContents {
205 fs[dest] = nil
206 }
207
208 // Process the generated bp file to make sure it is valid.
Paul Duffin36474d32021-03-12 12:19:43 +0000209 testSdkWithFs(t, snapshotBuildInfo.androidBpContents, fs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000210}
211
212type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
213
214// Check that the snapshot's generated Android.bp is correct.
215//
216// Both the expected and actual string are both trimmed before comparing.
217func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
218 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000219 info.t.Helper()
220 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000221 }
222}
223
Paul Duffind0759072021-02-17 11:23:00 +0000224// Check that the snapshot's unversioned generated Android.bp is correct.
225//
226// This func should be used to check the general snapshot generation code.
227//
228// Both the expected and actual string are both trimmed before comparing.
229func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
230 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000231 info.t.Helper()
232 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000233 }
234}
235
236// Check that the snapshot's versioned generated Android.bp is correct.
237//
238// This func should only be used to check the version specific snapshot generation code,
239// i.e. the encoding of version into module names and the generation of the _snapshot module. The
240// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
241// func.
242//
243// Both the expected and actual string are both trimmed before comparing.
244func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
245 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000246 info.t.Helper()
247 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000248 }
249}
250
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000251// Check that the snapshot's copy rules are correct.
252//
253// The copy rules are formatted as <src> -> <dest>, one per line and then compared
254// to the supplied expected string. Both the expected and actual string are trimmed
255// before comparing.
256func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
257 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000258 info.t.Helper()
259 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000260 }
261}
262
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000263func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
264 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000265 info.t.Helper()
266 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000267 }
268}
269
Paul Duffin3d1248c2020-04-09 00:10:17 +0100270// Check that the specified paths match the list of zips to merge with the intermediate zip.
271func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000272 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000273 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000274 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000275 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000276 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100277
Paul Duffin36474d32021-03-12 12:19:43 +0000278 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000279 }
280}
281
282// Encapsulates information about the snapshot build structure in order to insulate tests from
283// knowing too much about internal structures.
284//
285// All source/input paths are relative either the build directory. All dest/output paths are
286// relative to the snapshot root directory.
287type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000288 t *testing.T
289
290 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000291 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000292
293 // The contents of the generated Android.bp file
294 androidBpContents string
295
Paul Duffind0759072021-02-17 11:23:00 +0000296 // The contents of the unversioned Android.bp file
297 androidUnversionedBpContents string
298
299 // The contents of the versioned Android.bp file
300 androidVersionedBpContents string
301
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000302 // The paths, relative to the snapshot root, of all files and directories copied into the
303 // snapshot.
304 snapshotContents []string
305
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000306 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
307 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000308 copyRules string
309
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000310 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
311 // per line, of the format src -> dest
312 otherCopyRules string
313
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000314 // The path to the intermediate zip, which is a zip created from the source files copied
315 // into the snapshot directory and which will be merged with other zips to form the final output.
316 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
317 intermediateZip string
318
319 // The paths to the zips to merge into the output zip, does not include the intermediate
320 // zip.
321 mergeZips []string
322
323 // The final output zip.
324 outputZip string
325}