2019-12-16 17:37:59 +00:00
|
|
|
// 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 proto_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/internal/impl"
|
|
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
|
|
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestValidate tests the internal message validator.
|
|
|
|
//
|
|
|
|
// Despite being more properly associated with the internal/impl package,
|
|
|
|
// it is located here to take advantage of the test wire encoder/decoder inputs.
|
|
|
|
|
|
|
|
func TestValidateValid(t *testing.T) {
|
|
|
|
for _, test := range testValidMessages {
|
|
|
|
for _, m := range test.decodeTo {
|
|
|
|
t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
|
|
|
|
mt := m.ProtoReflect().Type()
|
2020-02-04 00:17:31 +00:00
|
|
|
want := impl.ValidationValid
|
2019-12-16 17:37:59 +00:00
|
|
|
if test.validationStatus != 0 {
|
|
|
|
want = test.validationStatus
|
|
|
|
}
|
|
|
|
var opts piface.UnmarshalOptions
|
|
|
|
opts.Resolver = protoregistry.GlobalTypes
|
2020-02-04 00:17:31 +00:00
|
|
|
out, status := impl.Validate(test.wire, mt, opts)
|
|
|
|
if status != want {
|
|
|
|
t.Errorf("Validate(%x) = %v, want %v", test.wire, status, want)
|
|
|
|
}
|
|
|
|
if got, want := out.Initialized, !test.partial; got != want && !test.nocheckValidInit && status == impl.ValidationValid {
|
|
|
|
t.Errorf("Validate(%x): initialized = %v, want %v", test.wire, got, want)
|
2019-12-16 17:37:59 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateInvalid(t *testing.T) {
|
|
|
|
for _, test := range testInvalidMessages {
|
|
|
|
for _, m := range test.decodeTo {
|
|
|
|
t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
|
|
|
|
mt := m.ProtoReflect().Type()
|
|
|
|
var opts piface.UnmarshalOptions
|
|
|
|
opts.Resolver = protoregistry.GlobalTypes
|
2020-02-04 00:17:31 +00:00
|
|
|
_, got := impl.Validate(test.wire, mt, opts)
|
|
|
|
want := impl.ValidationInvalid
|
|
|
|
if got != want {
|
2019-12-16 17:37:59 +00:00
|
|
|
t.Errorf("Validate(%x) = %v, want %v", test.wire, got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|