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