| OLD | NEW |
| (Empty) |
| 1 #region Copyright notice and license | |
| 2 // Protocol Buffers - Google's data interchange format | |
| 3 // Copyright 2015 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 #endregion | |
| 32 | |
| 33 using Conformance; | |
| 34 using System; | |
| 35 using System.IO; | |
| 36 | |
| 37 namespace Google.Protobuf.Conformance | |
| 38 { | |
| 39 /// <summary> | |
| 40 /// Conformance tests. The test runner will provide JSON or proto data on st
din, | |
| 41 /// and this program will produce its output on stdout. | |
| 42 /// </summary> | |
| 43 class Program | |
| 44 { | |
| 45 private static void Main(string[] args) | |
| 46 { | |
| 47 // This way we get the binary streams instead of readers/writers. | |
| 48 var input = new BinaryReader(Console.OpenStandardInput()); | |
| 49 var output = new BinaryWriter(Console.OpenStandardOutput()); | |
| 50 | |
| 51 int count = 0; | |
| 52 while (RunTest(input, output)) | |
| 53 { | |
| 54 count++; | |
| 55 } | |
| 56 Console.Error.WriteLine("Received EOF after {0} tests", count); | |
| 57 } | |
| 58 | |
| 59 private static bool RunTest(BinaryReader input, BinaryWriter output) | |
| 60 { | |
| 61 int? size = ReadInt32(input); | |
| 62 if (size == null) | |
| 63 { | |
| 64 return false; | |
| 65 } | |
| 66 byte[] inputData = input.ReadBytes(size.Value); | |
| 67 if (inputData.Length != size.Value) | |
| 68 { | |
| 69 throw new EndOfStreamException("Read " + inputData.Length + " by
tes of data when expecting " + size); | |
| 70 } | |
| 71 ConformanceRequest request = ConformanceRequest.Parser.ParseFrom(inp
utData); | |
| 72 ConformanceResponse response = PerformRequest(request); | |
| 73 byte[] outputData = response.ToByteArray(); | |
| 74 output.Write(outputData.Length); | |
| 75 output.Write(outputData); | |
| 76 // Ready for another test... | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 private static ConformanceResponse PerformRequest(ConformanceRequest req
uest) | |
| 81 { | |
| 82 TestAllTypes message; | |
| 83 switch (request.PayloadCase) | |
| 84 { | |
| 85 case ConformanceRequest.PayloadOneofCase.JsonPayload: | |
| 86 return new ConformanceResponse { Skipped = "JSON parsing not
implemented in C# yet" }; | |
| 87 case ConformanceRequest.PayloadOneofCase.ProtobufPayload: | |
| 88 try | |
| 89 { | |
| 90 message = TestAllTypes.Parser.ParseFrom(request.Protobuf
Payload); | |
| 91 } | |
| 92 catch (InvalidProtocolBufferException e) | |
| 93 { | |
| 94 return new ConformanceResponse { ParseError = e.Message
}; | |
| 95 } | |
| 96 break; | |
| 97 default: | |
| 98 throw new Exception("Unsupported request payload: " + reques
t.PayloadCase); | |
| 99 } | |
| 100 switch (request.RequestedOutputFormat) | |
| 101 { | |
| 102 case global::Conformance.WireFormat.JSON: | |
| 103 return new ConformanceResponse { JsonPayload = JsonFormatter
.Default.Format(message) }; | |
| 104 case global::Conformance.WireFormat.PROTOBUF: | |
| 105 return new ConformanceResponse { ProtobufPayload = message.T
oByteString() }; | |
| 106 default: | |
| 107 throw new Exception("Unsupported request output format: " +
request.PayloadCase); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 private static int? ReadInt32(BinaryReader input) | |
| 112 { | |
| 113 byte[] bytes = input.ReadBytes(4); | |
| 114 if (bytes.Length == 0) | |
| 115 { | |
| 116 // Cleanly reached the end of the stream | |
| 117 return null; | |
| 118 } | |
| 119 if (bytes.Length != 4) | |
| 120 { | |
| 121 throw new EndOfStreamException("Read " + bytes.Length + " bytes
of size when expecting 4"); | |
| 122 } | |
| 123 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] <<
24); | |
| 124 } | |
| 125 } | |
| 126 } | |
| OLD | NEW |