blob: 1a75e3d25a69ad06641674409446de29a505e615 [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 Duffin91e38192019-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 Duffin91e38192019-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 Duffin2ac2bef2019-07-16 14:18:22 +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 Duffin91e38192019-08-05 15:07:57 +010068 expectedErrors: []string{
69 "all usages of 'art' have been migrated",
70 },
Paul Duffin2ac2bef2019-07-16 14:18:22 +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 Duffin91e38192019-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 Duffin91e38192019-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 Duffin91e38192019-08-05 15:07:57 +0100150 expectedErrors: []string{
151 "manifest enforcement should be independent",
152 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800153 },
154 {
155 name: "libhidltransport enforce_vintf_manifest.cflags",
156 fs: map[string][]byte{
157 "Blueprints": []byte(`
158 cc_library {
159 name: "libhidltransport",
160 product_variables: {
161 enforce_vintf_manifest: {
162 cflags: ["-DSHOULD_NOT_EXIST"],
163 },
164 },
165 }`),
166 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800167 },
168
169 {
170 name: "no treble_linker_namespaces.cflags",
171 fs: map[string][]byte{
172 "Blueprints": []byte(`
173 cc_library {
174 name: "libexample",
175 product_variables: {
176 treble_linker_namespaces: {
177 cflags: ["-DSHOULD_NOT_EXIST"],
178 },
179 },
180 }`),
181 },
Paul Duffin91e38192019-08-05 15:07:57 +0100182 expectedErrors: []string{
183 "nothing should care if linker namespaces are enabled or not",
184 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800185 },
186 {
187 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
188 fs: map[string][]byte{
189 "Blueprints": []byte(`
190 cc_library {
191 name: "libc_bionic_ndk",
192 product_variables: {
193 treble_linker_namespaces: {
194 cflags: ["-DSHOULD_NOT_EXIST"],
195 },
196 },
197 }`),
198 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800199 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100200 {
Colin Crossc35c5f92019-03-05 15:06:16 -0800201 name: "java_device_for_host",
202 fs: map[string][]byte{
203 "Blueprints": []byte(`
204 java_device_for_host {
205 name: "device_for_host",
206 libs: ["core-libart"],
207 }`),
208 },
Paul Duffin91e38192019-08-05 15:07:57 +0100209 expectedErrors: []string{
210 "java_device_for_host can only be used in whitelisted projects",
211 },
Colin Crossc35c5f92019-03-05 15:06:16 -0800212 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100213 // Libcore rule tests
214 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100215 name: "sdk_version: \"none\" inside core libraries",
216 fs: map[string][]byte{
217 "libcore/Blueprints": []byte(`
218 java_library {
219 name: "inside_core_libraries",
220 sdk_version: "none",
221 }`),
222 },
223 },
224 {
225 name: "sdk_version: \"none\" outside core libraries",
226 fs: map[string][]byte{
227 "Blueprints": []byte(`
228 java_library {
229 name: "outside_core_libraries",
230 sdk_version: "none",
231 }`),
232 },
Paul Duffin91e38192019-08-05 15:07:57 +0100233 expectedErrors: []string{
234 "module \"outside_core_libraries\": violates neverallow",
235 },
Paul Duffin52d398a2019-06-11 12:31:14 +0100236 },
237 {
238 name: "sdk_version: \"current\"",
239 fs: map[string][]byte{
240 "Blueprints": []byte(`
241 java_library {
242 name: "outside_core_libraries",
243 sdk_version: "current",
244 }`),
245 },
246 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800247}
248
249func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800250 config := TestConfig(buildDir, nil)
251
252 for _, test := range neverallowTests {
Logan Chienee97c3e2018-03-12 16:34:26 +0800253
Paul Duffin91e38192019-08-05 15:07:57 +0100254 t.Run(test.name, func(t *testing.T) {
255 _, errs := testNeverallow(config, test.fs)
256 CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
Logan Chienee97c3e2018-03-12 16:34:26 +0800257 })
258 }
259}
260
Paul Duffin91e38192019-08-05 15:07:57 +0100261func testNeverallow(config Config, fs map[string][]byte) (*TestContext, []error) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800262 ctx := NewTestContext()
263 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100264 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100265 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossc35c5f92019-03-05 15:06:16 -0800266 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800267 ctx.PostDepsMutators(registerNeverallowMutator)
268 ctx.Register()
269
270 ctx.MockFileSystem(fs)
271
272 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
273 if len(errs) > 0 {
274 return ctx, errs
275 }
276
277 _, errs = ctx.PrepareBuildActions(config)
278 return ctx, errs
279}
280
Neil Fullerdf5f3562018-10-21 17:19:10 +0100281type mockCcLibraryProperties struct {
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100282 Include_dirs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800283 Vendor_available *bool
Paul Duffin35781882019-07-25 15:41:09 +0100284 Static_libs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800285
286 Vndk struct {
287 Enabled *bool
288 Support_system_process *bool
289 Extends *string
290 }
291
292 Product_variables struct {
293 Enforce_vintf_manifest struct {
294 Cflags []string
295 }
296
297 Treble_linker_namespaces struct {
298 Cflags []string
299 }
300 }
301}
302
303type mockCcLibraryModule struct {
304 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100305 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800306}
307
308func newMockCcLibraryModule() Module {
309 m := &mockCcLibraryModule{}
310 m.AddProperties(&m.properties)
311 InitAndroidModule(m)
312 return m
313}
314
Paul Duffin35781882019-07-25 15:41:09 +0100315type neverallowTestDependencyTag struct {
316 blueprint.BaseDependencyTag
317 name string
318}
319
320var staticDepTag = neverallowTestDependencyTag{name: "static"}
321
322func (c *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
323 for _, lib := range c.properties.Static_libs {
324 ctx.AddDependency(ctx.Module(), staticDepTag, lib)
325 }
326}
327
Logan Chienee97c3e2018-03-12 16:34:26 +0800328func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
329}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100330
331type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100332 Libs []string
333 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100334}
335
336type mockJavaLibraryModule struct {
337 ModuleBase
338 properties mockJavaLibraryProperties
339}
340
341func newMockJavaLibraryModule() Module {
342 m := &mockJavaLibraryModule{}
343 m.AddProperties(&m.properties)
344 InitAndroidModule(m)
345 return m
346}
347
Neil Fullerdf5f3562018-10-21 17:19:10 +0100348func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
349}