blob: 6fd2cd71d1718e0dac8ecfe01e47b4e37ba6b3d1 [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 },
Logan Chienee97c3e2018-03-12 16:34:26 +080094
95 {
96 name: "no treble_linker_namespaces.cflags",
97 fs: map[string][]byte{
98 "Blueprints": []byte(`
99 cc_library {
100 name: "libexample",
101 product_variables: {
102 treble_linker_namespaces: {
103 cflags: ["-DSHOULD_NOT_EXIST"],
104 },
105 },
106 }`),
107 },
108 expectedError: "nothing should care if linker namespaces are enabled or not",
109 },
110 {
111 name: "libc_bionic_ndk treble_linker_namespaces.cflags",
112 fs: map[string][]byte{
113 "Blueprints": []byte(`
114 cc_library {
115 name: "libc_bionic_ndk",
116 product_variables: {
117 treble_linker_namespaces: {
118 cflags: ["-DSHOULD_NOT_EXIST"],
119 },
120 },
121 }`),
122 },
123 expectedError: "",
124 },
Neil Fullerdf5f3562018-10-21 17:19:10 +0100125 {
126 name: "dependency on core-libart",
127 fs: map[string][]byte{
128 "Blueprints": []byte(`
129 java_library {
130 name: "needs_core_libart",
131 libs: ["core-libart"],
132 }`),
133 },
134 expectedError: "Only core libraries projects can depend on core-libart",
135 },
Dongwon Kang50a299f2019-02-04 09:00:51 -0800136 {
137 name: "dependency on updatable-media",
138 fs: map[string][]byte{
139 "Blueprints": []byte(`
140 java_library {
141 name: "needs_updatable_media",
142 libs: ["updatable-media"],
143 }`),
144 },
145 expectedError: "updatable-media includes private APIs. Use updatable_media_stubs instead.",
146 },
Colin Crossfd4f7432019-03-05 15:06:16 -0800147 {
148 name: "java_device_for_host",
149 fs: map[string][]byte{
150 "Blueprints": []byte(`
151 java_device_for_host {
152 name: "device_for_host",
153 libs: ["core-libart"],
154 }`),
155 },
156 expectedError: "java_device_for_host can only be used in whitelisted projects",
157 },
Logan Chienee97c3e2018-03-12 16:34:26 +0800158}
159
160func TestNeverallow(t *testing.T) {
161 buildDir, err := ioutil.TempDir("", "soong_neverallow_test")
162 if err != nil {
163 t.Fatal(err)
164 }
165 defer os.RemoveAll(buildDir)
166
167 config := TestConfig(buildDir, nil)
168
169 for _, test := range neverallowTests {
170 t.Run(test.name, func(t *testing.T) {
171 _, errs := testNeverallow(t, config, test.fs)
172
173 if test.expectedError == "" {
174 FailIfErrored(t, errs)
175 } else {
176 FailIfNoMatchingErrors(t, test.expectedError, errs)
177 }
178 })
179 }
180}
181
182func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) {
183 ctx := NewTestContext()
184 ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100185 ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Colin Crossfd4f7432019-03-05 15:06:16 -0800186 ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
Logan Chienee97c3e2018-03-12 16:34:26 +0800187 ctx.PostDepsMutators(registerNeverallowMutator)
188 ctx.Register()
189
190 ctx.MockFileSystem(fs)
191
192 _, errs := ctx.ParseBlueprintsFiles("Blueprints")
193 if len(errs) > 0 {
194 return ctx, errs
195 }
196
197 _, errs = ctx.PrepareBuildActions(config)
198 return ctx, errs
199}
200
Neil Fullerdf5f3562018-10-21 17:19:10 +0100201type mockCcLibraryProperties struct {
Logan Chienee97c3e2018-03-12 16:34:26 +0800202 Vendor_available *bool
203
204 Vndk struct {
205 Enabled *bool
206 Support_system_process *bool
207 Extends *string
208 }
209
210 Product_variables struct {
211 Enforce_vintf_manifest struct {
212 Cflags []string
213 }
214
215 Treble_linker_namespaces struct {
216 Cflags []string
217 }
218 }
219}
220
221type mockCcLibraryModule struct {
222 ModuleBase
Neil Fullerdf5f3562018-10-21 17:19:10 +0100223 properties mockCcLibraryProperties
Logan Chienee97c3e2018-03-12 16:34:26 +0800224}
225
226func newMockCcLibraryModule() Module {
227 m := &mockCcLibraryModule{}
228 m.AddProperties(&m.properties)
229 InitAndroidModule(m)
230 return m
231}
232
Logan Chienee97c3e2018-03-12 16:34:26 +0800233func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
234}
Neil Fullerdf5f3562018-10-21 17:19:10 +0100235
236type mockJavaLibraryProperties struct {
237 Libs []string
238}
239
240type mockJavaLibraryModule struct {
241 ModuleBase
242 properties mockJavaLibraryProperties
243}
244
245func newMockJavaLibraryModule() Module {
246 m := &mockJavaLibraryModule{}
247 m.AddProperties(&m.properties)
248 InitAndroidModule(m)
249 return m
250}
251
Neil Fullerdf5f3562018-10-21 17:19:10 +0100252func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
253}