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() |
Damien Neil | cadb4ab | 2020-02-03 16:17:31 -0800 | [diff] [blame] | 26 | want := impl.ValidationValid |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 27 | if test.validationStatus != 0 { |
| 28 | want = test.validationStatus |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 29 | } |
| 30 | var opts piface.UnmarshalOptions |
| 31 | opts.Resolver = protoregistry.GlobalTypes |
Damien Neil | cadb4ab | 2020-02-03 16:17:31 -0800 | [diff] [blame] | 32 | out, status := impl.Validate(test.wire, mt, opts) |
| 33 | if status != want { |
| 34 | t.Errorf("Validate(%x) = %v, want %v", test.wire, status, want) |
| 35 | } |
| 36 | if got, want := out.Initialized, !test.partial; got != want && !test.nocheckValidInit && status == impl.ValidationValid { |
| 37 | t.Errorf("Validate(%x): initialized = %v, want %v", test.wire, got, want) |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 38 | } |
| 39 | }) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestValidateInvalid(t *testing.T) { |
| 45 | for _, test := range testInvalidMessages { |
| 46 | for _, m := range test.decodeTo { |
| 47 | t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) { |
| 48 | mt := m.ProtoReflect().Type() |
| 49 | var opts piface.UnmarshalOptions |
| 50 | opts.Resolver = protoregistry.GlobalTypes |
Damien Neil | cadb4ab | 2020-02-03 16:17:31 -0800 | [diff] [blame] | 51 | _, got := impl.Validate(test.wire, mt, opts) |
| 52 | want := impl.ValidationInvalid |
| 53 | if got != want { |
Damien Neil | b0c26f1 | 2019-12-16 09:37:59 -0800 | [diff] [blame] | 54 | t.Errorf("Validate(%x) = %v, want %v", test.wire, got, want) |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | } |