protobuf/conformance/ConformanceJava.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
36 import com.google.protobuf.Parser;
38 import com.google.protobuf.conformance.Conformance;
41 import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
42 import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2;
43 import com.google.protobuf_test_messages.proto3.TestMessagesProto3;
44 import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
45 import java.nio.ByteBuffer;
46 import java.util.ArrayList;
47 
48 class ConformanceJava {
49  private int testCount = 0;
50  private TypeRegistry typeRegistry;
51 
52  private boolean readFromStdin(byte[] buf, int len) throws Exception {
53  int ofs = 0;
54  while (len > 0) {
55  int read = System.in.read(buf, ofs, len);
56  if (read == -1) {
57  return false; // EOF
58  }
59  ofs += read;
60  len -= read;
61  }
62 
63  return true;
64  }
65 
66  private void writeToStdout(byte[] buf) throws Exception {
67  System.out.write(buf);
68  }
69 
70  // Returns -1 on EOF (the actual values will always be positive).
71  private int readLittleEndianIntFromStdin() throws Exception {
72  byte[] buf = new byte[4];
73  if (!readFromStdin(buf, 4)) {
74  return -1;
75  }
76  return (buf[0] & 0xff)
77  | ((buf[1] & 0xff) << 8)
78  | ((buf[2] & 0xff) << 16)
79  | ((buf[3] & 0xff) << 24);
80  }
81 
82  private void writeLittleEndianIntToStdout(int val) throws Exception {
83  byte[] buf = new byte[4];
84  buf[0] = (byte) val;
85  buf[1] = (byte) (val >> 8);
86  buf[2] = (byte) (val >> 16);
87  buf[3] = (byte) (val >> 24);
88  writeToStdout(buf);
89  }
90 
91  private enum BinaryDecoderType {
92  BTYE_STRING_DECODER,
93  BYTE_ARRAY_DECODER,
94  ARRAY_BYTE_BUFFER_DECODER,
95  READONLY_ARRAY_BYTE_BUFFER_DECODER,
96  DIRECT_BYTE_BUFFER_DECODER,
97  READONLY_DIRECT_BYTE_BUFFER_DECODER,
98  INPUT_STREAM_DECODER;
99  }
100 
101  private static class BinaryDecoder<T extends AbstractMessage> {
102  public T decode(
105  switch (type) {
106  case BTYE_STRING_DECODER:
107  case BYTE_ARRAY_DECODER:
108  return parser.parseFrom(bytes, extensions);
109  case ARRAY_BYTE_BUFFER_DECODER:
110  {
111  ByteBuffer buffer = ByteBuffer.allocate(bytes.size());
112  bytes.copyTo(buffer);
113  buffer.flip();
115  }
116  case READONLY_ARRAY_BYTE_BUFFER_DECODER:
117  {
118  return parser.parseFrom(
119  CodedInputStream.newInstance(bytes.asReadOnlyByteBuffer()), extensions);
120  }
121  case DIRECT_BYTE_BUFFER_DECODER:
122  {
123  ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
124  bytes.copyTo(buffer);
125  buffer.flip();
127  }
128  case READONLY_DIRECT_BYTE_BUFFER_DECODER:
129  {
130  ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
131  bytes.copyTo(buffer);
132  buffer.flip();
133  return parser.parseFrom(
134  CodedInputStream.newInstance(buffer.asReadOnlyBuffer()), extensions);
135  }
136  case INPUT_STREAM_DECODER:
137  {
138  return parser.parseFrom(bytes.newInput(), extensions);
139  }
140  }
141  return null;
142  }
143  }
144 
145  private <T extends AbstractMessage> T parseBinary(
148  ArrayList<T> messages = new ArrayList<>();
149  ArrayList<InvalidProtocolBufferException> exceptions = new ArrayList<>();
150 
151  for (int i = 0; i < BinaryDecoderType.values().length; i++) {
152  messages.add(null);
153  exceptions.add(null);
154  }
155  if (messages.isEmpty()) {
156  throw new RuntimeException("binary decoder types missing");
157  }
158 
159  BinaryDecoder<T> decoder = new BinaryDecoder<>();
160 
161  boolean hasMessage = false;
162  boolean hasException = false;
163  for (int i = 0; i < BinaryDecoderType.values().length; ++i) {
164  try {
165  // = BinaryDecoderType.values()[i].parseProto3(bytes);
166  messages.set(i, decoder.decode(bytes, BinaryDecoderType.values()[i], parser, extensions));
167  hasMessage = true;
168  } catch (InvalidProtocolBufferException e) {
169  exceptions.set(i, e);
170  hasException = true;
171  }
172  }
173 
174  if (hasMessage && hasException) {
175  StringBuilder sb =
176  new StringBuilder("Binary decoders disagreed on whether the payload was valid.\n");
177  for (int i = 0; i < BinaryDecoderType.values().length; ++i) {
178  sb.append(BinaryDecoderType.values()[i].name());
179  if (messages.get(i) != null) {
180  sb.append(" accepted the payload.\n");
181  } else {
182  sb.append(" rejected the payload.\n");
183  }
184  }
185  throw new RuntimeException(sb.toString());
186  }
187 
188  if (hasException) {
189  // We do not check if exceptions are equal. Different implementations may return different
190  // exception messages. Throw an arbitrary one out instead.
191  InvalidProtocolBufferException exception = null;
193  if (exception != null) {
194  exception.addSuppressed(e);
195  } else {
196  exception = e;
197  }
198  }
199  throw exception;
200  }
201 
202  // Fast path comparing all the messages with the first message, assuming equality being
203  // symmetric and transitive.
204  boolean allEqual = true;
205  for (int i = 1; i < messages.size(); ++i) {
206  if (!messages.get(0).equals(messages.get(i))) {
207  allEqual = false;
208  break;
209  }
210  }
211 
212  // Slow path: compare and find out all unequal pairs.
213  if (!allEqual) {
214  StringBuilder sb = new StringBuilder();
215  for (int i = 0; i < messages.size() - 1; ++i) {
216  for (int j = i + 1; j < messages.size(); ++j) {
217  if (!messages.get(i).equals(messages.get(j))) {
218  sb.append(BinaryDecoderType.values()[i].name())
219  .append(" and ")
220  .append(BinaryDecoderType.values()[j].name())
221  .append(" parsed the payload differently.\n");
222  }
223  }
224  }
225  throw new RuntimeException(sb.toString());
226  }
227 
228  return messages.get(0);
229  }
230 
231  private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
232  com.google.protobuf.AbstractMessage testMessage;
233  boolean isProto3 =
234  request.getMessageType().equals("protobuf_test_messages.proto3.TestAllTypesProto3");
235  boolean isProto2 =
236  request.getMessageType().equals("protobuf_test_messages.proto2.TestAllTypesProto2");
237 
238  switch (request.getPayloadCase()) {
239  case PROTOBUF_PAYLOAD:
240  {
241  if (isProto3) {
242  try {
244  TestMessagesProto3.registerAllExtensions(extensions);
245  testMessage =
246  parseBinary(
247  request.getProtobufPayload(), TestAllTypesProto3.parser(), extensions);
248  } catch (InvalidProtocolBufferException e) {
249  return Conformance.ConformanceResponse.newBuilder()
250  .setParseError(e.getMessage())
251  .build();
252  }
253  } else if (isProto2) {
254  try {
256  TestMessagesProto2.registerAllExtensions(extensions);
257  testMessage =
258  parseBinary(
259  request.getProtobufPayload(), TestAllTypesProto2.parser(), extensions);
260  } catch (InvalidProtocolBufferException e) {
261  return Conformance.ConformanceResponse.newBuilder()
262  .setParseError(e.getMessage())
263  .build();
264  }
265  } else {
266  throw new RuntimeException("Protobuf request doesn't have specific payload type.");
267  }
268  break;
269  }
270  case JSON_PAYLOAD:
271  {
272  try {
274  if (request.getTestCategory()
275  == Conformance.TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST) {
276  parser = parser.ignoringUnknownFields();
277  }
278  if (isProto3) {
279  TestMessagesProto3.TestAllTypesProto3.Builder builder =
280  TestMessagesProto3.TestAllTypesProto3.newBuilder();
281  parser.merge(request.getJsonPayload(), builder);
282  testMessage = builder.build();
283  } else if (isProto2) {
284  TestMessagesProto2.TestAllTypesProto2.Builder builder =
285  TestMessagesProto2.TestAllTypesProto2.newBuilder();
286  parser.merge(request.getJsonPayload(), builder);
287  testMessage = builder.build();
288  } else {
289  throw new RuntimeException("Protobuf request doesn't have specific payload type.");
290  }
291  } catch (InvalidProtocolBufferException e) {
292  return Conformance.ConformanceResponse.newBuilder()
293  .setParseError(e.getMessage())
294  .build();
295  }
296  break;
297  }
298  case TEXT_PAYLOAD:
299  {
300  if (isProto3) {
301  try {
302  TestMessagesProto3.TestAllTypesProto3.Builder builder =
303  TestMessagesProto3.TestAllTypesProto3.newBuilder();
304  TextFormat.merge(request.getTextPayload(), builder);
305  testMessage = builder.build();
306  } catch (TextFormat.ParseException e) {
307  return Conformance.ConformanceResponse.newBuilder()
308  .setParseError(e.getMessage())
309  .build();
310  }
311  } else if (isProto2) {
312  try {
313  TestMessagesProto2.TestAllTypesProto2.Builder builder =
314  TestMessagesProto2.TestAllTypesProto2.newBuilder();
315  TextFormat.merge(request.getTextPayload(), builder);
316  testMessage = builder.build();
317  } catch (TextFormat.ParseException e) {
318  return Conformance.ConformanceResponse.newBuilder()
319  .setParseError(e.getMessage())
320  .build();
321  }
322  } else {
323  throw new RuntimeException("Protobuf request doesn't have specific payload type.");
324  }
325  break;
326  }
327  case PAYLOAD_NOT_SET:
328  {
329  throw new RuntimeException("Request didn't have payload.");
330  }
331 
332  default:
333  {
334  throw new RuntimeException("Unexpected payload case.");
335  }
336  }
337 
338  switch (request.getRequestedOutputFormat()) {
339  case UNSPECIFIED:
340  throw new RuntimeException("Unspecified output format.");
341 
342  case PROTOBUF:
343  {
344  ByteString messageString = testMessage.toByteString();
345  return Conformance.ConformanceResponse.newBuilder()
346  .setProtobufPayload(messageString)
347  .build();
348  }
349 
350  case JSON:
351  try {
352  return Conformance.ConformanceResponse.newBuilder()
353  .setJsonPayload(
354  JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage))
355  .build();
356  } catch (InvalidProtocolBufferException | IllegalArgumentException e) {
357  return Conformance.ConformanceResponse.newBuilder()
358  .setSerializeError(e.getMessage())
359  .build();
360  }
361 
362  case TEXT_FORMAT:
363  return Conformance.ConformanceResponse.newBuilder()
364  .setTextPayload(TextFormat.printToString(testMessage))
365  .build();
366 
367  default:
368  {
369  throw new RuntimeException("Unexpected request output.");
370  }
371  }
372  }
373 
374  private boolean doTestIo() throws Exception {
375  int bytes = readLittleEndianIntFromStdin();
376 
377  if (bytes == -1) {
378  return false; // EOF
379  }
380 
381  byte[] serializedInput = new byte[bytes];
382 
383  if (!readFromStdin(serializedInput, bytes)) {
384  throw new RuntimeException("Unexpected EOF from test program.");
385  }
386 
387  Conformance.ConformanceRequest request =
388  Conformance.ConformanceRequest.parseFrom(serializedInput);
389  Conformance.ConformanceResponse response = doTest(request);
390  byte[] serializedOutput = response.toByteArray();
391 
392  writeLittleEndianIntToStdout(serializedOutput.length);
393  writeToStdout(serializedOutput);
394 
395  return true;
396  }
397 
398  public void run() throws Exception {
399  typeRegistry =
401  .add(TestMessagesProto3.TestAllTypesProto3.getDescriptor())
402  .build();
403  while (doTestIo()) {
404  this.testCount++;
405  }
406 
407  System.err.println(
408  "ConformanceJava: received EOF from test runner after " + this.testCount + " tests");
409  }
410 
411  public static void main(String[] args) throws Exception {
412  new ConformanceJava().run();
413  }
414 }
com.google.protobuf.util.JsonFormat.TypeRegistry.newBuilder
static Builder newBuilder()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:517
absl::str_format_internal::LengthMod::j
@ j
com.google.protobuf.util.JsonFormat.parser
static Parser parser()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:396
StringBuilder
Definition: protobuf/ruby/ext/google/protobuf_c/protobuf.c:57
com.google.protobuf.CodedInputStream.newInstance
static CodedInputStream newInstance(final InputStream input)
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/CodedInputStream.java:79
com.google.protobuf.TextFormat.printToString
static String printToString(final MessageOrBuilder message)
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TextFormat.java:143
com.google.protobuf.util.JsonFormat.printer
static Printer printer()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:109
com.google.protobuf.util.JsonFormat.Printer.print
String print(MessageOrBuilder message)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:379
benchmark.request
request
Definition: benchmark.py:77
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ConformanceJava.BinaryDecoder
Definition: bloaty/third_party/protobuf/conformance/ConformanceJava.java:71
ConformanceJava.BinaryDecoderType
Definition: bloaty/third_party/protobuf/conformance/ConformanceJava.java:61
com.google.protobuf
Definition: bloaty/third_party/protobuf/benchmarks/java/src/main/java/com/google/protobuf/ProtoCaliperBenchmark.java:2
exceptions
static const char *const exceptions[]
Definition: v3name_test.cc:90
T
#define T(upbtypeconst, upbtype, ctype, default_value)
com.google.protobuf.TextFormat.ParseException
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TextFormat.java:1279
absl::FormatConversionChar::e
@ e
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
asyncio_get_stats.parser
parser
Definition: asyncio_get_stats.py:34
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
com.google.protobuf.util
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/Durations.java:31
com.google.protobuf.Parser
Definition: protobuf/java/core/src/main/java/com/google/protobuf/Parser.java:47
com.google.protobuf.util.JsonFormat
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:101
com.google.protobuf.CodedInputStream
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/CodedInputStream.java:61
main
int main(int argc, char **argv)
Definition: examples/cpp/compression/greeter_client.cc:78
com.google.protobuf.util.JsonFormat.Printer.usingTypeRegistry
Printer usingTypeRegistry(TypeRegistry oldRegistry)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:167
StringBuilder
struct StringBuilder StringBuilder
Definition: protobuf/ruby/ext/google/protobuf_c/protobuf.c:63
com.google.protobuf.util.JsonFormat.TypeRegistry.Builder.build
TypeRegistry build()
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:577
com.google.protobuf.util.JsonFormat.Parser.usingTypeRegistry
Parser usingTypeRegistry(TypeRegistry oldRegistry)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:433
conf.extensions
list extensions
Definition: doc/python/sphinx/conf.py:54
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
java
ares::byte
unsigned char byte
Definition: ares-test.h:33
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
client.run
def run()
Definition: examples/python/async_streaming/client.py:109
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
com.google.protobuf.ExtensionRegistry.newInstance
static ExtensionRegistry newInstance()
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java:93
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
com.google.protobuf.util.JsonFormat.TypeRegistry
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:507
com.google
com
com.google.protobuf.util.JsonFormat.Parser
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:407
com.google.protobuf.ExtensionRegistry
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ExtensionRegistry.java:91
com.google.protobuf.AbstractMessage
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/AbstractMessage.java:52
ConformanceJava.BinaryDecoder.decode
T decode(ByteString bytes, BinaryDecoderType type, Parser< T > parser, ExtensionRegistry extensions)
Definition: protobuf/conformance/ConformanceJava.java:102
com.google.protobuf.InvalidProtocolBufferException
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java:41
com.google.protobuf.util.JsonFormat.TypeRegistry.Builder.add
Builder add(Descriptor messageType)
Definition: bloaty/third_party/protobuf/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java:550
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
com.google.protobuf.TextFormat
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TextFormat.java:55
com.google.protobuf.TextFormat.merge
static void merge(final Readable input, final Message.Builder builder)
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/TextFormat.java:1363
com.google.protobuf.ByteString
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ByteString.java:67


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:54