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 | }, |
| 56 | |
| 57 | { |
| 58 | name: "no enforce_vintf_manifest.cflags", |
| 59 | fs: map[string][]byte{ |
| 60 | "Blueprints": []byte(` |
| 61 | cc_library { |
| 62 | name: "libexample", |
| 63 | product_variables: { |
| 64 | enforce_vintf_manifest: { |
| 65 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 66 | }, |
| 67 | }, |
| 68 | }`), |
| 69 | }, |
| 70 | expectedError: "manifest enforcement should be independent", |
| 71 | }, |
| 72 | { |
| 73 | name: "libhidltransport enforce_vintf_manifest.cflags", |
| 74 | fs: map[string][]byte{ |
| 75 | "Blueprints": []byte(` |
| 76 | cc_library { |
| 77 | name: "libhidltransport", |
| 78 | product_variables: { |
| 79 | enforce_vintf_manifest: { |
| 80 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 81 | }, |
| 82 | }, |
| 83 | }`), |
| 84 | }, |
| 85 | expectedError: "", |
| 86 | }, |
| 87 | |
| 88 | { |
| 89 | name: "no treble_linker_namespaces.cflags", |
| 90 | fs: map[string][]byte{ |
| 91 | "Blueprints": []byte(` |
| 92 | cc_library { |
| 93 | name: "libexample", |
| 94 | product_variables: { |
| 95 | treble_linker_namespaces: { |
| 96 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 97 | }, |
| 98 | }, |
| 99 | }`), |
| 100 | }, |
| 101 | expectedError: "nothing should care if linker namespaces are enabled or not", |
| 102 | }, |
| 103 | { |
| 104 | name: "libc_bionic_ndk treble_linker_namespaces.cflags", |
| 105 | fs: map[string][]byte{ |
| 106 | "Blueprints": []byte(` |
| 107 | cc_library { |
| 108 | name: "libc_bionic_ndk", |
| 109 | product_variables: { |
| 110 | treble_linker_namespaces: { |
| 111 | cflags: ["-DSHOULD_NOT_EXIST"], |
| 112 | }, |
| 113 | }, |
| 114 | }`), |
| 115 | }, |
| 116 | expectedError: "", |
| 117 | }, |
| 118 | } |
| 119 | |
| 120 | func TestNeverallow(t *testing.T) { |
| 121 | buildDir, err := ioutil.TempDir("", "soong_neverallow_test") |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | defer os.RemoveAll(buildDir) |
| 126 | |
| 127 | config := TestConfig(buildDir, nil) |
| 128 | |
| 129 | for _, test := range neverallowTests { |
| 130 | t.Run(test.name, func(t *testing.T) { |
| 131 | _, errs := testNeverallow(t, config, test.fs) |
| 132 | |
| 133 | if test.expectedError == "" { |
| 134 | FailIfErrored(t, errs) |
| 135 | } else { |
| 136 | FailIfNoMatchingErrors(t, test.expectedError, errs) |
| 137 | } |
| 138 | }) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func testNeverallow(t *testing.T, config Config, fs map[string][]byte) (*TestContext, []error) { |
| 143 | ctx := NewTestContext() |
| 144 | ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule)) |
| 145 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 146 | ctx.Register() |
| 147 | |
| 148 | ctx.MockFileSystem(fs) |
| 149 | |
| 150 | _, errs := ctx.ParseBlueprintsFiles("Blueprints") |
| 151 | if len(errs) > 0 { |
| 152 | return ctx, errs |
| 153 | } |
| 154 | |
| 155 | _, errs = ctx.PrepareBuildActions(config) |
| 156 | return ctx, errs |
| 157 | } |
| 158 | |
| 159 | type mockProperties struct { |
| 160 | Vendor_available *bool |
| 161 | |
| 162 | Vndk struct { |
| 163 | Enabled *bool |
| 164 | Support_system_process *bool |
| 165 | Extends *string |
| 166 | } |
| 167 | |
| 168 | Product_variables struct { |
| 169 | Enforce_vintf_manifest struct { |
| 170 | Cflags []string |
| 171 | } |
| 172 | |
| 173 | Treble_linker_namespaces struct { |
| 174 | Cflags []string |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | type mockCcLibraryModule struct { |
| 180 | ModuleBase |
| 181 | properties mockProperties |
| 182 | } |
| 183 | |
| 184 | func newMockCcLibraryModule() Module { |
| 185 | m := &mockCcLibraryModule{} |
| 186 | m.AddProperties(&m.properties) |
| 187 | InitAndroidModule(m) |
| 188 | return m |
| 189 | } |
| 190 | |
| 191 | func (p *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 192 | } |
| 193 | |
| 194 | func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) { |
| 195 | } |