blob: 29815e0dfc875cff36a96d2b8b5e508ca45b33e8 [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 (
18 "io/ioutil"
19 "os"
20 "strings"
21 "testing"
22
23 "android/soong/android"
24 "android/soong/apex"
25 "android/soong/cc"
26 "android/soong/java"
27)
28
29func testSdkContext(bp string) (*android.TestContext, android.Config) {
30 config := android.TestArchConfig(buildDir, nil)
31 ctx := android.NewTestArchContext()
32
33 // from android package
34 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
35 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
36 ctx.BottomUp("prebuilts", android.PrebuiltMutator).Parallel()
37 })
38 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
39 ctx.TopDown("prebuilt_select", android.PrebuiltSelectModuleMutator).Parallel()
40 ctx.BottomUp("prebuilt_postdeps", android.PrebuiltPostDepsMutator).Parallel()
41 })
42
43 // from java package
44 ctx.RegisterModuleType("android_app_certificate", java.AndroidAppCertificateFactory)
45 ctx.RegisterModuleType("java_library", java.LibraryFactory)
46 ctx.RegisterModuleType("java_import", java.ImportFactory)
47 ctx.RegisterModuleType("droidstubs", java.DroidstubsFactory)
48 ctx.RegisterModuleType("prebuilt_stubs_sources", java.PrebuiltStubsSourcesFactory)
49
50 // from cc package
51 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
52 ctx.RegisterModuleType("cc_library_shared", cc.LibrarySharedFactory)
53 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
54 ctx.RegisterModuleType("cc_prebuilt_library_shared", cc.PrebuiltSharedLibraryFactory)
55 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
56 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
57 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
58 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
59 ctx.BottomUp("image", android.ImageMutator).Parallel()
60 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
61 ctx.BottomUp("vndk", cc.VndkMutator).Parallel()
62 ctx.BottomUp("test_per_src", cc.TestPerSrcMutator).Parallel()
63 ctx.BottomUp("version", cc.VersionMutator).Parallel()
64 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
65 })
66
67 // from apex package
68 ctx.RegisterModuleType("apex", apex.BundleFactory)
69 ctx.RegisterModuleType("apex_key", apex.ApexKeyFactory)
70 ctx.PostDepsMutators(apex.RegisterPostDepsMutators)
71
72 // from this package
73 ctx.RegisterModuleType("sdk", ModuleFactory)
74 ctx.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
75 ctx.PreDepsMutators(RegisterPreDepsMutators)
76 ctx.PostDepsMutators(RegisterPostDepsMutators)
77
78 ctx.Register()
79
80 bp = bp + `
81 apex_key {
82 name: "myapex.key",
83 public_key: "myapex.avbpubkey",
84 private_key: "myapex.pem",
85 }
86
87 android_app_certificate {
88 name: "myapex.cert",
89 certificate: "myapex",
90 }
91 ` + cc.GatherRequiredDepsForTest(android.Android)
92
93 ctx.MockFileSystem(map[string][]byte{
94 "Android.bp": []byte(bp),
95 "build/make/target/product/security": nil,
96 "apex_manifest.json": nil,
97 "system/sepolicy/apex/myapex-file_contexts": nil,
98 "system/sepolicy/apex/myapex2-file_contexts": nil,
99 "myapex.avbpubkey": nil,
100 "myapex.pem": nil,
101 "myapex.x509.pem": nil,
102 "myapex.pk8": nil,
103 "Test.java": nil,
104 "Test.cpp": nil,
105 "include/Test.h": nil,
106 "aidl/foo/bar/Test.aidl": nil,
107 "libfoo.so": nil,
108 "stubs-sources/foo/bar/Foo.java": nil,
109 "foo/bar/Foo.java": nil,
110 })
111
112 return ctx, config
113}
114
115func testSdk(t *testing.T, bp string) (*android.TestContext, android.Config) {
116 ctx, config := testSdkContext(bp)
117 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
118 android.FailIfErrored(t, errs)
119 _, errs = ctx.PrepareBuildActions(config)
120 android.FailIfErrored(t, errs)
121 return ctx, config
122}
123
124func testSdkError(t *testing.T, pattern, bp string) {
125 t.Helper()
126 ctx, config := testSdkContext(bp)
127 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
128 if len(errs) > 0 {
129 android.FailIfNoMatchingErrors(t, pattern, errs)
130 return
131 }
132 _, errs = ctx.PrepareBuildActions(config)
133 if len(errs) > 0 {
134 android.FailIfNoMatchingErrors(t, pattern, errs)
135 return
136 }
137
138 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
139}
140
141func ensureListContains(t *testing.T, result []string, expected string) {
142 t.Helper()
143 if !android.InList(expected, result) {
144 t.Errorf("%q is not found in %v", expected, result)
145 }
146}
147
148func pathsToStrings(paths android.Paths) []string {
149 var ret []string
150 for _, p := range paths {
151 ret = append(ret, p.String())
152 }
153 return ret
154}
155
156func checkSnapshotAndroidBpContents(t *testing.T, s *sdk, expectedContents string) {
157 t.Helper()
158 androidBpContents := strings.NewReplacer("\\n", "\n").Replace(s.GetAndroidBpContentsForTests())
159 if androidBpContents != expectedContents {
160 t.Errorf("Android.bp contents do not match, expected %s, actual %s", expectedContents, androidBpContents)
161 }
162}
163
164var buildDir string
165
166func setUp() {
167 var err error
168 buildDir, err = ioutil.TempDir("", "soong_sdk_test")
169 if err != nil {
170 panic(err)
171 }
172}
173
174func tearDown() {
175 _ = os.RemoveAll(buildDir)
176}
177
178func runTestWithBuildDir(m *testing.M) {
179 run := func() int {
180 setUp()
181 defer tearDown()
182
183 return m.Run()
184 }
185
186 os.Exit(run())
187}
188
189func SkipIfNotLinux(t *testing.T) {
190 t.Helper()
191 if android.BuildOs != android.Linux {
192 t.Skipf("Skipping as sdk snapshot generation is only supported on %s not %s", android.Linux, android.BuildOs)
193 }
194}