| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 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 |  | 
 | 15 | package java | 
 | 16 |  | 
 | 17 | import ( | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 18 | 	"path/filepath" | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 19 | 	"runtime" | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 20 | 	"testing" | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 21 |  | 
 | 22 | 	"android/soong/android" | 
 | 23 | 	"android/soong/cc" | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 24 | ) | 
 | 25 |  | 
 | 26 | var prepForJavaFuzzTest = android.GroupFixturePreparers( | 
 | 27 | 	PrepareForTestWithJavaDefaultModules, | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 28 | 	cc.PrepareForTestWithCcBuildComponents, | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 29 | 	android.FixtureRegisterWithContext(RegisterJavaFuzzBuildComponents), | 
 | 30 | ) | 
 | 31 |  | 
 | 32 | func TestJavaFuzz(t *testing.T) { | 
 | 33 | 	result := prepForJavaFuzzTest.RunTestWithBp(t, ` | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 34 | 		java_fuzz { | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 35 | 			name: "foo", | 
 | 36 | 			srcs: ["a.java"], | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 37 | 			host_supported: true, | 
 | 38 | 			device_supported: false, | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 39 | 			libs: ["bar"], | 
 | 40 | 			static_libs: ["baz"], | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 41 |             jni_libs: [ | 
 | 42 |                 "libjni", | 
 | 43 |             ], | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 44 | 		} | 
 | 45 |  | 
 | 46 | 		java_library_host { | 
 | 47 | 			name: "bar", | 
 | 48 | 			srcs: ["b.java"], | 
 | 49 | 		} | 
 | 50 |  | 
 | 51 | 		java_library_host { | 
 | 52 | 			name: "baz", | 
 | 53 | 			srcs: ["c.java"], | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 54 | 		} | 
 | 55 |  | 
 | 56 | 		cc_library_shared { | 
 | 57 | 			name: "libjni", | 
 | 58 | 			host_supported: true, | 
 | 59 | 			device_supported: false, | 
 | 60 | 			stl: "none", | 
 | 61 | 		} | 
 | 62 | 		`) | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 63 |  | 
 | 64 | 	osCommonTarget := result.Config.BuildOSCommonTarget.String() | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 65 |  | 
| Jihoon Kang | 5cb82e3 | 2022-11-17 20:25:08 +0000 | [diff] [blame] | 66 | 	javac := result.ModuleForTests("foo", osCommonTarget).Rule("javac") | 
 | 67 | 	combineJar := result.ModuleForTests("foo", osCommonTarget).Description("for javac") | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 68 |  | 
 | 69 | 	if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { | 
 | 70 | 		t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) | 
 | 71 | 	} | 
 | 72 |  | 
 | 73 | 	baz := result.ModuleForTests("baz", osCommonTarget).Rule("javac").Output.String() | 
| Colin Cross | f06d8dc | 2023-07-18 22:11:07 -0700 | [diff] [blame] | 74 | 	barOut := filepath.Join("out", "soong", ".intermediates", "bar", osCommonTarget, "javac-header", "bar.jar") | 
 | 75 | 	bazOut := filepath.Join("out", "soong", ".intermediates", "baz", osCommonTarget, "javac-header", "baz.jar") | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 76 |  | 
 | 77 | 	android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barOut) | 
 | 78 | 	android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazOut) | 
 | 79 |  | 
 | 80 | 	if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { | 
 | 81 | 		t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) | 
 | 82 | 	} | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 83 |  | 
 | 84 | 	ctx := result.TestContext | 
| Cory Barker | eaf7f5e | 2023-02-03 00:20:52 +0000 | [diff] [blame] | 85 | 	foo := ctx.ModuleForTests("foo", osCommonTarget).Module().(*JavaFuzzTest) | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 86 |  | 
| Jihoon Kang | 5cb82e3 | 2022-11-17 20:25:08 +0000 | [diff] [blame] | 87 | 	expected := "lib64/libjni.so" | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 88 | 	if runtime.GOOS == "darwin" { | 
| Jihoon Kang | a2d3947 | 2022-11-21 22:10:38 +0000 | [diff] [blame] | 89 | 		expected = "lib64/libjni.dylib" | 
| Muhammad Haseeb Ahmad | 7e74405 | 2022-03-25 22:50:53 +0000 | [diff] [blame] | 90 | 	} | 
 | 91 |  | 
 | 92 | 	fooJniFilePaths := foo.jniFilePaths | 
 | 93 | 	if len(fooJniFilePaths) != 1 || fooJniFilePaths[0].Rel() != expected { | 
 | 94 | 		t.Errorf(`expected foo test data relative path [%q], got %q`, | 
 | 95 | 			expected, fooJniFilePaths.Strings()) | 
 | 96 | 	} | 
| Muhammad Haseeb Ahmad | aa1d0cf | 2022-01-01 05:14:32 +0000 | [diff] [blame] | 97 | } |