Skip to content

Commit

Permalink
Upgrade the target SDK in Android tests to version 34, which is recom…
Browse files Browse the repository at this point in the history
…mended by Google Play.

To do this, we need to use the PlatformTestStorage API to load test data. Otherwise
the tests can't access the test data as normal file access are not permitted anymore.

See:
https://developer.android.com/google/play/requirements/target-sdk
PiperOrigin-RevId: 668483337
Change-Id: I085a5ead8169d30fc61962d95eb81c645e870c18
  • Loading branch information
juergw authored and copybara-github committed Aug 28, 2024
1 parent 196de16 commit 4994f5c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"],
)
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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);
}
}
1 change: 1 addition & 0 deletions tink_java_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tools/create_maven_build_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
Expand Down

0 comments on commit 4994f5c

Please sign in to comment.