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 ( |
| 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | var 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 Chien | af29bad | 2018-03-12 16:35:58 +0800 | [diff] [blame] | 56 | { |
| 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 Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 78 | |
| 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 | }, |
| 94 | { |
| 95 | name: "libhidltransport enforce_vintf_manifest.cflags", |
| 96 | fs: map[string][]byte{ |
| 97 | "Blueprints": []byte(` |
| 98 | cc_library { |
| 99 | name: "libhidltransport", |
| 100 | product_variables: { |
| 101 | enforce_vintf_manifest: { |
| 102 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 103 | }, |
| 104 | }, |
| 105 | }`), |
| 106 | }, |
| 107 | expectedError: "", |
| 108 | }, |
| 109 | |
| 110 | { |
| 111 | name: "no treble_linker_namespaces.cflags", |
| 112 | fs: map[string][]byte{ |
| 113 | "Blueprints": []byte(` |
| 114 | cc_library { |
| 115 | name: "libexample", |
| 116 | product_variables: { |
| 117 | treble_linker_namespaces: { |
| 118 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 119 | }, |
| 120 | }, |
| 121 | }`), |
| 122 | }, |
| 123 | expectedError: "nothing should care if linker namespaces are enabled or not", |
| 124 | }, |
| 125 | { |
| 126 | name: "libc_bionic_ndk treble_linker_namespaces.cflags", |
| 127 | fs: map[string][]byte{ |
| 128 | "Blueprints": []byte(` |
| 129 | cc_library { |
| 130 | name: "libc_bionic_ndk", |
| 131 | product_variables: { |
| 132 | treble_linker_namespaces: { |
| 133 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 134 | }, |
| 135 | }, |
| 136 | }`), |
| 137 | }, |
| 138 | expectedError: "", |
| 139 | }, |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 140 | { |
| 141 | name: "dependency on core-libart", |
| 142 | fs: map[string][]byte{ |
| 143 | "Blueprints": []byte(` |
| 144 | java_library { |
| 145 | name: "needs_core_libart", |
| 146 | libs: ["core-libart"], |
| 147 | }`), |
| 148 | }, |
| 149 | expectedError: "Only core libraries projects can depend on core-libart", |
| 150 | }, |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 151 | { |
| 152 | name: "java_device_for_host", |
| 153 | fs: map[string][]byte{ |
| 154 | "Blueprints": []byte(` |
| 155 | java_device_for_host { |
| 156 | name: "device_for_host", |
| 157 | libs: ["core-libart"], |
| 158 | }`), |
| 159 | }, |
| 160 | expectedError: "java_device_for_host can only be used in whitelisted projects", |
| 161 | }, |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func TestNeverallow(t *testing.T) { |
| 165 | buildDir, err := ioutil.TempDir("", "soong_neverallow_test") |
| 166 | if err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | defer os.RemoveAll(buildDir) |
| 170 | |
| 171 | config := TestConfig(buildDir, nil) |
| 172 | |
| 173 | for _, test := range neverallowTests { |
| 174 | t.Run(test.name, func(t *testing.T) { |
| 175 | _, errs := testNeverallow(t, config, test.fs) |
| 176 | |
| 177 | if test.expectedError == "" { |
| 178 | FailIfErrored(t, errs) |
| 179 | } else { |
| 180 | FailIfNoMatchingErrors(t, test.expectedError, errs) |
| 181 | } |
| 182 | }) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) { |
| 187 | ctx := NewTestContext() |
| 188 | ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule)) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 189 | ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule)) |
Colin Cross | c35c5f9 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 190 | ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule)) |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 191 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 192 | ctx.Register() |
| 193 | |
| 194 | ctx.MockFileSystem(fs) |
| 195 | |
| 196 | _, errs := ctx.ParseBlueprintsFiles("Blueprints") |
| 197 | if len(errs) > 0 { |
| 198 | return ctx, errs |
| 199 | } |
| 200 | |
| 201 | _, errs = ctx.PrepareBuildActions(config) |
| 202 | return ctx, errs |
| 203 | } |
| 204 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 205 | type mockCcLibraryProperties struct { |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 206 | Vendor_available *bool |
| 207 | |
| 208 | Vndk struct { |
| 209 | Enabled *bool |
| 210 | Support_system_process *bool |
| 211 | Extends *string |
| 212 | } |
| 213 | |
| 214 | Product_variables struct { |
| 215 | Enforce_vintf_manifest struct { |
| 216 | Cflags []string |
| 217 | } |
| 218 | |
| 219 | Treble_linker_namespaces struct { |
| 220 | Cflags []string |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | type mockCcLibraryModule struct { |
| 226 | ModuleBase |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 227 | properties mockCcLibraryProperties |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | func newMockCcLibraryModule() Module { |
| 231 | m := &mockCcLibraryModule{} |
| 232 | m.AddProperties(&m.properties) |
| 233 | InitAndroidModule(m) |
| 234 | return m |
| 235 | } |
| 236 | |
Logan Chien | ee97c3e | 2018-03-12 16:34:26 +0800 | [diff] [blame] | 237 | func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 238 | } |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 239 | |
| 240 | type mockJavaLibraryProperties struct { |
| 241 | Libs []string |
| 242 | } |
| 243 | |
| 244 | type mockJavaLibraryModule struct { |
| 245 | ModuleBase |
| 246 | properties mockJavaLibraryProperties |
| 247 | } |
| 248 | |
| 249 | func newMockJavaLibraryModule() Module { |
| 250 | m := &mockJavaLibraryModule{} |
| 251 | m.AddProperties(&m.properties) |
| 252 | InitAndroidModule(m) |
| 253 | return m |
| 254 | } |
| 255 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 256 | func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 257 | } |