blob: 40ccf14a1c79201139dfaed8e993b28ae21a8e91 [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 },
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))
Colin Crossc35c5f92019-03-05 15:06:16 -0800203 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800204 ctx.PostDepsMutators(registerNeverallowMutator)
205 ctx.Register()
206
207 ctx.MockFileSystem(fs)
208
209 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
210 if len(errs) > 0 {
211 return ctx, errs
212 }
213
214 _, errs = ctx.PrepareBuildActions(config)
215 return ctx, errs
216}
217
Neil Fullerdf5f3562018-10-21 17:19:10 +0100218type mockCcLibraryProperties struct {
Logan Chienee97c3e2018-03-12 16:34:26 +0800219 Vendor_available *bool
220
221 Vndk struct {
222 Enabled *bool
223 Support_system_process *bool
224 Extends *string
225 }
226
227 Product_variables struct {
228 Enforce_vintf_manifest struct {
229 Cflags []string
230 }
231
232 Treble_linker_namespaces struct {
233 Cflags []string
234 }
235 }
236}
237
238type mockCcLibraryModule struct {
239 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100240 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800241}
242
243func newMockCcLibraryModule() Module {
244 m := &mockCcLibraryModule{}
245 m.AddProperties(&m.properties)
246 InitAndroidModule(m)
247 return m
248}
249
Logan Chienee97c3e2018-03-12 16:34:26 +0800250func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
251}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100252
253type mockJavaLibraryProperties struct {
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100254 Libs []string
255 No_standard_libs *bool
Neil Fullerdf5f3562018-10-21 17:19:10 +0100256}
257
258type mockJavaLibraryModule struct {
259 ModuleBase
260 properties mockJavaLibraryProperties
261}
262
263func newMockJavaLibraryModule() Module {
264 m := &mockJavaLibraryModule{}
265 m.AddProperties(&m.properties)
266 InitAndroidModule(m)
267 return m
268}
269
Neil Fullerdf5f3562018-10-21 17:19:10 +0100270func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
271}