blob: 7f254b1d93dabc178b1fa65a2a6c04ed62cc6ff2 [file] [log] [blame]
Logan Chienee97c3e2018-03-12 16:34:26 +08001// Copyright 2018 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 android
16
17import (
Logan Chienee97c3e2018-03-12 16:34:26 +080018 "testing"
Paul Duffin35781882019-07-25 15:41:09 +010019
20 "github.com/google/blueprint"
Logan Chienee97c3e2018-03-12 16:34:26 +080021)
22
Paul Duffin35781882019-07-25 15:41:09 +010023func init() {
24 // Add extra rules needed for testing.
25 AddNeverAllowRules(
26 NeverAllow().InDirectDeps("not_allowed_in_direct_deps"),
27 )
28}
29
Logan Chienee97c3e2018-03-12 16:34:26 +080030var neverallowTests = []struct {
31 name string
32 fs map[string][]byte
33 expectedError string
34}{
Paul Duffin35781882019-07-25 15:41:09 +010035 // Test General Functionality
36
37 // in direct deps tests
38 {
39 name: "not_allowed_in_direct_deps",
40 fs: map[string][]byte{
41 "top/Blueprints": []byte(`
42 cc_library {
43 name: "not_allowed_in_direct_deps",
44 }`),
45 "other/Blueprints": []byte(`
46 cc_library {
47 name: "libother",
48 static_libs: ["not_allowed_in_direct_deps"],
49 }`),
50 },
51 expectedError: `module "libother": violates neverallow deps:not_allowed_in_direct_deps`,
52 },
53
54 // Test specific rules
55
Paul Duffinc8111702019-07-22 12:13:55 +010056 // include_dir rule tests
57 {
58 name: "include_dir not allowed to reference art",
59 fs: map[string][]byte{
60 "other/Blueprints": []byte(`
61 cc_library {
62 name: "libother",
63 include_dirs: ["art/libdexfile/include"],
64 }`),
65 },
66 expectedError: "all usages of 'art' have been migrated",
67 },
68 {
69 name: "include_dir can reference another location",
70 fs: map[string][]byte{
71 "other/Blueprints": []byte(`
72 cc_library {
73 name: "libother",
74 include_dirs: ["another/include"],
75 }`),
76 },
77 },
78 // Treble rule tests
Logan Chienee97c3e2018-03-12 16:34:26 +080079 {
80 name: "no vndk.enabled under vendor directory",
81 fs: map[string][]byte{
82 "vendor/Blueprints": []byte(`
83 cc_library {
84 name: "libvndk",
85 vendor_available: true,
86 vndk: {
87 enabled: true,
88 },
89 }`),
90 },
91 expectedError: "VNDK can never contain a library that is device dependent",
92 },
93 {
94 name: "no vndk.enabled under device directory",
95 fs: map[string][]byte{
96 "device/Blueprints": []byte(`
97 cc_library {
98 name: "libvndk",
99 vendor_available: true,
100 vndk: {
101 enabled: true,
102 },
103 }`),
104 },
105 expectedError: "VNDK can never contain a library that is device dependent",
106 },
Logan Chienaf29bad2018-03-12 16:35:58 +0800107 {
108 name: "vndk-ext under vendor or device directory",
109 fs: map[string][]byte{
110 "device/Blueprints": []byte(`
111 cc_library {
112 name: "libvndk1_ext",
113 vendor: true,
114 vndk: {
115 enabled: true,
116 },
117 }`),
118 "vendor/Blueprints": []byte(`
119 cc_library {
120 name: "libvndk2_ext",
121 vendor: true,
122 vndk: {
123 enabled: true,
124 },
125 }`),
126 },
127 expectedError: "",
128 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800129
130 {
131 name: "no enforce_vintf_manifest.cflags",
132 fs: map[string][]byte{
133 "Blueprints": []byte(`
134 cc_library {
135 name: "libexample",
136 product_variables: {
137 enforce_vintf_manifest: {
138 cflags: ["-DSHOULD_NOT_EXIST"],
139 },
140 },
141 }`),
142 },
143 expectedError: "manifest enforcement should be independent",
144 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800145
146 {
147 name: "no treble_linker_namespaces.cflags",
148 fs: map[string][]byte{
149 "Blueprints": []byte(`
150 cc_library {
151 name: "libexample",
152 product_variables: {
153 treble_linker_namespaces: {
154 cflags: ["-DSHOULD_NOT_EXIST"],
155 },
156 },
157 }`),
158 },
159 expectedError: "nothing should care if linker namespaces are enabled or not",
160 },
161 {
162 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
163 fs: map[string][]byte{
164 "Blueprints": []byte(`
165 cc_library {
166 name: "libc_bionic_ndk",
167 product_variables: {
168 treble_linker_namespaces: {
169 cflags: ["-DSHOULD_NOT_EXIST"],
170 },
171 },
172 }`),
173 },
174 expectedError: "",
175 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100176 {
Dongwon Kang50a299f2019-02-04 09:00:51 -0800177 name: "dependency on updatable-media",
178 fs: map[string][]byte{
179 "Blueprints": []byte(`
180 java_library {
181 name: "needs_updatable_media",
182 libs: ["updatable-media"],
183 }`),
184 },
185 expectedError: "updatable-media includes private APIs. Use updatable_media_stubs instead.",
186 },
Colin Crossfd4f7432019-03-05 15:06:16 -0800187 {
188 name: "java_device_for_host",
189 fs: map[string][]byte{
190 "Blueprints": []byte(`
191 java_device_for_host {
192 name: "device_for_host",
193 libs: ["core-libart"],
194 }`),
195 },
196 expectedError: "java_device_for_host can only be used in whitelisted projects",
197 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100198 // Libcore rule tests
199 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100200 name: "sdk_version: \"none\" inside core libraries",
201 fs: map[string][]byte{
202 "libcore/Blueprints": []byte(`
203 java_library {
204 name: "inside_core_libraries",
205 sdk_version: "none",
206 }`),
207 },
208 },
209 {
210 name: "sdk_version: \"none\" outside core libraries",
211 fs: map[string][]byte{
212 "Blueprints": []byte(`
213 java_library {
214 name: "outside_core_libraries",
215 sdk_version: "none",
216 }`),
217 },
218 expectedError: "module \"outside_core_libraries\": violates neverallow",
219 },
220 {
221 name: "sdk_version: \"current\"",
222 fs: map[string][]byte{
223 "Blueprints": []byte(`
224 java_library {
225 name: "outside_core_libraries",
226 sdk_version: "current",
227 }`),
228 },
229 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800230}
231
232func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800233 config := TestConfig(buildDir, nil)
234
235 for _, test := range neverallowTests {
236 t.Run(test.name, func(t *testing.T) {
237 _, errs := testNeverallow(t, config, test.fs)
238
239 if test.expectedError == "" {
240 FailIfErrored(t, errs)
241 } else {
242 FailIfNoMatchingErrors(t, test.expectedError, errs)
243 }
244 })
245 }
246}
247
248func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
249 ctx := NewTestContext()
250 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100251 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100252 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossfd4f7432019-03-05 15:06:16 -0800253 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800254 ctx.PostDepsMutators(registerNeverallowMutator)
255 ctx.Register()
256
257 ctx.MockFileSystem(fs)
258
259 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
260 if len(errs) > 0 {
261 return ctx, errs
262 }
263
264 _, errs = ctx.PrepareBuildActions(config)
265 return ctx, errs
266}
267
Neil Fullerdf5f3562018-10-21 17:19:10 +0100268type mockCcLibraryProperties struct {
Paul Duffinc8111702019-07-22 12:13:55 +0100269 Include_dirs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800270 Vendor_available *bool
Paul Duffin35781882019-07-25 15:41:09 +0100271 Static_libs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800272
273 Vndk struct {
274 Enabled *bool
275 Support_system_process *bool
276 Extends *string
277 }
278
279 Product_variables struct {
280 Enforce_vintf_manifest struct {
281 Cflags []string
282 }
283
284 Treble_linker_namespaces struct {
285 Cflags []string
286 }
287 }
288}
289
290type mockCcLibraryModule struct {
291 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100292 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800293}
294
295func newMockCcLibraryModule() Module {
296 m := &mockCcLibraryModule{}
297 m.AddProperties(&m.properties)
298 InitAndroidModule(m)
299 return m
300}
301
Paul Duffin35781882019-07-25 15:41:09 +0100302type neverallowTestDependencyTag struct {
303 blueprint.BaseDependencyTag
304 name string
305}
306
307var staticDepTag = neverallowTestDependencyTag{name: "static"}
308
309func (c *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
310 for _, lib := range c.properties.Static_libs {
311 ctx.AddDependency(ctx.Module(), staticDepTag, lib)
312 }
313}
314
Logan Chienee97c3e2018-03-12 16:34:26 +0800315func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
316}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100317
318type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100319 Libs []string
320 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100321}
322
323type mockJavaLibraryModule struct {
324 ModuleBase
325 properties mockJavaLibraryProperties
326}
327
328func newMockJavaLibraryModule() Module {
329 m := &mockJavaLibraryModule{}
330 m.AddProperties(&m.properties)
331 InitAndroidModule(m)
332 return m
333}
334
Neil Fullerdf5f3562018-10-21 17:19:10 +0100335func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
336}