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