blob: c60de217c87cf93ec01dc4559099ae5431f34280 [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 (
18 "io/ioutil"
19 "os"
20 "testing"
21)
22
23var neverallowTests = []struct {
24 name string
25 fs map[string][]byte
26 expectedError string
27}{
28 {
29 name: "no vndk.enabled under vendor directory",
30 fs: map[string][]byte{
31 "vendor/Blueprints": []byte(`
32 cc_library {
33 name: "libvndk",
34 vendor_available: true,
35 vndk: {
36 enabled: true,
37 },
38 }`),
39 },
40 expectedError: "VNDK can never contain a library that is device dependent",
41 },
42 {
43 name: "no vndk.enabled under device directory",
44 fs: map[string][]byte{
45 "device/Blueprints": []byte(`
46 cc_library {
47 name: "libvndk",
48 vendor_available: true,
49 vndk: {
50 enabled: true,
51 },
52 }`),
53 },
54 expectedError: "VNDK can never contain a library that is device dependent",
55 },
Logan Chienaf29bad2018-03-12 16:35:58 +080056 {
57 name: "vndk-ext under vendor or device directory",
58 fs: map[string][]byte{
59 "device/Blueprints": []byte(`
60 cc_library {
61 name: "libvndk1_ext",
62 vendor: true,
63 vndk: {
64 enabled: true,
65 },
66 }`),
67 "vendor/Blueprints": []byte(`
68 cc_library {
69 name: "libvndk2_ext",
70 vendor: true,
71 vndk: {
72 enabled: true,
73 },
74 }`),
75 },
76 expectedError: "",
77 },
Logan Chienee97c3e2018-03-12 16:34:26 +080078
79 {
80 name: "no enforce_vintf_manifest.cflags",
81 fs: map[string][]byte{
82 "Blueprints": []byte(`
83 cc_library {
84 name: "libexample",
85 product_variables: {
86 enforce_vintf_manifest: {
87 cflags: ["-DSHOULD_NOT_EXIST"],
88 },
89 },
90 }`),
91 },
92 expectedError: "manifest enforcement should be independent",
93 },
94 {
95 name: "libhidltransport enforce_vintf_manifest.cflags",
96 fs: map[string][]byte{
97 "Blueprints": []byte(`
98 cc_library {
99 name: "libhidltransport",
100 product_variables: {
101 enforce_vintf_manifest: {
102 cflags: ["-DSHOULD_NOT_EXIST"],
103 },
104 },
105 }`),
106 },
107 expectedError: "",
108 },
109
110 {
111 name: "no treble_linker_namespaces.cflags",
112 fs: map[string][]byte{
113 "Blueprints": []byte(`
114 cc_library {
115 name: "libexample",
116 product_variables: {
117 treble_linker_namespaces: {
118 cflags: ["-DSHOULD_NOT_EXIST"],
119 },
120 },
121 }`),
122 },
123 expectedError: "nothing should care if linker namespaces are enabled or not",
124 },
125 {
126 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
127 fs: map[string][]byte{
128 "Blueprints": []byte(`
129 cc_library {
130 name: "libc_bionic_ndk",
131 product_variables: {
132 treble_linker_namespaces: {
133 cflags: ["-DSHOULD_NOT_EXIST"],
134 },
135 },
136 }`),
137 },
138 expectedError: "",
139 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100140 {
Colin Crossc35c5f92019-03-05 15:06:16 -0800141 name: "java_device_for_host",
142 fs: map[string][]byte{
143 "Blueprints": []byte(`
144 java_device_for_host {
145 name: "device_for_host",
146 libs: ["core-libart"],
147 }`),
148 },
149 expectedError: "java_device_for_host can only be used in whitelisted projects",
150 },
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100151 // Libcore rule tests
152 {
153 name: "no_standard_libs: true inside core libraries",
154 fs: map[string][]byte{
155 "libcore/Blueprints": []byte(`
156 java_library {
157 name: "inside_core_libraries",
158 no_standard_libs: true,
159 }`),
160 },
161 },
162 {
163 name: "no_standard_libs: true outside core libraries",
164 fs: map[string][]byte{
165 "Blueprints": []byte(`
166 java_library {
167 name: "outside_core_libraries",
168 no_standard_libs: true,
169 }`),
170 },
171 expectedError: "module \"outside_core_libraries\": violates neverallow",
172 },
173 {
174 name: "no_standard_libs: false",
175 fs: map[string][]byte{
176 "Blueprints": []byte(`
177 java_library {
178 name: "outside_core_libraries",
179 no_standard_libs: false,
180 }`),
181 },
182 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800183}
184
185func TestNeverallow(t *testing.T) {
186 buildDir, err := ioutil.TempDir("", "soong_neverallow_test")
187 if err != nil {
188 t.Fatal(err)
189 }
190 defer os.RemoveAll(buildDir)
191
192 config := TestConfig(buildDir, nil)
193
194 for _, test := range neverallowTests {
195 t.Run(test.name, func(t *testing.T) {
196 _, errs := testNeverallow(t, config, test.fs)
197
198 if test.expectedError == "" {
199 FailIfErrored(t, errs)
200 } else {
201 FailIfNoMatchingErrors(t, test.expectedError, errs)
202 }
203 })
204 }
205}
206
207func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
208 ctx := NewTestContext()
209 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100210 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossc35c5f92019-03-05 15:06:16 -0800211 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800212 ctx.PostDepsMutators(registerNeverallowMutator)
213 ctx.Register()
214
215 ctx.MockFileSystem(fs)
216
217 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
218 if len(errs) > 0 {
219 return ctx, errs
220 }
221
222 _, errs = ctx.PrepareBuildActions(config)
223 return ctx, errs
224}
225
Neil Fullerdf5f3562018-10-21 17:19:10 +0100226type mockCcLibraryProperties struct {
Logan Chienee97c3e2018-03-12 16:34:26 +0800227 Vendor_available *bool
228
229 Vndk struct {
230 Enabled *bool
231 Support_system_process *bool
232 Extends *string
233 }
234
235 Product_variables struct {
236 Enforce_vintf_manifest struct {
237 Cflags []string
238 }
239
240 Treble_linker_namespaces struct {
241 Cflags []string
242 }
243 }
244}
245
246type mockCcLibraryModule struct {
247 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100248 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800249}
250
251func newMockCcLibraryModule() Module {
252 m := &mockCcLibraryModule{}
253 m.AddProperties(&m.properties)
254 InitAndroidModule(m)
255 return m
256}
257
Logan Chienee97c3e2018-03-12 16:34:26 +0800258func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
259}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100260
261type mockJavaLibraryProperties struct {
Paul Duffinb6c6bdd2019-06-07 11:43:55 +0100262 Libs []string
263 No_standard_libs *bool
Neil Fullerdf5f3562018-10-21 17:19:10 +0100264}
265
266type mockJavaLibraryModule struct {
267 ModuleBase
268 properties mockJavaLibraryProperties
269}
270
271func newMockJavaLibraryModule() Module {
272 m := &mockJavaLibraryModule{}
273 m.AddProperties(&m.properties)
274 InitAndroidModule(m)
275 return m
276}
277
Neil Fullerdf5f3562018-10-21 17:19:10 +0100278func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
279}