1const ConformanceRequest = goog.require('proto.conformance.ConformanceRequest'); 2const {doTest} = goog.require('javascript.protobuf.conformance'); 3const fs = require('fs'); 4 5 6/** 7 * Reads a buffer of N bytes. 8 * @param {number} bytes Number of bytes to read. 9 * @return {!Buffer} Buffer which contains data. 10 */ 11function readBuffer(bytes) { 12 // Linux cannot use process.stdin.fd (which isn't set up as sync) 13 const buf = new Buffer.alloc(bytes); 14 const fd = fs.openSync('/dev/stdin', 'r'); 15 fs.readSync(fd, buf, 0, bytes); 16 fs.closeSync(fd); 17 return buf; 18} 19 20/** 21 * Writes all data in buffer. 22 * @param {!Buffer} buffer Buffer which contains data. 23 */ 24function writeBuffer(buffer) { 25 // Under linux, process.stdout.fd is async. Needs to open stdout in a synced 26 // way for sync write. 27 const fd = fs.openSync('/dev/stdout', 'w'); 28 fs.writeSync(fd, buffer, 0, buffer.length); 29 fs.closeSync(fd); 30} 31 32/** 33 * Returns true if the test ran successfully, false on legitimate EOF. 34 * @return {boolean} Whether to continue test. 35 */ 36function runConformanceTest() { 37 const requestLengthBuf = readBuffer(4); 38 const requestLength = requestLengthBuf.readInt32LE(0); 39 if (!requestLength) { 40 return false; 41 } 42 43 const serializedRequest = readBuffer(requestLength); 44 const array = new Uint8Array(serializedRequest); 45 const request = ConformanceRequest.deserialize(array.buffer); 46 const response = doTest(request); 47 48 const serializedResponse = response.serialize(); 49 50 const responseLengthBuf = new Buffer.alloc(4); 51 responseLengthBuf.writeInt32LE(serializedResponse.byteLength, 0); 52 writeBuffer(responseLengthBuf); 53 writeBuffer(new Buffer.from(serializedResponse)); 54 55 return true; 56} 57 58while (true) { 59 if (!runConformanceTest()) { 60 break; 61 } 62} 63