1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
32 // conformance implements the conformance test subprocess protocol as
33 // documented in conformance.proto.
42 pb "github.com/golang/protobuf/_conformance/conformance_proto"
43 "github.com/golang/protobuf/jsonpb"
44 "github.com/golang/protobuf/proto"
49 inbuf := make([]byte, 0, 4096)
50 outbuf := proto.NewBuffer(nil)
52 if _, err := io.ReadFull(os.Stdin, sizeBuf[:]); err == io.EOF {
54 } else if err != nil {
55 fmt.Fprintln(os.Stderr, "go conformance: read request:", err)
58 size := binary.LittleEndian.Uint32(sizeBuf[:])
59 if int(size) > cap(inbuf) {
60 inbuf = make([]byte, size)
63 if _, err := io.ReadFull(os.Stdin, inbuf); err != nil {
64 fmt.Fprintln(os.Stderr, "go conformance: read request:", err)
68 req := new(pb.ConformanceRequest)
69 if err := proto.Unmarshal(inbuf, req); err != nil {
70 fmt.Fprintln(os.Stderr, "go conformance: parse request:", err)
75 if err := outbuf.Marshal(res); err != nil {
76 fmt.Fprintln(os.Stderr, "go conformance: marshal response:", err)
79 binary.LittleEndian.PutUint32(sizeBuf[:], uint32(len(outbuf.Bytes())))
80 if _, err := os.Stdout.Write(sizeBuf[:]); err != nil {
81 fmt.Fprintln(os.Stderr, "go conformance: write response:", err)
84 if _, err := os.Stdout.Write(outbuf.Bytes()); err != nil {
85 fmt.Fprintln(os.Stderr, "go conformance: write response:", err)
92 var jsonMarshaler = jsonpb.Marshaler{
96 func handle(req *pb.ConformanceRequest) *pb.ConformanceResponse {
98 var msg pb.TestAllTypes
99 switch p := req.Payload.(type) {
100 case *pb.ConformanceRequest_ProtobufPayload:
101 err = proto.Unmarshal(p.ProtobufPayload, &msg)
102 case *pb.ConformanceRequest_JsonPayload:
103 err = jsonpb.UnmarshalString(p.JsonPayload, &msg)
104 if err != nil && err.Error() == "unmarshaling Any not supported yet" {
105 return &pb.ConformanceResponse{
106 Result: &pb.ConformanceResponse_Skipped{
107 Skipped: err.Error(),
112 return &pb.ConformanceResponse{
113 Result: &pb.ConformanceResponse_RuntimeError{
114 RuntimeError: "unknown request payload type",
119 return &pb.ConformanceResponse{
120 Result: &pb.ConformanceResponse_ParseError{
121 ParseError: err.Error(),
125 switch req.RequestedOutputFormat {
126 case pb.WireFormat_PROTOBUF:
127 p, err := proto.Marshal(&msg)
129 return &pb.ConformanceResponse{
130 Result: &pb.ConformanceResponse_SerializeError{
131 SerializeError: err.Error(),
135 return &pb.ConformanceResponse{
136 Result: &pb.ConformanceResponse_ProtobufPayload{
140 case pb.WireFormat_JSON:
141 p, err := jsonMarshaler.MarshalToString(&msg)
143 return &pb.ConformanceResponse{
144 Result: &pb.ConformanceResponse_SerializeError{
145 SerializeError: err.Error(),
149 return &pb.ConformanceResponse{
150 Result: &pb.ConformanceResponse_JsonPayload{
155 return &pb.ConformanceResponse{
156 Result: &pb.ConformanceResponse_RuntimeError{
157 RuntimeError: "unknown output format",