blob: cfab4f951888d250180468f6eb807c9deb29e84e [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 {
Paul Duffinb5af6202019-08-05 15:07:57 +010031 name string
32 fs map[string][]byte
33 expectedErrors []string
Logan Chienee97c3e2018-03-12 16:34:26 +080034}{
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 },
Paul Duffinb5af6202019-08-05 15:07:57 +010051 expectedErrors: []string{
52 `module "libother": violates neverallow deps:not_allowed_in_direct_deps`,
53 },
Paul Duffin35781882019-07-25 15:41:09 +010054 },
55
56 // Test specific rules
57
Paul Duffinc8111702019-07-22 12:13:55 +010058 // include_dir rule tests
59 {
60 name: "include_dir not allowed to reference art",
61 fs: map[string][]byte{
62 "other/Blueprints": []byte(`
63 cc_library {
64 name: "libother",
65 include_dirs: ["art/libdexfile/include"],
66 }`),
67 },
Paul Duffinb5af6202019-08-05 15:07:57 +010068 expectedErrors: []string{
69 "all usages of 'art' have been migrated",
70 },
Paul Duffinc8111702019-07-22 12:13:55 +010071 },
72 {
73 name: "include_dir can reference another location",
74 fs: map[string][]byte{
75 "other/Blueprints": []byte(`
76 cc_library {
77 name: "libother",
78 include_dirs: ["another/include"],
79 }`),
80 },
81 },
82 // Treble rule tests
Logan Chienee97c3e2018-03-12 16:34:26 +080083 {
84 name: "no vndk.enabled under vendor directory",
85 fs: map[string][]byte{
86 "vendor/Blueprints": []byte(`
87 cc_library {
88 name: "libvndk",
89 vendor_available: true,
90 vndk: {
91 enabled: true,
92 },
93 }`),
94 },
Paul Duffinb5af6202019-08-05 15:07:57 +010095 expectedErrors: []string{
96 "VNDK can never contain a library that is device dependent",
97 },
Logan Chienee97c3e2018-03-12 16:34:26 +080098 },
99 {
100 name: "no vndk.enabled under device directory",
101 fs: map[string][]byte{
102 "device/Blueprints": []byte(`
103 cc_library {
104 name: "libvndk",
105 vendor_available: true,
106 vndk: {
107 enabled: true,
108 },
109 }`),
110 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100111 expectedErrors: []string{
112 "VNDK can never contain a library that is device dependent",
113 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800114 },
Logan Chienaf29bad2018-03-12 16:35:58 +0800115 {
116 name: "vndk-ext under vendor or device directory",
117 fs: map[string][]byte{
118 "device/Blueprints": []byte(`
119 cc_library {
120 name: "libvndk1_ext",
121 vendor: true,
122 vndk: {
123 enabled: true,
124 },
125 }`),
126 "vendor/Blueprints": []byte(`
127 cc_library {
128 name: "libvndk2_ext",
129 vendor: true,
130 vndk: {
131 enabled: true,
132 },
133 }`),
134 },
Logan Chienaf29bad2018-03-12 16:35:58 +0800135 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800136
137 {
138 name: "no enforce_vintf_manifest.cflags",
139 fs: map[string][]byte{
140 "Blueprints": []byte(`
141 cc_library {
142 name: "libexample",
143 product_variables: {
144 enforce_vintf_manifest: {
145 cflags: ["-DSHOULD_NOT_EXIST"],
146 },
147 },
148 }`),
149 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100150 expectedErrors: []string{
151 "manifest enforcement should be independent",
152 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800153 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800154
155 {
156 name: "no treble_linker_namespaces.cflags",
157 fs: map[string][]byte{
158 "Blueprints": []byte(`
159 cc_library {
160 name: "libexample",
161 product_variables: {
162 treble_linker_namespaces: {
163 cflags: ["-DSHOULD_NOT_EXIST"],
164 },
165 },
166 }`),
167 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100168 expectedErrors: []string{
169 "nothing should care if linker namespaces are enabled or not",
170 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800171 },
172 {
173 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
174 fs: map[string][]byte{
175 "Blueprints": []byte(`
176 cc_library {
177 name: "libc_bionic_ndk",
178 product_variables: {
179 treble_linker_namespaces: {
180 cflags: ["-DSHOULD_NOT_EXIST"],
181 },
182 },
183 }`),
184 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800185 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100186 {
Dongwon Kang50a299f2019-02-04 09:00:51 -0800187 name: "dependency on updatable-media",
188 fs: map[string][]byte{
189 "Blueprints": []byte(`
190 java_library {
191 name: "needs_updatable_media",
192 libs: ["updatable-media"],
193 }`),
194 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100195 expectedErrors: []string{
196 "updatable-media includes private APIs. Use updatable_media_stubs instead.",
197 },
Dongwon Kang50a299f2019-02-04 09:00:51 -0800198 },
Colin Crossfd4f7432019-03-05 15:06:16 -0800199 {
200 name: "java_device_for_host",
201 fs: map[string][]byte{
202 "Blueprints": []byte(`
203 java_device_for_host {
204 name: "device_for_host",
205 libs: ["core-libart"],
206 }`),
207 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100208 expectedErrors: []string{
209 "java_device_for_host can only be used in whitelisted projects",
210 },
Colin Crossfd4f7432019-03-05 15:06:16 -0800211 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100212 // Libcore rule tests
213 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100214 name: "sdk_version: \"none\" inside core libraries",
215 fs: map[string][]byte{
216 "libcore/Blueprints": []byte(`
217 java_library {
218 name: "inside_core_libraries",
219 sdk_version: "none",
220 }`),
221 },
222 },
223 {
224 name: "sdk_version: \"none\" outside core libraries",
225 fs: map[string][]byte{
226 "Blueprints": []byte(`
227 java_library {
228 name: "outside_core_libraries",
229 sdk_version: "none",
230 }`),
231 },
Paul Duffinb5af6202019-08-05 15:07:57 +0100232 expectedErrors: []string{
233 "module \"outside_core_libraries\": violates neverallow",
234 },
Paul Duffin52d398a2019-06-11 12:31:14 +0100235 },
236 {
237 name: "sdk_version: \"current\"",
238 fs: map[string][]byte{
239 "Blueprints": []byte(`
240 java_library {
241 name: "outside_core_libraries",
242 sdk_version: "current",
243 }`),
244 },
245 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800246}
247
248func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800249 config := TestConfig(buildDir, nil)
250
251 for _, test := range neverallowTests {
Logan Chienee97c3e2018-03-12 16:34:26 +0800252
Paul Duffinb5af6202019-08-05 15:07:57 +0100253 t.Run(test.name, func(t *testing.T) {
254 _, errs := testNeverallow(config, test.fs)
255 CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
Logan Chienee97c3e2018-03-12 16:34:26 +0800256 })
257 }
258}
259
Paul Duffinb5af6202019-08-05 15:07:57 +0100260func testNeverallow(config Config, fs map[string][]byte) (*TestContext, []error) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800261 ctx := NewTestContext()
262 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100263 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100264 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossfd4f7432019-03-05 15:06:16 -0800265 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800266 ctx.PostDepsMutators(registerNeverallowMutator)
267 ctx.Register()
268
269 ctx.MockFileSystem(fs)
270
271 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
272 if len(errs) > 0 {
273 return ctx, errs
274 }
275
276 _, errs = ctx.PrepareBuildActions(config)
277 return ctx, errs
278}
279
Neil Fullerdf5f3562018-10-21 17:19:10 +0100280type mockCcLibraryProperties struct {
Paul Duffinc8111702019-07-22 12:13:55 +0100281 Include_dirs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800282 Vendor_available *bool
Paul Duffin35781882019-07-25 15:41:09 +0100283 Static_libs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800284
285 Vndk struct {
286 Enabled *bool
287 Support_system_process *bool
288 Extends *string
289 }
290
291 Product_variables struct {
292 Enforce_vintf_manifest struct {
293 Cflags []string
294 }
295
296 Treble_linker_namespaces struct {
297 Cflags []string
298 }
299 }
300}
301
302type mockCcLibraryModule struct {
303 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100304 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800305}
306
307func newMockCcLibraryModule() Module {
308 m := &mockCcLibraryModule{}
309 m.AddProperties(&m.properties)
310 InitAndroidModule(m)
311 return m
312}
313
Paul Duffin35781882019-07-25 15:41:09 +0100314type neverallowTestDependencyTag struct {
315 blueprint.BaseDependencyTag
316 name string
317}
318
319var staticDepTag = neverallowTestDependencyTag{name: "static"}
320
321func (c *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
322 for _, lib := range c.properties.Static_libs {
323 ctx.AddDependency(ctx.Module(), staticDepTag, lib)
324 }
325}
326
Logan Chienee97c3e2018-03-12 16:34:26 +0800327func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
328}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100329
330type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100331 Libs []string
332 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100333}
334
335type mockJavaLibraryModule struct {
336 ModuleBase
337 properties mockJavaLibraryProperties
338}
339
340func newMockJavaLibraryModule() Module {
341 m := &mockJavaLibraryModule{}
342 m.AddProperties(&m.properties)
343 InitAndroidModule(m)
344 return m
345}
346
Neil Fullerdf5f3562018-10-21 17:19:10 +0100347func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
348}