2018-03-12 09:34:26 +01:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-07-25 16:41:09 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2018-03-12 09:34:26 +01:00
|
|
|
)
|
|
|
|
|
2019-07-25 16:41:09 +02:00
|
|
|
func init() {
|
|
|
|
// Add extra rules needed for testing.
|
|
|
|
AddNeverAllowRules(
|
|
|
|
NeverAllow().InDirectDeps("not_allowed_in_direct_deps"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-03-12 09:34:26 +01:00
|
|
|
var neverallowTests = []struct {
|
2019-08-05 16:07:57 +02:00
|
|
|
name string
|
|
|
|
fs map[string][]byte
|
|
|
|
expectedErrors []string
|
2018-03-12 09:34:26 +01:00
|
|
|
}{
|
2019-07-25 16:41:09 +02:00
|
|
|
// Test General Functionality
|
|
|
|
|
|
|
|
// in direct deps tests
|
|
|
|
{
|
|
|
|
name: "not_allowed_in_direct_deps",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"top/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "not_allowed_in_direct_deps",
|
|
|
|
}`),
|
|
|
|
"other/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libother",
|
|
|
|
static_libs: ["not_allowed_in_direct_deps"],
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
`module "libother": violates neverallow deps:not_allowed_in_direct_deps`,
|
|
|
|
},
|
2019-07-25 16:41:09 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Test specific rules
|
|
|
|
|
2019-07-16 15:18:22 +02:00
|
|
|
// include_dir rule tests
|
|
|
|
{
|
|
|
|
name: "include_dir not allowed to reference art",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"other/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libother",
|
|
|
|
include_dirs: ["art/libdexfile/include"],
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
"all usages of 'art' have been migrated",
|
|
|
|
},
|
2019-07-16 15:18:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "include_dir can reference another location",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"other/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libother",
|
|
|
|
include_dirs: ["another/include"],
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Treble rule tests
|
2018-03-12 09:34:26 +01:00
|
|
|
{
|
|
|
|
name: "no vndk.enabled under vendor directory",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"vendor/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libvndk",
|
|
|
|
vendor_available: true,
|
|
|
|
vndk: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
"VNDK can never contain a library that is device dependent",
|
|
|
|
},
|
2018-03-12 09:34:26 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no vndk.enabled under device directory",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"device/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libvndk",
|
|
|
|
vendor_available: true,
|
|
|
|
vndk: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
"VNDK can never contain a library that is device dependent",
|
|
|
|
},
|
2018-03-12 09:34:26 +01:00
|
|
|
},
|
2018-03-12 09:35:58 +01:00
|
|
|
{
|
|
|
|
name: "vndk-ext under vendor or device directory",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"device/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libvndk1_ext",
|
|
|
|
vendor: true,
|
|
|
|
vndk: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
"vendor/Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libvndk2_ext",
|
|
|
|
vendor: true,
|
|
|
|
vndk: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
},
|
2018-03-12 09:34:26 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
name: "no enforce_vintf_manifest.cflags",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libexample",
|
|
|
|
product_variables: {
|
|
|
|
enforce_vintf_manifest: {
|
|
|
|
cflags: ["-DSHOULD_NOT_EXIST"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
"manifest enforcement should be independent",
|
|
|
|
},
|
2018-03-12 09:34:26 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "libhidltransport enforce_vintf_manifest.cflags",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libhidltransport",
|
|
|
|
product_variables: {
|
|
|
|
enforce_vintf_manifest: {
|
|
|
|
cflags: ["-DSHOULD_NOT_EXIST"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "no treble_linker_namespaces.cflags",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libexample",
|
|
|
|
product_variables: {
|
|
|
|
treble_linker_namespaces: {
|
|
|
|
cflags: ["-DSHOULD_NOT_EXIST"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
"nothing should care if linker namespaces are enabled or not",
|
|
|
|
},
|
2018-03-12 09:34:26 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "libc_bionic_ndk treble_linker_namespaces.cflags",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"Blueprints": []byte(`
|
|
|
|
cc_library {
|
|
|
|
name: "libc_bionic_ndk",
|
|
|
|
product_variables: {
|
|
|
|
treble_linker_namespaces: {
|
|
|
|
cflags: ["-DSHOULD_NOT_EXIST"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
},
|
2019-03-06 00:06:16 +01:00
|
|
|
{
|
|
|
|
name: "java_device_for_host",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"Blueprints": []byte(`
|
|
|
|
java_device_for_host {
|
|
|
|
name: "device_for_host",
|
|
|
|
libs: ["core-libart"],
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
"java_device_for_host can only be used in whitelisted projects",
|
|
|
|
},
|
2019-03-06 00:06:16 +01:00
|
|
|
},
|
2019-06-07 12:43:55 +02:00
|
|
|
// Libcore rule tests
|
2019-06-11 13:31:14 +02:00
|
|
|
{
|
|
|
|
name: "sdk_version: \"none\" inside core libraries",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"libcore/Blueprints": []byte(`
|
|
|
|
java_library {
|
|
|
|
name: "inside_core_libraries",
|
|
|
|
sdk_version: "none",
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sdk_version: \"none\" outside core libraries",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"Blueprints": []byte(`
|
|
|
|
java_library {
|
|
|
|
name: "outside_core_libraries",
|
|
|
|
sdk_version: "none",
|
|
|
|
}`),
|
|
|
|
},
|
2019-08-05 16:07:57 +02:00
|
|
|
expectedErrors: []string{
|
|
|
|
"module \"outside_core_libraries\": violates neverallow",
|
|
|
|
},
|
2019-06-11 13:31:14 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sdk_version: \"current\"",
|
|
|
|
fs: map[string][]byte{
|
|
|
|
"Blueprints": []byte(`
|
|
|
|
java_library {
|
|
|
|
name: "outside_core_libraries",
|
|
|
|
sdk_version: "current",
|
|
|
|
}`),
|
|
|
|
},
|
|
|
|
},
|
2018-03-12 09:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNeverallow(t *testing.T) {
|
|
|
|
config := TestConfig(buildDir, nil)
|
|
|
|
|
|
|
|
for _, test := range neverallowTests {
|
|
|
|
|
2019-08-05 16:07:57 +02:00
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
_, errs := testNeverallow(config, test.fs)
|
|
|
|
CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
|
2018-03-12 09:34:26 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 16:07:57 +02:00
|
|
|
func testNeverallow(config Config, fs map[string][]byte) (*TestContext, []error) {
|
2018-03-12 09:34:26 +01:00
|
|
|
ctx := NewTestContext()
|
|
|
|
ctx.RegisterModuleType("cc_library", ModuleFactoryAdaptor(newMockCcLibraryModule))
|
2018-10-21 18:19:10 +02:00
|
|
|
ctx.RegisterModuleType("java_library", ModuleFactoryAdaptor(newMockJavaLibraryModule))
|
2019-06-11 14:54:26 +02:00
|
|
|
ctx.RegisterModuleType("java_library_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
|
2019-03-06 00:06:16 +01:00
|
|
|
ctx.RegisterModuleType("java_device_for_host", ModuleFactoryAdaptor(newMockJavaLibraryModule))
|
2018-03-12 09:34:26 +01:00
|
|
|
ctx.PostDepsMutators(registerNeverallowMutator)
|
|
|
|
ctx.Register()
|
|
|
|
|
|
|
|
ctx.MockFileSystem(fs)
|
|
|
|
|
|
|
|
_, errs := ctx.ParseBlueprintsFiles("Blueprints")
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return ctx, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
|
|
return ctx, errs
|
|
|
|
}
|
|
|
|
|
2018-10-21 18:19:10 +02:00
|
|
|
type mockCcLibraryProperties struct {
|
2019-07-16 15:18:22 +02:00
|
|
|
Include_dirs []string
|
2018-03-12 09:34:26 +01:00
|
|
|
Vendor_available *bool
|
2019-07-25 16:41:09 +02:00
|
|
|
Static_libs []string
|
2018-03-12 09:34:26 +01:00
|
|
|
|
|
|
|
Vndk struct {
|
|
|
|
Enabled *bool
|
|
|
|
Support_system_process *bool
|
|
|
|
Extends *string
|
|
|
|
}
|
|
|
|
|
|
|
|
Product_variables struct {
|
|
|
|
Enforce_vintf_manifest struct {
|
|
|
|
Cflags []string
|
|
|
|
}
|
|
|
|
|
|
|
|
Treble_linker_namespaces struct {
|
|
|
|
Cflags []string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockCcLibraryModule struct {
|
|
|
|
ModuleBase
|
2018-10-21 18:19:10 +02:00
|
|
|
properties mockCcLibraryProperties
|
2018-03-12 09:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newMockCcLibraryModule() Module {
|
|
|
|
m := &mockCcLibraryModule{}
|
|
|
|
m.AddProperties(&m.properties)
|
|
|
|
InitAndroidModule(m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-07-25 16:41:09 +02:00
|
|
|
type neverallowTestDependencyTag struct {
|
|
|
|
blueprint.BaseDependencyTag
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
var staticDepTag = neverallowTestDependencyTag{name: "static"}
|
|
|
|
|
|
|
|
func (c *mockCcLibraryModule) DepsMutator(ctx BottomUpMutatorContext) {
|
|
|
|
for _, lib := range c.properties.Static_libs {
|
|
|
|
ctx.AddDependency(ctx.Module(), staticDepTag, lib)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 09:34:26 +01:00
|
|
|
func (p *mockCcLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
|
|
|
|
}
|
2018-10-21 18:19:10 +02:00
|
|
|
|
|
|
|
type mockJavaLibraryProperties struct {
|
2019-06-11 14:40:47 +02:00
|
|
|
Libs []string
|
|
|
|
Sdk_version *string
|
2018-10-21 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockJavaLibraryModule struct {
|
|
|
|
ModuleBase
|
|
|
|
properties mockJavaLibraryProperties
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockJavaLibraryModule() Module {
|
|
|
|
m := &mockJavaLibraryModule{}
|
|
|
|
m.AddProperties(&m.properties)
|
|
|
|
InitAndroidModule(m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *mockJavaLibraryModule) GenerateAndroidBuildActions(ModuleContext) {
|
|
|
|
}
|