From fec6c23734b4bfdb0d4e08890fcbce93df8110d0 Mon Sep 17 00:00:00 2001 From: Anton Hansson Date: Thu, 9 Apr 2020 14:18:21 +0100 Subject: [PATCH] Expand neverallow for sdk_version: none Allow the platform stubs to specify sdk_version: none, and add generic support for a regexp matcher. Bug: 144149403 Test: m Change-Id: Icaece0d9797bace8ae9741f7a029b9ea57fcbbb9 Merged-In: Icaece0d9797bace8ae9741f7a029b9ea57fcbbb9 --- android/neverallow.go | 24 +++++++++++++++++++++++- android/neverallow_test.go | 10 ++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/android/neverallow.go b/android/neverallow.go index 8fcfb8a6e..ea307cabe 100644 --- a/android/neverallow.go +++ b/android/neverallow.go @@ -17,6 +17,7 @@ package android import ( "path/filepath" "reflect" + "regexp" "strconv" "strings" @@ -146,7 +147,8 @@ func createLibcoreRules() []Rule { rules := []Rule{ NeverAllow(). NotIn(coreLibraryProjects...). - With("sdk_version", "none"), + With("sdk_version", "none"). + WithoutMatcher("name", Regexp("^android_.*stubs_current$")), } return rules @@ -254,6 +256,18 @@ func (m *startsWithMatcher) String() string { return ".starts-with(" + m.prefix + ")" } +type regexMatcher struct { + re *regexp.Regexp +} + +func (m *regexMatcher) test(value string) bool { + return m.re.MatchString(value) +} + +func (m *regexMatcher) String() string { + return ".regexp(" + m.re.String() + ")" +} + type ruleProperty struct { fields []string // e.x.: Vndk.Enabled matcher ValueMatcher @@ -457,6 +471,14 @@ func StartsWith(prefix string) ValueMatcher { return &startsWithMatcher{prefix} } +func Regexp(re string) ValueMatcher { + r, err := regexp.Compile(re) + if err != nil { + panic(err) + } + return ®exMatcher{r} +} + // assorted utils func cleanPaths(paths []string) []string { diff --git a/android/neverallow_test.go b/android/neverallow_test.go index 6f07a4a73..79929362f 100644 --- a/android/neverallow_test.go +++ b/android/neverallow_test.go @@ -226,6 +226,16 @@ var neverallowTests = []struct { }`), }, }, + { + name: "sdk_version: \"none\" on android_*stubs_current stub", + fs: map[string][]byte{ + "frameworks/base/Android.bp": []byte(` + java_library { + name: "android_stubs_current", + sdk_version: "none", + }`), + }, + }, { name: "sdk_version: \"none\" outside core libraries", fs: map[string][]byte{