blob: 2fc42e31f9788be0a275460780561bb1a5a09c36 [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
23var neverallowTests = []struct {
Paul Duffin115445b2019-08-07 15:31:07 +010024 // The name of the test.
25 name string
26
27 // Optional test specific rules. If specified then they are used instead of the default rules.
28 rules []Rule
29
30 // Additional contents to add to the virtual filesystem used by the tests.
31 fs map[string][]byte
32
33 // The expected error patterns. If empty then no errors are expected, otherwise each error
34 // reported must be matched by at least one of these patterns. A pattern matches if the error
35 // message contains the pattern. A pattern does not have to match the whole error message.
Paul Duffinb5af6202019-08-05 15:07:57 +010036 expectedErrors []string
Logan Chienee97c3e2018-03-12 16:34:26 +080037}{
Paul Duffin35781882019-07-25 15:41:09 +010038 // Test General Functionality
39
40 // in direct deps tests
41 {
42 name: "not_allowed_in_direct_deps",
Paul Duffin115445b2019-08-07 15:31:07 +010043 rules: []Rule{
44 NeverAllow().InDirectDeps("not_allowed_in_direct_deps"),
45 },
Paul Duffin35781882019-07-25 15:41:09 +010046 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -080047 "top/Android.bp": []byte(`
Paul Duffin35781882019-07-25 15:41:09 +010048 cc_library {
49 name: "not_allowed_in_direct_deps",
50 }`),
Colin Cross98be1bb2019-12-13 20:41:13 -080051 "other/Android.bp": []byte(`
Paul Duffin35781882019-07-25 15:41:09 +010052 cc_library {
53 name: "libother",
54 static_libs: ["not_allowed_in_direct_deps"],
55 }`),
56 },
Paul Duffinb5af6202019-08-05 15:07:57 +010057 expectedErrors: []string{
58 `module "libother": violates neverallow deps:not_allowed_in_direct_deps`,
59 },
Paul Duffin35781882019-07-25 15:41:09 +010060 },
61
Paul Duffin115445b2019-08-07 15:31:07 +010062 // Test android specific rules
Paul Duffin35781882019-07-25 15:41:09 +010063
Paul Duffinc8111702019-07-22 12:13:55 +010064 // include_dir rule tests
65 {
66 name: "include_dir not allowed to reference art",
67 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -080068 "other/Android.bp": []byte(`
Paul Duffinc8111702019-07-22 12:13:55 +010069 cc_library {
70 name: "libother",
71 include_dirs: ["art/libdexfile/include"],
72 }`),
73 },
Paul Duffinb5af6202019-08-05 15:07:57 +010074 expectedErrors: []string{
75 "all usages of 'art' have been migrated",
76 },
Paul Duffinc8111702019-07-22 12:13:55 +010077 },
78 {
79 name: "include_dir can reference another location",
80 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -080081 "other/Android.bp": []byte(`
Paul Duffinc8111702019-07-22 12:13:55 +010082 cc_library {
83 name: "libother",
84 include_dirs: ["another/include"],
85 }`),
86 },
87 },
88 // Treble rule tests
Logan Chienee97c3e2018-03-12 16:34:26 +080089 {
90 name: "no vndk.enabled under vendor directory",
91 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -080092 "vendor/Android.bp": []byte(`
Logan Chienee97c3e2018-03-12 16:34:26 +080093 cc_library {
94 name: "libvndk",
95 vendor_available: true,
96 vndk: {
97 enabled: true,
98 },
99 }`),
100 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100101 expectedErrors: []string{
102 "VNDK can never contain a library that is device dependent",
103 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800104 },
105 {
106 name: "no vndk.enabled under device directory",
107 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800108 "device/Android.bp": []byte(`
Logan Chienee97c3e2018-03-12 16:34:26 +0800109 cc_library {
110 name: "libvndk",
111 vendor_available: true,
112 vndk: {
113 enabled: true,
114 },
115 }`),
116 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100117 expectedErrors: []string{
118 "VNDK can never contain a library that is device dependent",
119 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800120 },
Logan Chienaf29bad2018-03-12 16:35:58 +0800121 {
122 name: "vndk-ext under vendor or device directory",
123 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800124 "device/Android.bp": []byte(`
Logan Chienaf29bad2018-03-12 16:35:58 +0800125 cc_library {
126 name: "libvndk1_ext",
127 vendor: true,
128 vndk: {
129 enabled: true,
130 },
131 }`),
Colin Cross98be1bb2019-12-13 20:41:13 -0800132 "vendor/Android.bp": []byte(`
Logan Chienaf29bad2018-03-12 16:35:58 +0800133 cc_library {
134 name: "libvndk2_ext",
135 vendor: true,
136 vndk: {
137 enabled: true,
138 },
139 }`),
140 },
Logan Chienaf29bad2018-03-12 16:35:58 +0800141 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800142
143 {
144 name: "no enforce_vintf_manifest.cflags",
145 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800146 "Android.bp": []byte(`
Logan Chienee97c3e2018-03-12 16:34:26 +0800147 cc_library {
148 name: "libexample",
149 product_variables: {
150 enforce_vintf_manifest: {
151 cflags: ["-DSHOULD_NOT_EXIST"],
152 },
153 },
154 }`),
155 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100156 expectedErrors: []string{
157 "manifest enforcement should be independent",
158 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800159 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800160
161 {
162 name: "no treble_linker_namespaces.cflags",
163 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800164 "Android.bp": []byte(`
Logan Chienee97c3e2018-03-12 16:34:26 +0800165 cc_library {
166 name: "libexample",
167 product_variables: {
168 treble_linker_namespaces: {
169 cflags: ["-DSHOULD_NOT_EXIST"],
170 },
171 },
172 }`),
173 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100174 expectedErrors: []string{
175 "nothing should care if linker namespaces are enabled or not",
176 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800177 },
178 {
179 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
180 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800181 "Android.bp": []byte(`
Logan Chienee97c3e2018-03-12 16:34:26 +0800182 cc_library {
183 name: "libc_bionic_ndk",
184 product_variables: {
185 treble_linker_namespaces: {
186 cflags: ["-DSHOULD_NOT_EXIST"],
187 },
188 },
189 }`),
190 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800191 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100192 {
Dongwon Kang50a299f2019-02-04 09:00:51 -0800193 name: "dependency on updatable-media",
194 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800195 "Android.bp": []byte(`
Dongwon Kang50a299f2019-02-04 09:00:51 -0800196 java_library {
197 name: "needs_updatable_media",
198 libs: ["updatable-media"],
199 }`),
200 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100201 expectedErrors: []string{
202 "updatable-media includes private APIs. Use updatable_media_stubs instead.",
203 },
Dongwon Kang50a299f2019-02-04 09:00:51 -0800204 },
Colin Crossfd4f7432019-03-05 15:06:16 -0800205 {
206 name: "java_device_for_host",
207 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800208 "Android.bp": []byte(`
Colin Crossfd4f7432019-03-05 15:06:16 -0800209 java_device_for_host {
210 name: "device_for_host",
211 libs: ["core-libart"],
212 }`),
213 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100214 expectedErrors: []string{
215 "java_device_for_host can only be used in whitelisted projects",
216 },
Colin Crossfd4f7432019-03-05 15:06:16 -0800217 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100218 // Libcore rule tests
219 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100220 name: "sdk_version: \"none\" inside core libraries",
221 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800222 "libcore/Android.bp": []byte(`
Paul Duffin52d398a2019-06-11 12:31:14 +0100223 java_library {
224 name: "inside_core_libraries",
225 sdk_version: "none",
226 }`),
227 },
228 },
229 {
Anton Hansson45376402020-04-09 14:18:21 +0100230 name: "sdk_version: \"none\" on android_*stubs_current stub",
231 fs: map[string][]byte{
232 "frameworks/base/Android.bp": []byte(`
233 java_library {
234 name: "android_stubs_current",
235 sdk_version: "none",
236 }`),
237 },
238 },
239 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100240 name: "sdk_version: \"none\" outside core libraries",
241 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800242 "Android.bp": []byte(`
Paul Duffin52d398a2019-06-11 12:31:14 +0100243 java_library {
244 name: "outside_core_libraries",
245 sdk_version: "none",
246 }`),
247 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100248 expectedErrors: []string{
249 "module \"outside_core_libraries\": violates neverallow",
250 },
Paul Duffin52d398a2019-06-11 12:31:14 +0100251 },
252 {
253 name: "sdk_version: \"current\"",
254 fs: map[string][]byte{
Colin Cross98be1bb2019-12-13 20:41:13 -0800255 "Android.bp": []byte(`
Paul Duffin52d398a2019-06-11 12:31:14 +0100256 java_library {
257 name: "outside_core_libraries",
258 sdk_version: "current",
259 }`),
260 },
261 },
Colin Crossc511bc52020-04-07 16:50:32 +0000262 // CC sdk rule tests
263 {
264 name: `"sdk_variant_only" outside whitelist`,
265 fs: map[string][]byte{
266 "Android.bp": []byte(`
267 cc_library {
268 name: "outside_whitelist",
269 sdk_version: "current",
270 sdk_variant_only: true,
271 }`),
272 },
273 expectedErrors: []string{
274 `module "outside_whitelist": violates neverallow`,
275 },
276 },
277 {
278 name: `"sdk_variant_only: false" outside whitelist`,
279 fs: map[string][]byte{
280 "Android.bp": []byte(`
281 cc_library {
282 name: "outside_whitelist",
283 sdk_version: "current",
284 sdk_variant_only: false,
285 }`),
286 },
287 expectedErrors: []string{
288 `module "outside_whitelist": violates neverallow`,
289 },
290 },
291 {
292 name: `"platform" outside whitelist`,
293 fs: map[string][]byte{
294 "Android.bp": []byte(`
295 cc_library {
296 name: "outside_whitelist",
297 platform: {
298 shared_libs: ["libfoo"],
299 },
300 }`),
301 },
302 expectedErrors: []string{
303 `module "outside_whitelist": violates neverallow`,
304 },
305 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800306}
307
308func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800309 for _, test := range neverallowTests {
Paul Duffin115445b2019-08-07 15:31:07 +0100310 // Create a test per config to allow for test specific config, e.g. test rules.
Colin Cross98be1bb2019-12-13 20:41:13 -0800311 config := TestConfig(buildDir, nil, "", test.fs)
Logan Chienee97c3e2018-03-12 16:34:26 +0800312
Paul Duffinb5af6202019-08-05 15:07:57 +0100313 t.Run(test.name, func(t *testing.T) {
Paul Duffin115445b2019-08-07 15:31:07 +0100314 // If the test has its own rules then use them instead of the default ones.
315 if test.rules != nil {
Artur Satayevc5570ac2020-04-09 16:06:36 +0100316 SetTestNeverallowRules(config, test.rules)
Paul Duffin115445b2019-08-07 15:31:07 +0100317 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800318 _, errs := testNeverallow(config)
Paul Duffinb5af6202019-08-05 15:07:57 +0100319 CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
Logan Chienee97c3e2018-03-12 16:34:26 +0800320 })
321 }
322}
323
Colin Cross98be1bb2019-12-13 20:41:13 -0800324func testNeverallow(config Config) (*TestContext, []error) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800325 ctx := NewTestContext()
Colin Cross4b49b762019-11-22 15:25:03 -0800326 ctx.RegisterModuleType("cc_library", newMockCcLibraryModule)
327 ctx.RegisterModuleType("java_library", newMockJavaLibraryModule)
328 ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
329 ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
Artur Satayevc5570ac2020-04-09 16:06:36 +0100330 ctx.PostDepsMutators(RegisterNeverallowMutator)
Colin Cross98be1bb2019-12-13 20:41:13 -0800331 ctx.Register(config)
Logan Chienee97c3e2018-03-12 16:34:26 +0800332
Colin Cross98be1bb2019-12-13 20:41:13 -0800333 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chienee97c3e2018-03-12 16:34:26 +0800334 if len(errs) > 0 {
335 return ctx, errs
336 }
337
338 _, errs = ctx.PrepareBuildActions(config)
339 return ctx, errs
340}
341
Neil Fullerdf5f3562018-10-21 17:19:10 +0100342type mockCcLibraryProperties struct {
Paul Duffinc8111702019-07-22 12:13:55 +0100343 Include_dirs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800344 Vendor_available *bool
Paul Duffin35781882019-07-25 15:41:09 +0100345 Static_libs []string
Colin Crossc511bc52020-04-07 16:50:32 +0000346 Sdk_version *string
347 Sdk_variant_only *bool
Logan Chienee97c3e2018-03-12 16:34:26 +0800348
349 Vndk struct {
350 Enabled *bool
351 Support_system_process *bool
352 Extends *string
353 }
354
355 Product_variables struct {
356 Enforce_vintf_manifest struct {
357 Cflags []string
358 }
359
360 Treble_linker_namespaces struct {
361 Cflags []string
362 }
363 }
Colin Crossc511bc52020-04-07 16:50:32 +0000364
365 Platform struct {
366 Shared_libs []string
367 }
Logan Chienee97c3e2018-03-12 16:34:26 +0800368}
369
370type mockCcLibraryModule struct {
371 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100372 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800373}
374
375func newMockCcLibraryModule() Module {
376 m := &mockCcLibraryModule{}
377 m.AddProperties(&m.properties)
378 InitAndroidModule(m)
379 return m
380}
381
Paul Duffin35781882019-07-25 15:41:09 +0100382type neverallowTestDependencyTag struct {
383 blueprint.BaseDependencyTag
384 name string
385}
386
387var staticDepTag = neverallowTestDependencyTag{name: "static"}
388
389func (c *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
390 for _, lib := range c.properties.Static_libs {
391 ctx.AddDependency(ctx.Module(), staticDepTag, lib)
392 }
393}
394
Logan Chienee97c3e2018-03-12 16:34:26 +0800395func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
396}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100397
398type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100399 Libs []string
400 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100401}
402
403type mockJavaLibraryModule struct {
404 ModuleBase
405 properties mockJavaLibraryProperties
406}
407
408func newMockJavaLibraryModule() Module {
409 m := &mockJavaLibraryModule{}
410 m.AddProperties(&m.properties)
411 InitAndroidModule(m)
412 return m
413}
414
Neil Fullerdf5f3562018-10-21 17:19:10 +0100415func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
416}