38 import com.
google.protobuf_test_messages.proto2.TestMessagesProto2;
39 import com.
google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2;
40 import com.
google.protobuf_test_messages.proto3.TestMessagesProto3;
41 import com.
google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
42 import java.nio.ByteBuffer;
43 import java.util.ArrayList;
45 class ConformanceJavaLite {
46 private int testCount = 0;
48 private boolean readFromStdin(
byte[]
buf,
int len)
throws Exception {
62 private void writeToStdout(
byte[]
buf)
throws Exception {
63 System.out.write(
buf);
67 private int readLittleEndianIntFromStdin() throws Exception {
68 byte[]
buf =
new byte[4];
69 if (!readFromStdin(
buf, 4)) {
72 return (
buf[0] & 0xff)
73 | ((
buf[1] & 0xff) << 8)
74 | ((
buf[2] & 0xff) << 16)
75 | ((
buf[3] & 0xff) << 24);
78 private void writeLittleEndianIntToStdout(
int val)
throws Exception {
79 byte[]
buf =
new byte[4];
105 case BTYE_STRING_DECODER:
106 case BYTE_ARRAY_DECODER:
108 case ARRAY_BYTE_BUFFER_DECODER:
110 ByteBuffer
buffer = ByteBuffer.allocate(
bytes.size());
115 case READONLY_ARRAY_BYTE_BUFFER_DECODER:
120 case DIRECT_BYTE_BUFFER_DECODER:
122 ByteBuffer
buffer = ByteBuffer.allocateDirect(
bytes.size());
127 case READONLY_DIRECT_BYTE_BUFFER_DECODER:
129 ByteBuffer
buffer = ByteBuffer.allocateDirect(
bytes.size());
135 case INPUT_STREAM_DECODER:
144 private <T extends MessageLite>
T parseBinary(
147 ArrayList<T> messages =
new ArrayList<>();
148 ArrayList<InvalidProtocolBufferException>
exceptions =
new ArrayList<>();
150 for (
int i = 0;
i < BinaryDecoderType.values().
length;
i++) {
154 if (messages.isEmpty()) {
155 throw new RuntimeException(
"binary decoder types missing");
158 BinaryDecoder<T> decoder =
new BinaryDecoder<>();
160 boolean hasMessage =
false;
161 boolean hasException =
false;
162 for (
int i = 0;
i < BinaryDecoderType.values().
length; ++
i) {
172 if (hasMessage && hasException) {
174 new StringBuilder(
"Binary decoders disagreed on whether the payload was valid.\n");
175 for (
int i = 0;
i < BinaryDecoderType.values().
length; ++
i) {
176 sb.append(BinaryDecoderType.values()[
i].name());
177 if (messages.get(
i) !=
null) {
178 sb.append(
" accepted the payload.\n");
180 sb.append(
" rejected the payload.\n");
183 throw new RuntimeException(sb.toString());
191 if (exception !=
null) {
192 exception.addSuppressed(e);
202 boolean allEqual =
true;
203 for (
int i = 1;
i < messages.size(); ++
i) {
204 if (!messages.get(0).equals(messages.get(
i))) {
213 for (
int i = 0;
i < messages.size() - 1; ++
i) {
214 for (
int j =
i + 1;
j < messages.size(); ++
j) {
215 if (!messages.get(
i).equals(messages.get(j))) {
216 sb.append(BinaryDecoderType.values()[
i].name())
218 .append(BinaryDecoderType.values()[j].name())
219 .append(
" parsed the payload differently.\n");
223 throw new RuntimeException(sb.toString());
226 return messages.get(0);
229 private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest
request) {
232 request.getMessageType().equals(
"protobuf_test_messages.proto3.TestAllTypesProto3");
234 request.getMessageType().equals(
"protobuf_test_messages.proto2.TestAllTypesProto2");
236 switch (
request.getPayloadCase()) {
237 case PROTOBUF_PAYLOAD:
242 TestMessagesProto3.registerAllExtensions(
extensions);
247 return Conformance.ConformanceResponse.newBuilder()
248 .setParseError(
e.getMessage())
251 }
else if (isProto2) {
254 TestMessagesProto2.registerAllExtensions(
extensions);
259 return Conformance.ConformanceResponse.newBuilder()
260 .setParseError(
e.getMessage())
264 throw new RuntimeException(
"Protobuf request doesn't have specific payload type.");
270 return Conformance.ConformanceResponse.newBuilder()
271 .setSkipped(
"Lite runtime does not support JSON format.")
276 return Conformance.ConformanceResponse.newBuilder()
277 .setSkipped(
"Lite runtime does not support Text format.")
280 case PAYLOAD_NOT_SET:
282 throw new RuntimeException(
"Request didn't have payload.");
286 throw new RuntimeException(
"Unexpected payload case.");
290 switch (
request.getRequestedOutputFormat()) {
292 throw new RuntimeException(
"Unspecified output format.");
295 return Conformance.ConformanceResponse.newBuilder()
296 .setProtobufPayload(testMessage.toByteString())
300 return Conformance.ConformanceResponse.newBuilder()
301 .setSkipped(
"Lite runtime does not support JSON format.")
305 return Conformance.ConformanceResponse.newBuilder()
306 .setSkipped(
"Lite runtime does not support Text format.")
310 throw new RuntimeException(
"Unexpected request output.");
315 private boolean doTestIo() throws Exception {
316 int bytes = readLittleEndianIntFromStdin();
322 byte[] serializedInput =
new byte[
bytes];
324 if (!readFromStdin(serializedInput,
bytes)) {
325 throw new RuntimeException(
"Unexpected EOF from test program.");
328 Conformance.ConformanceRequest
request =
329 Conformance.ConformanceRequest.parseFrom(serializedInput);
331 byte[] serializedOutput =
response.toByteArray();
333 writeLittleEndianIntToStdout(serializedOutput.length);
334 writeToStdout(serializedOutput);
339 public void run() throws Exception {
345 "ConformanceJavaLite: received EOF from test runner after " + this.testCount +
" tests");
348 public static void main(String[]
args)
throws Exception {
349 new ConformanceJavaLite().run();