• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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 (
18	"path/filepath"
19	"runtime"
20	"testing"
21
22	"android/soong/android"
23	"android/soong/cc"
24)
25
26var prepForJavaFuzzTest = android.GroupFixturePreparers(
27	PrepareForTestWithJavaDefaultModules,
28	cc.PrepareForTestWithCcBuildComponents,
29	android.FixtureRegisterWithContext(RegisterJavaFuzzBuildComponents),
30)
31
32func TestJavaFuzz(t *testing.T) {
33	t.Parallel()
34	result := prepForJavaFuzzTest.RunTestWithBp(t, `
35		java_fuzz {
36			name: "foo",
37			srcs: ["a.java"],
38			host_supported: true,
39			device_supported: false,
40			libs: ["bar"],
41			static_libs: ["baz"],
42            jni_libs: [
43                "libjni",
44            ],
45		}
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"],
55		}
56
57		cc_library_shared {
58			name: "libjni",
59			host_supported: true,
60			device_supported: false,
61			stl: "none",
62		}
63		`)
64
65	osCommonTarget := result.Config.BuildOSCommonTarget.String()
66
67	javac := result.ModuleForTests(t, "foo", osCommonTarget).Rule("javac")
68	combineJar := result.ModuleForTests(t, "foo", osCommonTarget).Description("for javac")
69
70	if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
71		t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
72	}
73
74	baz := result.ModuleForTests(t, "baz", osCommonTarget).Rule("javac").Output.String()
75	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")
77
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	}
84
85	ctx := result.TestContext
86	foo := ctx.ModuleForTests(t, "foo", osCommonTarget).Module().(*JavaFuzzTest)
87
88	expected := "lib64/libjni.so"
89	if runtime.GOOS == "darwin" {
90		expected = "lib64/libjni.dylib"
91	}
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	}
98}
99