blob: 02b4362114c497eeb343533535f5af93da1b1ad2 [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}{
Paul Duffin2ac2bef2019-07-16 14:18:22 +010026 // include_dir rule tests
27 {
28 name: "include_dir not allowed to reference art",
29 fs: map[string][]byte{
30 "other/Blueprints": []byte(`
31 cc_library {
32 name: "libother",
33 include_dirs: ["art/libdexfile/include"],
34 }`),
35 },
36 expectedError: "all usages of 'art' have been migrated",
37 },
38 {
39 name: "include_dir can reference another location",
40 fs: map[string][]byte{
41 "other/Blueprints": []byte(`
42 cc_library {
43 name: "libother",
44 include_dirs: ["another/include"],
45 }`),
46 },
47 },
48 // Treble rule tests
Logan Chienee97c3e2018-03-12 16:34:26 +080049 {
50 name: "no vndk.enabled under vendor directory",
51 fs: map[string][]byte{
52 "vendor/Blueprints": []byte(`
53 cc_library {
54 name: "libvndk",
55 vendor_available: true,
56 vndk: {
57 enabled: true,
58 },
59 }`),
60 },
61 expectedError: "VNDK can never contain a library that is device dependent",
62 },
63 {
64 name: "no vndk.enabled under device directory",
65 fs: map[string][]byte{
66 "device/Blueprints": []byte(`
67 cc_library {
68 name: "libvndk",
69 vendor_available: true,
70 vndk: {
71 enabled: true,
72 },
73 }`),
74 },
75 expectedError: "VNDK can never contain a library that is device dependent",
76 },
Logan Chienaf29bad2018-03-12 16:35:58 +080077 {
78 name: "vndk-ext under vendor or device directory",
79 fs: map[string][]byte{
80 "device/Blueprints": []byte(`
81 cc_library {
82 name: "libvndk1_ext",
83 vendor: true,
84 vndk: {
85 enabled: true,
86 },
87 }`),
88 "vendor/Blueprints": []byte(`
89 cc_library {
90 name: "libvndk2_ext",
91 vendor: true,
92 vndk: {
93 enabled: true,
94 },
95 }`),
96 },
97 expectedError: "",
98 },
Logan Chienee97c3e2018-03-12 16:34:26 +080099
100 {
101 name: "no enforce_vintf_manifest.cflags",
102 fs: map[string][]byte{
103 "Blueprints": []byte(`
104 cc_library {
105 name: "libexample",
106 product_variables: {
107 enforce_vintf_manifest: {
108 cflags: ["-DSHOULD_NOT_EXIST"],
109 },
110 },
111 }`),
112 },
113 expectedError: "manifest enforcement should be independent",
114 },
115 {
116 name: "libhidltransport enforce_vintf_manifest.cflags",
117 fs: map[string][]byte{
118 "Blueprints": []byte(`
119 cc_library {
120 name: "libhidltransport",
121 product_variables: {
122 enforce_vintf_manifest: {
123 cflags: ["-DSHOULD_NOT_EXIST"],
124 },
125 },
126 }`),
127 },
128 expectedError: "",
129 },
130
131 {
132 name: "no treble_linker_namespaces.cflags",
133 fs: map[string][]byte{
134 "Blueprints": []byte(`
135 cc_library {
136 name: "libexample",
137 product_variables: {
138 treble_linker_namespaces: {
139 cflags: ["-DSHOULD_NOT_EXIST"],
140 },
141 },
142 }`),
143 },
144 expectedError: "nothing should care if linker namespaces are enabled or not",
145 },
146 {
147 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
148 fs: map[string][]byte{
149 "Blueprints": []byte(`
150 cc_library {
151 name: "libc_bionic_ndk",
152 product_variables: {
153 treble_linker_namespaces: {
154 cflags: ["-DSHOULD_NOT_EXIST"],
155 },
156 },
157 }`),
158 },
159 expectedError: "",
160 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100161 {
Colin Crossc35c5f92019-03-05 15:06:16 -0800162 name: "java_device_for_host",
163 fs: map[string][]byte{
164 "Blueprints": []byte(`
165 java_device_for_host {
166 name: "device_for_host",
167 libs: ["core-libart"],
168 }`),
169 },
170 expectedError: "java_device_for_host can only be used in whitelisted projects",
171 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100172 // Libcore rule tests
173 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100174 name: "sdk_version: \"none\" inside core libraries",
175 fs: map[string][]byte{
176 "libcore/Blueprints": []byte(`
177 java_library {
178 name: "inside_core_libraries",
179 sdk_version: "none",
180 }`),
181 },
182 },
183 {
184 name: "sdk_version: \"none\" outside core libraries",
185 fs: map[string][]byte{
186 "Blueprints": []byte(`
187 java_library {
188 name: "outside_core_libraries",
189 sdk_version: "none",
190 }`),
191 },
192 expectedError: "module \"outside_core_libraries\": violates neverallow",
193 },
194 {
195 name: "sdk_version: \"current\"",
196 fs: map[string][]byte{
197 "Blueprints": []byte(`
198 java_library {
199 name: "outside_core_libraries",
200 sdk_version: "current",
201 }`),
202 },
203 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800204}
205
206func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800207 config := TestConfig(buildDir, nil)
208
209 for _, test := range neverallowTests {
210 t.Run(test.name, func(t *testing.T) {
211 _, errs := testNeverallow(t, config, test.fs)
212
213 if test.expectedError == "" {
214 FailIfErrored(t, errs)
215 } else {
216 FailIfNoMatchingErrors(t, test.expectedError, errs)
217 }
218 })
219 }
220}
221
222func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
223 ctx := NewTestContext()
224 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100225 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100226 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossc35c5f92019-03-05 15:06:16 -0800227 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800228 ctx.PostDepsMutators(registerNeverallowMutator)
229 ctx.Register()
230
231 ctx.MockFileSystem(fs)
232
233 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
234 if len(errs) > 0 {
235 return ctx, errs
236 }
237
238 _, errs = ctx.PrepareBuildActions(config)
239 return ctx, errs
240}
241
Neil Fullerdf5f3562018-10-21 17:19:10 +0100242type mockCcLibraryProperties struct {
Paul Duffin2ac2bef2019-07-16 14:18:22 +0100243 Include_dirs []string
Logan Chienee97c3e2018-03-12 16:34:26 +0800244 Vendor_available *bool
245
246 Vndk struct {
247 Enabled *bool
248 Support_system_process *bool
249 Extends *string
250 }
251
252 Product_variables struct {
253 Enforce_vintf_manifest struct {
254 Cflags []string
255 }
256
257 Treble_linker_namespaces struct {
258 Cflags []string
259 }
260 }
261}
262
263type mockCcLibraryModule struct {
264 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100265 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800266}
267
268func newMockCcLibraryModule() Module {
269 m := &mockCcLibraryModule{}
270 m.AddProperties(&m.properties)
271 InitAndroidModule(m)
272 return m
273}
274
Logan Chienee97c3e2018-03-12 16:34:26 +0800275func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
276}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100277
278type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100279 Libs []string
280 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100281}
282
283type mockJavaLibraryModule struct {
284 ModuleBase
285 properties mockJavaLibraryProperties
286}
287
288func newMockJavaLibraryModule() Module {
289 m := &mockJavaLibraryModule{}
290 m.AddProperties(&m.properties)
291 InitAndroidModule(m)
292 return m
293}
294
Neil Fullerdf5f3562018-10-21 17:19:10 +0100295func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
296}