blob: 920b9a55c89a0bb46ec5c76427b5ea2cfd9606ca [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 Duffin2ac2bef2019-07-16 14:18:22 +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 },
145 {
146 name: "libhidltransport enforce_vintf_manifest.cflags",
147 fs: map[string][]byte{
148 "Blueprints": []byte(`
149 cc_library {
150 name: "libhidltransport",
151 product_variables: {
152 enforce_vintf_manifest: {
153 cflags: ["-DSHOULD_NOT_EXIST"],
154 },
155 },
156 }`),
157 },
158 expectedError: "",
159 },
160
161 {
162 name: "no treble_linker_namespaces.cflags",
163 fs: map[string][]byte{
164 "Blueprints": []byte(`
165 cc_library {
166 name: "libexample",
167 product_variables: {
168 treble_linker_namespaces: {
169 cflags: ["-DSHOULD_NOT_EXIST"],
170 },
171 },
172 }`),
173 },
174 expectedError: "nothing should care if linker namespaces are enabled or not",
175 },
176 {
177 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
178 fs: map[string][]byte{
179 "Blueprints": []byte(`
180 cc_library {
181 name: "libc_bionic_ndk",
182 product_variables: {
183 treble_linker_namespaces: {
184 cflags: ["-DSHOULD_NOT_EXIST"],
185 },
186 },
187 }`),
188 },
189 expectedError: "",
190 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100191 {
Colin Crossc35c5f92019-03-05 15:06:16 -0800192 name: "java_device_for_host",
193 fs: map[string][]byte{
194 "Blueprints": []byte(`
195 java_device_for_host {
196 name: "device_for_host",
197 libs: ["core-libart"],
198 }`),
199 },
200 expectedError: "java_device_for_host can only be used in whitelisted projects",
201 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100202 // Libcore rule tests
203 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100204 name: "sdk_version: \"none\" inside core libraries",
205 fs: map[string][]byte{
206 "libcore/Blueprints": []byte(`
207 java_library {
208 name: "inside_core_libraries",
209 sdk_version: "none",
210 }`),
211 },
212 },
213 {
214 name: "sdk_version: \"none\" outside core libraries",
215 fs: map[string][]byte{
216 "Blueprints": []byte(`
217 java_library {
218 name: "outside_core_libraries",
219 sdk_version: "none",
220 }`),
221 },
222 expectedError: "module \"outside_core_libraries\": violates neverallow",
223 },
224 {
225 name: "sdk_version: \"current\"",
226 fs: map[string][]byte{
227 "Blueprints": []byte(`
228 java_library {
229 name: "outside_core_libraries",
230 sdk_version: "current",
231 }`),
232 },
233 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800234}
235
236func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800237 config := TestConfig(buildDir, nil)
238
239 for _, test := range neverallowTests {
240 t.Run(test.name, func(t *testing.T) {
241 _, errs := testNeverallow(t, config, test.fs)
242
243 if test.expectedError == "" {
244 FailIfErrored(t, errs)
245 } else {
246 FailIfNoMatchingErrors(t, test.expectedError, errs)
247 }
248 })
249 }
250}
251
252func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
253 ctx := NewTestContext()
254 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100255 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100256 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossc35c5f92019-03-05 15:06:16 -0800257 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800258 ctx.PostDepsMutators(registerNeverallowMutator)
259 ctx.Register()
260
261 ctx.MockFileSystem(fs)
262
263 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
264 if len(errs) > 0 {
265 return ctx, errs
266 }
267
268 _, errs = ctx.PrepareBuildActions(config)
269 return ctx, errs
270}
271
Neil Fullerdf5f3562018-10-21 17:19:10 +0100272type mockCcLibraryProperties struct {
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100273 Include_dirs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800274 Vendor_available *bool
Paul Duffin35781882019-07-25 15:41:09 +0100275 Static_libs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800276
277 Vndk struct {
278 Enabled *bool
279 Support_system_process *bool
280 Extends *string
281 }
282
283 Product_variables struct {
284 Enforce_vintf_manifest struct {
285 Cflags []string
286 }
287
288 Treble_linker_namespaces struct {
289 Cflags []string
290 }
291 }
292}
293
294type mockCcLibraryModule struct {
295 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100296 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800297}
298
299func newMockCcLibraryModule() Module {
300 m := &mockCcLibraryModule{}
301 m.AddProperties(&m.properties)
302 InitAndroidModule(m)
303 return m
304}
305
Paul Duffin35781882019-07-25 15:41:09 +0100306type neverallowTestDependencyTag struct {
307 blueprint.BaseDependencyTag
308 name string
309}
310
311var staticDepTag = neverallowTestDependencyTag{name: "static"}
312
313func (c *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
314 for _, lib := range c.properties.Static_libs {
315 ctx.AddDependency(ctx.Module(), staticDepTag, lib)
316 }
317}
318
Logan Chienee97c3e2018-03-12 16:34:26 +0800319func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
320}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100321
322type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100323 Libs []string
324 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100325}
326
327type mockJavaLibraryModule struct {
328 ModuleBase
329 properties mockJavaLibraryProperties
330}
331
332func newMockJavaLibraryModule() Module {
333 m := &mockJavaLibraryModule{}
334 m.AddProperties(&m.properties)
335 InitAndroidModule(m)
336 return m
337}
338
Neil Fullerdf5f3562018-10-21 17:19:10 +0100339func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
340}