aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/google.golang.org/protobuf/proto/validate_test.go
blob: e8069983d351758139253ecc61d6c98ddc07ab21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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"
	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()
				want := impl.ValidationValid
				if test.validationStatus != 0 {
					want = test.validationStatus
				}
				out, status := impl.Validate(mt, piface.UnmarshalInput{
					Buf: test.wire,
				})
				if status != want {
					t.Errorf("Validate(%x) = %v, want %v", test.wire, status, want)
				}
				if got, want := (out.Flags&piface.UnmarshalInitialized != 0), !test.partial; got != want && !test.nocheckValidInit && status == impl.ValidationValid {
					t.Errorf("Validate(%x): initialized = %v, want %v", test.wire, got, want)
				}
			})
		}
	}
}

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()
				_, got := impl.Validate(mt, piface.UnmarshalInput{
					Buf: test.wire,
				})
				want := impl.ValidationInvalid
				if got != want {
					t.Errorf("Validate(%x) = %v, want %v", test.wire, got, want)
				}
			})
		}
	}
}