Prepare methods in RuntimeVersion for Protobuf Java cross-domain version validation. No changes to Protobuf Java gencode yet.

PiperOrigin-RevId: 587025016
pull/14918/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent f84332aed4
commit fedcd62136
  1. 37
      java/core/src/main/java/com/google/protobuf/RuntimeVersion.java
  2. 26
      java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java

@ -7,6 +7,8 @@
package com.google.protobuf;
import java.util.logging.Logger;
/**
* Provides the version of this Protobuf Java runtime, and methods for Protobuf Java gencode to
* validate that versions are compatible. Fields and methods in this class should be only accessed
@ -29,6 +31,31 @@ public final class RuntimeVersion {
public static final String SUFFIX = "-dev";
private static final String VERSION_STRING = versionString(MAJOR, MINOR, PATCH, SUFFIX);
/**
* Validates that the gencode is in the same domain as the runtime.
*
* <p>This method will be directly called by the google-internal gencode to verify no cross-domain
* usages.
*
* @param gencodeDomain the domain where Protobuf Java code was generated.
* @throws ProtobufRuntimeVersionException if gencodeDomain is not the same as DOMAIN.
*/
public static void validateProtobufGencodeDomain(RuntimeDomain gencodeDomain) {
// Check the environmental variable, and temporarily disable validation if it's set to true.
String disableFlag = java.lang.System.getenv("TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK");
if ((disableFlag != null && disableFlag.equals("true"))) {
return;
}
if (gencodeDomain != DOMAIN) {
throw new ProtobufRuntimeVersionException(
String.format(
"Mismatched Protobuf Gencode/Runtime domains: gencode %s, runtime %s. Cross-domain"
+ " usage of Protobuf is not supported.",
gencodeDomain, DOMAIN));
}
}
/**
* Validates that the gencode version is compatible with this runtime version according to
* https://protobuf.dev/support/cross-version-runtime-guarantee/.
@ -37,7 +64,7 @@ public final class RuntimeVersion {
*
* <p>This method is only for Protobuf Java gencode; do not call it elsewhere.
*
* @param domain the domain where Protobuf Java code was generated. Currently ignored.
* @param domain the domain where Protobuf Java code was generated.
* @param major the major version of Protobuf Java gencode.
* @param minor the minor version of Protobuf Java gencode.
* @param patch the micro/patch version of Protobuf Java gencode.
@ -47,18 +74,14 @@ public final class RuntimeVersion {
public static void validateProtobufGencodeVersion(
RuntimeDomain domain, int major, int minor, int patch, String suffix) {
// Check the environmental variable, and temporarily disable poison pills if it's set to true.
String disableFlag = java.lang.System.getenv("TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK");
if (disableFlag != null && disableFlag.equals("true")) {
return;
}
// Check that version numbers are valid.
if (major < 0 || minor < 0 || patch < 0) {
throw new ProtobufRuntimeVersionException(
"Invalid gencode version: " + versionString(major, minor, patch, suffix));
}
validateProtobufGencodeDomain(domain);
String gencodeVersionString = versionString(major, minor, patch, suffix);
// Check that runtime major version is the same as the gencode major version.
if (major != MAJOR) {

@ -24,12 +24,26 @@ public final class RuntimeVersionTest {
RuntimeVersion.ProtobufRuntimeVersionException.class,
() ->
RuntimeVersion.validateProtobufGencodeVersion(
RuntimeVersion.DOMAIN,
-1,
RuntimeVersion.MINOR,
RuntimeVersion.PATCH,
RuntimeVersion.SUFFIX));
assertThat(thrown).hasMessageThat().contains("Invalid gencode version: -1");
RuntimeVersion.DOMAIN, 1, -2, -3, ""));
assertThat(thrown).hasMessageThat().contains("Invalid gencode version: 1.-2.-3");
}
@Test
public void versionValidation_crossDomainDisallowed() {
RuntimeVersion.RuntimeDomain gencodeDomain = RuntimeVersion.RuntimeDomain.GOOGLE_INTERNAL;
RuntimeVersion.ProtobufRuntimeVersionException thrown =
assertThrows(
RuntimeVersion.ProtobufRuntimeVersionException.class,
() -> RuntimeVersion.validateProtobufGencodeDomain(gencodeDomain));
assertThat(thrown).hasMessageThat().contains("Mismatched Protobuf Gencode/Runtime domains");
}
@Test
public void versionValidation_sameDomainAllowed() {
RuntimeVersion.RuntimeDomain gencodeDomain = RuntimeVersion.RuntimeDomain.PUBLIC;
RuntimeVersion.validateProtobufGencodeDomain(gencodeDomain);
}
@Test

Loading…
Cancel
Save