blob: 1fb571fb98f6c98549154ac3d8940d268fedb2c6 [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 Duffin82d90432019-11-30 09:24:33 +000019 "io/ioutil"
20 "os"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000021 "path/filepath"
Paul Duffin82d90432019-11-30 09:24:33 +000022 "strings"
23 "testing"
24
25 "android/soong/android"
26 "android/soong/apex"
27 "android/soong/cc"
Paul Duffind6ceb862021-03-04 23:02:31 +000028 "android/soong/genrule"
Paul Duffin82d90432019-11-30 09:24:33 +000029 "android/soong/java"
30)
31
Paul Duffin4a2a29c2021-03-09 22:27:13 +000032var sdkFixtureFactory = android.NewFixtureFactory(
33 &buildDir,
34 apex.PrepareForTestWithApexBuildComponents,
35 cc.PrepareForTestWithCcDefaultModules,
36 genrule.PrepareForTestWithGenRuleBuildComponents,
37 java.PrepareForTestWithJavaBuildComponents,
38 PrepareForTestWithSdkBuildComponents,
Martin Stjernholm7feceb22020-07-11 04:33:29 +010039
Paul Duffin4a2a29c2021-03-09 22:27:13 +000040 android.FixtureAddTextFile("sdk/tests/Android.bp", `
Colin Cross98be1bb2019-12-13 20:41:13 -080041 apex_key {
42 name: "myapex.key",
43 public_key: "myapex.avbpubkey",
44 private_key: "myapex.pem",
45 }
46
47 android_app_certificate {
48 name: "myapex.cert",
49 certificate: "myapex",
50 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000051 `),
Colin Cross98be1bb2019-12-13 20:41:13 -080052
Paul Duffin4a2a29c2021-03-09 22:27:13 +000053 android.FixtureMergeMockFs(map[string][]byte{
Martin Stjernholmcc776012020-07-07 03:22:21 +010054 "build/make/target/product/security": nil,
55 "apex_manifest.json": nil,
56 "system/sepolicy/apex/myapex-file_contexts": nil,
57 "system/sepolicy/apex/myapex2-file_contexts": nil,
58 "system/sepolicy/apex/mysdkapex-file_contexts": nil,
59 "myapex.avbpubkey": nil,
60 "myapex.pem": nil,
61 "myapex.x509.pem": nil,
62 "myapex.pk8": nil,
Paul Duffin4a2a29c2021-03-09 22:27:13 +000063 }),
Colin Cross98be1bb2019-12-13 20:41:13 -080064
Paul Duffin4a2a29c2021-03-09 22:27:13 +000065 cc.PrepareForTestOnWindows,
66 android.FixtureModifyConfig(func(config android.Config) {
67 // Add windows as a default disable OS to test behavior when some OS variants
68 // are disabled.
69 config.Targets[android.Windows] = []android.Target{
70 {android.Windows, android.Arch{ArchType: android.X86_64}, android.NativeBridgeDisabled, "", "", true},
Martin Stjernholm7feceb22020-07-11 04:33:29 +010071 }
Paul Duffin4a2a29c2021-03-09 22:27:13 +000072 }),
73)
Martin Stjernholm7feceb22020-07-11 04:33:29 +010074
Paul Duffin4a2a29c2021-03-09 22:27:13 +000075var PrepareForTestWithSdkBuildComponents = android.GroupFixturePreparers(
76 android.FixtureRegisterWithContext(registerModuleExportsBuildComponents),
77 android.FixtureRegisterWithContext(registerSdkBuildComponents),
78)
Paul Duffin82d90432019-11-30 09:24:33 +000079
Paul Duffin4a2a29c2021-03-09 22:27:13 +000080func testSdkWithFs(t *testing.T, bp string, fs android.MockFS) *android.TestResult {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +000081 t.Helper()
Paul Duffin4a2a29c2021-03-09 22:27:13 +000082 return sdkFixtureFactory.RunTest(t, fs.AddToFixture(), android.FixtureWithRootAndroidBp(bp))
Martin Stjernholm7feceb22020-07-11 04:33:29 +010083}
84
Paul Duffin82d90432019-11-30 09:24:33 +000085func testSdkError(t *testing.T, pattern, bp string) {
86 t.Helper()
Paul Duffin4a2a29c2021-03-09 22:27:13 +000087 sdkFixtureFactory.
88 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
89 RunTestWithBp(t, bp)
Paul Duffin82d90432019-11-30 09:24:33 +000090}
91
92func ensureListContains(t *testing.T, result []string, expected string) {
93 t.Helper()
94 if !android.InList(expected, result) {
95 t.Errorf("%q is not found in %v", expected, result)
96 }
97}
98
99func pathsToStrings(paths android.Paths) []string {
100 var ret []string
101 for _, p := range paths {
102 ret = append(ret, p.String())
103 }
104 return ret
105}
106
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000107// Analyse the sdk build rules to extract information about what it is doing.
108
109// e.g. find the src/dest pairs from each cp command, the various zip files
110// generated, etc.
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000111func getSdkSnapshotBuildInfo(result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000112 info := &snapshotBuildInfo{
Paul Duffin981b94b2021-03-11 12:32:12 +0000113 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000114 androidBpContents: sdk.GetAndroidBpContentsForTests(),
115 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
116 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000117 }
118
119 buildParams := sdk.BuildParamsForTests()
120 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000121 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000122 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000123 for _, bp := range buildParams {
124 switch bp.Rule.String() {
125 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000126 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000127 // Get destination relative to the snapshot root
128 dest := output.Rel()
129 src := android.NormalizePathForTesting(bp.Input)
130 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000131 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
132 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000133 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
134 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000135 } else {
136 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000137 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000138
139 case repackageZip.String():
140 // Add the destdir to the snapshot contents as that is effectively where
141 // the content of the repackaged zip is copied.
142 dest := bp.Args["destdir"]
143 info.snapshotContents = append(info.snapshotContents, dest)
144
145 case zipFiles.String():
146 // This could be an intermediate zip file and not the actual output zip.
147 // In that case this will be overridden when the rule to merge the zips
148 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000149 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000150
151 case mergeZips.String():
152 // Copy the current outputZip to the intermediateZip.
153 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000154 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000155 if info.intermediateZip != mergeInput {
Paul Duffin981b94b2021-03-11 12:32:12 +0000156 result.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000157 info.intermediateZip, mergeInput)
158 }
159
160 // Override output zip (which was actually the intermediate zip file) with the actual
161 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000162 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000163
164 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000165 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000166 }
167 }
168
169 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000170 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000171
172 return info
173}
174
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000175// Check the snapshot build rules.
176//
177// Takes a list of functions which check different facets of the snapshot build rules.
178// Allows each test to customize what is checked without duplicating lots of code
179// or proliferating check methods of different flavors.
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000180func CheckSnapshot(result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
Paul Duffin981b94b2021-03-11 12:32:12 +0000181 result.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000182
Paul Duffin1356d8c2020-02-25 19:26:33 +0000183 // The sdk CommonOS variant is always responsible for generating the snapshot.
184 variant := android.CommonOS.Name
185
Paul Duffin981b94b2021-03-11 12:32:12 +0000186 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000187
Paul Duffin981b94b2021-03-11 12:32:12 +0000188 snapshotBuildInfo := getSdkSnapshotBuildInfo(result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000189
190 // Check state of the snapshot build.
191 for _, checker := range checkers {
192 checker(snapshotBuildInfo)
193 }
194
195 // Make sure that the generated zip file is in the correct place.
196 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000197 if dir != "" {
198 dir = filepath.Clean(dir) + "/"
199 }
Paul Duffin981b94b2021-03-11 12:32:12 +0000200 result.AssertStringEquals("Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000201 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000202
203 // Populate a mock filesystem with the files that would have been copied by
204 // the rules.
205 fs := make(map[string][]byte)
206 for _, dest := range snapshotBuildInfo.snapshotContents {
207 fs[dest] = nil
208 }
209
210 // Process the generated bp file to make sure it is valid.
Paul Duffin981b94b2021-03-11 12:32:12 +0000211 testSdkWithFs(result.T, snapshotBuildInfo.androidBpContents, fs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000212}
213
214type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
215
216// Check that the snapshot's generated Android.bp is correct.
217//
218// Both the expected and actual string are both trimmed before comparing.
219func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
220 return func(info *snapshotBuildInfo) {
Paul Duffina3cb2b32021-03-10 09:15:22 +0000221 info.r.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000222 info.r.AssertTrimmedStringEquals("Android.bp contents do not match", expected, info.androidBpContents)
223 }
224}
225
Paul Duffind0759072021-02-17 11:23:00 +0000226// Check that the snapshot's unversioned generated Android.bp is correct.
227//
228// This func should be used to check the general snapshot generation code.
229//
230// Both the expected and actual string are both trimmed before comparing.
231func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
232 return func(info *snapshotBuildInfo) {
Paul Duffina3cb2b32021-03-10 09:15:22 +0000233 info.r.Helper()
Paul Duffind0759072021-02-17 11:23:00 +0000234 info.r.AssertTrimmedStringEquals("unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
235 }
236}
237
238// Check that the snapshot's versioned generated Android.bp is correct.
239//
240// This func should only be used to check the version specific snapshot generation code,
241// i.e. the encoding of version into module names and the generation of the _snapshot module. The
242// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
243// func.
244//
245// Both the expected and actual string are both trimmed before comparing.
246func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
247 return func(info *snapshotBuildInfo) {
Paul Duffina3cb2b32021-03-10 09:15:22 +0000248 info.r.Helper()
Paul Duffind0759072021-02-17 11:23:00 +0000249 info.r.AssertTrimmedStringEquals("versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
250 }
251}
252
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000253// Check that the snapshot's copy rules are correct.
254//
255// The copy rules are formatted as <src> -> <dest>, one per line and then compared
256// to the supplied expected string. Both the expected and actual string are trimmed
257// before comparing.
258func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
259 return func(info *snapshotBuildInfo) {
Paul Duffina3cb2b32021-03-10 09:15:22 +0000260 info.r.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000261 info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.copyRules)
262 }
263}
264
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000265func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
266 return func(info *snapshotBuildInfo) {
Paul Duffina3cb2b32021-03-10 09:15:22 +0000267 info.r.Helper()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000268 info.r.AssertTrimmedStringEquals("Incorrect copy rules", expected, info.otherCopyRules)
269 }
270}
271
Paul Duffin3d1248c2020-04-09 00:10:17 +0100272// Check that the specified paths match the list of zips to merge with the intermediate zip.
273func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000274 return func(info *snapshotBuildInfo) {
Paul Duffina3cb2b32021-03-10 09:15:22 +0000275 info.r.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000276 if info.intermediateZip == "" {
Paul Duffina3cb2b32021-03-10 09:15:22 +0000277 info.r.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000278 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100279
280 info.r.AssertDeepEquals("mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000281 }
282}
283
284// Encapsulates information about the snapshot build structure in order to insulate tests from
285// knowing too much about internal structures.
286//
287// All source/input paths are relative either the build directory. All dest/output paths are
288// relative to the snapshot root directory.
289type snapshotBuildInfo struct {
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000290 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000291
292 // The contents of the generated Android.bp file
293 androidBpContents string
294
Paul Duffind0759072021-02-17 11:23:00 +0000295 // The contents of the unversioned Android.bp file
296 androidUnversionedBpContents string
297
298 // The contents of the versioned Android.bp file
299 androidVersionedBpContents string
300
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000301 // The paths, relative to the snapshot root, of all files and directories copied into the
302 // snapshot.
303 snapshotContents []string
304
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000305 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
306 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000307 copyRules string
308
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000309 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
310 // per line, of the format src -> dest
311 otherCopyRules string
312
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000313 // The path to the intermediate zip, which is a zip created from the source files copied
314 // into the snapshot directory and which will be merged with other zips to form the final output.
315 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
316 intermediateZip string
317
318 // The paths to the zips to merge into the output zip, does not include the intermediate
319 // zip.
320 mergeZips []string
321
322 // The final output zip.
323 outputZip string
324}
325
Paul Duffin82d90432019-11-30 09:24:33 +0000326var buildDir string
327
328func setUp() {
329 var err error
330 buildDir, err = ioutil.TempDir("", "soong_sdk_test")
331 if err != nil {
332 panic(err)
333 }
334}
335
336func tearDown() {
337 _ = os.RemoveAll(buildDir)
338}
339
340func runTestWithBuildDir(m *testing.M) {
341 run := func() int {
342 setUp()
343 defer tearDown()
344
345 return m.Run()
346 }
347
348 os.Exit(run())
349}