blob: 17e40f0bd6ffb51f6f29cdfb6d81dd6acccb7116 [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 },
Logan Chienee97c3e2018-03-12 16:34:26 +080092
93 {
94 name: "no treble_linker_namespaces.cflags",
95 fs: map[string][]byte{
96 "Blueprints": []byte(`
97 cc_library {
98 name: "libexample",
99 product_variables: {
100 treble_linker_namespaces: {
101 cflags: ["-DSHOULD_NOT_EXIST"],
102 },
103 },
104 }`),
105 },
106 expectedError: "nothing should care if linker namespaces are enabled or not",
107 },
108 {
109 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
110 fs: map[string][]byte{
111 "Blueprints": []byte(`
112 cc_library {
113 name: "libc_bionic_ndk",
114 product_variables: {
115 treble_linker_namespaces: {
116 cflags: ["-DSHOULD_NOT_EXIST"],
117 },
118 },
119 }`),
120 },
121 expectedError: "",
122 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100123 {
Dongwon Kang50a299f2019-02-04 09:00:51 -0800124 name: "dependency on updatable-media",
125 fs: map[string][]byte{
126 "Blueprints": []byte(`
127 java_library {
128 name: "needs_updatable_media",
129 libs: ["updatable-media"],
130 }`),
131 },
132 expectedError: "updatable-media includes private APIs. Use updatable_media_stubs instead.",
133 },
Colin Crossfd4f7432019-03-05 15:06:16 -0800134 {
135 name: "java_device_for_host",
136 fs: map[string][]byte{
137 "Blueprints": []byte(`
138 java_device_for_host {
139 name: "device_for_host",
140 libs: ["core-libart"],
141 }`),
142 },
143 expectedError: "java_device_for_host can only be used in whitelisted projects",
144 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100145 // Libcore rule tests
146 {
Paul Duffin52d398a2019-06-11 12:31:14 +0100147 name: "sdk_version: \"none\" inside core libraries",
148 fs: map[string][]byte{
149 "libcore/Blueprints": []byte(`
150 java_library {
151 name: "inside_core_libraries",
152 sdk_version: "none",
153 }`),
154 },
155 },
156 {
157 name: "sdk_version: \"none\" outside core libraries",
158 fs: map[string][]byte{
159 "Blueprints": []byte(`
160 java_library {
161 name: "outside_core_libraries",
162 sdk_version: "none",
163 }`),
164 },
165 expectedError: "module \"outside_core_libraries\": violates neverallow",
166 },
167 {
168 name: "sdk_version: \"current\"",
169 fs: map[string][]byte{
170 "Blueprints": []byte(`
171 java_library {
172 name: "outside_core_libraries",
173 sdk_version: "current",
174 }`),
175 },
176 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800177}
178
179func TestNeverallow(t *testing.T) {
Logan Chienee97c3e2018-03-12 16:34:26 +0800180 config := TestConfig(buildDir, nil)
181
182 for _, test := range neverallowTests {
183 t.Run(test.name, func(t *testing.T) {
184 _, errs := testNeverallow(t, config, test.fs)
185
186 if test.expectedError == "" {
187 FailIfErrored(t, errs)
188 } else {
189 FailIfNoMatchingErrors(t, test.expectedError, errs)
190 }
191 })
192 }
193}
194
195func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
196 ctx := NewTestContext()
197 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100198 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Paul Duffinb815ada2019-06-11 13:54:26 +0100199 ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossfd4f7432019-03-05 15:06:16 -0800200 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800201 ctx.PostDepsMutators(registerNeverallowMutator)
202 ctx.Register()
203
204 ctx.MockFileSystem(fs)
205
206 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
207 if len(errs) > 0 {
208 return ctx, errs
209 }
210
211 _, errs = ctx.PrepareBuildActions(config)
212 return ctx, errs
213}
214
Neil Fullerdf5f3562018-10-21 17:19:10 +0100215type mockCcLibraryProperties struct {
Logan Chienee97c3e2018-03-12 16:34:26 +0800216 Vendor_available *bool
217
218 Vndk struct {
219 Enabled *bool
220 Support_system_process *bool
221 Extends *string
222 }
223
224 Product_variables struct {
225 Enforce_vintf_manifest struct {
226 Cflags []string
227 }
228
229 Treble_linker_namespaces struct {
230 Cflags []string
231 }
232 }
233}
234
235type mockCcLibraryModule struct {
236 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100237 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800238}
239
240func newMockCcLibraryModule() Module {
241 m := &mockCcLibraryModule{}
242 m.AddProperties(&m.properties)
243 InitAndroidModule(m)
244 return m
245}
246
Logan Chienee97c3e2018-03-12 16:34:26 +0800247func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
248}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100249
250type mockJavaLibraryProperties struct {
Paul Duffina3d09862019-06-11 13:40:47 +0100251 Libs []string
252 Sdk_version *string
Neil Fullerdf5f3562018-10-21 17:19:10 +0100253}
254
255type mockJavaLibraryModule struct {
256 ModuleBase
257 properties mockJavaLibraryProperties
258}
259
260func newMockJavaLibraryModule() Module {
261 m := &mockJavaLibraryModule{}
262 m.AddProperties(&m.properties)
263 InitAndroidModule(m)
264 return m
265}
266
Neil Fullerdf5f3562018-10-21 17:19:10 +0100267func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
268}