Program.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(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. 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<TestAllTypes>(request.JsonPayload);
  87. break;
  88. case ConformanceRequest.PayloadOneofCase.ProtobufPayload:
  89. message = TestAllTypes.Parser.ParseFrom(request.ProtobufPayload);
  90. break;
  91. default:
  92. throw new Exception("Unsupported request payload: " + request.PayloadCase);
  93. }
  94. }
  95. catch (InvalidProtocolBufferException e)
  96. {
  97. return new ConformanceResponse { ParseError = e.Message };
  98. }
  99. switch (request.RequestedOutputFormat)
  100. {
  101. case global::Conformance.WireFormat.JSON:
  102. var formatter = new JsonFormatter(new JsonFormatter.Settings(false, typeRegistry));
  103. return new ConformanceResponse { JsonPayload = formatter.Format(message) };
  104. case global::Conformance.WireFormat.PROTOBUF:
  105. return new ConformanceResponse { ProtobufPayload = message.ToByteString() };
  106. default:
  107. throw new Exception("Unsupported request output format: " + request.PayloadCase);
  108. }
  109. }
  110. private static int? ReadInt32(BinaryReader input)
  111. {
  112. byte[] bytes = input.ReadBytes(4);
  113. if (bytes.Length == 0)
  114. {
  115. // Cleanly reached the end of the stream
  116. return null;
  117. }
  118. if (bytes.Length != 4)
  119. {
  120. throw new EndOfStreamException("Read " + bytes.Length + " bytes of size when expecting 4");
  121. }
  122. return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
  123. }
  124. }
  125. }