diff --git a/src/main/java/com/google/crypto/tink/internal/testing/BUILD.bazel b/src/main/java/com/google/crypto/tink/internal/testing/BUILD.bazel index f383ca3b5..e1b21b5d5 100644 --- a/src/main/java/com/google/crypto/tink/internal/testing/BUILD.bazel +++ b/src/main/java/com/google/crypto/tink/internal/testing/BUILD.bazel @@ -93,14 +93,19 @@ java_library( srcs = ["BigIntegerTestUtil.java"], ) +java_library( + name = "build_dispatched_test_code", + srcs = ["BuildDispatchedTestCode.java"], +) + java_library( name = "test_files", srcs = ["TestFiles.java"], - deps = ["//src/main/java/com/google/crypto/tink/testing:test_util"], + deps = [":build_dispatched_test_code"], ) android_library( name = "test_files-android", srcs = ["TestFiles.java"], - deps = ["//src/main/java/com/google/crypto/tink/testing:test_util-android"], + deps = ["//src_android/main/java/com/google/crypto/tink/internal/testing:build_dispatched_test_code-android"], ) diff --git a/src/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java b/src/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java new file mode 100644 index 000000000..a047835e1 --- /dev/null +++ b/src/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java @@ -0,0 +1,38 @@ +// Copyright 2024 Google LLC +// +// 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 specified language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// + +package com.google.crypto.tink.internal.testing; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; + +/** + * Static testonly utility functions which need to be compiled with different code in Android and + * Java. + * + *

This is the Java version. The android code can be found in + * third_party/tink/java_src/src_android/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java + */ +final class BuildDispatchedTestCode { + + private BuildDispatchedTestCode() {} + + public static InputStream openInputFile(String pathname) throws FileNotFoundException { + return new FileInputStream(new File(pathname)); + } +} diff --git a/src/main/java/com/google/crypto/tink/internal/testing/TestFiles.java b/src/main/java/com/google/crypto/tink/internal/testing/TestFiles.java index 829caf8bd..b15fe62a8 100644 --- a/src/main/java/com/google/crypto/tink/internal/testing/TestFiles.java +++ b/src/main/java/com/google/crypto/tink/internal/testing/TestFiles.java @@ -16,23 +16,15 @@ package com.google.crypto.tink.internal.testing; -import com.google.crypto.tink.testing.TestUtil; -import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; /** Helper functions for reading test files. */ public final class TestFiles { - /** Provides an InputStream to a test file. */ + /** Provides an InputStream to a test file dependency. */ public static InputStream openInputFile(String pathname) throws FileNotFoundException { - String path = pathname; - if (TestUtil.isAndroid()) { - // TODO(juerg): Use the PlatformTestStorage API on Android. - path = "/sdcard/googletest/test_runfiles/google3/" + path; // Special prefix for Android. - } - return new FileInputStream(new File(path)); + return BuildDispatchedTestCode.openInputFile(pathname); } private TestFiles() {} diff --git a/src_android/main/java/com/google/crypto/tink/internal/testing/BUILD.bazel b/src_android/main/java/com/google/crypto/tink/internal/testing/BUILD.bazel new file mode 100644 index 000000000..53b2b2e11 --- /dev/null +++ b/src_android/main/java/com/google/crypto/tink/internal/testing/BUILD.bazel @@ -0,0 +1,15 @@ +load("@build_bazel_rules_android//android:rules.bzl", "android_library") + +licenses(["notice"]) + +package( + default_testonly = 1, + default_visibility = ["//visibility:public"], +) + +android_library( + name = "build_dispatched_test_code-android", + testonly = 1, + srcs = ["BuildDispatchedTestCode.java"], + deps = ["@maven//:androidx_test_monitor"], +) diff --git a/src_android/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java b/src_android/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java new file mode 100644 index 000000000..16a41b465 --- /dev/null +++ b/src_android/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java @@ -0,0 +1,37 @@ +// Copyright 2024 Google LLC +// +// 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 specified language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// + +package com.google.crypto.tink.internal.testing; + +import androidx.test.platform.io.PlatformTestStorageRegistry; +import java.io.FileNotFoundException; +import java.io.InputStream; + +/** + * Static testonly utility functions which need to be compiled with different code in Android and + * Java. + * + *

This is the Android version. The Java code can be found in + * third_party/tink/java_src/src/main/java/com/google/crypto/tink/internal/testing/BuildDispatchedTestCode.java + */ +final class BuildDispatchedTestCode { + + private BuildDispatchedTestCode() {} + + public static InputStream openInputFile(String pathname) throws FileNotFoundException { + return PlatformTestStorageRegistry.getInstance().openInputFile("google3/" + pathname); + } +} diff --git a/tink_java_deps.bzl b/tink_java_deps.bzl index f28ad6d87..e2043b4cc 100644 --- a/tink_java_deps.bzl +++ b/tink_java_deps.bzl @@ -7,6 +7,7 @@ TINK_MAVEN_ARTIFACTS = [ "com.google.protobuf:protobuf-java:3.25.3", "com.google.protobuf:protobuf-javalite:3.25.3", "androidx.annotation:annotation:1.8.2", + "androidx.test:monitor:1.7.2", "com.google.api-client:google-api-client:2.2.0", "com.google.code.findbugs:jsr305:3.0.2", "com.google.code.gson:gson:2.10.1", diff --git a/tools/create_maven_build_file.sh b/tools/create_maven_build_file.sh index 2e82f21fc..282771354 100755 --- a/tools/create_maven_build_file.sh +++ b/tools/create_maven_build_file.sh @@ -123,7 +123,7 @@ except kind(java_library,${TINK_JAVA_INTEGRATION_PREFIX}/...)" \ except attr(testonly,1,${TINK_JAVA_PREFIX}/...)" > "${expected_android_deps}" "${BAZEL_CMD}" query "kind(android_library,${TINK_JAVA_ANDROID_PREFIX}/...) \ -except attr(testonly,1,${TINK_JAVA_PREFIX}/...)" >> "${expected_android_deps}" +except attr(testonly,1,${TINK_JAVA_ANDROID_PREFIX}/...)" >> "${expected_android_deps}" create_build_file "${expected_tink_deps}" "${expected_android_deps}" }