blob: 62c5142d238a581940f67221e17edaf5b9b157ce [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"
19)
20
21var neverallowTests = []struct {
22 name string
23 fs map[string][]byte
24 expectedError string
25}{
26 {
27 name: "no vndk.enabled under vendor directory",
28 fs: map[string][]byte{
29 "vendor/Blueprints": []byte(`
30 cc_library {
31 name: "libvndk",
32 vendor_available: true,
33 vndk: {
34 enabled: true,
35 },
36 }`),
37 },
38 expectedError: "VNDK can never contain a library that is device dependent",
39 },
40 {
41 name: "no vndk.enabled under device directory",
42 fs: map[string][]byte{
43 "device/Blueprints": []byte(`
44 cc_library {
45 name: "libvndk",
46 vendor_available: true,
47 vndk: {
48 enabled: true,
49 },
50 }`),
51 },
52 expectedError: "VNDK can never contain a library that is device dependent",
53 },
Logan Chienaf29bad2018-03-12 16:35:58 +080054 {
55 name: "vndk-ext under vendor or device directory",
56 fs: map[string][]byte{
57 "device/Blueprints": []byte(`
58 cc_library {
59 name: "libvndk1_ext",
60 vendor: true,
61 vndk: {
62 enabled: true,
63 },
64 }`),
65 "vendor/Blueprints": []byte(`
66 cc_library {
67 name: "libvndk2_ext",
68 vendor: true,
69 vndk: {
70 enabled: true,
71 },
72 }`),
73 },
74 expectedError: "",
75 },
Logan Chienee97c3e2018-03-12 16:34:26 +080076
77 {
78 name: "no enforce_vintf_manifest.cflags",
79 fs: map[string][]byte{
80 "Blueprints": []byte(`
81 cc_library {
82 name: "libexample",
83 product_variables: {
84 enforce_vintf_manifest: {
85 cflags: ["-DSHOULD_NOT_EXIST"],
86 },
87 },
88 }`),
89 },
90 expectedError: "manifest enforcement should be independent",
91 },
92 {
93 name: "libhidltransport enforce_vintf_manifest.cflags",
94 fs: map[string][]byte{
95 "Blueprints": []byte(`
96 cc_library {
97 name: "libhidltransport",
98 product_variables: {
99 enforce_vintf_manifest: {
100 cflags: ["-DSHOULD_NOT_EXIST"],
101 },
102 },
103 }`),
104 },
105 expectedError: "",
106 },
107
108 {
109 name: "no treble_linker_namespaces.cflags",
110 fs: map[string][]byte{
111 "Blueprints": []byte(`
112 cc_library {
113 name: "libexample",
114 product_variables: {
115 treble_linker_namespaces: {
116 cflags: ["-DSHOULD_NOT_EXIST"],
117 },
118 },
119 }`),
120 },
121 expectedError: "nothing should care if linker namespaces are enabled or not",
122 },
123 {
124 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
125 fs: map[string][]byte{
126 "Blueprints": []byte(`
127 cc_library {
128 name: "libc_bionic_ndk",
129 product_variables: {
130 treble_linker_namespaces: {
131 cflags: ["-DSHOULD_NOT_EXIST"],
132 },
133 },
134 }`),
135 },
136 expectedError: "",
137 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100138 {
Colin Crossc35c5f92019-03-05 15:06:16 -0800139 name: "java_device_for_host",
140 fs: map[string][]byte{
141 "Blueprints": []byte(`
142 java_device_for_host {
143 name: "device_for_host",
144 libs: ["core-libart"],
145 }`),
146 },
147 expectedError: "java_device_for_host can only be used in whitelisted projects",
148 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100149 // Libcore rule tests
150 {
151 name: "no_standard_libs: true inside core libraries",
152 fs: map[string][]byte{
153 "libcore/Blueprints": []byte(`
154 java_library {
155 name: "inside_core_libraries",
156 no_standard_libs: true,
157 }`),
158 },
159 },
160 {
161 name: "no_standard_libs: true outside core libraries",
162 fs: map[string][]byte{
163 "Blueprints": []byte(`
164 java_library {
165 name: "outside_core_libraries",
166 no_standard_libs: true,
167 }`),
168 },
169 expectedError: "module \"outside_core_libraries\": violates neverallow",
170 },
171 {
172 name: "no_standard_libs: false",
173 fs: map[string][]byte{
174 "Blueprints": []byte(`
175 java_library {
176 name: "outside_core_libraries",
177 no_standard_libs: false,
178 }`),
179 },
180 },
Paul Duffin52d398a2019-06-11 12:31:14 +0100181 {
182 name: "sdk_version: \"none\" inside core libraries",
183 fs: map[string][]byte{
184 "libcore/Blueprints": []byte(`
185 java_library {
186 name: "inside_core_libraries",
187 sdk_version: "none",
188 }`),
189 },
190 },
191 {
192 name: "sdk_version: \"none\" outside core libraries",
193 fs: map[string][]byte{
194 "Blueprints": []byte(`
195 java_library {
196 name: "outside_core_libraries",
197 sdk_version: "none",
198 }`),
199 },
200 expectedError: "module \"outside_core_libraries\": violates neverallow",
201 },
202 {
203 name: "sdk_version: \"current\"",
204 fs: map[string][]byte{
205 "Blueprints": []byte(`
206 java_library {
207 name: "outside_core_libraries",
208 sdk_version: "current",
209 }`),
210 },
211 },
Paul Duffinb815ada2019-06-11 13:54:26 +0100212 // java_library_host rule tests
213 {
214 name: "java_library_host with no_standard_libs: true",
215 fs: map[string][]byte{
216 "libcore/Blueprints": []byte(`
217 java_library_host {
218 name: "inside_core_libraries",
219 no_standard_libs: true,
220 }`),
221 },
222 expectedError: "module \"inside_core_libraries\": violates neverallow",
223 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800224}
225
226func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800227 config := TestConfig(buildDir, nil)
228
229 for _, test := range neverallowTests {
230 t.Run(test.name, func(t *testing.T) {
231 _, errs := testNeverallow(t, config, test.fs)
232
233 if test.expectedError == "" {
234 FailIfErrored(t, errs)
235 } else {
236 FailIfNoMatchingErrors(t, test.expectedError, errs)
237 }
238 })
239 }
240}
241
242func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
243 ctx := NewTestContext()
244 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100245 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100246 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossc35c5f92019-03-05 15:06:16 -0800247 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800248 ctx.PostDepsMutators(registerNeverallowMutator)
249 ctx.Register()
250
251 ctx.MockFileSystem(fs)
252
253 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
254 if len(errs) > 0 {
255 return ctx, errs
256 }
257
258 _, errs = ctx.PrepareBuildActions(config)
259 return ctx, errs
260}
261
Neil Fullerdf5f3562018-10-21 17:19:10 +0100262type mockCcLibraryProperties struct {
Logan Chienee97c3e2018-03-12 16:34:26 +0800263 Vendor_available *bool
264
265 Vndk struct {
266 Enabled *bool
267 Support_system_process *bool
268 Extends *string
269 }
270
271 Product_variables struct {
272 Enforce_vintf_manifest struct {
273 Cflags []string
274 }
275
276 Treble_linker_namespaces struct {
277 Cflags []string
278 }
279 }
280}
281
282type mockCcLibraryModule struct {
283 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100284 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800285}
286
287func newMockCcLibraryModule() Module {
288 m := &mockCcLibraryModule{}
289 m.AddProperties(&m.properties)
290 InitAndroidModule(m)
291 return m
292}
293
Logan Chienee97c3e2018-03-12 16:34:26 +0800294func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
295}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100296
297type mockJavaLibraryProperties struct {
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100298 Libs []string
299 No_standard_libs *bool
Paul Duffin52d398a2019-06-11 12:31:14 +0100300 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100301}
302
303type mockJavaLibraryModule struct {
304 ModuleBase
305 properties mockJavaLibraryProperties
306}
307
308func newMockJavaLibraryModule() Module {
309 m := &mockJavaLibraryModule{}
310 m.AddProperties(&m.properties)
311 InitAndroidModule(m)
312 return m
313}
314
Neil Fullerdf5f3562018-10-21 17:19:10 +0100315func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
316}