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