blob: a5519f8f08059dd88413b9f8450f279dc89cedc2 [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.
Paul Duffinfe9a9e32021-03-11 17:41:01 +0000108//
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000109// e.g. find the src/dest pairs from each cp command, the various zip files
110// generated, etc.
Paul Duffin36474d32021-03-12 12:19:43 +0000111func getSdkSnapshotBuildInfo(t *testing.T, result *android.TestResult, sdk *sdk) *snapshotBuildInfo {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000112 info := &snapshotBuildInfo{
Paul Duffin36474d32021-03-12 12:19:43 +0000113 t: t,
Paul Duffin981b94b2021-03-11 12:32:12 +0000114 r: result,
Paul Duffind0759072021-02-17 11:23:00 +0000115 androidBpContents: sdk.GetAndroidBpContentsForTests(),
116 androidUnversionedBpContents: sdk.GetUnversionedAndroidBpContentsForTests(),
117 androidVersionedBpContents: sdk.GetVersionedAndroidBpContentsForTests(),
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000118 }
119
120 buildParams := sdk.BuildParamsForTests()
121 copyRules := &strings.Builder{}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000122 otherCopyRules := &strings.Builder{}
Paul Duffine1ddcc92020-03-03 16:01:26 +0000123 snapshotDirPrefix := sdk.builderForTests.snapshotDir.String() + "/"
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000124 for _, bp := range buildParams {
125 switch bp.Rule.String() {
126 case android.Cp.String():
Paul Duffine1ddcc92020-03-03 16:01:26 +0000127 output := bp.Output
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000128 // Get destination relative to the snapshot root
129 dest := output.Rel()
130 src := android.NormalizePathForTesting(bp.Input)
131 // We differentiate between copy rules for the snapshot, and copy rules for the install file.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000132 if strings.HasPrefix(output.String(), snapshotDirPrefix) {
133 // Get source relative to build directory.
Paul Duffine1ddcc92020-03-03 16:01:26 +0000134 _, _ = fmt.Fprintf(copyRules, "%s -> %s\n", src, dest)
135 info.snapshotContents = append(info.snapshotContents, dest)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000136 } else {
137 _, _ = fmt.Fprintf(otherCopyRules, "%s -> %s\n", src, dest)
Paul Duffine1ddcc92020-03-03 16:01:26 +0000138 }
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000139
140 case repackageZip.String():
141 // Add the destdir to the snapshot contents as that is effectively where
142 // the content of the repackaged zip is copied.
143 dest := bp.Args["destdir"]
144 info.snapshotContents = append(info.snapshotContents, dest)
145
146 case zipFiles.String():
147 // This could be an intermediate zip file and not the actual output zip.
148 // In that case this will be overridden when the rule to merge the zips
149 // is processed.
Paul Duffin9b478b02019-12-10 13:41:51 +0000150 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000151
152 case mergeZips.String():
153 // Copy the current outputZip to the intermediateZip.
154 info.intermediateZip = info.outputZip
Paul Duffin9b478b02019-12-10 13:41:51 +0000155 mergeInput := android.NormalizePathForTesting(bp.Input)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000156 if info.intermediateZip != mergeInput {
Paul Duffin36474d32021-03-12 12:19:43 +0000157 t.Errorf("Expected intermediate zip %s to be an input to merge zips but found %s instead",
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000158 info.intermediateZip, mergeInput)
159 }
160
161 // Override output zip (which was actually the intermediate zip file) with the actual
162 // output zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000163 info.outputZip = android.NormalizePathForTesting(bp.Output)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000164
165 // Save the zips to be merged into the intermediate zip.
Paul Duffin9b478b02019-12-10 13:41:51 +0000166 info.mergeZips = android.NormalizePathsForTesting(bp.Inputs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000167 }
168 }
169
170 info.copyRules = copyRules.String()
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000171 info.otherCopyRules = otherCopyRules.String()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000172
173 return info
174}
175
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000176// Check the snapshot build rules.
177//
178// Takes a list of functions which check different facets of the snapshot build rules.
179// Allows each test to customize what is checked without duplicating lots of code
180// or proliferating check methods of different flavors.
Paul Duffin36474d32021-03-12 12:19:43 +0000181func CheckSnapshot(t *testing.T, result *android.TestResult, name string, dir string, checkers ...snapshotBuildInfoChecker) {
182 t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000183
Paul Duffin1356d8c2020-02-25 19:26:33 +0000184 // The sdk CommonOS variant is always responsible for generating the snapshot.
185 variant := android.CommonOS.Name
186
Paul Duffin981b94b2021-03-11 12:32:12 +0000187 sdk := result.Module(name, variant).(*sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000188
Paul Duffin36474d32021-03-12 12:19:43 +0000189 snapshotBuildInfo := getSdkSnapshotBuildInfo(t, result, sdk)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000190
191 // Check state of the snapshot build.
192 for _, checker := range checkers {
193 checker(snapshotBuildInfo)
194 }
195
196 // Make sure that the generated zip file is in the correct place.
197 actual := snapshotBuildInfo.outputZip
Paul Duffin593b3c92019-12-05 14:31:48 +0000198 if dir != "" {
199 dir = filepath.Clean(dir) + "/"
200 }
Paul Duffin36474d32021-03-12 12:19:43 +0000201 android.AssertStringEquals(t, "Snapshot zip file in wrong place",
Paul Duffin593b3c92019-12-05 14:31:48 +0000202 fmt.Sprintf(".intermediates/%s%s/%s/%s-current.zip", dir, name, variant, name), actual)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000203
204 // Populate a mock filesystem with the files that would have been copied by
205 // the rules.
206 fs := make(map[string][]byte)
207 for _, dest := range snapshotBuildInfo.snapshotContents {
208 fs[dest] = nil
209 }
210
211 // Process the generated bp file to make sure it is valid.
Paul Duffin36474d32021-03-12 12:19:43 +0000212 testSdkWithFs(t, snapshotBuildInfo.androidBpContents, fs)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000213}
214
215type snapshotBuildInfoChecker func(info *snapshotBuildInfo)
216
217// Check that the snapshot's generated Android.bp is correct.
218//
219// Both the expected and actual string are both trimmed before comparing.
220func checkAndroidBpContents(expected string) snapshotBuildInfoChecker {
221 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000222 info.t.Helper()
223 android.AssertTrimmedStringEquals(info.t, "Android.bp contents do not match", expected, info.androidBpContents)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000224 }
225}
226
Paul Duffind0759072021-02-17 11:23:00 +0000227// Check that the snapshot's unversioned generated Android.bp is correct.
228//
229// This func should be used to check the general snapshot generation code.
230//
231// Both the expected and actual string are both trimmed before comparing.
232func checkUnversionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
233 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000234 info.t.Helper()
235 android.AssertTrimmedStringEquals(info.t, "unversioned Android.bp contents do not match", expected, info.androidUnversionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000236 }
237}
238
239// Check that the snapshot's versioned generated Android.bp is correct.
240//
241// This func should only be used to check the version specific snapshot generation code,
242// i.e. the encoding of version into module names and the generation of the _snapshot module. The
243// general snapshot generation code should be checked using the checkUnversionedAndroidBpContents()
244// func.
245//
246// Both the expected and actual string are both trimmed before comparing.
247func checkVersionedAndroidBpContents(expected string) snapshotBuildInfoChecker {
248 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000249 info.t.Helper()
250 android.AssertTrimmedStringEquals(info.t, "versioned Android.bp contents do not match", expected, info.androidVersionedBpContents)
Paul Duffind0759072021-02-17 11:23:00 +0000251 }
252}
253
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000254// Check that the snapshot's copy rules are correct.
255//
256// The copy rules are formatted as <src> -> <dest>, one per line and then compared
257// to the supplied expected string. Both the expected and actual string are trimmed
258// before comparing.
259func checkAllCopyRules(expected string) snapshotBuildInfoChecker {
260 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000261 info.t.Helper()
262 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.copyRules)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000263 }
264}
265
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000266func checkAllOtherCopyRules(expected string) snapshotBuildInfoChecker {
267 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000268 info.t.Helper()
269 android.AssertTrimmedStringEquals(info.t, "Incorrect copy rules", expected, info.otherCopyRules)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000270 }
271}
272
Paul Duffin3d1248c2020-04-09 00:10:17 +0100273// Check that the specified paths match the list of zips to merge with the intermediate zip.
274func checkMergeZips(expected ...string) snapshotBuildInfoChecker {
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000275 return func(info *snapshotBuildInfo) {
Paul Duffin36474d32021-03-12 12:19:43 +0000276 info.t.Helper()
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000277 if info.intermediateZip == "" {
Paul Duffin36474d32021-03-12 12:19:43 +0000278 info.t.Errorf("No intermediate zip file was created")
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000279 }
Paul Duffin3d1248c2020-04-09 00:10:17 +0100280
Paul Duffin36474d32021-03-12 12:19:43 +0000281 android.AssertDeepEquals(info.t, "mismatching merge zip files", expected, info.mergeZips)
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000282 }
283}
284
285// Encapsulates information about the snapshot build structure in order to insulate tests from
286// knowing too much about internal structures.
287//
288// All source/input paths are relative either the build directory. All dest/output paths are
289// relative to the snapshot root directory.
290type snapshotBuildInfo struct {
Paul Duffin36474d32021-03-12 12:19:43 +0000291 t *testing.T
292
293 // The result from RunTest()
Paul Duffin4a2a29c2021-03-09 22:27:13 +0000294 r *android.TestResult
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000295
296 // The contents of the generated Android.bp file
297 androidBpContents string
298
Paul Duffind0759072021-02-17 11:23:00 +0000299 // The contents of the unversioned Android.bp file
300 androidUnversionedBpContents string
301
302 // The contents of the versioned Android.bp file
303 androidVersionedBpContents string
304
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000305 // The paths, relative to the snapshot root, of all files and directories copied into the
306 // snapshot.
307 snapshotContents []string
308
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000309 // A formatted representation of the src/dest pairs for a snapshot, one pair per line,
310 // of the format src -> dest
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000311 copyRules string
312
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000313 // A formatted representation of the src/dest pairs for files not in a snapshot, one pair
314 // per line, of the format src -> dest
315 otherCopyRules string
316
Paul Duffinc3c5d5e2019-11-29 20:45:22 +0000317 // The path to the intermediate zip, which is a zip created from the source files copied
318 // into the snapshot directory and which will be merged with other zips to form the final output.
319 // Is am empty string if there is no intermediate zip because there are no zips to merge in.
320 intermediateZip string
321
322 // The paths to the zips to merge into the output zip, does not include the intermediate
323 // zip.
324 mergeZips []string
325
326 // The final output zip.
327 outputZip string
328}
329
Paul Duffin82d90432019-11-30 09:24:33 +0000330var buildDir string
331
332func setUp() {
333 var err error
334 buildDir, err = ioutil.TempDir("", "soong_sdk_test")
335 if err != nil {
336 panic(err)
337 }
338}
339
340func tearDown() {
341 _ = os.RemoveAll(buildDir)
342}
343
344func runTestWithBuildDir(m *testing.M) {
345 run := func() int {
346 setUp()
347 defer tearDown()
348
349 return m.Run()
350 }
351
352 os.Exit(run())
353}