Program.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. using Conformance;
  33. using Google.Protobuf.Reflection;
  34. using System;
  35. using System.IO;
  36. namespace Google.Protobuf.Conformance
  37. {
  38. /// <summary>
  39. /// Conformance tests. The test runner will provide JSON or proto data on stdin,
  40. /// and this program will produce its output on stdout.
  41. /// </summary>
  42. class Program
  43. {
  44. private static void Main(string[] args)
  45. {
  46. // This way we get the binary streams instead of readers/writers.
  47. var input = new BinaryReader(Console.OpenStandardInput());
  48. var output = new BinaryWriter(Console.OpenStandardOutput());
  49. var typeRegistry = TypeRegistry.FromMessages(ProtobufTestMessages.Proto3.TestAllTypes.Descriptor);
  50. int count = 0;
  51. while (RunTest(input, output, typeRegistry))
  52. {
  53. count++;
  54. }
  55. Console.Error.WriteLine("Received EOF after {0} tests", count);
  56. }
  57. private static bool RunTest(BinaryReader input, BinaryWriter output, TypeRegistry typeRegistry)
  58. {
  59. int? size = ReadInt32(input);
  60. if (size == null)
  61. {
  62. return false;
  63. }
  64. byte[] inputData = input.ReadBytes(size.Value);
  65. if (inputData.Length != size.Value)
  66. {
  67. throw new EndOfStreamException("Read " + inputData.Length + " bytes of data when expecting " + size);
  68. }
  69. ConformanceRequest request = ConformanceRequest.Parser.ParseFrom(inputData);
  70. ConformanceResponse response = PerformRequest(request, typeRegistry);
  71. byte[] outputData = response.ToByteArray();
  72. output.Write(outputData.Length);
  73. output.Write(outputData);
  74. // Ready for another test...
  75. return true;
  76. }
  77. private static ConformanceResponse PerformRequest(ConformanceRequest request, TypeRegistry typeRegistry)
  78. {
  79. ProtobufTestMessages.Proto3.TestAllTypes message;
  80. try
  81. {
  82. switch (request.PayloadCase)
  83. {
  84. case ConformanceRequest.PayloadOneofCase.JsonPayload:
  85. var parser = new JsonParser(new JsonParser.Settings(20, typeRegistry));
  86. message = parser.Parse<ProtobufTestMessages.Proto3.TestAllTypes>(request.JsonPayload);
  87. break;
  88. case ConformanceRequest.PayloadOneofCase.ProtobufPayload:
  89. {
  90. if (request.MessageType.Equals("proto3"))
  91. {
  92. message = ProtobufTestMessages.Proto3.TestAllTypes.Parser.ParseFrom(request.ProtobufPayload);
  93. }
  94. else if (request.MessageType.Equals("proto2"))
  95. {
  96. response.Skipped = "CSharp doesn't support proto2";
  97. return response;
  98. }
  99. else
  100. {
  101. throw new Exception(" Protobuf request doesn't have specific payload type");
  102. }
  103. break;
  104. }
  105. default:
  106. throw new Exception("Unsupported request payload: " + request.PayloadCase);
  107. }
  108. }
  109. catch (InvalidProtocolBufferException e)
  110. {
  111. return new ConformanceResponse { ParseError = e.Message };
  112. }
  113. catch (InvalidJsonException e)
  114. {
  115. return new ConformanceResponse { ParseError = e.Message };
  116. }
  117. try
  118. {
  119. switch (request.RequestedOutputFormat)
  120. {
  121. case global::Conformance.WireFormat.Json:
  122. var formatter = new JsonFormatter(new JsonFormatter.Settings(false, typeRegistry));
  123. return new ConformanceResponse { JsonPayload = formatter.Format(message) };
  124. case global::Conformance.WireFormat.Protobuf:
  125. return new ConformanceResponse { ProtobufPayload = message.ToByteString() };
  126. default:
  127. throw new Exception("Unsupported request output format: " + request.PayloadCase);
  128. }
  129. }
  130. catch (InvalidOperationException e)
  131. {
  132. return new ConformanceResponse { SerializeError = e.Message };
  133. }
  134. }
  135. private static int? ReadInt32(BinaryReader input)
  136. {
  137. byte[] bytes = input.ReadBytes(4);
  138. if (bytes.Length == 0)
  139. {
  140. // Cleanly reached the end of the stream
  141. return null;
  142. }
  143. if (bytes.Length != 4)
  144. {
  145. throw new EndOfStreamException("Read " + bytes.Length + " bytes of size when expecting 4");
  146. }
  147. return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
  148. }
  149. }
  150. }