Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 1 | // 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 | package proto_test |
| 6 | |
| 7 | import ( |
| 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 | |
| 21 | func 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 | |
| 42 | func 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 | } |