File: lazy_buffersharing_test.go

package info (click to toggle)
golang-google-protobuf 1.36.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 16,816 kB
  • sloc: sh: 94; makefile: 4
file content (151 lines) | stat: -rw-r--r-- 4,633 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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 impl_test

import (
	"testing"

	mixedpb "google.golang.org/protobuf/internal/testprotos/mixed"
	"google.golang.org/protobuf/proto"
)

var enableLazy = proto.UnmarshalOptions{}
var disableLazy = proto.UnmarshalOptions{
	NoLazyDecoding: true,
}

func TestCopyTopLevelLazy(t *testing.T) {
	testCopyTopLevel(t, enableLazy)
}

func TestCopyTopLevelEager(t *testing.T) {
	testCopyTopLevel(t, disableLazy)
}

// testCopyTopLevel tests that the buffer is copied to a safe location
// when the opaque proto is the top level proto
func testCopyTopLevel(t *testing.T, unmarshalOpts proto.UnmarshalOptions) {
	m := mixedpb.OpaqueLazy_builder{
		Opaque: mixedpb.OpaqueLazy_builder{
			OptionalInt32: proto.Int32(23),
		}.Build(),
	}.Build()
	if got, want := m.GetOpaque().GetOptionalInt32(), int32(23); got != want {
		t.Errorf("Build(): unexpected optional_int32: got %v, want %v", got, want)
	}
	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("Could not marshal healthy proto %v.", m)
	}
	m2 := &mixedpb.OpaqueLazy{}
	if err := unmarshalOpts.Unmarshal(b, m2); err != nil {
		t.Fatalf("Could not unmarshal healthy proto buffer: %v.", b)
	}
	for i := 0; i < len(b); i++ {
		b[i] = byte(0xFF)
	}
	if got, want := m2.GetOpaque().GetOptionalInt32(), int32(23); got != want {
		t.Errorf("Mixed proto referred to shared buffer: got %v, want %v", got, want)
	}
}

func TestCopyWhenContainedInOpenLazy(t *testing.T) {
	testCopyWhenContainedInOpen(t, enableLazy)
}

func TestCopyWhenContainedInOpenEager(t *testing.T) {
	testCopyWhenContainedInOpen(t, disableLazy)
}

// testCopyWhenContainedInOpen tests that the buffer is copied
// for opaque messages that are not on the top level
func testCopyWhenContainedInOpen(t *testing.T, unmarshalOpts proto.UnmarshalOptions) {
	m := &mixedpb.OpenLazy{
		Opaque: mixedpb.OpaqueLazy_builder{
			Opaque: mixedpb.OpaqueLazy_builder{
				OptionalInt32: proto.Int32(23),
			}.Build(),
		}.Build(),
	}
	if got, want := m.GetOpaque().GetOpaque().GetOptionalInt32(), int32(23); got != want {
		t.Errorf("Build(): unexpected optional_int32: got %v, want %v", got, want)
	}
	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("Could not marshal healthy proto %v.", m)
	}
	m2 := &mixedpb.OpenLazy{}
	if err := unmarshalOpts.Unmarshal(b, m2); err != nil {
		t.Fatalf("Could not unmarshal healthy proto buffer: %v.", b)
	}
	for i := 0; i < len(b); i++ {
		b[i] = byte(0xFF)
	}
	if got, want := m2.GetOpaque().GetOpaque().GetOptionalInt32(), int32(23); got != want {
		t.Errorf("Build(): unexpected optional_int32: got %v, want %v", got, want)
	}
}

func TestNoExcessiveCopyLazy(t *testing.T) {
	testNoExcessiveCopy(t, enableLazy)
}

func TestNoExcessiveCopyEager(t *testing.T) {
	testNoExcessiveCopy(t, disableLazy)
}

// testNoExcessiveCopy tests that an opaque submessage does share the buffer
// if the message above already got it copied
func testNoExcessiveCopy(t *testing.T, unmarshalOpts proto.UnmarshalOptions) {
	m := &mixedpb.OpenLazy{
		Opaque: mixedpb.OpaqueLazy_builder{
			Opaque: mixedpb.OpaqueLazy_builder{
				OptionalInt32: proto.Int32(23),
			}.Build(),
		}.Build(),
	}
	if got, want := m.GetOpaque().GetOpaque().GetOptionalInt32(), int32(23); got != want {
		t.Errorf("Build(): unexpected optional_int32: got %v, want %v", got, want)
	}
	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("Could not marshal healthy proto %v.", m)
	}
	mm := &mixedpb.OpenLazy{}
	if err := unmarshalOpts.Unmarshal(b, mm); err != nil {
		t.Fatalf("Could not unmarshal healthy proto buffer: %v.", b)
	}
	m2 := mm.GetOpaque()
	m3 := mm.GetOpaque().GetOpaque()
	// Now, if we deliberately destroy the OpaqueM2 buffer, the OpaqueM3 buffer should
	// be destroyed as well
	if m2.XXX_lazyUnmarshalInfo == nil {
		if m3.XXX_lazyUnmarshalInfo != nil {
			t.Errorf("Inconsistent lazyUnmarshalInfo for subprotos")
		}
		// nothing to check, we don't have backing store
		return
	}

	if m3.XXX_lazyUnmarshalInfo == nil {
		t.Errorf("Inconsistent lazyUnmarshalInfo for subprotos (2)")
		return
	}
	b = (*m2.XXX_lazyUnmarshalInfo).Protobuf
	m2len := len(b)
	for i := 0; i < len(b); i++ {
		b[i] = byte(0xFF)
	}
	b = (*m3.XXX_lazyUnmarshalInfo).Protobuf
	if m2len != 0 && len(b) == 0 {
		t.Errorf("The lazy backing store for submessage is empty when it is not for the surronding message: %v.", m2len)
	}
	for i, x := range b {
		if x != byte(0xFF) {
			t.Errorf("Backing store for protocol buffer is not shared (index = %d, x = 0x%x)", i, x)
		}
	}

}