Program.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.TestAllTypesProto3.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.TestAllTypesProto3 message;
  80. try
  81. {
  82. switch (request.PayloadCase)
  83. {
  84. case ConformanceRequest.PayloadOneofCase.JsonPayload:
  85. if (request.TestCategory == global::Conformance.TestCategory.JsonIgnoreUnknownParsingTest) {
  86. return new ConformanceResponse { Skipped = "CSharp doesn't support skipping unknown fields in json parsing." };
  87. }
  88. var parser = new JsonParser(new JsonParser.Settings(20, typeRegistry));
  89. message = parser.Parse<ProtobufTestMessages.Proto3.TestAllTypesProto3>(request.JsonPayload);
  90. break;
  91. case ConformanceRequest.PayloadOneofCase.ProtobufPayload:
  92. {
  93. if (request.MessageType.Equals("protobuf_test_messages.proto3.TestAllTypesProto3"))
  94. {
  95. message = ProtobufTestMessages.Proto3.TestAllTypesProto3.Parser.ParseFrom(request.ProtobufPayload);
  96. }
  97. else if (request.MessageType.Equals("protobuf_test_messages.proto2.TestAllTypesProto2"))
  98. {
  99. return new ConformanceResponse { Skipped = "CSharp doesn't support proto2" };
  100. }
  101. else
  102. {
  103. throw new Exception(" Protobuf request doesn't have specific payload type");
  104. }
  105. break;
  106. }
  107. default:
  108. throw new Exception("Unsupported request payload: " + request.PayloadCase);
  109. }
  110. }
  111. catch (InvalidProtocolBufferException e)
  112. {
  113. return new ConformanceResponse { ParseError = e.Message };
  114. }
  115. catch (InvalidJsonException e)
  116. {
  117. return new ConformanceResponse { ParseError = e.Message };
  118. }
  119. try
  120. {
  121. switch (request.RequestedOutputFormat)
  122. {
  123. case global::Conformance.WireFormat.Json:
  124. var formatter = new JsonFormatter(new JsonFormatter.Settings(false, typeRegistry));
  125. return new ConformanceResponse { JsonPayload = formatter.Format(message) };
  126. case global::Conformance.WireFormat.Protobuf:
  127. return new ConformanceResponse { ProtobufPayload = message.ToByteString() };
  128. default:
  129. throw new Exception("Unsupported request output format: " + request.PayloadCase);
  130. }
  131. }
  132. catch (InvalidOperationException e)
  133. {
  134. return new ConformanceResponse { SerializeError = e.Message };
  135. }
  136. }
  137. private static int? ReadInt32(BinaryReader input)
  138. {
  139. byte[] bytes = input.ReadBytes(4);
  140. if (bytes.Length == 0)
  141. {
  142. // Cleanly reached the end of the stream
  143. return null;
  144. }
  145. if (bytes.Length != 4)
  146. {
  147. throw new EndOfStreamException("Read " + bytes.Length + " bytes of size when expecting 4");
  148. }
  149. return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
  150. }
  151. }
  152. }