ConformanceJava.java 11.8 KB
Newer Older
1
import com.google.protobuf.AbstractMessage;
2
import com.google.protobuf.ByteString;
3
import com.google.protobuf.CodedInputStream;
4
import com.google.protobuf.ExtensionRegistry;
5 6 7 8
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Parser;
import com.google.protobuf.TextFormat;
import com.google.protobuf.conformance.Conformance;
9
import com.google.protobuf.util.JsonFormat;
10
import com.google.protobuf.util.JsonFormat.TypeRegistry;
11 12 13 14
import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
15
import java.nio.ByteBuffer;
16
import java.util.ArrayList;
17 18 19

class ConformanceJava {
  private int testCount = 0;
20
  private TypeRegistry typeRegistry;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

  private boolean readFromStdin(byte[] buf, int len) throws Exception {
    int ofs = 0;
    while (len > 0) {
      int read = System.in.read(buf, ofs, len);
      if (read == -1) {
        return false;  // EOF
      }
      ofs += read;
      len -= read;
    }

    return true;
  }

  private void writeToStdout(byte[] buf) throws Exception {
    System.out.write(buf);
  }

  // Returns -1 on EOF (the actual values will always be positive).
  private int readLittleEndianIntFromStdin() throws Exception {
    byte[] buf = new byte[4];
    if (!readFromStdin(buf, 4)) {
      return -1;
    }
46 47 48 49
    return (buf[0] & 0xff)
        | ((buf[1] & 0xff) << 8)
        | ((buf[2] & 0xff) << 16)
        | ((buf[3] & 0xff) << 24);
50 51 52 53 54 55 56 57 58 59
  }

  private void writeLittleEndianIntToStdout(int val) throws Exception {
    byte[] buf = new byte[4];
    buf[0] = (byte)val;
    buf[1] = (byte)(val >> 8);
    buf[2] = (byte)(val >> 16);
    buf[3] = (byte)(val >> 24);
    writeToStdout(buf);
  }
60

61 62 63 64 65 66 67 68
  private enum BinaryDecoderType {
    BTYE_STRING_DECODER,
    BYTE_ARRAY_DECODER,
    ARRAY_BYTE_BUFFER_DECODER,
    READONLY_ARRAY_BYTE_BUFFER_DECODER,
    DIRECT_BYTE_BUFFER_DECODER,
    READONLY_DIRECT_BYTE_BUFFER_DECODER,
    INPUT_STREAM_DECODER;
69 70
  }

71
  private static class BinaryDecoder <MessageType extends AbstractMessage> {
72
    public MessageType decode (ByteString bytes, BinaryDecoderType type,
73
        Parser <MessageType> parser, ExtensionRegistry extensions)
74
      throws InvalidProtocolBufferException {
75
      switch (type) {
76
        case BTYE_STRING_DECODER:
77 78 79 80 81 82 83 84 85 86 87 88
          return parser.parseFrom(bytes, extensions);
        case BYTE_ARRAY_DECODER:
          return parser.parseFrom(bytes.toByteArray(), extensions);
        case ARRAY_BYTE_BUFFER_DECODER: {
          ByteBuffer buffer = ByteBuffer.allocate(bytes.size());
          bytes.copyTo(buffer);
          buffer.flip();
          try {
            return parser.parseFrom(CodedInputStream.newInstance(buffer), extensions);
          } catch (InvalidProtocolBufferException e) {
            throw e;
          }
89
        }
90 91 92 93 94 95 96
        case READONLY_ARRAY_BYTE_BUFFER_DECODER: {
          try {
            return parser.parseFrom(
                CodedInputStream.newInstance(bytes.asReadOnlyByteBuffer()), extensions);
          } catch (InvalidProtocolBufferException e) {
            throw e;
          }
97
        }
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        case DIRECT_BYTE_BUFFER_DECODER: {
          ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
          bytes.copyTo(buffer);
          buffer.flip();
          try {
            return parser.parseFrom(CodedInputStream.newInstance(buffer), extensions);
          } catch (InvalidProtocolBufferException e) {
            throw e;
          }
        }
        case READONLY_DIRECT_BYTE_BUFFER_DECODER: {
          ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
          bytes.copyTo(buffer);
          buffer.flip();
          try {
            return parser.parseFrom(
                CodedInputStream.newInstance(buffer.asReadOnlyBuffer()), extensions);
          } catch (InvalidProtocolBufferException e) {
            throw e;
          }
        }
        case INPUT_STREAM_DECODER: {
          try {
            return parser.parseFrom(bytes.newInput(), extensions);
          } catch (InvalidProtocolBufferException e) {
            throw e;
124 125
          }
        }
126 127
        default :
          return null;
128 129 130
      }
    }
  }
131 132 133

  private <MessageType extends AbstractMessage> MessageType parseBinary(
      ByteString bytes, Parser <MessageType> parser, ExtensionRegistry extensions)
Yilun Chong's avatar
Yilun Chong committed
134
      throws InvalidProtocolBufferException {
135 136 137
    ArrayList <MessageType> messages = new ArrayList <MessageType> ();
    ArrayList <InvalidProtocolBufferException> exceptions =
        new ArrayList <InvalidProtocolBufferException>();
138

139 140 141 142 143
    for (int i = 0; i < BinaryDecoderType.values().length; i++) {
      messages.add(null);
      exceptions.add(null);
    }
    BinaryDecoder <MessageType> decoder = new BinaryDecoder <MessageType> ();
Yilun Chong's avatar
Yilun Chong committed
144 145 146

    boolean hasMessage = false;
    boolean hasException = false;
147
    for (int i = 0; i < BinaryDecoderType.values().length; ++i) {
Yilun Chong's avatar
Yilun Chong committed
148
      try {
149 150
        //= BinaryDecoderType.values()[i].parseProto3(bytes);
        messages.set(i, decoder.decode(bytes, BinaryDecoderType.values()[i], parser, extensions));
Yilun Chong's avatar
Yilun Chong committed
151 152
        hasMessage = true;
      } catch (InvalidProtocolBufferException e) {
153
        exceptions.set(i, e);
Yilun Chong's avatar
Yilun Chong committed
154 155 156 157 158 159 160
        hasException = true;
      }
    }

    if (hasMessage && hasException) {
      StringBuilder sb =
          new StringBuilder("Binary decoders disagreed on whether the payload was valid.\n");
161 162 163
      for (int i = 0; i < BinaryDecoderType.values().length; ++i) {
        sb.append(BinaryDecoderType.values()[i].name());
        if (messages.get(i) != null) {
Yilun Chong's avatar
Yilun Chong committed
164 165 166 167 168 169 170 171 172 173 174
          sb.append(" accepted the payload.\n");
        } else {
          sb.append(" rejected the payload.\n");
        }
      }
      throw new RuntimeException(sb.toString());
    }

    if (hasException) {
      // We do not check if exceptions are equal. Different implementations may return different
      // exception messages. Throw an arbitrary one out instead.
175
      throw exceptions.get(0);
Yilun Chong's avatar
Yilun Chong committed
176 177 178 179 180
    }

    // Fast path comparing all the messages with the first message, assuming equality being
    // symmetric and transitive.
    boolean allEqual = true;
181 182
    for (int i = 1; i < messages.size(); ++i) {
      if (!messages.get(0).equals(messages.get(i))) {
Yilun Chong's avatar
Yilun Chong committed
183 184 185 186 187 188 189 190
        allEqual = false;
        break;
      }
    }

    // Slow path: compare and find out all unequal pairs.
    if (!allEqual) {
      StringBuilder sb = new StringBuilder();
191 192 193 194
      for (int i = 0; i < messages.size() - 1; ++i) {
        for (int j = i + 1; j < messages.size(); ++j) {
          if (!messages.get(i).equals(messages.get(j))) {
            sb.append(BinaryDecoderType.values()[i].name())
Yilun Chong's avatar
Yilun Chong committed
195
                .append(" and ")
196
                .append(BinaryDecoderType.values()[j].name())
Yilun Chong's avatar
Yilun Chong committed
197 198 199 200 201 202 203
                .append(" parsed the payload differently.\n");
          }
        }
      }
      throw new RuntimeException(sb.toString());
    }

204
    return messages.get(0);
Yilun Chong's avatar
Yilun Chong committed
205 206
  }

207
  private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
Yilun Chong's avatar
Yilun Chong committed
208
    com.google.protobuf.AbstractMessage testMessage;
209
    boolean isProto3 = request.getMessageType().equals("protobuf_test_messages.proto3.TestAllTypesProto3");
210
    boolean isProto2 = request.getMessageType().equals("protobuf_test_messages.proto2.TestAllTypesProto2");
211 212 213

    switch (request.getPayloadCase()) {
      case PROTOBUF_PAYLOAD: {
Yilun Chong's avatar
Yilun Chong committed
214 215
        if (isProto3) {
          try {
216 217
            ExtensionRegistry extensions = ExtensionRegistry.newInstance();
            TestMessagesProto3.registerAllExtensions(extensions);
218
            testMessage = parseBinary(request.getProtobufPayload(), TestAllTypesProto3.parser(), extensions);
Yilun Chong's avatar
Yilun Chong committed
219 220 221
          } catch (InvalidProtocolBufferException e) {
            return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
          }
222
        } else if (isProto2) {
Yilun Chong's avatar
Yilun Chong committed
223
          try {
224 225 226
            ExtensionRegistry extensions = ExtensionRegistry.newInstance();
            TestMessagesProto2.registerAllExtensions(extensions);
            testMessage = parseBinary(request.getProtobufPayload(), TestAllTypesProto2.parser(), extensions);
Yilun Chong's avatar
Yilun Chong committed
227 228 229 230 231
          } catch (InvalidProtocolBufferException e) {
            return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
          }
        } else {
          throw new RuntimeException("Protobuf request doesn't have specific payload type.");
232 233 234 235
        }
        break;
      }
      case JSON_PAYLOAD: {
236
        try {
237
          TestMessagesProto3.TestAllTypesProto3.Builder builder =
238
              TestMessagesProto3.TestAllTypesProto3.newBuilder();
239 240 241 242 243 244
          JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(typeRegistry);
          if (request.getTestCategory()
              == Conformance.TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST) {
            parser = parser.ignoringUnknownFields();
          }
          parser.merge(request.getJsonPayload(), builder);
245 246 247 248 249
          testMessage = builder.build();
        } catch (InvalidProtocolBufferException e) {
          return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
        }
        break;
250
      }
251 252 253 254 255 256 257 258 259 260 261
      case TEXT_PAYLOAD: {
        try {
          TestMessagesProto3.TestAllTypesProto3.Builder builder =
              TestMessagesProto3.TestAllTypesProto3.newBuilder();
          TextFormat.merge(request.getTextPayload(), builder);
          testMessage = builder.build();
        } catch (TextFormat.ParseException e) {
          return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
        }
        break;
      }
262 263 264 265 266 267 268 269 270
      case PAYLOAD_NOT_SET: {
        throw new RuntimeException("Request didn't have payload.");
      }

      default: {
        throw new RuntimeException("Unexpected payload case.");
      }
    }

271
    switch (request.getRequestedOutputFormat()) {
272 273 274
      case UNSPECIFIED:
        throw new RuntimeException("Unspecified output format.");

Yilun Chong's avatar
Yilun Chong committed
275
      case PROTOBUF: {
276
        ByteString MessageString = testMessage.toByteString();
Yilun Chong's avatar
Yilun Chong committed
277 278
        return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(MessageString).build();
      }
279 280

      case JSON:
281 282 283 284 285 286 287
        try {
          return Conformance.ConformanceResponse.newBuilder().setJsonPayload(
              JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage)).build();
        } catch (InvalidProtocolBufferException | IllegalArgumentException e) {
          return Conformance.ConformanceResponse.newBuilder().setSerializeError(
              e.getMessage()).build();
        }
288

289 290 291 292
      case TEXT_FORMAT:
        return Conformance.ConformanceResponse.newBuilder().setTextPayload(
            TextFormat.printToString(testMessage)).build();

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      default: {
        throw new RuntimeException("Unexpected request output.");
      }
    }
  }

  private boolean doTestIo() throws Exception {
    int bytes = readLittleEndianIntFromStdin();

    if (bytes == -1) {
      return false;  // EOF
    }

    byte[] serializedInput = new byte[bytes];

    if (!readFromStdin(serializedInput, bytes)) {
      throw new RuntimeException("Unexpected EOF from test program.");
    }

    Conformance.ConformanceRequest request =
        Conformance.ConformanceRequest.parseFrom(serializedInput);
    Conformance.ConformanceResponse response = doTest(request);
    byte[] serializedOutput = response.toByteArray();

    writeLittleEndianIntToStdout(serializedOutput.length);
    writeToStdout(serializedOutput);

    return true;
  }

  public void run() throws Exception {
324
    typeRegistry = TypeRegistry.newBuilder().add(
325
        TestMessagesProto3.TestAllTypesProto3.getDescriptor()).build();
326
    while (doTestIo()) {
327
      this.testCount++;
328 329 330 331 332 333 334 335 336 337
    }

    System.err.println("ConformanceJava: received EOF from test runner after " +
        this.testCount + " tests");
  }

  public static void main(String[] args) throws Exception {
    new ConformanceJava().run();
  }
}