blob: 490115a19af46bc45c31b238b05a22340a8ac28c [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()
Damien Neilcadb4ab2020-02-03 16:17:31 -080026 want := impl.ValidationValid
Damien Neilb0c26f12019-12-16 09:37:59 -080027 if test.validationStatus != 0 {
28 want = test.validationStatus
Damien Neilb0c26f12019-12-16 09:37:59 -080029 }
30 var opts piface.UnmarshalOptions
31 opts.Resolver = protoregistry.GlobalTypes
Damien Neilcadb4ab2020-02-03 16:17:31 -080032 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 Neilb0c26f12019-12-16 09:37:59 -080038 }
39 })
40 }
41 }
42}
43
44func 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 Neilcadb4ab2020-02-03 16:17:31 -080051 _, got := impl.Validate(test.wire, mt, opts)
52 want := impl.ValidationInvalid
53 if got != want {
Damien Neilb0c26f12019-12-16 09:37:59 -080054 t.Errorf("Validate(%x) = %v, want %v", test.wire, got, want)
55 }
56 })
57 }
58 }
59}