blob: ee3c94f0c2cbe303942f36e4466c6885a2a6537b [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 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100151 name: "sdk_version: \"none\" inside core libraries",
152 fs: map[string][]byte{
153 "libcore/Blueprints": []byte(`
154 java_library {
155 name: "inside_core_libraries",
156 sdk_version: "none",
157 }`),
158 },
159 },
160 {
161 name: "sdk_version: \"none\" outside core libraries",
162 fs: map[string][]byte{
163 "Blueprints": []byte(`
164 java_library {
165 name: "outside_core_libraries",
166 sdk_version: "none",
167 }`),
168 },
169 expectedError: "module \"outside_core_libraries\": violates neverallow",
170 },
171 {
172 name: "sdk_version: \"current\"",
173 fs: map[string][]byte{
174 "Blueprints": []byte(`
175 java_library {
176 name: "outside_core_libraries",
177 sdk_version: "current",
178 }`),
179 },
180 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800181}
182
183func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800184 config := TestConfig(buildDir, nil)
185
186 for _, test := range neverallowTests {
187 t.Run(test.name, func(t *testing.T) {
188 _, errs := testNeverallow(t, config, test.fs)
189
190 if test.expectedError == "" {
191 FailIfErrored(t, errs)
192 } else {
193 FailIfNoMatchingErrors(t, test.expectedError, errs)
194 }
195 })
196 }
197}
198
199func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
200 ctx := NewTestContext()
201 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100202 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100203 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossc35c5f92019-03-05 15:06:16 -0800204 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800205 ctx.PostDepsMutators(registerNeverallowMutator)
206 ctx.Register()
207
208 ctx.MockFileSystem(fs)
209
210 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
211 if len(errs) > 0 {
212 return ctx, errs
213 }
214
215 _, errs = ctx.PrepareBuildActions(config)
216 return ctx, errs
217}
218
Neil Fullerdf5f3562018-10-21 17:19:10 +0100219type mockCcLibraryProperties struct {
Logan Chienee97c3e2018-03-12 16:34:26 +0800220 Vendor_available *bool
221
222 Vndk struct {
223 Enabled *bool
224 Support_system_process *bool
225 Extends *string
226 }
227
228 Product_variables struct {
229 Enforce_vintf_manifest struct {
230 Cflags []string
231 }
232
233 Treble_linker_namespaces struct {
234 Cflags []string
235 }
236 }
237}
238
239type mockCcLibraryModule struct {
240 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100241 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800242}
243
244func newMockCcLibraryModule() Module {
245 m := &mockCcLibraryModule{}
246 m.AddProperties(&m.properties)
247 InitAndroidModule(m)
248 return m
249}
250
Logan Chienee97c3e2018-03-12 16:34:26 +0800251func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
252}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100253
254type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100255 Libs []string
256 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100257}
258
259type mockJavaLibraryModule struct {
260 ModuleBase
261 properties mockJavaLibraryProperties
262}
263
264func newMockJavaLibraryModule() Module {
265 m := &mockJavaLibraryModule{}
266 m.AddProperties(&m.properties)
267 InitAndroidModule(m)
268 return m
269}
270
Neil Fullerdf5f3562018-10-21 17:19:10 +0100271func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
272}