conformance.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. // conformance implements the conformance test subprocess protocol as
  32. // documented in conformance.proto.
  33. package main
  34. import (
  35. "encoding/binary"
  36. "fmt"
  37. "io"
  38. "os"
  39. pb "github.com/golang/protobuf/conformance/internal/conformance_proto"
  40. "github.com/golang/protobuf/jsonpb"
  41. "github.com/golang/protobuf/proto"
  42. )
  43. func main() {
  44. var sizeBuf [4]byte
  45. inbuf := make([]byte, 0, 4096)
  46. outbuf := proto.NewBuffer(nil)
  47. for {
  48. if _, err := io.ReadFull(os.Stdin, sizeBuf[:]); err == io.EOF {
  49. break
  50. } else if err != nil {
  51. fmt.Fprintln(os.Stderr, "go conformance: read request:", err)
  52. os.Exit(1)
  53. }
  54. size := binary.LittleEndian.Uint32(sizeBuf[:])
  55. if int(size) > cap(inbuf) {
  56. inbuf = make([]byte, size)
  57. }
  58. inbuf = inbuf[:size]
  59. if _, err := io.ReadFull(os.Stdin, inbuf); err != nil {
  60. fmt.Fprintln(os.Stderr, "go conformance: read request:", err)
  61. os.Exit(1)
  62. }
  63. req := new(pb.ConformanceRequest)
  64. if err := proto.Unmarshal(inbuf, req); err != nil {
  65. fmt.Fprintln(os.Stderr, "go conformance: parse request:", err)
  66. os.Exit(1)
  67. }
  68. res := handle(req)
  69. if err := outbuf.Marshal(res); err != nil {
  70. fmt.Fprintln(os.Stderr, "go conformance: marshal response:", err)
  71. os.Exit(1)
  72. }
  73. binary.LittleEndian.PutUint32(sizeBuf[:], uint32(len(outbuf.Bytes())))
  74. if _, err := os.Stdout.Write(sizeBuf[:]); err != nil {
  75. fmt.Fprintln(os.Stderr, "go conformance: write response:", err)
  76. os.Exit(1)
  77. }
  78. if _, err := os.Stdout.Write(outbuf.Bytes()); err != nil {
  79. fmt.Fprintln(os.Stderr, "go conformance: write response:", err)
  80. os.Exit(1)
  81. }
  82. outbuf.Reset()
  83. }
  84. }
  85. var jsonMarshaler = jsonpb.Marshaler{
  86. OrigName: true,
  87. }
  88. func handle(req *pb.ConformanceRequest) *pb.ConformanceResponse {
  89. var err error
  90. var msg pb.TestAllTypes
  91. switch p := req.Payload.(type) {
  92. case *pb.ConformanceRequest_ProtobufPayload:
  93. err = proto.Unmarshal(p.ProtobufPayload, &msg)
  94. case *pb.ConformanceRequest_JsonPayload:
  95. err = jsonpb.UnmarshalString(p.JsonPayload, &msg)
  96. default:
  97. return &pb.ConformanceResponse{
  98. Result: &pb.ConformanceResponse_RuntimeError{
  99. RuntimeError: "unknown request payload type",
  100. },
  101. }
  102. }
  103. if err != nil {
  104. return &pb.ConformanceResponse{
  105. Result: &pb.ConformanceResponse_ParseError{
  106. ParseError: err.Error(),
  107. },
  108. }
  109. }
  110. switch req.RequestedOutputFormat {
  111. case pb.WireFormat_PROTOBUF:
  112. p, err := proto.Marshal(&msg)
  113. if err != nil {
  114. return &pb.ConformanceResponse{
  115. Result: &pb.ConformanceResponse_SerializeError{
  116. SerializeError: err.Error(),
  117. },
  118. }
  119. }
  120. return &pb.ConformanceResponse{
  121. Result: &pb.ConformanceResponse_ProtobufPayload{
  122. ProtobufPayload: p,
  123. },
  124. }
  125. case pb.WireFormat_JSON:
  126. p, err := jsonMarshaler.MarshalToString(&msg)
  127. if err != nil {
  128. return &pb.ConformanceResponse{
  129. Result: &pb.ConformanceResponse_SerializeError{
  130. SerializeError: err.Error(),
  131. },
  132. }
  133. }
  134. return &pb.ConformanceResponse{
  135. Result: &pb.ConformanceResponse_JsonPayload{
  136. JsonPayload: p,
  137. },
  138. }
  139. default:
  140. return &pb.ConformanceResponse{
  141. Result: &pb.ConformanceResponse_RuntimeError{
  142. RuntimeError: "unknown output format",
  143. },
  144. }
  145. }
  146. }