Program.cs 5.2 KB

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