File: testmessages_opaque_test.go

package info (click to toggle)
golang-google-protobuf 1.36.7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 14,996 kB
  • sloc: sh: 94; makefile: 4
file content (96 lines) | stat: -rw-r--r-- 3,274 bytes parent folder | download | duplicates (2)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2024 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 (
	"flag"
	"fmt"
	"os"
	"strings"
	"testing"

	"google.golang.org/protobuf/internal/impl"
	"google.golang.org/protobuf/internal/protobuild"
	"google.golang.org/protobuf/reflect/protoreflect"
	"google.golang.org/protobuf/reflect/protoregistry"
	"google.golang.org/protobuf/testing/protopack"

	_ "google.golang.org/protobuf/internal/testprotos/lazy"
	_ "google.golang.org/protobuf/internal/testprotos/lazy/lazy_opaque"
	_ "google.golang.org/protobuf/internal/testprotos/required"
	_ "google.golang.org/protobuf/internal/testprotos/required/required_opaque"
	_ "google.golang.org/protobuf/internal/testprotos/test"
	_ "google.golang.org/protobuf/internal/testprotos/test3"
	_ "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_hybrid"
	_ "google.golang.org/protobuf/internal/testprotos/testeditions/testeditions_opaque"
)

var testLazyUnmarshal = flag.Bool("test_lazy_unmarshal", false, "test lazy proto.Unmarshal")

func TestMain(m *testing.M) {
	flag.Parse()
	impl.EnableLazyUnmarshal(*testLazyUnmarshal)
	os.Exit(m.Run())
}

var relatedMessages = func() map[protoreflect.MessageType][]protoreflect.MessageType {
	related := map[protoreflect.MessageType][]protoreflect.MessageType{}
	const opaqueNamePrefix = "opaque."
	protoregistry.GlobalTypes.RangeMessages(func(mt protoreflect.MessageType) bool {
		name := mt.Descriptor().FullName()
		if !strings.HasPrefix(string(name), opaqueNamePrefix) {
			return true
		}
		mt1, err := protoregistry.GlobalTypes.FindMessageByName(name[len(opaqueNamePrefix):])
		if err != nil {
			panic(fmt.Sprintf("%v: can't find related message", name))
		}
		related[mt1] = append(related[mt1], mt)
		return true
	})
	return related
}()

func init() {
	testValidMessages = append(testValidMessages, []testProto{
		{
			desc:          "lazy field contains wrong wire type",
			checkFastInit: true,
			decodeTo: makeMessages(protobuild.Message{
				"optional_nested_message": protobuild.Message{
					protobuild.Unknown: protopack.Message{
						protopack.Tag{2, protopack.VarintType}, protopack.Varint(3),
					}.Marshal(),
				},
			}),
			wire: protopack.Message{
				protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
					protopack.Tag{2, protopack.VarintType}, protopack.Varint(3),
				}),
			}.Marshal(),
		}, {
			desc:          "lazy field contains right and wrong wire type",
			checkFastInit: true,
			decodeTo: makeMessages(protobuild.Message{
				"optional_nested_message": protobuild.Message{
					"corecursive": protobuild.Message{
						"optional_int32": 2,
					},
					protobuild.Unknown: protopack.Message{
						protopack.Tag{2, protopack.VarintType}, protopack.Varint(3),
					}.Marshal(),
				},
			}),
			wire: protopack.Message{
				protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
					protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
						protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
					}),
					protopack.Tag{2, protopack.VarintType}, protopack.Varint(3),
				}),
			}.Marshal(),
		},
	}...)
}