blob: a779929cdc9bacc3af3590a2659edb12be4712ef [file] [log] [blame]
Herbie Ongd64dceb2019-04-25 01:19:57 -07001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// This binary implements the conformance test subprocess protocol as documented
6// in conformance.proto.
7package main
8
9import (
10 "encoding/binary"
11 "io"
12 "log"
13 "os"
14
15 "github.com/golang/protobuf/v2/encoding/jsonpb"
16 "github.com/golang/protobuf/v2/proto"
17
18 pb "github.com/golang/protobuf/v2/internal/testprotos/conformance"
19)
20
21func main() {
22 var sizeBuf [4]byte
23 inbuf := make([]byte, 0, 4096)
24 for {
25 _, err := io.ReadFull(os.Stdin, sizeBuf[:])
26 if err == io.EOF {
27 break
28 }
29 if err != nil {
30 log.Fatalf("conformance: read request: %v", err)
31 }
32 size := binary.LittleEndian.Uint32(sizeBuf[:])
33 if int(size) > cap(inbuf) {
34 inbuf = make([]byte, size)
35 }
36 inbuf = inbuf[:size]
37 if _, err := io.ReadFull(os.Stdin, inbuf); err != nil {
38 log.Fatalf("conformance: read request: %v", err)
39 }
40
41 req := &pb.ConformanceRequest{}
42 if err := proto.Unmarshal(inbuf, req); err != nil {
43 log.Fatalf("conformance: parse request: %v", err)
44 }
45 res := handle(req)
46
47 out, err := proto.Marshal(res)
48 if err != nil {
49 log.Fatalf("conformance: marshal response: %v", err)
50 }
51 binary.LittleEndian.PutUint32(sizeBuf[:], uint32(len(out)))
52 if _, err := os.Stdout.Write(sizeBuf[:]); err != nil {
53 log.Fatalf("conformance: write response: %v", err)
54 }
55 if _, err := os.Stdout.Write(out); err != nil {
56 log.Fatalf("conformance: write response: %v", err)
57 }
58 }
59}
60
61func handle(req *pb.ConformanceRequest) *pb.ConformanceResponse {
62 var err error
63 var msg proto.Message = &pb.TestAllTypesProto2{}
64 if req.GetMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3" {
65 msg = &pb.TestAllTypesProto3{}
66 }
67
68 switch p := req.Payload.(type) {
69 case *pb.ConformanceRequest_ProtobufPayload:
70 err = proto.Unmarshal(p.ProtobufPayload, msg)
71 case *pb.ConformanceRequest_JsonPayload:
72 err = jsonpb.Unmarshal(msg, []byte(p.JsonPayload))
73 default:
74 return &pb.ConformanceResponse{
75 Result: &pb.ConformanceResponse_RuntimeError{
76 RuntimeError: "unknown request payload type",
77 },
78 }
79 }
80 if err != nil {
81 return &pb.ConformanceResponse{
82 Result: &pb.ConformanceResponse_ParseError{
83 ParseError: err.Error(),
84 },
85 }
86 }
87
88 switch req.RequestedOutputFormat {
89 case pb.WireFormat_PROTOBUF:
90 p, err := proto.Marshal(msg)
91 if err != nil {
92 return &pb.ConformanceResponse{
93 Result: &pb.ConformanceResponse_SerializeError{
94 SerializeError: err.Error(),
95 },
96 }
97 }
98 return &pb.ConformanceResponse{
99 Result: &pb.ConformanceResponse_ProtobufPayload{
100 ProtobufPayload: p,
101 },
102 }
103 case pb.WireFormat_JSON:
104 p, err := jsonpb.Marshal(msg)
105 if err != nil {
106 return &pb.ConformanceResponse{
107 Result: &pb.ConformanceResponse_SerializeError{
108 SerializeError: err.Error(),
109 },
110 }
111 }
112 return &pb.ConformanceResponse{
113 Result: &pb.ConformanceResponse_JsonPayload{
114 JsonPayload: string(p),
115 },
116 }
117 default:
118 return &pb.ConformanceResponse{
119 Result: &pb.ConformanceResponse_RuntimeError{
120 RuntimeError: "unknown output format",
121 },
122 }
123 }
124}