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