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