2020-08-06 06:40:41 +08:00
|
|
|
// Copyright 2020 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 java
|
|
|
|
|
|
|
|
import (
|
2020-07-03 22:31:32 +08:00
|
|
|
"fmt"
|
2021-03-13 05:44:02 +08:00
|
|
|
"path/filepath"
|
2020-08-06 06:40:41 +08:00
|
|
|
"testing"
|
2020-07-03 22:31:32 +08:00
|
|
|
|
2021-02-05 01:49:33 +08:00
|
|
|
"android/soong/android"
|
2020-07-03 22:31:32 +08:00
|
|
|
"github.com/google/blueprint/proptools"
|
2020-08-06 06:40:41 +08:00
|
|
|
)
|
|
|
|
|
2021-04-21 21:10:42 +08:00
|
|
|
// TODO(b/177892522): Move these tests into a more appropriate place.
|
|
|
|
|
2021-03-13 05:44:02 +08:00
|
|
|
func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
|
|
|
|
return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
|
|
|
|
variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
|
|
|
|
})
|
2020-07-03 22:31:32 +08:00
|
|
|
}
|
|
|
|
|
2021-04-21 21:10:42 +08:00
|
|
|
var prepareForTestWithDefaultPlatformBootclasspath = android.FixtureAddTextFile("frameworks/base/boot/Android.bp", `
|
|
|
|
platform_bootclasspath {
|
|
|
|
name: "platform-bootclasspath",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2021-03-22 23:36:52 +08:00
|
|
|
var hiddenApiFixtureFactory = android.GroupFixturePreparers(
|
|
|
|
prepareForJavaTest, PrepareForTestWithHiddenApiBuildComponents)
|
2020-08-06 06:40:41 +08:00
|
|
|
|
|
|
|
func TestHiddenAPISingleton(t *testing.T) {
|
2021-03-29 09:16:14 +08:00
|
|
|
result := android.GroupFixturePreparers(
|
|
|
|
hiddenApiFixtureFactory,
|
2021-04-13 03:02:36 +08:00
|
|
|
FixtureConfigureBootJars("platform:foo"),
|
2021-04-21 21:10:42 +08:00
|
|
|
prepareForTestWithDefaultPlatformBootclasspath,
|
2021-03-13 05:44:02 +08:00
|
|
|
).RunTestWithBp(t, `
|
2020-08-06 06:40:41 +08:00
|
|
|
java_library {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.java"],
|
|
|
|
compile_dex: true,
|
2021-02-05 01:49:33 +08:00
|
|
|
}
|
2021-03-13 05:44:02 +08:00
|
|
|
`)
|
2020-08-06 06:40:41 +08:00
|
|
|
|
2021-04-21 21:10:42 +08:00
|
|
|
hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
|
|
|
|
hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
|
2021-03-23 00:24:49 +08:00
|
|
|
want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
|
2020-08-06 06:40:41 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 23:34:13 +08:00
|
|
|
func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
|
2021-05-14 14:52:42 +08:00
|
|
|
expectedErrorMessage := "module prebuilt_foo{os:android,arch:common} does not provide a dex jar"
|
2021-03-13 05:44:02 +08:00
|
|
|
|
2021-03-29 09:16:14 +08:00
|
|
|
android.GroupFixturePreparers(
|
|
|
|
hiddenApiFixtureFactory,
|
2021-04-13 03:02:36 +08:00
|
|
|
FixtureConfigureBootJars("platform:foo"),
|
2021-04-21 21:10:42 +08:00
|
|
|
prepareForTestWithDefaultPlatformBootclasspath,
|
2021-03-13 05:44:02 +08:00
|
|
|
).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
|
|
|
|
RunTestWithBp(t, `
|
2021-02-25 23:34:13 +08:00
|
|
|
java_library {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.java"],
|
|
|
|
compile_dex: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
java_import {
|
|
|
|
name: "foo",
|
|
|
|
jars: ["a.jar"],
|
|
|
|
prefer: true,
|
|
|
|
}
|
2021-03-13 05:44:02 +08:00
|
|
|
`)
|
2021-02-25 23:34:13 +08:00
|
|
|
}
|
|
|
|
|
2020-08-06 06:40:41 +08:00
|
|
|
func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
|
2021-03-29 09:16:14 +08:00
|
|
|
result := android.GroupFixturePreparers(
|
|
|
|
hiddenApiFixtureFactory,
|
2021-04-13 03:02:36 +08:00
|
|
|
FixtureConfigureBootJars("platform:foo"),
|
2021-04-21 21:10:42 +08:00
|
|
|
prepareForTestWithDefaultPlatformBootclasspath,
|
2021-03-13 05:44:02 +08:00
|
|
|
).RunTestWithBp(t, `
|
2020-08-06 06:40:41 +08:00
|
|
|
java_import {
|
|
|
|
name: "foo",
|
|
|
|
jars: ["a.jar"],
|
|
|
|
compile_dex: true,
|
|
|
|
}
|
2021-03-13 05:44:02 +08:00
|
|
|
`)
|
2020-08-06 06:40:41 +08:00
|
|
|
|
2021-04-21 21:10:42 +08:00
|
|
|
hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
|
|
|
|
hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
|
2021-03-23 00:24:49 +08:00
|
|
|
want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
|
2020-08-06 06:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
|
2021-03-29 09:16:14 +08:00
|
|
|
result := android.GroupFixturePreparers(
|
|
|
|
hiddenApiFixtureFactory,
|
2021-04-13 03:02:36 +08:00
|
|
|
FixtureConfigureBootJars("platform:foo"),
|
2021-04-21 21:10:42 +08:00
|
|
|
prepareForTestWithDefaultPlatformBootclasspath,
|
2021-03-13 05:44:02 +08:00
|
|
|
).RunTestWithBp(t, `
|
2020-08-06 06:40:41 +08:00
|
|
|
java_library {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.java"],
|
|
|
|
compile_dex: true,
|
2021-02-05 01:49:33 +08:00
|
|
|
}
|
2020-08-06 06:40:41 +08:00
|
|
|
|
|
|
|
java_import {
|
|
|
|
name: "foo",
|
|
|
|
jars: ["a.jar"],
|
|
|
|
compile_dex: true,
|
|
|
|
prefer: false,
|
2021-02-05 01:49:33 +08:00
|
|
|
}
|
2021-03-13 05:44:02 +08:00
|
|
|
`)
|
2020-08-06 06:40:41 +08:00
|
|
|
|
2021-04-21 21:10:42 +08:00
|
|
|
hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
|
|
|
|
hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
|
2021-03-23 00:24:49 +08:00
|
|
|
fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
|
2020-08-06 06:40:41 +08:00
|
|
|
|
2021-03-23 00:24:49 +08:00
|
|
|
prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar"
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
|
2020-08-06 06:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
|
2021-03-29 09:16:14 +08:00
|
|
|
result := android.GroupFixturePreparers(
|
|
|
|
hiddenApiFixtureFactory,
|
2021-04-13 03:02:36 +08:00
|
|
|
FixtureConfigureBootJars("platform:foo"),
|
2021-04-21 21:10:42 +08:00
|
|
|
prepareForTestWithDefaultPlatformBootclasspath,
|
2021-03-13 05:44:02 +08:00
|
|
|
).RunTestWithBp(t, `
|
2020-08-06 06:40:41 +08:00
|
|
|
java_library {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.java"],
|
|
|
|
compile_dex: true,
|
2021-02-05 01:49:33 +08:00
|
|
|
}
|
2020-08-06 06:40:41 +08:00
|
|
|
|
|
|
|
java_import {
|
|
|
|
name: "foo",
|
|
|
|
jars: ["a.jar"],
|
|
|
|
compile_dex: true,
|
|
|
|
prefer: true,
|
2021-02-05 01:49:33 +08:00
|
|
|
}
|
2021-03-13 05:44:02 +08:00
|
|
|
`)
|
2020-08-06 06:40:41 +08:00
|
|
|
|
2021-04-21 21:10:42 +08:00
|
|
|
hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
|
|
|
|
hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
|
2021-03-23 00:24:49 +08:00
|
|
|
prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
|
2020-08-06 06:40:41 +08:00
|
|
|
|
2021-03-23 00:24:49 +08:00
|
|
|
fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
|
2020-08-06 06:40:41 +08:00
|
|
|
}
|
2020-07-03 22:31:32 +08:00
|
|
|
|
|
|
|
func TestHiddenAPISingletonSdks(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
unbundledBuild bool
|
|
|
|
publicStub string
|
|
|
|
systemStub string
|
|
|
|
testStub string
|
|
|
|
corePlatformStub string
|
2021-03-13 16:28:35 +08:00
|
|
|
|
|
|
|
// Additional test preparer
|
|
|
|
preparer android.FixturePreparer
|
2020-07-03 22:31:32 +08:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "testBundled",
|
|
|
|
unbundledBuild: false,
|
|
|
|
publicStub: "android_stubs_current",
|
|
|
|
systemStub: "android_system_stubs_current",
|
|
|
|
testStub: "android_test_stubs_current",
|
|
|
|
corePlatformStub: "legacy.core.platform.api.stubs",
|
2021-03-13 16:28:35 +08:00
|
|
|
preparer: android.GroupFixturePreparers(),
|
2020-07-03 22:31:32 +08:00
|
|
|
}, {
|
|
|
|
name: "testUnbundled",
|
|
|
|
unbundledBuild: true,
|
|
|
|
publicStub: "sdk_public_current_android",
|
|
|
|
systemStub: "sdk_system_current_android",
|
|
|
|
testStub: "sdk_test_current_android",
|
|
|
|
corePlatformStub: "legacy.core.platform.api.stubs",
|
2021-03-13 16:28:35 +08:00
|
|
|
preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
|
2020-07-03 22:31:32 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-03-29 09:16:14 +08:00
|
|
|
result := android.GroupFixturePreparers(
|
|
|
|
hiddenApiFixtureFactory,
|
2021-03-13 16:28:35 +08:00
|
|
|
tc.preparer,
|
2021-04-21 21:10:42 +08:00
|
|
|
prepareForTestWithDefaultPlatformBootclasspath,
|
2021-03-13 05:44:02 +08:00
|
|
|
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
|
|
|
|
variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
|
|
|
|
}),
|
|
|
|
).RunTest(t)
|
2020-07-03 22:31:32 +08:00
|
|
|
|
2021-04-21 21:10:42 +08:00
|
|
|
hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common")
|
|
|
|
hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags")
|
2020-07-03 22:31:32 +08:00
|
|
|
wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
|
2020-07-03 22:31:32 +08:00
|
|
|
|
|
|
|
wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
|
2020-07-03 22:31:32 +08:00
|
|
|
|
|
|
|
wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
|
|
|
|
|
|
|
|
wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
|
|
|
|
android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
|
2020-07-03 22:31:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateDexedPath(subDir, dex, module string) string {
|
2021-03-23 00:24:49 +08:00
|
|
|
return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
|
2020-07-03 22:31:32 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:44:02 +08:00
|
|
|
func generateDexPath(moduleDir string, module string) string {
|
|
|
|
return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
|
2020-07-03 22:31:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateSdkDexPath(module string, unbundled bool) string {
|
|
|
|
if unbundled {
|
|
|
|
return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
|
|
|
|
}
|
2021-03-13 05:44:02 +08:00
|
|
|
return generateDexPath(defaultJavaDir, module)
|
2020-07-03 22:31:32 +08:00
|
|
|
}
|
2021-01-09 01:34:44 +08:00
|
|
|
|
|
|
|
func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
|
|
|
|
|
|
|
|
// The idea behind this test is to ensure that when the build is
|
|
|
|
// confugured with a PrebuiltHiddenApiDir that the rules for the
|
|
|
|
// hiddenapi singleton copy the prebuilts to the typical output
|
|
|
|
// location, and then use that output location for the hiddenapi encode
|
|
|
|
// dex step.
|
|
|
|
|
|
|
|
// Where to find the prebuilt hiddenapi files:
|
|
|
|
prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
|
|
|
|
|
2021-03-29 09:16:14 +08:00
|
|
|
result := android.GroupFixturePreparers(
|
|
|
|
hiddenApiFixtureFactory,
|
2021-04-13 03:02:36 +08:00
|
|
|
FixtureConfigureBootJars("platform:foo"),
|
2021-03-13 05:44:02 +08:00
|
|
|
fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
|
|
|
|
).RunTestWithBp(t, `
|
2021-01-09 01:34:44 +08:00
|
|
|
java_import {
|
|
|
|
name: "foo",
|
|
|
|
jars: ["a.jar"],
|
|
|
|
compile_dex: true,
|
|
|
|
}
|
2021-03-13 05:44:02 +08:00
|
|
|
`)
|
2021-01-09 01:34:44 +08:00
|
|
|
|
|
|
|
expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
|
2021-03-23 00:24:49 +08:00
|
|
|
expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
|
|
|
|
expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
|
2021-01-09 01:34:44 +08:00
|
|
|
|
2021-03-13 05:44:02 +08:00
|
|
|
foo := result.ModuleForTests("foo", "android_common")
|
2021-01-09 01:34:44 +08:00
|
|
|
|
2021-03-13 05:44:02 +08:00
|
|
|
hiddenAPI := result.SingletonForTests("hiddenapi")
|
2021-01-09 01:34:44 +08:00
|
|
|
cpRule := hiddenAPI.Rule("Cp")
|
|
|
|
actualCpInput := cpRule.BuildParams.Input
|
|
|
|
actualCpOutput := cpRule.BuildParams.Output
|
2021-03-29 07:42:57 +08:00
|
|
|
encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
|
2021-01-09 01:34:44 +08:00
|
|
|
actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
|
|
|
|
|
2021-03-23 00:24:49 +08:00
|
|
|
android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
|
2021-01-09 01:34:44 +08:00
|
|
|
|
2021-03-23 00:24:49 +08:00
|
|
|
android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
|
2021-01-09 01:34:44 +08:00
|
|
|
|
2021-03-13 05:44:02 +08:00
|
|
|
android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
|
2021-01-09 01:34:44 +08:00
|
|
|
}
|