conformance_testee.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. goog.module('javascript.protobuf.conformance');
  2. const ConformanceRequest = goog.require('proto.conformance.ConformanceRequest');
  3. const ConformanceResponse = goog.require('proto.conformance.ConformanceResponse');
  4. const TestAllTypesProto2 = goog.require('proto.conformance.TestAllTypesProto2');
  5. const TestAllTypesProto3 = goog.require('proto.conformance.TestAllTypesProto3');
  6. const WireFormat = goog.require('proto.conformance.WireFormat');
  7. const base64 = goog.require('goog.crypt.base64');
  8. /**
  9. * Creates a `proto.conformance.ConformanceResponse` response according to the
  10. * `proto.conformance.ConformanceRequest` request.
  11. * @param {!ConformanceRequest} request
  12. * @return {!ConformanceResponse} response
  13. */
  14. function doTest(request) {
  15. const response = ConformanceResponse.createEmpty();
  16. if(request.getPayloadCase() === ConformanceRequest.PayloadCase.JSON_PAYLOAD) {
  17. response.setSkipped('Json is not supported as input format.');
  18. return response;
  19. }
  20. if(request.getPayloadCase() === ConformanceRequest.PayloadCase.TEXT_PAYLOAD) {
  21. response.setSkipped('Text format is not supported as input format.');
  22. return response;
  23. }
  24. if(request.getPayloadCase() === ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET) {
  25. response.setRuntimeError('Request didn\'t have payload.');
  26. return response;
  27. }
  28. if(request.getPayloadCase() !== ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD) {
  29. throw new Error('Request didn\'t have accepted input format.');
  30. }
  31. if (request.getRequestedOutputFormat() === WireFormat.JSON) {
  32. response.setSkipped('Json is not supported as output format.');
  33. return response;
  34. }
  35. if (request.getRequestedOutputFormat() === WireFormat.TEXT_FORMAT) {
  36. response.setSkipped('Text format is not supported as output format.');
  37. return response;
  38. }
  39. if (request.getRequestedOutputFormat() === WireFormat.TEXT_FORMAT) {
  40. response.setRuntimeError('Unspecified output format');
  41. return response;
  42. }
  43. if (request.getRequestedOutputFormat() !== WireFormat.PROTOBUF) {
  44. throw new Error('Request didn\'t have accepted output format.');
  45. }
  46. if (request.getMessageType() === 'conformance.FailureSet') {
  47. response.setProtobufPayload(new ArrayBuffer(0));
  48. } else if (
  49. request.getMessageType() ===
  50. 'protobuf_test_messages.proto2.TestAllTypesProto2') {
  51. try {
  52. const testMessage =
  53. TestAllTypesProto2.deserialize(request.getProtobufPayload());
  54. response.setProtobufPayload(testMessage.serialize());
  55. } catch (err) {
  56. response.setParseError(err.toString());
  57. }
  58. } else if (
  59. request.getMessageType() ===
  60. 'protobuf_test_messages.proto3.TestAllTypesProto3') {
  61. try {
  62. const testMessage =
  63. TestAllTypesProto3.deserialize(request.getProtobufPayload());
  64. response.setProtobufPayload(testMessage.serialize());
  65. } catch (err) {
  66. response.setParseError(err.toString());
  67. }
  68. } else {
  69. throw new Error(
  70. `Payload message not supported: ${request.getMessageType()}.`);
  71. }
  72. return response;
  73. }
  74. /**
  75. * Same as doTest, but both request and response are in base64.
  76. * @param {string} base64Request
  77. * @return {string} response
  78. */
  79. function runConformanceTest(base64Request) {
  80. const request =
  81. ConformanceRequest.deserialize(
  82. base64.decodeStringToUint8Array(base64Request).buffer);
  83. const response = doTest(request);
  84. return base64.encodeByteArray(new Uint8Array(response.serialize()));
  85. }
  86. // Needed for node test
  87. exports.doTest = doTest;
  88. // Needed for browser test
  89. goog.exportSymbol('runConformanceTest', runConformanceTest);