blob: 0a2c94517a9d4ec398d5d0324ba46777cec603cf [file] [log] [blame]
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +00001// Copyright 2021 The Android Open Source Project
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
15package java
16
17import (
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000018 "path/filepath"
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000019 "runtime"
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000020 "testing"
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000021
22 "android/soong/android"
23 "android/soong/cc"
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000024)
25
26var prepForJavaFuzzTest = android.GroupFixturePreparers(
27 PrepareForTestWithJavaDefaultModules,
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000028 cc.PrepareForTestWithCcBuildComponents,
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000029 android.FixtureRegisterWithContext(RegisterJavaFuzzBuildComponents),
30)
31
32func TestJavaFuzz(t *testing.T) {
33 result := prepForJavaFuzzTest.RunTestWithBp(t, `
34 java_fuzz_host {
35 name: "foo",
36 srcs: ["a.java"],
37 libs: ["bar"],
38 static_libs: ["baz"],
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000039 jni_libs: [
40 "libjni",
41 ],
42 sanitizers: [
43 "address",
44 "fuzzer",
45 ],
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000046 }
47
48 java_library_host {
49 name: "bar",
50 srcs: ["b.java"],
51 }
52
53 java_library_host {
54 name: "baz",
55 srcs: ["c.java"],
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000056 }
57
58 cc_library_shared {
59 name: "libjni",
60 host_supported: true,
61 device_supported: false,
62 stl: "none",
63 }
64 `)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000065
66 osCommonTarget := result.Config.BuildOSCommonTarget.String()
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000067
68 osCommonTargetWithSan := osCommonTarget + "_asan" + "_fuzzer"
69 javac := result.ModuleForTests("foo", osCommonTargetWithSan).Rule("javac")
70 combineJar := result.ModuleForTests("foo", osCommonTargetWithSan).Description("for javac")
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000071
72 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
73 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
74 }
75
76 baz := result.ModuleForTests("baz", osCommonTarget).Rule("javac").Output.String()
77 barOut := filepath.Join("out", "soong", ".intermediates", "bar", osCommonTarget, "javac", "bar.jar")
78 bazOut := filepath.Join("out", "soong", ".intermediates", "baz", osCommonTarget, "javac", "baz.jar")
79
80 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barOut)
81 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazOut)
82
83 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
84 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
85 }
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000086
87 ctx := result.TestContext
88 foo := ctx.ModuleForTests("foo", osCommonTargetWithSan).Module().(*JavaFuzzLibrary)
89
90 expected := "libjni.so"
91 if runtime.GOOS == "darwin" {
92 expected = "libjni.dylib"
93 }
94
95 fooJniFilePaths := foo.jniFilePaths
96 if len(fooJniFilePaths) != 1 || fooJniFilePaths[0].Rel() != expected {
97 t.Errorf(`expected foo test data relative path [%q], got %q`,
98 expected, fooJniFilePaths.Strings())
99 }
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +0000100}