blob: bd4b8117fa35fcd2a6dbe878e35933486a50569a [file] [log] [blame]
Damien Neilb0c26f12019-12-16 09:37:59 -08001// 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
5package proto_test
6
7import (
8 "fmt"
9 "testing"
10
11 "google.golang.org/protobuf/internal/impl"
12 "google.golang.org/protobuf/reflect/protoregistry"
13 piface "google.golang.org/protobuf/runtime/protoiface"
14)
15
16// TestValidate tests the internal message validator.
17//
18// Despite being more properly associated with the internal/impl package,
19// it is located here to take advantage of the test wire encoder/decoder inputs.
20
21func TestValidateValid(t *testing.T) {
22 for _, test := range testValidMessages {
23 for _, m := range test.decodeTo {
24 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
25 mt := m.ProtoReflect().Type()
26 want := impl.ValidationValidInitialized
27 if test.validationStatus != 0 {
28 want = test.validationStatus
29 } else if test.partial {
30 want = impl.ValidationValidMaybeUninitalized
31 }
32 var opts piface.UnmarshalOptions
33 opts.Resolver = protoregistry.GlobalTypes
34 if got, want := impl.Validate(test.wire, mt, opts), want; got != want {
35 t.Errorf("Validate(%x) = %v, want %v", test.wire, got, want)
36 }
37 })
38 }
39 }
40}
41
42func TestValidateInvalid(t *testing.T) {
43 for _, test := range testInvalidMessages {
44 for _, m := range test.decodeTo {
45 t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
46 mt := m.ProtoReflect().Type()
47 var opts piface.UnmarshalOptions
48 opts.Resolver = protoregistry.GlobalTypes
49 if got, want := impl.Validate(test.wire, mt, opts), impl.ValidationInvalid; got != want {
50 t.Errorf("Validate(%x) = %v, want %v", test.wire, got, want)
51 }
52 })
53 }
54 }
55}