blob: 735b8da933c204d22db0994bb4dcfb64a955929e [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) {
Colin Cross844cb6a2025-01-29 15:53:21 -080033 t.Parallel()
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000034 result := prepForJavaFuzzTest.RunTestWithBp(t, `
Cory Barkereaf7f5e2023-02-03 00:20:52 +000035 java_fuzz {
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000036 name: "foo",
37 srcs: ["a.java"],
Cory Barkereaf7f5e2023-02-03 00:20:52 +000038 host_supported: true,
39 device_supported: false,
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000040 libs: ["bar"],
41 static_libs: ["baz"],
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000042 jni_libs: [
43 "libjni",
44 ],
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000045 }
46
47 java_library_host {
48 name: "bar",
49 srcs: ["b.java"],
50 }
51
52 java_library_host {
53 name: "baz",
54 srcs: ["c.java"],
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000055 }
56
57 cc_library_shared {
58 name: "libjni",
59 host_supported: true,
60 device_supported: false,
61 stl: "none",
62 }
63 `)
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000064
65 osCommonTarget := result.Config.BuildOSCommonTarget.String()
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000066
Colin Cross90607e92025-02-11 14:58:07 -080067 javac := result.ModuleForTests(t, "foo", osCommonTarget).Rule("javac")
68 combineJar := result.ModuleForTests(t, "foo", osCommonTarget).Description("for javac")
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000069
70 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
71 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
72 }
73
Colin Cross90607e92025-02-11 14:58:07 -080074 baz := result.ModuleForTests(t, "baz", osCommonTarget).Rule("javac").Output.String()
Colin Cross8ff4af32025-02-19 15:17:02 -080075 barOut := filepath.Join("out", "soong", ".intermediates", "bar", osCommonTarget, "local-javac-header", "bar.jar")
76 bazOut := filepath.Join("out", "soong", ".intermediates", "baz", osCommonTarget, "local-javac-header", "baz.jar")
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000077
78 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barOut)
79 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazOut)
80
81 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
82 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
83 }
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000084
85 ctx := result.TestContext
Colin Cross90607e92025-02-11 14:58:07 -080086 foo := ctx.ModuleForTests(t, "foo", osCommonTarget).Module().(*JavaFuzzTest)
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000087
Jihoon Kang5cb82e32022-11-17 20:25:08 +000088 expected := "lib64/libjni.so"
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000089 if runtime.GOOS == "darwin" {
Jihoon Kanga2d39472022-11-21 22:10:38 +000090 expected = "lib64/libjni.dylib"
Muhammad Haseeb Ahmad7e744052022-03-25 22:50:53 +000091 }
92
93 fooJniFilePaths := foo.jniFilePaths
94 if len(fooJniFilePaths) != 1 || fooJniFilePaths[0].Rel() != expected {
95 t.Errorf(`expected foo test data relative path [%q], got %q`,
96 expected, fooJniFilePaths.Strings())
97 }
Muhammad Haseeb Ahmadaa1d0cf2022-01-01 05:14:32 +000098}