xref: /aosp_15_r20/external/protobuf/conformance/conformance_nodejs.js (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1#!/usr/bin/env node
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// https://developers.google.com/protocol-buffers/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10//     * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//     * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16//     * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32var conformance = require('conformance_pb');
33var test_messages_proto3 = require('google/protobuf/test_messages_proto3_pb');
34var test_messages_proto2 = require('google/protobuf/test_messages_proto2_pb');
35var fs = require('fs');
36
37var testCount = 0;
38
39function doTest(request) {
40  var testMessage;
41  var response = new conformance.ConformanceResponse();
42
43  try {
44    if (request.getRequestedOutputFormat() == conformance.WireFormat.JSON) {
45      response.setSkipped("JSON not supported.");
46      return response;
47    }
48
49    if (request.getRequestedOutputFormat() ==
50        conformance.WireFormat.TEXT_FORMAT) {
51      response.setSkipped('Text format is not supported as output format.');
52      return response;
53    }
54
55    switch (request.getPayloadCase()) {
56      case conformance.ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD: {
57        if (request.getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
58          try {
59            testMessage = test_messages_proto3.TestAllTypesProto3.deserializeBinary(
60                request.getProtobufPayload());
61          } catch (err) {
62            response.setParseError(err.toString());
63            return response;
64          }
65        } else if (request.getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2"){
66          try {
67            testMessage = test_messages_proto2.TestAllTypesProto2.deserializeBinary(
68                request.getProtobufPayload());
69          } catch (err) {
70            response.setParseError(err.toString());
71            return response;
72          }
73        } else {
74          throw "Protobuf request doesn\'t have specific payload type";
75        }
76      } break;
77
78      case conformance.ConformanceRequest.PayloadCase.JSON_PAYLOAD:
79        response.setSkipped("JSON not supported.");
80        return response;
81
82	  case conformance.ConformanceRequest.PayloadCase.TEXT_PAYLOAD:
83	    response.setSkipped("Text format not supported.");
84        return response;
85
86      case conformance.ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET:
87        response.setRuntimeError("Request didn't have payload");
88        return response;
89    }
90
91    switch (request.getRequestedOutputFormat()) {
92      case conformance.WireFormat.UNSPECIFIED:
93        response.setRuntimeError("Unspecified output format");
94        return response;
95
96      case conformance.WireFormat.PROTOBUF:
97        response.setProtobufPayload(testMessage.serializeBinary());
98
99      case conformance.WireFormat.JSON:
100        response.setSkipped("JSON not supported.");
101        return response;
102
103      default:
104        throw "Request didn't have requested output format";
105    }
106  } catch (err) {
107    response.setRuntimeError(err.toString());
108  }
109
110  return response;
111}
112
113function onEof(totalRead) {
114  if (totalRead == 0) {
115    return undefined;
116  } else {
117    throw "conformance_nodejs: premature EOF on stdin.";
118  }
119}
120
121// Utility function to read a buffer of N bytes.
122function readBuffer(bytes) {
123  var buf = new Buffer(bytes);
124  var totalRead = 0;
125  while (totalRead < bytes) {
126    var read = 0;
127    try {
128      read = fs.readSync(process.stdin.fd, buf, totalRead, bytes - totalRead);
129    } catch (e) {
130      if (e.code == 'EOF') {
131        return onEof(totalRead)
132      } else if (e.code == 'EAGAIN') {
133      } else {
134        throw "conformance_nodejs: Error reading from stdin." + e;
135      }
136    }
137
138    totalRead += read;
139  }
140
141  return buf;
142}
143
144function writeBuffer(buffer) {
145  var totalWritten = 0;
146  while (totalWritten < buffer.length) {
147    totalWritten += fs.writeSync(
148        process.stdout.fd, buffer, totalWritten, buffer.length - totalWritten);
149  }
150}
151
152// Returns true if the test ran successfully, false on legitimate EOF.
153// If EOF is encountered in an unexpected place, raises IOError.
154function doTestIo() {
155  var lengthBuf = readBuffer(4);
156  if (!lengthBuf) {
157    return false;
158  }
159
160  var length = lengthBuf.readInt32LE(0);
161  var serializedRequest = readBuffer(length);
162  if (!serializedRequest) {
163    throw "conformance_nodejs: Failed to read request.";
164  }
165
166  serializedRequest = new Uint8Array(serializedRequest);
167  var request =
168      conformance.ConformanceRequest.deserializeBinary(serializedRequest);
169  var response = doTest(request);
170
171  var serializedResponse = response.serializeBinary();
172
173  lengthBuf = new Buffer(4);
174  lengthBuf.writeInt32LE(serializedResponse.length, 0);
175  writeBuffer(lengthBuf);
176  writeBuffer(new Buffer(serializedResponse));
177
178  testCount += 1
179
180  return true;
181}
182
183while (true) {
184  if (!doTestIo()) {
185    console.error('conformance_nodejs: received EOF from test runner ' +
186                  "after " + testCount + " tests, exiting")
187    break;
188  }
189}
190