1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package conformance_test
import (
"encoding/binary"
"flag"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
pb "google.golang.org/protobuf/internal/testprotos/conformance"
)
func init() {
// When the environment variable RUN_AS_CONFORMANCE_PLUGIN is set,
// we skip running the tests and instead act as a conformance plugin.
// This allows the binary to pass itself to conformance.
if os.Getenv("RUN_AS_CONFORMANCE_PLUGIN") == "1" {
main()
os.Exit(0)
}
}
var (
execute = flag.Bool("execute", false, "execute the conformance test")
protoRoot = flag.String("protoroot", os.Getenv("PROTOBUF_ROOT"), "The root of the protobuf source tree.")
)
func Test(t *testing.T) {
if !*execute {
t.SkipNow()
}
binPath := filepath.Join(*protoRoot, "bazel-bin", "conformance", "conformance_test_runner")
cmd := exec.Command(binPath,
"--failure_list", "failing_tests.txt",
"--text_format_failure_list", "failing_tests_text_format.txt",
"--enforce_recommended",
os.Args[0])
cmd.Env = append(os.Environ(), "RUN_AS_CONFORMANCE_PLUGIN=1")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("execution error: %v\n\n%s", err, out)
}
}
func main() {
var sizeBuf [4]byte
inbuf := make([]byte, 0, 4096)
for {
_, err := io.ReadFull(os.Stdin, sizeBuf[:])
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("conformance: read request: %v", err)
}
size := binary.LittleEndian.Uint32(sizeBuf[:])
if int(size) > cap(inbuf) {
inbuf = make([]byte, size)
}
inbuf = inbuf[:size]
if _, err := io.ReadFull(os.Stdin, inbuf); err != nil {
log.Fatalf("conformance: read request: %v", err)
}
req := &pb.ConformanceRequest{}
if err := proto.Unmarshal(inbuf, req); err != nil {
log.Fatalf("conformance: parse request: %v", err)
}
res := handle(req)
out, err := proto.Marshal(res)
if err != nil {
log.Fatalf("conformance: marshal response: %v", err)
}
binary.LittleEndian.PutUint32(sizeBuf[:], uint32(len(out)))
if _, err := os.Stdout.Write(sizeBuf[:]); err != nil {
log.Fatalf("conformance: write response: %v", err)
}
if _, err := os.Stdout.Write(out); err != nil {
log.Fatalf("conformance: write response: %v", err)
}
}
}
func handle(req *pb.ConformanceRequest) (res *pb.ConformanceResponse) {
var msg proto.Message = &pb.TestAllTypesProto2{}
if req.GetMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3" {
msg = &pb.TestAllTypesProto3{}
}
// Unmarshal the test message.
var err error
switch p := req.Payload.(type) {
case *pb.ConformanceRequest_ProtobufPayload:
err = proto.Unmarshal(p.ProtobufPayload, msg)
case *pb.ConformanceRequest_JsonPayload:
err = protojson.UnmarshalOptions{
DiscardUnknown: req.TestCategory == pb.TestCategory_JSON_IGNORE_UNKNOWN_PARSING_TEST,
}.Unmarshal([]byte(p.JsonPayload), msg)
case *pb.ConformanceRequest_TextPayload:
err = prototext.Unmarshal([]byte(p.TextPayload), msg)
default:
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_RuntimeError{
RuntimeError: "unknown request payload type",
},
}
}
if err != nil {
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_ParseError{
ParseError: err.Error(),
},
}
}
// Marshal the test message.
var b []byte
switch req.RequestedOutputFormat {
case pb.WireFormat_PROTOBUF:
b, err = proto.Marshal(msg)
res = &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_ProtobufPayload{
ProtobufPayload: b,
},
}
case pb.WireFormat_JSON:
b, err = protojson.Marshal(msg)
res = &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_JsonPayload{
JsonPayload: string(b),
},
}
case pb.WireFormat_TEXT_FORMAT:
b, err = prototext.MarshalOptions{
EmitUnknown: req.PrintUnknownFields,
}.Marshal(msg)
res = &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_TextPayload{
TextPayload: string(b),
},
}
default:
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_RuntimeError{
RuntimeError: "unknown output format",
},
}
}
if err != nil {
return &pb.ConformanceResponse{
Result: &pb.ConformanceResponse_SerializeError{
SerializeError: err.Error(),
},
}
}
return res
}
|