Skip to content

Commit

Permalink
Prepare methods in RuntimeVersion for Protobuf Java cross-domain vers…
Browse files Browse the repository at this point in the history
…ion validation. No changes to Protobuf Java gencode yet.

PiperOrigin-RevId: 587025016
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 1, 2023
1 parent f84332a commit fedcd62
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
37 changes: 30 additions & 7 deletions java/core/src/main/java/com/google/protobuf/RuntimeVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +31,31 @@ public enum RuntimeDomain {
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/.
Expand All @@ -37,7 +64,7 @@ public enum RuntimeDomain {
*
* <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.
Expand All @@ -47,18 +74,14 @@ public enum RuntimeDomain {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ public void versionValidation_invalidVersionNumbers() {
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
Expand Down

0 comments on commit fedcd62

Please sign in to comment.