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