blob: 9c43d53e6366ee755a49039a13453f2a1444788f [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 Duffinb815ada2019-06-11 13:54:26 +0100181 // java_library_host rule tests
182 {
183 name: "java_library_host with no_standard_libs: true",
184 fs: map[string][]byte{
185 "libcore/Blueprints": []byte(`
186 java_library_host {
187 name: "inside_core_libraries",
188 no_standard_libs: true,
189 }`),
190 },
191 expectedError: "module \"inside_core_libraries\": violates neverallow",
192 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800193}
194
195func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800196 config := TestConfig(buildDir, nil)
197
198 for _, test := range neverallowTests {
199 t.Run(test.name, func(t *testing.T) {
200 _, errs := testNeverallow(t, config, test.fs)
201
202 if test.expectedError == "" {
203 FailIfErrored(t, errs)
204 } else {
205 FailIfNoMatchingErrors(t, test.expectedError, errs)
206 }
207 })
208 }
209}
210
211func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
212 ctx := NewTestContext()
213 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100214 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100215 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossc35c5f92019-03-05 15:06:16 -0800216 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800217 ctx.PostDepsMutators(registerNeverallowMutator)
218 ctx.Register()
219
220 ctx.MockFileSystem(fs)
221
222 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
223 if len(errs) > 0 {
224 return ctx, errs
225 }
226
227 _, errs = ctx.PrepareBuildActions(config)
228 return ctx, errs
229}
230
Neil Fullerdf5f3562018-10-21 17:19:10 +0100231type mockCcLibraryProperties struct {
Logan Chienee97c3e2018-03-12 16:34:26 +0800232 Vendor_available *bool
233
234 Vndk struct {
235 Enabled *bool
236 Support_system_process *bool
237 Extends *string
238 }
239
240 Product_variables struct {
241 Enforce_vintf_manifest struct {
242 Cflags []string
243 }
244
245 Treble_linker_namespaces struct {
246 Cflags []string
247 }
248 }
249}
250
251type mockCcLibraryModule struct {
252 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100253 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800254}
255
256func newMockCcLibraryModule() Module {
257 m := &mockCcLibraryModule{}
258 m.AddProperties(&m.properties)
259 InitAndroidModule(m)
260 return m
261}
262
Logan Chienee97c3e2018-03-12 16:34:26 +0800263func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
264}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100265
266type mockJavaLibraryProperties struct {
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100267 Libs []string
268 No_standard_libs *bool
Neil Fullerdf5f3562018-10-21 17:19:10 +0100269}
270
271type mockJavaLibraryModule struct {
272 ModuleBase
273 properties mockJavaLibraryProperties
274}
275
276func newMockJavaLibraryModule() Module {
277 m := &mockJavaLibraryModule{}
278 m.AddProperties(&m.properties)
279 InitAndroidModule(m)
280 return m
281}
282
Neil Fullerdf5f3562018-10-21 17:19:10 +0100283func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
284}