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