• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 package com.google.protobuf;
9 
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.assertThrows;
12 
13 import java.util.logging.Logger;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.junit.runners.JUnit4;
17 
18 @RunWith(JUnit4.class)
19 public final class RuntimeVersionTest {
20 
21   @Test
versionValidation_invalidVersionNumbers()22   public void versionValidation_invalidVersionNumbers() {
23     RuntimeVersion.ProtobufRuntimeVersionException thrown =
24         assertThrows(
25             RuntimeVersion.ProtobufRuntimeVersionException.class,
26             () ->
27                 RuntimeVersion.validateProtobufGencodeVersion(
28                     RuntimeVersion.DOMAIN, 1, -2, -3, "", "dummy"));
29     assertThat(thrown).hasMessageThat().contains("Invalid gencode version: 1.-2.-3");
30   }
31 
32   @Test
versionValidation_crossDomainDisallowed()33   public void versionValidation_crossDomainDisallowed() {
34 
35     RuntimeVersion.RuntimeDomain gencodeDomain =
36         RuntimeVersion.RuntimeDomain.GOOGLE_INTERNAL;
37     RuntimeVersion.ProtobufRuntimeVersionException thrown =
38         assertThrows(
39             RuntimeVersion.ProtobufRuntimeVersionException.class,
40             () ->
41                 RuntimeVersion.validateProtobufGencodeVersion(
42                     gencodeDomain, 1, 2, 3, "", "testing.Foo"));
43     assertThat(thrown)
44         .hasMessageThat()
45         .contains("Detected mismatched Protobuf Gencode/Runtime domains when loading testing.Foo");
46   }
47 
48   @Test
versionValidation_mismatchingMajorDisallowed()49   public void versionValidation_mismatchingMajorDisallowed() {
50     int gencodeMajor = 1;
51     RuntimeVersion.ProtobufRuntimeVersionException thrown =
52         assertThrows(
53             RuntimeVersion.ProtobufRuntimeVersionException.class,
54             () ->
55                 RuntimeVersion.validateProtobufGencodeVersion(
56                     RuntimeVersion.DOMAIN,
57                     gencodeMajor,
58                     RuntimeVersion.MINOR,
59                     RuntimeVersion.PATCH,
60                     RuntimeVersion.SUFFIX,
61                     "testing.Foo"));
62     assertThat(thrown)
63         .hasMessageThat()
64         .contains(
65             "Detected mismatched Protobuf Gencode/Runtime major versions when loading testing.Foo");
66   }
67 
68   @Test
versionValidation_versionNumbersAllTheSameAllowed()69   public void versionValidation_versionNumbersAllTheSameAllowed() {
70     RuntimeVersion.validateProtobufGencodeVersion(
71         RuntimeVersion.DOMAIN,
72         RuntimeVersion.MAJOR,
73         RuntimeVersion.MINOR,
74         RuntimeVersion.PATCH,
75         RuntimeVersion.SUFFIX,
76         "dummy");
77   }
78 
79   @Test
versionValidation_newerRuntimeVersionAllowed()80   public void versionValidation_newerRuntimeVersionAllowed() {
81     int gencodeMinor = RuntimeVersion.MINOR - 1;
82     RuntimeVersion.validateProtobufGencodeVersion(
83         RuntimeVersion.DOMAIN,
84         RuntimeVersion.MAJOR,
85         gencodeMinor,
86         RuntimeVersion.PATCH,
87         RuntimeVersion.SUFFIX,
88         "dummy");
89   }
90 
91   @Test
versionValidation_olderRuntimeVersionDisallowed()92   public void versionValidation_olderRuntimeVersionDisallowed() {
93     int gencodeMinor = RuntimeVersion.MINOR + 1;
94     RuntimeVersion.ProtobufRuntimeVersionException thrown =
95         assertThrows(
96             RuntimeVersion.ProtobufRuntimeVersionException.class,
97             () ->
98                 RuntimeVersion.validateProtobufGencodeVersion(
99                     RuntimeVersion.DOMAIN,
100                     RuntimeVersion.MAJOR,
101                     gencodeMinor,
102                     RuntimeVersion.PATCH,
103                     RuntimeVersion.SUFFIX,
104                     "testing.Foo"));
105     assertThat(thrown)
106         .hasMessageThat()
107         .contains(
108             "Detected incompatible Protobuf Gencode/Runtime versions when loading testing.Foo");
109 
110     int gencodePatch = RuntimeVersion.PATCH + 1;
111     thrown =
112         assertThrows(
113             RuntimeVersion.ProtobufRuntimeVersionException.class,
114             () ->
115                 RuntimeVersion.validateProtobufGencodeVersion(
116                     RuntimeVersion.DOMAIN,
117                     RuntimeVersion.MAJOR,
118                     RuntimeVersion.MINOR,
119                     gencodePatch,
120                     RuntimeVersion.SUFFIX,
121                     "testing.Bar"));
122     assertThat(thrown)
123         .hasMessageThat()
124         .contains(
125             "Detected incompatible Protobuf Gencode/Runtime versions when loading testing.Bar");
126   }
127 
128   @Test
versionValidation_differentVersionSuffixDisallowed()129   public void versionValidation_differentVersionSuffixDisallowed() {
130     String gencodeSuffix = "-test";
131     RuntimeVersion.ProtobufRuntimeVersionException thrown =
132         assertThrows(
133             RuntimeVersion.ProtobufRuntimeVersionException.class,
134             () ->
135                 RuntimeVersion.validateProtobufGencodeVersion(
136                     RuntimeVersion.DOMAIN,
137                     RuntimeVersion.MAJOR,
138                     RuntimeVersion.MINOR,
139                     RuntimeVersion.PATCH,
140                     gencodeSuffix,
141                     "testing.Foo"));
142     assertThat(thrown)
143         .hasMessageThat()
144         .contains(
145             "Detected mismatched Protobuf Gencode/Runtime version suffixes when loading"
146                 + " testing.Foo");
147   }
148 
149   @Test
versionValidation_gencodeOneMajorVersionOlderWarning()150   public void versionValidation_gencodeOneMajorVersionOlderWarning() {
151     TestUtil.TestLogHandler logHandler = new TestUtil.TestLogHandler();
152     Logger logger = Logger.getLogger(RuntimeVersion.class.getName());
153     logger.addHandler(logHandler);
154     RuntimeVersion.validateProtobufGencodeVersion(
155         RuntimeVersion.DOMAIN,
156         RuntimeVersion.MAJOR - 1,
157         RuntimeVersion.MINOR,
158         RuntimeVersion.PATCH,
159         RuntimeVersion.SUFFIX,
160         "dummy");
161     assertThat(logHandler.getStoredLogRecords()).hasSize(1);
162     assertThat(logHandler.getStoredLogRecords().get(0).getMessage())
163         .contains("is exactly one major version older than the runtime version");
164   }
165 }
166