Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 18 | "testing" |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 19 | |
| 20 | "github.com/google/blueprint" |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 21 | ) |
| 22 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 23 | func init() { |
| 24 | // Add extra rules needed for testing. |
| 25 | AddNeverAllowRules( |
| 26 | NeverAllow().InDirectDeps("not_allowed_in_direct_deps"), |
| 27 | ) |
| 28 | } |
| 29 | |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 30 | var neverallowTests = []struct { |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 31 | name string |
| 32 | fs map[string][]byte |
| 33 | expectedErrors []string |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 34 | }{ |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 35 | // Test General Functionality |
| 36 | |
| 37 | // in direct deps tests |
| 38 | { |
| 39 | name: "not_allowed_in_direct_deps", |
| 40 | fs: map[string][]byte{ |
| 41 | "top/Blueprints": []byte(` |
| 42 | cc_library { |
| 43 | name: "not_allowed_in_direct_deps", |
| 44 | }`), |
| 45 | "other/Blueprints": []byte(` |
| 46 | cc_library { |
| 47 | name: "libother", |
| 48 | static_libs: ["not_allowed_in_direct_deps"], |
| 49 | }`), |
| 50 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 51 | expectedErrors: []string{ |
| 52 | `module "libother": violates neverallow deps:not_allowed_in_direct_deps`, |
| 53 | }, |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 54 | }, |
| 55 | |
| 56 | // Test specific rules |
| 57 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 58 | // include_dir rule tests |
| 59 | { |
| 60 | name: "include_dir not allowed to reference art", |
| 61 | fs: map[string][]byte{ |
| 62 | "other/Blueprints": []byte(` |
| 63 | cc_library { |
| 64 | name: "libother", |
| 65 | include_dirs: ["art/libdexfile/include"], |
| 66 | }`), |
| 67 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 68 | expectedErrors: []string{ |
| 69 | "all usages of 'art' have been migrated", |
| 70 | }, |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 71 | }, |
| 72 | { |
| 73 | name: "include_dir can reference another location", |
| 74 | fs: map[string][]byte{ |
| 75 | "other/Blueprints": []byte(` |
| 76 | cc_library { |
| 77 | name: "libother", |
| 78 | include_dirs: ["another/include"], |
| 79 | }`), |
| 80 | }, |
| 81 | }, |
| 82 | // Treble rule tests |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 83 | { |
| 84 | name: "no vndk.enabled under vendor directory", |
| 85 | fs: map[string][]byte{ |
| 86 | "vendor/Blueprints": []byte(` |
| 87 | cc_library { |
| 88 | name: "libvndk", |
| 89 | vendor_available: true, |
| 90 | vndk: { |
| 91 | enabled: true, |
| 92 | }, |
| 93 | }`), |
| 94 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 95 | expectedErrors: []string{ |
| 96 | "VNDK can never contain a library that is device dependent", |
| 97 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 98 | }, |
| 99 | { |
| 100 | name: "no vndk.enabled under device directory", |
| 101 | fs: map[string][]byte{ |
| 102 | "device/Blueprints": []byte(` |
| 103 | cc_library { |
| 104 | name: "libvndk", |
| 105 | vendor_available: true, |
| 106 | vndk: { |
| 107 | enabled: true, |
| 108 | }, |
| 109 | }`), |
| 110 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 111 | expectedErrors: []string{ |
| 112 | "VNDK can never contain a library that is device dependent", |
| 113 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 114 | }, |
Logan Chien | af29bad | 2018-03-12 16:35:58 +0800 | [diff] [blame] | 115 | { |
| 116 | name: "vndk-ext under vendor or device directory", |
| 117 | fs: map[string][]byte{ |
| 118 | "device/Blueprints": []byte(` |
| 119 | cc_library { |
| 120 | name: "libvndk1_ext", |
| 121 | vendor: true, |
| 122 | vndk: { |
| 123 | enabled: true, |
| 124 | }, |
| 125 | }`), |
| 126 | "vendor/Blueprints": []byte(` |
| 127 | cc_library { |
| 128 | name: "libvndk2_ext", |
| 129 | vendor: true, |
| 130 | vndk: { |
| 131 | enabled: true, |
| 132 | }, |
| 133 | }`), |
| 134 | }, |
Logan Chien | af29bad | 2018-03-12 16:35:58 +0800 | [diff] [blame] | 135 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 136 | |
| 137 | { |
| 138 | name: "no enforce_vintf_manifest.cflags", |
| 139 | fs: map[string][]byte{ |
| 140 | "Blueprints": []byte(` |
| 141 | cc_library { |
| 142 | name: "libexample", |
| 143 | product_variables: { |
| 144 | enforce_vintf_manifest: { |
| 145 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 146 | }, |
| 147 | }, |
| 148 | }`), |
| 149 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 150 | expectedErrors: []string{ |
| 151 | "manifest enforcement should be independent", |
| 152 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 153 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 154 | |
| 155 | { |
| 156 | name: "no treble_linker_namespaces.cflags", |
| 157 | fs: map[string][]byte{ |
| 158 | "Blueprints": []byte(` |
| 159 | cc_library { |
| 160 | name: "libexample", |
| 161 | product_variables: { |
| 162 | treble_linker_namespaces: { |
| 163 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 164 | }, |
| 165 | }, |
| 166 | }`), |
| 167 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 168 | expectedErrors: []string{ |
| 169 | "nothing should care if linker namespaces are enabled or not", |
| 170 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 171 | }, |
| 172 | { |
| 173 | name: "libc_bionic_ndk treble_linker_namespaces.cflags", |
| 174 | fs: map[string][]byte{ |
| 175 | "Blueprints": []byte(` |
| 176 | cc_library { |
| 177 | name: "libc_bionic_ndk", |
| 178 | product_variables: { |
| 179 | treble_linker_namespaces: { |
| 180 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 181 | }, |
| 182 | }, |
| 183 | }`), |
| 184 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 185 | }, |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 186 | { |
Dongwon Kang | 50a299f | 2019-02-04 09:00:51 -0800 | [diff] [blame] | 187 | name: "dependency on updatable-media", |
| 188 | fs: map[string][]byte{ |
| 189 | "Blueprints": []byte(` |
| 190 | java_library { |
| 191 | name: "needs_updatable_media", |
| 192 | libs: ["updatable-media"], |
| 193 | }`), |
| 194 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 195 | expectedErrors: []string{ |
| 196 | "updatable-media includes private APIs. Use updatable_media_stubs instead.", |
| 197 | }, |
Dongwon Kang | 50a299f | 2019-02-04 09:00:51 -0800 | [diff] [blame] | 198 | }, |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 199 | { |
| 200 | name: "java_device_for_host", |
| 201 | fs: map[string][]byte{ |
| 202 | "Blueprints": []byte(` |
| 203 | java_device_for_host { |
| 204 | name: "device_for_host", |
| 205 | libs: ["core-libart"], |
| 206 | }`), |
| 207 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 208 | expectedErrors: []string{ |
| 209 | "java_device_for_host can only be used in whitelisted projects", |
| 210 | }, |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 211 | }, |
Paul Duffin | b6c6bdd | 2019-06-07 11:43:55 +0100 | [diff] [blame] | 212 | // Libcore rule tests |
| 213 | { |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 214 | name: "sdk_version: \"none\" inside core libraries", |
| 215 | fs: map[string][]byte{ |
| 216 | "libcore/Blueprints": []byte(` |
| 217 | java_library { |
| 218 | name: "inside_core_libraries", |
| 219 | sdk_version: "none", |
| 220 | }`), |
| 221 | }, |
| 222 | }, |
| 223 | { |
| 224 | name: "sdk_version: \"none\" outside core libraries", |
| 225 | fs: map[string][]byte{ |
| 226 | "Blueprints": []byte(` |
| 227 | java_library { |
| 228 | name: "outside_core_libraries", |
| 229 | sdk_version: "none", |
| 230 | }`), |
| 231 | }, |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 232 | expectedErrors: []string{ |
| 233 | "module \"outside_core_libraries\": violates neverallow", |
| 234 | }, |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 235 | }, |
| 236 | { |
| 237 | name: "sdk_version: \"current\"", |
| 238 | fs: map[string][]byte{ |
| 239 | "Blueprints": []byte(` |
| 240 | java_library { |
| 241 | name: "outside_core_libraries", |
| 242 | sdk_version: "current", |
| 243 | }`), |
| 244 | }, |
| 245 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | func TestNeverallow(t *testing.T) { |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 249 | config := TestConfig(buildDir, nil) |
| 250 | |
| 251 | for _, test := range neverallowTests { |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 252 | |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 253 | t.Run(test.name, func(t *testing.T) { |
| 254 | _, errs := testNeverallow(config, test.fs) |
| 255 | CheckErrorsAgainstExpectations(t, errs, test.expectedErrors) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 256 | }) |
| 257 | } |
| 258 | } |
| 259 | |
Paul Duffin | b5af620 | 2019-08-05 15:07:57 +0100 | [diff] [blame^] | 260 | func testNeverallow(config Config, fs map[string][]byte) (*TestContext, []error) { |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 261 | ctx := NewTestContext() |
| 262 | ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule)) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 263 | ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule)) |
Paul Duffin | b815ada | 2019-06-11 13:54:26 +0100 | [diff] [blame] | 264 | ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule)) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 265 | ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule)) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 266 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 267 | ctx.Register() |
| 268 | |
| 269 | ctx.MockFileSystem(fs) |
| 270 | |
| 271 | _, errs := ctx.ParseBlueprintsFiles("Blueprints") |
| 272 | if len(errs) > 0 { |
| 273 | return ctx, errs |
| 274 | } |
| 275 | |
| 276 | _, errs = ctx.PrepareBuildActions(config) |
| 277 | return ctx, errs |
| 278 | } |
| 279 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 280 | type mockCcLibraryProperties struct { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 281 | Include_dirs []string |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 282 | Vendor_available *bool |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 283 | Static_libs []string |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 284 | |
| 285 | Vndk struct { |
| 286 | Enabled *bool |
| 287 | Support_system_process *bool |
| 288 | Extends *string |
| 289 | } |
| 290 | |
| 291 | Product_variables struct { |
| 292 | Enforce_vintf_manifest struct { |
| 293 | Cflags []string |
| 294 | } |
| 295 | |
| 296 | Treble_linker_namespaces struct { |
| 297 | Cflags []string |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | type mockCcLibraryModule struct { |
| 303 | ModuleBase |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 304 | properties mockCcLibraryProperties |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | func newMockCcLibraryModule() Module { |
| 308 | m := &mockCcLibraryModule{} |
| 309 | m.AddProperties(&m.properties) |
| 310 | InitAndroidModule(m) |
| 311 | return m |
| 312 | } |
| 313 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 314 | type neverallowTestDependencyTag struct { |
| 315 | blueprint.BaseDependencyTag |
| 316 | name string |
| 317 | } |
| 318 | |
| 319 | var staticDepTag = neverallowTestDependencyTag{name: "static"} |
| 320 | |
| 321 | func (c *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 322 | for _, lib := range c.properties.Static_libs { |
| 323 | ctx.AddDependency(ctx.Module(), staticDepTag, lib) |
| 324 | } |
| 325 | } |
| 326 | |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 327 | func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 328 | } |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 329 | |
| 330 | type mockJavaLibraryProperties struct { |
Paul Duffin | a3d0986 | 2019-06-11 13:40:47 +0100 | [diff] [blame] | 331 | Libs []string |
| 332 | Sdk_version *string |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | type mockJavaLibraryModule struct { |
| 336 | ModuleBase |
| 337 | properties mockJavaLibraryProperties |
| 338 | } |
| 339 | |
| 340 | func newMockJavaLibraryModule() Module { |
| 341 | m := &mockJavaLibraryModule{} |
| 342 | m.AddProperties(&m.properties) |
| 343 | InitAndroidModule(m) |
| 344 | return m |
| 345 | } |
| 346 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 347 | func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 348 | } |