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